OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "base/memory/ptr_util.h" |
| 8 #include "chromeos/media_perception/media_perception.pb.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 |
| 17 const char kTestDeviceContext[] = "Video camera"; |
| 18 |
| 19 void InitializeFakeFramePerception(const int index, |
| 20 mri::FramePerception* frame_perception) { |
| 21 frame_perception->set_frame_id(index); |
| 22 frame_perception->set_frame_width_in_px(3); |
| 23 frame_perception->set_frame_height_in_px(4); |
| 24 frame_perception->set_timestamp(5); |
| 25 |
| 26 mri::Entity* entity_one = frame_perception->add_entity(); |
| 27 entity_one->set_id(6); |
| 28 entity_one->set_type(mri::Entity::FACE); |
| 29 entity_one->set_confidence(7); |
| 30 |
| 31 mri::Entity* e_two = frame_perception->add_entity(); |
| 32 e_two->set_id(8); |
| 33 e_two->set_type(mri::Entity::PERSON); |
| 34 e_two->set_confidence(9); |
| 35 |
| 36 mri::BoundingBox* bounding_box_one = entity_one->mutable_bounding_box(); |
| 37 bounding_box_one->mutable_top_left()->set_x(10); |
| 38 bounding_box_one->mutable_top_left()->set_y(11); |
| 39 bounding_box_one->mutable_bottom_right()->set_x(12); |
| 40 bounding_box_one->mutable_bottom_right()->set_y(13); |
| 41 bounding_box_one->set_normalized(false); |
| 42 |
| 43 mri::BoundingBox* bounding_box_two = e_two->mutable_bounding_box(); |
| 44 bounding_box_two->mutable_top_left()->set_x(14); |
| 45 bounding_box_two->mutable_top_left()->set_y(15); |
| 46 bounding_box_two->set_normalized(true); |
| 47 } |
| 48 |
| 49 void ValidateFramePerceptionResult( |
| 50 const int index, |
| 51 const media_perception::FramePerception& frame_perception_result) { |
| 52 ASSERT_TRUE(frame_perception_result.frame_id); |
| 53 EXPECT_EQ(*frame_perception_result.frame_id, index); |
| 54 ASSERT_TRUE(frame_perception_result.frame_width_in_px); |
| 55 EXPECT_EQ(*frame_perception_result.frame_width_in_px, 3); |
| 56 ASSERT_TRUE(frame_perception_result.frame_height_in_px); |
| 57 EXPECT_EQ(*frame_perception_result.frame_height_in_px, 4); |
| 58 ASSERT_TRUE(frame_perception_result.timestamp); |
| 59 EXPECT_EQ(*frame_perception_result.timestamp, 5); |
| 60 |
| 61 ASSERT_EQ(2u, frame_perception_result.entities->size()); |
| 62 const media_perception::Entity& entity_result_one = |
| 63 frame_perception_result.entities->at(0); |
| 64 ASSERT_TRUE(entity_result_one.id); |
| 65 EXPECT_EQ(*entity_result_one.id, 6); |
| 66 ASSERT_TRUE(entity_result_one.confidence); |
| 67 EXPECT_EQ(*entity_result_one.confidence, 7); |
| 68 EXPECT_EQ(entity_result_one.type, media_perception::ENTITY_TYPE_FACE); |
| 69 |
| 70 const media_perception::Entity& entity_result_two = |
| 71 frame_perception_result.entities->at(1); |
| 72 ASSERT_TRUE(entity_result_two.id); |
| 73 EXPECT_EQ(*entity_result_two.id, 8); |
| 74 ASSERT_TRUE(entity_result_two.confidence); |
| 75 EXPECT_EQ(*entity_result_two.confidence, 9); |
| 76 EXPECT_EQ(entity_result_two.type, media_perception::ENTITY_TYPE_PERSON); |
| 77 |
| 78 const media_perception::BoundingBox* bounding_box_result_one = |
| 79 entity_result_one.bounding_box.get(); |
| 80 ASSERT_TRUE(bounding_box_result_one); |
| 81 ASSERT_TRUE(bounding_box_result_one->top_left); |
| 82 ASSERT_TRUE(bounding_box_result_one->top_left->x); |
| 83 EXPECT_EQ(*bounding_box_result_one->top_left->x, 10); |
| 84 ASSERT_TRUE(bounding_box_result_one->top_left->y); |
| 85 EXPECT_EQ(*bounding_box_result_one->top_left->y, 11); |
| 86 ASSERT_TRUE(bounding_box_result_one->bottom_right); |
| 87 ASSERT_TRUE(bounding_box_result_one->bottom_right->x); |
| 88 EXPECT_EQ(*bounding_box_result_one->bottom_right->x, 12); |
| 89 ASSERT_TRUE(bounding_box_result_one->bottom_right->y); |
| 90 EXPECT_EQ(*bounding_box_result_one->bottom_right->y, 13); |
| 91 EXPECT_FALSE(*bounding_box_result_one->normalized); |
| 92 |
| 93 const media_perception::BoundingBox* bounding_box_result_two = |
| 94 entity_result_two.bounding_box.get(); |
| 95 ASSERT_TRUE(bounding_box_result_two); |
| 96 ASSERT_TRUE(bounding_box_result_two->top_left); |
| 97 EXPECT_EQ(*bounding_box_result_two->top_left->x, 14); |
| 98 EXPECT_EQ(*bounding_box_result_two->top_left->y, 15); |
| 99 EXPECT_FALSE(bounding_box_result_two->bottom_right); |
| 100 EXPECT_TRUE(*bounding_box_result_two->normalized); |
| 101 } |
| 102 |
| 103 void InitializeFakeImageFrameData(mri::ImageFrame* image_frame) { |
| 104 image_frame->set_width(1); |
| 105 image_frame->set_height(2); |
| 106 image_frame->set_data_length(3); |
| 107 image_frame->set_pixel_data(" "); |
| 108 image_frame->set_format(mri::ImageFrame::JPEG); |
| 109 } |
| 110 |
| 111 void ValidateFakeImageFrameData( |
| 112 const media_perception::ImageFrame& image_frame_result) { |
| 113 ASSERT_TRUE(image_frame_result.width); |
| 114 EXPECT_EQ(*image_frame_result.width, 1); |
| 115 ASSERT_TRUE(image_frame_result.height); |
| 116 EXPECT_EQ(*image_frame_result.height, 2); |
| 117 ASSERT_TRUE(image_frame_result.data_length); |
| 118 EXPECT_EQ(*image_frame_result.data_length, 3); |
| 119 ASSERT_TRUE(image_frame_result.frame); |
| 120 EXPECT_EQ((*image_frame_result.frame).size(), 4ul); |
| 121 EXPECT_EQ(image_frame_result.format, media_perception::IMAGE_FORMAT_JPEG); |
| 122 } |
| 123 |
| 124 } // namespace |
| 125 |
| 126 // Verifies that the data is converted successfully and as expected in each of |
| 127 // these cases. |
| 128 |
| 129 TEST(MediaPerceptionConversionUtilsTest, MediaPerceptionProtoToIdl) { |
| 130 const int kFrameId = 2; |
| 131 mri::MediaPerception media_perception; |
| 132 // Fill in fake values for the media_perception proto. |
| 133 media_perception.set_timestamp(1); |
| 134 mri::FramePerception* frame_perception = |
| 135 media_perception.add_frame_perception(); |
| 136 InitializeFakeFramePerception(kFrameId, frame_perception); |
| 137 media_perception::MediaPerception media_perception_result = |
| 138 media_perception::MediaPerceptionProtoToIdl(media_perception); |
| 139 EXPECT_EQ(*media_perception_result.timestamp, 1); |
| 140 ASSERT_TRUE(media_perception_result.frame_perceptions); |
| 141 ASSERT_EQ(1u, media_perception_result.frame_perceptions->size()); |
| 142 ValidateFramePerceptionResult( |
| 143 kFrameId, media_perception_result.frame_perceptions->at(0)); |
| 144 } |
| 145 |
| 146 TEST(MediaPerceptionConversionUtilsTest, DiagnosticsProtoToIdl) { |
| 147 const size_t kNumSamples = 3; |
| 148 mri::Diagnostics diagnostics; |
| 149 for (size_t i = 0; i < kNumSamples; i++) { |
| 150 mri::PerceptionSample* perception_sample = |
| 151 diagnostics.add_perception_sample(); |
| 152 mri::FramePerception* frame_perception = |
| 153 perception_sample->mutable_frame_perception(); |
| 154 InitializeFakeFramePerception(i, frame_perception); |
| 155 mri::ImageFrame* image_frame = perception_sample->mutable_image_frame(); |
| 156 InitializeFakeImageFrameData(image_frame); |
| 157 } |
| 158 media_perception::Diagnostics diagnostics_result = |
| 159 media_perception::DiagnosticsProtoToIdl(diagnostics); |
| 160 ASSERT_EQ(kNumSamples, diagnostics_result.perception_samples->size()); |
| 161 for (size_t i = 0; i < kNumSamples; i++) { |
| 162 SCOPED_TRACE(testing::Message() << "Sample number: " << i); |
| 163 const media_perception::PerceptionSample& perception_sample_result = |
| 164 diagnostics_result.perception_samples->at(i); |
| 165 |
| 166 const media_perception::FramePerception* frame_perception_result = |
| 167 perception_sample_result.frame_perception.get(); |
| 168 ASSERT_TRUE(frame_perception_result); |
| 169 |
| 170 const media_perception::ImageFrame* image_frame_result = |
| 171 perception_sample_result.image_frame.get(); |
| 172 ASSERT_TRUE(image_frame_result); |
| 173 |
| 174 ValidateFramePerceptionResult(i, *frame_perception_result); |
| 175 ValidateFakeImageFrameData(*image_frame_result); |
| 176 } |
| 177 } |
| 178 |
| 179 TEST(MediaPerceptionConversionUtilsTest, StateProtoToIdl) { |
| 180 mri::State state; |
| 181 state.set_status(mri::State::RUNNING); |
| 182 media_perception::State state_result = |
| 183 media_perception::StateProtoToIdl(state); |
| 184 EXPECT_EQ(state_result.status, media_perception::STATUS_RUNNING); |
| 185 |
| 186 state.set_status(mri::State::STARTED); |
| 187 state.set_device_context(kTestDeviceContext); |
| 188 state_result = media_perception::StateProtoToIdl(state); |
| 189 EXPECT_EQ(state_result.status, media_perception::STATUS_STARTED); |
| 190 ASSERT_TRUE(state_result.device_context); |
| 191 EXPECT_EQ(*state_result.device_context, kTestDeviceContext); |
| 192 } |
| 193 |
| 194 TEST(MediaPerceptionConversionUtilsTest, StateIdlToProto) { |
| 195 media_perception::State state; |
| 196 state.status = media_perception::STATUS_UNINITIALIZED; |
| 197 mri::State state_proto = StateIdlToProto(state); |
| 198 EXPECT_EQ(state_proto.status(), mri::State::UNINITIALIZED); |
| 199 EXPECT_FALSE(state_proto.has_device_context()); |
| 200 |
| 201 state.status = media_perception::STATUS_SUSPENDED; |
| 202 state.device_context = base::MakeUnique<std::string>(kTestDeviceContext); |
| 203 state_proto = StateIdlToProto(state); |
| 204 EXPECT_EQ(state_proto.status(), mri::State::SUSPENDED); |
| 205 EXPECT_EQ(state_proto.device_context(), kTestDeviceContext); |
| 206 } |
| 207 |
| 208 } // namespace extensions |
OLD | NEW |