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 "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 { | |
|
tbarzic
2017/05/19 03:16:15
nit: nl
Luke Sorenson
2017/05/19 13:41:57
Done.
| |
| 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(); | |
|
tbarzic
2017/05/19 03:16:15
nit: nl around different items
Luke Sorenson
2017/05/19 13:41:57
Done.
| |
| 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); | |
|
tbarzic
2017/05/19 03:16:15
no CHECKS in test code -> ASSERT_TRUE()
Luke Sorenson
2017/05/19 13:41:57
Done.
| |
| 103 ASSERT_THAT(1, media_perception_result.frame_perceptions->size()); | |
|
tbarzic
2017/05/19 03:16:16
ASSERT_EQ
Luke Sorenson
2017/05/19 13:41:57
Done.
| |
| 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(kNumSamples, diagnostics_result.perception_samples->size()); | |
|
tbarzic
2017/05/19 03:16:16
ASSERT_EQ
Luke Sorenson
2017/05/19 13:41:57
Ah, this is the reason for using testing::SizeIs -
tbarzic
2017/05/19 17:41:04
I prefer using size_t for kNumSamples, or static_c
Luke Sorenson
2017/05/19 18:27:15
Changed to size_t for kNumSamples and included an
| |
| 123 for (int i = 0; i < kNumSamples; i++) { | |
| 124 const auto& perception_sample_result = | |
| 125 (*diagnostics_result.perception_samples)[i]; | |
| 126 const auto& frame_perception_result = | |
| 127 (*perception_sample_result.frame_perception); | |
| 128 const auto& image_frame_result = (*perception_sample_result.image_frame); | |
| 129 ValidateFramePerceptionResult(frame_perception_result); | |
| 130 ValidateFakeImageFrameData(image_frame_result); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 TEST(MediaPerceptionConversionUtilsTest, StateProtoToIdl) { | |
| 135 mri::State state; | |
| 136 state.set_status(mri::State::RUNNING); | |
| 137 media_perception::State state_result = | |
| 138 media_perception::StateProtoToIdl(state); | |
| 139 EXPECT_EQ(state_result.status, media_perception::STATUS_RUNNING); | |
| 140 | |
| 141 state.set_status(mri::State::STARTED); | |
| 142 state.set_device_context(kTestDeviceContext); | |
| 143 state_result = media_perception::StateProtoToIdl(state); | |
| 144 EXPECT_EQ(state_result.status, media_perception::STATUS_STARTED); | |
| 145 EXPECT_EQ(*state_result.device_context, kTestDeviceContext); | |
| 146 } | |
| 147 | |
| 148 TEST(MediaPerceptionConversionUtilsTest, StateIdlToProto) { | |
| 149 media_perception::State state; | |
| 150 state.status = media_perception::STATUS_UNINITIALIZED; | |
| 151 mri::State state_proto = StateIdlToProto(state); | |
| 152 EXPECT_EQ(state_proto.status(), mri::State::UNINITIALIZED); | |
| 153 EXPECT_FALSE(state_proto.has_device_context()); | |
| 154 | |
| 155 state.status = media_perception::STATUS_SUSPENDED; | |
| 156 state.device_context.reset(new std::string(kTestDeviceContext)); | |
| 157 state_proto = StateIdlToProto(state); | |
| 158 EXPECT_EQ(state_proto.status(), mri::State::SUSPENDED); | |
| 159 EXPECT_EQ(state_proto.device_context(), kTestDeviceContext); | |
| 160 } | |
| 161 | |
| 162 } // namespace extensions | |
| OLD | NEW |