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 | |
17 const char kTestDeviceContext[] = "Video camera"; | |
18 | |
19 void InitializeFakeFramePerception(mri::FramePerception* frame_perception) { | |
20 frame_perception->set_frame_id(2); | |
21 frame_perception->set_frame_width_in_px(3); | |
22 frame_perception->set_frame_height_in_px(4); | |
23 frame_perception->set_timestamp(5); | |
24 | |
25 mri::Entity* entity_one = frame_perception->add_entity(); | |
26 entity_one->set_id(6); | |
27 entity_one->set_type(mri::Entity::FACE); | |
28 entity_one->set_confidence(7); | |
29 | |
30 mri::Entity* e_two = frame_perception->add_entity(); | |
31 e_two->set_id(8); | |
32 e_two->set_type(mri::Entity::PERSON); | |
33 e_two->set_confidence(9); | |
34 | |
35 mri::BoundingBox* bounding_box_one = entity_one->mutable_bounding_box(); | |
36 bounding_box_one->mutable_top_left()->set_x(10); | |
37 bounding_box_one->mutable_top_left()->set_y(11); | |
38 bounding_box_one->mutable_bottom_right()->set_x(12); | |
39 bounding_box_one->mutable_bottom_right()->set_y(13); | |
40 bounding_box_one->set_normalized(false); | |
41 | |
42 mri::BoundingBox* bounding_box_two = e_two->mutable_bounding_box(); | |
43 bounding_box_two->mutable_top_left()->set_x(14); | |
44 bounding_box_two->mutable_top_left()->set_y(15); | |
45 bounding_box_two->set_normalized(true); | |
46 } | |
47 | |
48 void ValidateFramePerceptionResult( | |
49 const media_perception::FramePerception& frame_perception_result) { | |
50 EXPECT_EQ(*frame_perception_result.frame_id, 2); | |
51 EXPECT_EQ(*frame_perception_result.frame_width_in_px, 3); | |
52 EXPECT_EQ(*frame_perception_result.frame_height_in_px, 4); | |
tbarzic
2017/05/19 17:41:04
can you make this a little more readable :)
Luke Sorenson
2017/05/19 18:27:16
Done.
| |
53 EXPECT_EQ(*frame_perception_result.timestamp, 5); | |
54 const auto& entity_result_one = (*frame_perception_result.entities)[0]; | |
55 EXPECT_EQ(*entity_result_one.id, 6); | |
56 EXPECT_EQ(*entity_result_one.confidence, 7); | |
57 EXPECT_EQ(entity_result_one.type, media_perception::ENTITY_TYPE_FACE); | |
58 const auto& entity_result_two = (*frame_perception_result.entities)[1]; | |
tbarzic
2017/05/19 17:41:04
ASSERT entities size before accessing them
Luke Sorenson
2017/05/19 18:27:15
Done.
| |
59 EXPECT_EQ(*entity_result_two.id, 8); | |
60 EXPECT_EQ(*entity_result_two.confidence, 9); | |
61 EXPECT_EQ(entity_result_two.type, media_perception::ENTITY_TYPE_PERSON); | |
62 const auto& bounding_box_result_one = *entity_result_one.bounding_box; | |
63 EXPECT_EQ(*(*bounding_box_result_one.top_left).x, 10); | |
64 EXPECT_EQ(*(*bounding_box_result_one.top_left).y, 11); | |
65 EXPECT_EQ(*(*bounding_box_result_one.bottom_right).x, 12); | |
66 EXPECT_EQ(*(*bounding_box_result_one.bottom_right).y, 13); | |
67 EXPECT_FALSE(*bounding_box_result_one.normalized); | |
68 const auto& bounding_box_result_two = *entity_result_two.bounding_box; | |
69 EXPECT_EQ(*(*bounding_box_result_two.top_left).x, 14); | |
70 EXPECT_EQ(*(*bounding_box_result_two.top_left).y, 15); | |
71 EXPECT_FALSE(bounding_box_result_two.bottom_right); | |
72 EXPECT_TRUE(*bounding_box_result_two.normalized); | |
73 } | |
74 | |
75 void InitializeFakeImageFrameData(mri::ImageFrame* image_frame) { | |
76 image_frame->set_width(1); | |
77 image_frame->set_height(2); | |
78 image_frame->set_data_length(3); | |
79 image_frame->set_pixel_data(" "); | |
80 image_frame->set_format(mri::ImageFrame::JPEG); | |
81 } | |
82 | |
83 void ValidateFakeImageFrameData( | |
84 const media_perception::ImageFrame& image_frame_result) { | |
85 EXPECT_EQ(*image_frame_result.width, 1); | |
86 EXPECT_EQ(*image_frame_result.height, 2); | |
87 EXPECT_EQ(*image_frame_result.data_length, 3); | |
88 EXPECT_EQ((*image_frame_result.frame).size(), 4ul); | |
89 EXPECT_EQ(image_frame_result.format, media_perception::IMAGE_FORMAT_JPEG); | |
90 } | |
91 | |
92 } // namespace | |
93 | |
94 // Verifies that the data is converted successfully and as expected in each of | |
95 // these cases. | |
96 | |
97 TEST(MediaPerceptionConversionUtilsTest, MediaPerceptionProtoToIdl) { | |
98 mri::MediaPerception media_perception; | |
99 // Fill in fake values for the media_perception proto. | |
100 media_perception.set_timestamp(1); | |
101 mri::FramePerception* frame_perception = | |
102 media_perception.add_frame_perception(); | |
103 InitializeFakeFramePerception(frame_perception); | |
104 media_perception::MediaPerception media_perception_result = | |
105 media_perception::MediaPerceptionProtoToIdl(media_perception); | |
106 EXPECT_EQ(*media_perception_result.timestamp, 1); | |
107 ASSERT_TRUE(media_perception_result.frame_perceptions); | |
108 ASSERT_EQ(1ul, media_perception_result.frame_perceptions->size()); | |
tbarzic
2017/05/19 17:41:04
1u
Luke Sorenson
2017/05/19 18:27:16
Done.
| |
109 ValidateFramePerceptionResult( | |
110 media_perception_result.frame_perceptions->at(0)); | |
111 } | |
112 | |
113 TEST(MediaPerceptionConversionUtilsTest, DiagnosticsProtoToIdl) { | |
114 const int kNumSamples = 3; | |
115 mri::Diagnostics diagnostics; | |
116 for (int i = 0; i < kNumSamples; i++) { | |
117 mri::PerceptionSample* perception_sample = | |
118 diagnostics.add_perception_sample(); | |
119 mri::FramePerception* frame_perception = | |
120 perception_sample->mutable_frame_perception(); | |
121 InitializeFakeFramePerception(frame_perception); | |
122 mri::ImageFrame* image_frame = perception_sample->mutable_image_frame(); | |
123 InitializeFakeImageFrameData(image_frame); | |
124 } | |
125 media_perception::Diagnostics diagnostics_result = | |
126 media_perception::DiagnosticsProtoToIdl(diagnostics); | |
127 ASSERT_THAT(*diagnostics_result.perception_samples, | |
128 testing::SizeIs(kNumSamples)); | |
129 for (int i = 0; i < kNumSamples; i++) { | |
130 const auto& perception_sample_result = | |
tbarzic
2017/05/19 17:41:04
you should use auto only when the type is obvious
Luke Sorenson
2017/05/19 18:27:16
Done.
| |
131 (*diagnostics_result.perception_samples)[i]; | |
132 const auto& frame_perception_result = | |
133 (*perception_sample_result.frame_perception); | |
134 const auto& image_frame_result = (*perception_sample_result.image_frame); | |
135 ValidateFramePerceptionResult(frame_perception_result); | |
tbarzic
2017/05/19 17:41:04
it would be nicer if the test output which sample
Luke Sorenson
2017/05/19 18:27:16
Do you know of a good way to do that?
I could jus
tbarzic
2017/05/19 20:22:38
I think SCOPED_TRACE(message); should do the job
Luke Sorenson
2017/05/19 21:45:22
Done.
| |
136 ValidateFakeImageFrameData(image_frame_result); | |
137 } | |
138 } | |
139 | |
140 TEST(MediaPerceptionConversionUtilsTest, StateProtoToIdl) { | |
141 mri::State state; | |
142 state.set_status(mri::State::RUNNING); | |
143 media_perception::State state_result = | |
144 media_perception::StateProtoToIdl(state); | |
145 EXPECT_EQ(state_result.status, media_perception::STATUS_RUNNING); | |
146 | |
147 state.set_status(mri::State::STARTED); | |
148 state.set_device_context(kTestDeviceContext); | |
149 state_result = media_perception::StateProtoToIdl(state); | |
150 EXPECT_EQ(state_result.status, media_perception::STATUS_STARTED); | |
151 EXPECT_EQ(*state_result.device_context, kTestDeviceContext); | |
152 } | |
153 | |
154 TEST(MediaPerceptionConversionUtilsTest, StateIdlToProto) { | |
155 media_perception::State state; | |
156 state.status = media_perception::STATUS_UNINITIALIZED; | |
157 mri::State state_proto = StateIdlToProto(state); | |
158 EXPECT_EQ(state_proto.status(), mri::State::UNINITIALIZED); | |
159 EXPECT_FALSE(state_proto.has_device_context()); | |
160 | |
161 state.status = media_perception::STATUS_SUSPENDED; | |
162 state.device_context.reset(new std::string(kTestDeviceContext)); | |
tbarzic
2017/05/19 17:41:04
avoid new
Luke Sorenson
2017/05/19 18:27:16
Done.
| |
163 state_proto = StateIdlToProto(state); | |
164 EXPECT_EQ(state_proto.status(), mri::State::SUSPENDED); | |
165 EXPECT_EQ(state_proto.device_context(), kTestDeviceContext); | |
166 } | |
167 | |
168 } // namespace extensions | |
OLD | NEW |