Skip to content

Commit

Permalink
backend: add StartPhotoInterval to camera
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasVautherin committed May 24, 2018
1 parent e2184d9 commit ee6d592
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
19 changes: 18 additions & 1 deletion backend/src/plugins/camera/camera_service_impl.h
Expand Up @@ -25,7 +25,7 @@ class CameraServiceImpl final : public rpc::camera::CameraService::Service
}

template <typename ResponseType>
void fillResponseWithResult(ResponseType *response, dronecore::Camera::Result &camera_result) const
void fillResponseWithResult(ResponseType *response, dronecore::Camera::Result camera_result) const
{
auto rpc_result = static_cast<rpc::camera::CameraResult::Result>(camera_result);

Expand All @@ -36,6 +36,23 @@ class CameraServiceImpl final : public rpc::camera::CameraService::Service
response->set_allocated_camera_result(rpc_camera_result);
}

grpc::Status StartPhotoInterval(grpc::ServerContext * /* context */,
const rpc::camera::StartPhotoIntervalRequest *request,
rpc::camera::StartPhotoIntervalResponse *response) override
{
if (request == nullptr && response != nullptr) {
fillResponseWithResult(response, dronecore::Camera::Result::WRONG_ARGUMENT);
} else {
auto camera_result = _camera.start_photo_interval(request->interval_s());

if (response != nullptr) {
fillResponseWithResult(response, camera_result);
}
}

return grpc::Status::OK;
}

private:
Camera &_camera;
};
Expand Down
46 changes: 46 additions & 0 deletions backend/test/camera_service_impl_test.cpp
Expand Up @@ -5,6 +5,7 @@

namespace {

using testing::_;
using testing::NiceMock;
using testing::Return;

Expand All @@ -14,6 +15,8 @@ using CameraServiceImpl = dronecore::backend::CameraServiceImpl<MockCamera>;
using CameraResult = dronecore::rpc::camera::CameraResult;
using InputPair = std::pair<std::string, dronecore::Camera::Result>;

static constexpr auto ARBITRARY_FLOAT = 24.2f;

std::vector<InputPair> generateInputPairs();

class CameraServiceImplTest : public ::testing::TestWithParam<InputPair>
Expand Down Expand Up @@ -45,6 +48,49 @@ TEST_F(CameraServiceImplTest, takesPhotoEvenWhenArgsAreNull)
_camera_service.TakePhoto(nullptr, nullptr, nullptr);
}

TEST_P(CameraServiceImplTest, startPhotoIntervalResultIsTranslatedCorrectly)
{
ON_CALL(_camera, start_photo_interval(_))
.WillByDefault(Return(GetParam().second));
dronecore::rpc::camera::StartPhotoIntervalRequest request;
request.set_interval_s(ARBITRARY_FLOAT);
dronecore::rpc::camera::StartPhotoIntervalResponse response;

_camera_service.StartPhotoInterval(nullptr, &request, &response);

EXPECT_EQ(GetParam().first, CameraResult::Result_Name(response.camera_result().result()));
}

TEST_F(CameraServiceImplTest, startsPhotoIntervalEvenWhenContextAndResponseAreNull)
{
EXPECT_CALL(_camera, start_photo_interval(_))
.Times(1);
dronecore::rpc::camera::StartPhotoIntervalRequest request;
request.set_interval_s(ARBITRARY_FLOAT);

_camera_service.StartPhotoInterval(nullptr, &request, nullptr);
}

TEST_F(CameraServiceImplTest, startsPhotoIntervalWithRightParameter)
{
auto expected_interval = ARBITRARY_FLOAT;
EXPECT_CALL(_camera, start_photo_interval(expected_interval))
.Times(1);
dronecore::rpc::camera::StartPhotoIntervalRequest request;
request.set_interval_s(expected_interval);

_camera_service.StartPhotoInterval(nullptr, &request, nullptr);
}

TEST_F(CameraServiceImplTest, startPhotoIntervalReturnsWrongArgumentErrorIfRequestIsNull)
{
dronecore::rpc::camera::StartPhotoIntervalResponse response;

_camera_service.StartPhotoInterval(nullptr, nullptr, &response);

EXPECT_EQ("WRONG_ARGUMENT", CameraResult::Result_Name(response.camera_result().result()));
}

INSTANTIATE_TEST_CASE_P(CameraResultCorrespondences,
CameraServiceImplTest,
::testing::ValuesIn(generateInputPairs()));
Expand Down
2 changes: 2 additions & 0 deletions plugins/camera/mocks/camera_mock.h
Expand Up @@ -9,6 +9,8 @@ class MockCamera
{
public:
MOCK_CONST_METHOD0(take_photo, Camera::Result());
MOCK_CONST_METHOD1(start_photo_interval, Camera::Result(float));
MOCK_CONST_METHOD0(stop_photo_interval, Camera::Result());
};

} // namespace testing
Expand Down

0 comments on commit ee6d592

Please sign in to comment.