Chromium Code Reviews| 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 "base/memory/ptr_util.h" | |
| 8 #include "chromeos/media_perception/media_perception.pb.h" | |
| 9 #include "testing/gmock/include/gmock/gmock.h" | |
|
tbarzic
2017/05/19 20:22:38
do you need gmock?
Luke Sorenson
2017/05/19 21:45:22
Done.
tbarzic
2017/05/22 17:40:56
not done? :)
Luke Sorenson
2017/05/22 18:40:48
Oops, weird definitely remember removing that.
| |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace media_perception = extensions::api::media_perception_private; | |
| 13 | |
| 14 namespace extensions { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char kTestDeviceContext[] = "Video camera"; | |
| 19 | |
| 20 void InitializeFakeFramePerception(mri::FramePerception* frame_perception) { | |
| 21 frame_perception->set_frame_id(2); | |
| 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 media_perception::FramePerception& frame_perception_result) { | |
| 51 EXPECT_EQ(*frame_perception_result.frame_id, 2); | |
| 52 EXPECT_EQ(*frame_perception_result.frame_width_in_px, 3); | |
| 53 EXPECT_EQ(*frame_perception_result.frame_height_in_px, 4); | |
| 54 EXPECT_EQ(*frame_perception_result.timestamp, 5); | |
| 55 | |
| 56 ASSERT_EQ(2u, frame_perception_result.entities->size()); | |
| 57 const auto& entity_result_one = (*frame_perception_result.entities)[0]; | |
| 58 EXPECT_EQ(*entity_result_one.id, 6); | |
| 59 EXPECT_EQ(*entity_result_one.confidence, 7); | |
| 60 EXPECT_EQ(entity_result_one.type, media_perception::ENTITY_TYPE_FACE); | |
| 61 | |
| 62 const auto& entity_result_two = (*frame_perception_result.entities)[1]; | |
| 63 EXPECT_EQ(*entity_result_two.id, 8); | |
| 64 EXPECT_EQ(*entity_result_two.confidence, 9); | |
| 65 EXPECT_EQ(entity_result_two.type, media_perception::ENTITY_TYPE_PERSON); | |
| 66 | |
| 67 const auto& bounding_box_result_one = *entity_result_one.bounding_box; | |
|
tbarzic
2017/05/19 20:22:38
can you move this up with the rest of entity_resul
Luke Sorenson
2017/05/19 21:45:22
Done.
| |
| 68 EXPECT_EQ(*(*bounding_box_result_one.top_left).x, 10); | |
|
tbarzic
2017/05/19 20:22:38
bounding_box_result_one->top_left->x?
also, can yo
Luke Sorenson
2017/05/19 21:45:22
Done.
| |
| 69 EXPECT_EQ(*(*bounding_box_result_one.top_left).y, 11); | |
| 70 EXPECT_EQ(*(*bounding_box_result_one.bottom_right).x, 12); | |
| 71 EXPECT_EQ(*(*bounding_box_result_one.bottom_right).y, 13); | |
| 72 EXPECT_FALSE(*bounding_box_result_one.normalized); | |
| 73 | |
| 74 const auto& bounding_box_result_two = *entity_result_two.bounding_box; | |
| 75 EXPECT_EQ(*(*bounding_box_result_two.top_left).x, 14); | |
| 76 EXPECT_EQ(*(*bounding_box_result_two.top_left).y, 15); | |
| 77 EXPECT_FALSE(bounding_box_result_two.bottom_right); | |
| 78 EXPECT_TRUE(*bounding_box_result_two.normalized); | |
| 79 } | |
| 80 | |
| 81 void InitializeFakeImageFrameData(int index, mri::ImageFrame* image_frame) { | |
| 82 image_frame->set_width(index); | |
| 83 image_frame->set_height(2); | |
| 84 image_frame->set_data_length(3); | |
| 85 image_frame->set_pixel_data(" "); | |
| 86 image_frame->set_format(mri::ImageFrame::JPEG); | |
| 87 } | |
| 88 | |
| 89 void ValidateFakeImageFrameData( | |
| 90 int index, | |
| 91 const media_perception::ImageFrame& image_frame_result) { | |
| 92 EXPECT_EQ(*image_frame_result.width, index); | |
| 93 EXPECT_EQ(*image_frame_result.height, 2); | |
| 94 EXPECT_EQ(*image_frame_result.data_length, 3); | |
| 95 EXPECT_EQ((*image_frame_result.frame).size(), 4ul); | |
| 96 EXPECT_EQ(image_frame_result.format, media_perception::IMAGE_FORMAT_JPEG); | |
| 97 } | |
| 98 | |
| 99 } // namespace | |
| 100 | |
| 101 // Verifies that the data is converted successfully and as expected in each of | |
| 102 // these cases. | |
| 103 | |
| 104 TEST(MediaPerceptionConversionUtilsTest, MediaPerceptionProtoToIdl) { | |
| 105 mri::MediaPerception media_perception; | |
| 106 // Fill in fake values for the media_perception proto. | |
| 107 media_perception.set_timestamp(1); | |
| 108 mri::FramePerception* frame_perception = | |
| 109 media_perception.add_frame_perception(); | |
| 110 InitializeFakeFramePerception(frame_perception); | |
| 111 media_perception::MediaPerception media_perception_result = | |
| 112 media_perception::MediaPerceptionProtoToIdl(media_perception); | |
| 113 EXPECT_EQ(*media_perception_result.timestamp, 1); | |
| 114 ASSERT_TRUE(media_perception_result.frame_perceptions); | |
| 115 ASSERT_EQ(1u, media_perception_result.frame_perceptions->size()); | |
| 116 ValidateFramePerceptionResult( | |
| 117 media_perception_result.frame_perceptions->at(0)); | |
| 118 } | |
| 119 | |
| 120 TEST(MediaPerceptionConversionUtilsTest, DiagnosticsProtoToIdl) { | |
| 121 const size_t kNumSamples = 3; | |
| 122 mri::Diagnostics diagnostics; | |
| 123 for (size_t i = 0; i < kNumSamples; i++) { | |
| 124 mri::PerceptionSample* perception_sample = | |
| 125 diagnostics.add_perception_sample(); | |
| 126 mri::FramePerception* frame_perception = | |
| 127 perception_sample->mutable_frame_perception(); | |
| 128 InitializeFakeFramePerception(frame_perception); | |
| 129 mri::ImageFrame* image_frame = perception_sample->mutable_image_frame(); | |
| 130 InitializeFakeImageFrameData(i, image_frame); | |
| 131 } | |
| 132 media_perception::Diagnostics diagnostics_result = | |
| 133 media_perception::DiagnosticsProtoToIdl(diagnostics); | |
| 134 ASSERT_EQ(kNumSamples, diagnostics_result.perception_samples->size()); | |
| 135 for (size_t i = 0; i < kNumSamples; i++) { | |
| 136 const media_perception::PerceptionSample& perception_sample_result = | |
| 137 (*diagnostics_result.perception_samples)[i]; | |
| 138 const media_perception::FramePerception& frame_perception_result = | |
| 139 (*perception_sample_result.frame_perception); | |
| 140 const media_perception::ImageFrame& image_frame_result = | |
| 141 (*perception_sample_result.image_frame); | |
| 142 ValidateFramePerceptionResult(frame_perception_result); | |
| 143 ValidateFakeImageFrameData(i, image_frame_result); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 TEST(MediaPerceptionConversionUtilsTest, StateProtoToIdl) { | |
| 148 mri::State state; | |
| 149 state.set_status(mri::State::RUNNING); | |
| 150 media_perception::State state_result = | |
| 151 media_perception::StateProtoToIdl(state); | |
| 152 EXPECT_EQ(state_result.status, media_perception::STATUS_RUNNING); | |
| 153 | |
| 154 state.set_status(mri::State::STARTED); | |
| 155 state.set_device_context(kTestDeviceContext); | |
| 156 state_result = media_perception::StateProtoToIdl(state); | |
| 157 EXPECT_EQ(state_result.status, media_perception::STATUS_STARTED); | |
| 158 EXPECT_EQ(*state_result.device_context, kTestDeviceContext); | |
| 159 } | |
| 160 | |
| 161 TEST(MediaPerceptionConversionUtilsTest, StateIdlToProto) { | |
| 162 media_perception::State state; | |
| 163 state.status = media_perception::STATUS_UNINITIALIZED; | |
| 164 mri::State state_proto = StateIdlToProto(state); | |
| 165 EXPECT_EQ(state_proto.status(), mri::State::UNINITIALIZED); | |
| 166 EXPECT_FALSE(state_proto.has_device_context()); | |
| 167 | |
| 168 state.status = media_perception::STATUS_SUSPENDED; | |
| 169 state.device_context = base::MakeUnique<std::string>(kTestDeviceContext); | |
| 170 state_proto = StateIdlToProto(state); | |
| 171 EXPECT_EQ(state_proto.status(), mri::State::SUSPENDED); | |
| 172 EXPECT_EQ(state_proto.device_context(), kTestDeviceContext); | |
| 173 } | |
| 174 | |
| 175 } // namespace extensions | |
| OLD | NEW |