Chromium Code Reviews| Index: extensions/browser/api/media_perception_private/conversion_utils_unittest.cc |
| diff --git a/extensions/browser/api/media_perception_private/conversion_utils_unittest.cc b/extensions/browser/api/media_perception_private/conversion_utils_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e659f08c541fe8621da79d103aa890600ffdd7e4 |
| --- /dev/null |
| +++ b/extensions/browser/api/media_perception_private/conversion_utils_unittest.cc |
| @@ -0,0 +1,157 @@ |
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "extensions/browser/api/media_perception_private/conversion_utils.h" |
| + |
| +#include "chromeos/media_perception/media_perception.pb.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace media_perception = extensions::api::media_perception_private; |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| +const char kTestDeviceContext[] = "Video camera"; |
| +} // namespace |
| + |
| +void InitializeFakeFramePerception(mri::FramePerception* frame_perception) { |
| + frame_perception->set_frame_id(2); |
| + frame_perception->set_frame_width_in_px(3); |
| + frame_perception->set_frame_height_in_px(4); |
|
tbarzic
2017/05/17 21:04:42
move helper methods to namespace {}
Luke Sorenson
2017/05/17 23:07:48
Done.
|
| + frame_perception->set_timestamp(5); |
| + mri::Entity* e_one = frame_perception->add_entity(); |
| + e_one->set_id(6); |
| + e_one->set_type(mri::Entity::FACE); |
| + e_one->set_confidence(7); |
| + mri::Entity* e_two = frame_perception->add_entity(); |
| + e_two->set_id(8); |
| + e_two->set_type(mri::Entity::PERSON); |
| + e_two->set_confidence(9); |
| + mri::BoundingBox* b_one = e_one->mutable_bounding_box(); |
| + b_one->mutable_top_left()->set_x(10); |
| + b_one->mutable_top_left()->set_y(11); |
|
tbarzic
2017/05/17 21:04:41
can you tests cases where some of these are null?
Luke Sorenson
2017/05/17 23:07:48
Done.
|
| + b_one->mutable_bottom_right()->set_x(12); |
| + b_one->mutable_bottom_right()->set_y(13); |
| + b_one->set_normalized(false); |
| + mri::BoundingBox* b_two = e_two->mutable_bounding_box(); |
| + b_two->mutable_top_left()->set_x(14); |
| + b_two->mutable_top_left()->set_y(15); |
| + b_two->mutable_bottom_right()->set_x(16); |
| + b_two->mutable_bottom_right()->set_y(17); |
| + b_two->set_normalized(true); |
| +} |
| + |
| +void ValidateFramePerceptionResult( |
| + const media_perception::FramePerception& fp_result) { |
| + EXPECT_EQ(*fp_result.frame_id, 2); |
| + EXPECT_EQ(*fp_result.frame_width_in_px, 3); |
| + EXPECT_EQ(*fp_result.frame_height_in_px, 4); |
| + EXPECT_EQ(*fp_result.timestamp, 5); |
| + const auto& e_result_one = (*fp_result.entities)[0]; |
| + EXPECT_EQ(*e_result_one.id, 6); |
| + EXPECT_EQ(*e_result_one.confidence, 7); |
| + EXPECT_EQ(e_result_one.type, media_perception::ENTITY_TYPE_FACE); |
| + const auto& e_result_two = (*fp_result.entities)[1]; |
| + EXPECT_EQ(*e_result_two.id, 8); |
| + EXPECT_EQ(*e_result_two.confidence, 9); |
| + EXPECT_EQ(e_result_two.type, media_perception::ENTITY_TYPE_PERSON); |
| + const auto& b_result_one = *e_result_one.bounding_box; |
| + EXPECT_EQ(*(*b_result_one.top_left).x, 10); |
| + EXPECT_EQ(*(*b_result_one.top_left).y, 11); |
| + EXPECT_EQ(*(*b_result_one.bottom_right).x, 12); |
| + EXPECT_EQ(*(*b_result_one.bottom_right).y, 13); |
| + EXPECT_FALSE(*b_result_one.normalized); |
| + const auto& b_result_two = *e_result_two.bounding_box; |
| + EXPECT_EQ(*(*b_result_two.top_left).x, 14); |
| + EXPECT_EQ(*(*b_result_two.top_left).y, 15); |
| + EXPECT_EQ(*(*b_result_two.bottom_right).x, 16); |
| + EXPECT_EQ(*(*b_result_two.bottom_right).y, 17); |
| + EXPECT_TRUE(*b_result_two.normalized); |
| +} |
| + |
| +void InitializeFakeImageFrameData(mri::ImageFrame* image_frame) { |
| + image_frame->set_width(1); |
| + image_frame->set_height(2); |
| + image_frame->set_data_length(3); |
| + image_frame->set_pixel_data(" "); |
| + image_frame->set_format(mri::ImageFrame::JPEG); |
| +} |
| + |
| +void ValidateFakeImageFrameData(const media_perception::ImageFrame& if_result) { |
| + EXPECT_EQ(*if_result.width, 1); |
| + EXPECT_EQ(*if_result.height, 2); |
| + EXPECT_EQ(*if_result.data_length, 3); |
| + EXPECT_EQ((*if_result.frame).size(), 4ul); |
| + EXPECT_EQ(if_result.format, media_perception::IMAGE_FORMAT_JPEG); |
| +} |
| + |
| +// Verifies that the data is converted successfully and as expected in each of |
| +// these cases. |
| + |
| +TEST(ConversionUtilsTest, MediaPerceptionProtoToIdl) { |
|
tbarzic
2017/05/17 21:04:41
rename tests to MediaPerceptionConversionUtilsTest
Luke Sorenson
2017/05/17 23:07:49
Done.
|
| + mri::MediaPerception media_perception; |
| + // Fill in fake values for the media_perception proto. |
| + media_perception.set_timestamp(1); |
| + mri::FramePerception* frame_perception = |
| + media_perception.add_frame_perception(); |
| + InitializeFakeFramePerception(frame_perception); |
| + media_perception::MediaPerception mp_result = |
|
tbarzic
2017/05/17 21:04:41
add a new line after initialization is done
also,
Luke Sorenson
2017/05/17 23:07:48
Done.
|
| + MediaPerceptionProtoToIdl(media_perception); |
| + EXPECT_EQ(*mp_result.timestamp, 1); |
| + const auto& fp_result = (*mp_result.frame_perceptions)[0]; |
|
tbarzic
2017/05/17 21:04:41
assert that frame perception is set and its size i
Luke Sorenson
2017/05/17 23:07:48
Done.
|
| + ValidateFramePerceptionResult(fp_result); |
| +} |
| + |
| +TEST(ConversionUtilsTest, DiagnosticsProtoToIdl) { |
| + const int kNumSamples = 3; |
| + mri::Diagnostics diagnostics; |
| + for (int i = 0; i < kNumSamples; i++) { |
| + mri::PerceptionSample* perception_sample = |
| + diagnostics.add_perception_sample(); |
| + mri::FramePerception* frame_perception = |
| + perception_sample->mutable_frame_perception(); |
| + InitializeFakeFramePerception(frame_perception); |
| + mri::ImageFrame* image_frame = perception_sample->mutable_image_frame(); |
| + InitializeFakeImageFrameData(image_frame); |
| + } |
| + media_perception::Diagnostics d_result = DiagnosticsProtoToIdl(diagnostics); |
| + ASSERT_THAT(*d_result.perception_samples, testing::SizeIs(kNumSamples)); |
|
tbarzic
2017/05/17 21:04:41
ASSERT_EQ(kNumSamples, d_result.perception_samples
Luke Sorenson
2017/05/17 23:07:48
For testing the size of stl objects, I think I am
tbarzic
2017/05/18 19:28:01
testing::SizeIs is not really common in Chrome - i
Luke Sorenson
2017/05/18 21:24:16
Done.
|
| + for (int i = 0; i < kNumSamples; i++) { |
| + const auto& ps_result = (*d_result.perception_samples)[i]; |
| + const auto& fp_result = (*ps_result.frame_perception); |
| + const auto& if_result = (*ps_result.image_frame); |
| + ValidateFramePerceptionResult(fp_result); |
| + ValidateFakeImageFrameData(if_result); |
| + } |
| +} |
| + |
| +TEST(ConversionUtilsTest, StateProtoToIdl) { |
| + mri::State state; |
| + state.set_status(mri::State::RUNNING); |
| + media_perception::State s_result = StateProtoToIdl(state); |
| + EXPECT_EQ(s_result.status, media_perception::STATUS_RUNNING); |
| + |
| + state.set_status(mri::State::STARTED); |
| + state.set_device_context(kTestDeviceContext); |
| + s_result = StateProtoToIdl(state); |
| + EXPECT_EQ(s_result.status, media_perception::STATUS_STARTED); |
| + EXPECT_EQ(*s_result.device_context, kTestDeviceContext); |
| +} |
| + |
| +TEST(ConversionUtilsTest, StateIdlToProto) { |
| + media_perception::State state; |
| + state.status = media_perception::STATUS_UNINITIALIZED; |
| + mri::State s_proto = StateIdlToProto(state); |
| + EXPECT_EQ(s_proto.status(), mri::State::UNINITIALIZED); |
| + EXPECT_FALSE(s_proto.has_device_context()); |
| + |
| + state.status = media_perception::STATUS_SUSPENDED; |
| + state.device_context.reset(new std::string(kTestDeviceContext)); |
| + s_proto = StateIdlToProto(state); |
| + EXPECT_EQ(s_proto.status(), mri::State::SUSPENDED); |
| + EXPECT_EQ(s_proto.device_context(), kTestDeviceContext); |
| +} |
| + |
| +} // namespace extensions |