OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/browser/api/media_perception_private/conversion_utils.h" |
| 6 |
| 7 #include "chromeos/media_perception/media_perception.pb.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace media_perception = extensions::api::media_perception_private; |
| 12 |
| 13 namespace extensions { |
| 14 |
| 15 namespace { |
| 16 const char kTestDeviceContext[] = "Video camera"; |
| 17 |
| 18 void InitializeFakeFramePerception(mri::FramePerception* frame_perception) { |
| 19 frame_perception->set_frame_id(2); |
| 20 frame_perception->set_frame_width_in_px(3); |
| 21 frame_perception->set_frame_height_in_px(4); |
| 22 frame_perception->set_timestamp(5); |
| 23 mri::Entity* entity_one = frame_perception->add_entity(); |
| 24 entity_one->set_id(6); |
| 25 entity_one->set_type(mri::Entity::FACE); |
| 26 entity_one->set_confidence(7); |
| 27 mri::Entity* e_two = frame_perception->add_entity(); |
| 28 e_two->set_id(8); |
| 29 e_two->set_type(mri::Entity::PERSON); |
| 30 e_two->set_confidence(9); |
| 31 mri::BoundingBox* bounding_box_one = entity_one->mutable_bounding_box(); |
| 32 bounding_box_one->mutable_top_left()->set_x(10); |
| 33 bounding_box_one->mutable_top_left()->set_y(11); |
| 34 bounding_box_one->mutable_bottom_right()->set_x(12); |
| 35 bounding_box_one->mutable_bottom_right()->set_y(13); |
| 36 bounding_box_one->set_normalized(false); |
| 37 mri::BoundingBox* bounding_box_two = e_two->mutable_bounding_box(); |
| 38 bounding_box_two->mutable_top_left()->set_x(14); |
| 39 bounding_box_two->mutable_top_left()->set_y(15); |
| 40 bounding_box_two->set_normalized(true); |
| 41 } |
| 42 |
| 43 void ValidateFramePerceptionResult( |
| 44 const media_perception::FramePerception& frame_perception_result) { |
| 45 EXPECT_EQ(*frame_perception_result.frame_id, 2); |
| 46 EXPECT_EQ(*frame_perception_result.frame_width_in_px, 3); |
| 47 EXPECT_EQ(*frame_perception_result.frame_height_in_px, 4); |
| 48 EXPECT_EQ(*frame_perception_result.timestamp, 5); |
| 49 const auto& entity_result_one = (*frame_perception_result.entities)[0]; |
| 50 EXPECT_EQ(*entity_result_one.id, 6); |
| 51 EXPECT_EQ(*entity_result_one.confidence, 7); |
| 52 EXPECT_EQ(entity_result_one.type, media_perception::ENTITY_TYPE_FACE); |
| 53 const auto& entity_result_two = (*frame_perception_result.entities)[1]; |
| 54 EXPECT_EQ(*entity_result_two.id, 8); |
| 55 EXPECT_EQ(*entity_result_two.confidence, 9); |
| 56 EXPECT_EQ(entity_result_two.type, media_perception::ENTITY_TYPE_PERSON); |
| 57 const auto& bounding_box_result_one = *entity_result_one.bounding_box; |
| 58 EXPECT_EQ(*(*bounding_box_result_one.top_left).x, 10); |
| 59 EXPECT_EQ(*(*bounding_box_result_one.top_left).y, 11); |
| 60 EXPECT_EQ(*(*bounding_box_result_one.bottom_right).x, 12); |
| 61 EXPECT_EQ(*(*bounding_box_result_one.bottom_right).y, 13); |
| 62 EXPECT_FALSE(*bounding_box_result_one.normalized); |
| 63 const auto& bounding_box_result_two = *entity_result_two.bounding_box; |
| 64 EXPECT_EQ(*(*bounding_box_result_two.top_left).x, 14); |
| 65 EXPECT_EQ(*(*bounding_box_result_two.top_left).y, 15); |
| 66 EXPECT_FALSE(bounding_box_result_two.bottom_right); |
| 67 EXPECT_TRUE(*bounding_box_result_two.normalized); |
| 68 } |
| 69 |
| 70 void InitializeFakeImageFrameData(mri::ImageFrame* image_frame) { |
| 71 image_frame->set_width(1); |
| 72 image_frame->set_height(2); |
| 73 image_frame->set_data_length(3); |
| 74 image_frame->set_pixel_data(" "); |
| 75 image_frame->set_format(mri::ImageFrame::JPEG); |
| 76 } |
| 77 |
| 78 void ValidateFakeImageFrameData( |
| 79 const media_perception::ImageFrame& image_frame_result) { |
| 80 EXPECT_EQ(*image_frame_result.width, 1); |
| 81 EXPECT_EQ(*image_frame_result.height, 2); |
| 82 EXPECT_EQ(*image_frame_result.data_length, 3); |
| 83 EXPECT_EQ((*image_frame_result.frame).size(), 4ul); |
| 84 EXPECT_EQ(image_frame_result.format, media_perception::IMAGE_FORMAT_JPEG); |
| 85 } |
| 86 |
| 87 } // namespace |
| 88 |
| 89 // Verifies that the data is converted successfully and as expected in each of |
| 90 // these cases. |
| 91 |
| 92 TEST(MediaPerceptionConversionUtilsTest, MediaPerceptionProtoToIdl) { |
| 93 mri::MediaPerception media_perception; |
| 94 // Fill in fake values for the media_perception proto. |
| 95 media_perception.set_timestamp(1); |
| 96 mri::FramePerception* frame_perception = |
| 97 media_perception.add_frame_perception(); |
| 98 InitializeFakeFramePerception(frame_perception); |
| 99 media_perception::MediaPerception media_perception_result = |
| 100 media_perception::MediaPerceptionProtoToIdl(media_perception); |
| 101 EXPECT_EQ(*media_perception_result.timestamp, 1); |
| 102 CHECK(media_perception_result.frame_perceptions); |
| 103 ASSERT_THAT(*media_perception_result.frame_perceptions, testing::SizeIs(1)); |
| 104 ValidateFramePerceptionResult( |
| 105 media_perception_result.frame_perceptions->at(0)); |
| 106 } |
| 107 |
| 108 TEST(MediaPerceptionConversionUtilsTest, DiagnosticsProtoToIdl) { |
| 109 const int kNumSamples = 3; |
| 110 mri::Diagnostics diagnostics; |
| 111 for (int i = 0; i < kNumSamples; i++) { |
| 112 mri::PerceptionSample* perception_sample = |
| 113 diagnostics.add_perception_sample(); |
| 114 mri::FramePerception* frame_perception = |
| 115 perception_sample->mutable_frame_perception(); |
| 116 InitializeFakeFramePerception(frame_perception); |
| 117 mri::ImageFrame* image_frame = perception_sample->mutable_image_frame(); |
| 118 InitializeFakeImageFrameData(image_frame); |
| 119 } |
| 120 media_perception::Diagnostics diagnostics_result = |
| 121 media_perception::DiagnosticsProtoToIdl(diagnostics); |
| 122 ASSERT_THAT(*diagnostics_result.perception_samples, |
| 123 testing::SizeIs(kNumSamples)); |
| 124 for (int i = 0; i < kNumSamples; i++) { |
| 125 const auto& perception_sample_result = |
| 126 (*diagnostics_result.perception_samples)[i]; |
| 127 const auto& frame_perception_result = |
| 128 (*perception_sample_result.frame_perception); |
| 129 const auto& image_frame_result = (*perception_sample_result.image_frame); |
| 130 ValidateFramePerceptionResult(frame_perception_result); |
| 131 ValidateFakeImageFrameData(image_frame_result); |
| 132 } |
| 133 } |
| 134 |
| 135 TEST(MediaPerceptionConversionUtilsTest, StateProtoToIdl) { |
| 136 mri::State state; |
| 137 state.set_status(mri::State::RUNNING); |
| 138 media_perception::State state_result = |
| 139 media_perception::StateProtoToIdl(state); |
| 140 EXPECT_EQ(state_result.status, media_perception::STATUS_RUNNING); |
| 141 |
| 142 state.set_status(mri::State::STARTED); |
| 143 state.set_device_context(kTestDeviceContext); |
| 144 state_result = media_perception::StateProtoToIdl(state); |
| 145 EXPECT_EQ(state_result.status, media_perception::STATUS_STARTED); |
| 146 EXPECT_EQ(*state_result.device_context, kTestDeviceContext); |
| 147 } |
| 148 |
| 149 TEST(MediaPerceptionConversionUtilsTest, StateIdlToProto) { |
| 150 media_perception::State state; |
| 151 state.status = media_perception::STATUS_UNINITIALIZED; |
| 152 mri::State state_proto = StateIdlToProto(state); |
| 153 EXPECT_EQ(state_proto.status(), mri::State::UNINITIALIZED); |
| 154 EXPECT_FALSE(state_proto.has_device_context()); |
| 155 |
| 156 state.status = media_perception::STATUS_SUSPENDED; |
| 157 state.device_context.reset(new std::string(kTestDeviceContext)); |
| 158 state_proto = StateIdlToProto(state); |
| 159 EXPECT_EQ(state_proto.status(), mri::State::SUSPENDED); |
| 160 EXPECT_EQ(state_proto.device_context(), kTestDeviceContext); |
| 161 } |
| 162 |
| 163 } // namespace extensions |
OLD | NEW |