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" | |
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); | |
tbarzic
2017/05/22 17:40:57
ASSERT_TRUE(frame_perception_result.frame_id);
(si
Luke Sorenson
2017/05/22 18:40:49
Done.
| |
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]; | |
tbarzic
2017/05/22 17:40:57
frame_perception_result.entities->at(0);
Luke Sorenson
2017/05/22 18:40:49
Done.
| |
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); | |
tbarzic
2017/05/22 17:40:57
bounding_box_result_one here, before you move on t
Luke Sorenson
2017/05/22 18:40:49
Done.
| |
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 const auto* bounding_box_result_one = entity_result_one.bounding_box.get(); | |
tbarzic
2017/05/22 17:40:57
can you please use auto only where the type can ea
Luke Sorenson
2017/05/22 18:40:48
Done.
| |
67 const auto* bounding_box_result_two = entity_result_two.bounding_box.get(); | |
68 | |
69 ASSERT_TRUE(bounding_box_result_one->top_left); | |
tbarzic
2017/05/22 17:40:57
assert that bounding_box_result_one is set.
Luke Sorenson
2017/05/22 18:40:49
Done.
| |
70 EXPECT_EQ(*bounding_box_result_one->top_left->x, 10); | |
71 EXPECT_EQ(*bounding_box_result_one->top_left->y, 11); | |
72 ASSERT_TRUE(bounding_box_result_one->bottom_right); | |
73 EXPECT_EQ(*bounding_box_result_one->bottom_right->x, 12); | |
74 EXPECT_EQ(*bounding_box_result_one->bottom_right->y, 13); | |
75 EXPECT_FALSE(*bounding_box_result_one->normalized); | |
76 | |
77 ASSERT_TRUE(bounding_box_result_two->top_left); | |
78 EXPECT_EQ(*bounding_box_result_two->top_left->x, 14); | |
79 EXPECT_EQ(*bounding_box_result_two->top_left->y, 15); | |
80 EXPECT_FALSE(bounding_box_result_two->bottom_right); | |
81 EXPECT_TRUE(*bounding_box_result_two->normalized); | |
82 } | |
83 | |
84 void InitializeFakeImageFrameData(int index, mri::ImageFrame* image_frame) { | |
85 image_frame->set_width(index); | |
tbarzic
2017/05/22 17:40:57
if you want to include index in the result, frame_
Luke Sorenson
2017/05/22 18:40:49
Done.
| |
86 image_frame->set_height(2); | |
87 image_frame->set_data_length(3); | |
88 image_frame->set_pixel_data(" "); | |
89 image_frame->set_format(mri::ImageFrame::JPEG); | |
90 } | |
91 | |
92 void ValidateFakeImageFrameData( | |
93 int index, | |
94 const media_perception::ImageFrame& image_frame_result) { | |
95 EXPECT_EQ(*image_frame_result.width, index); | |
96 EXPECT_EQ(*image_frame_result.height, 2); | |
97 EXPECT_EQ(*image_frame_result.data_length, 3); | |
98 EXPECT_EQ((*image_frame_result.frame).size(), 4ul); | |
99 EXPECT_EQ(image_frame_result.format, media_perception::IMAGE_FORMAT_JPEG); | |
100 } | |
101 | |
102 } // namespace | |
103 | |
104 // Verifies that the data is converted successfully and as expected in each of | |
105 // these cases. | |
106 | |
107 TEST(MediaPerceptionConversionUtilsTest, MediaPerceptionProtoToIdl) { | |
108 mri::MediaPerception media_perception; | |
109 // Fill in fake values for the media_perception proto. | |
110 media_perception.set_timestamp(1); | |
111 mri::FramePerception* frame_perception = | |
112 media_perception.add_frame_perception(); | |
113 InitializeFakeFramePerception(frame_perception); | |
114 media_perception::MediaPerception media_perception_result = | |
115 media_perception::MediaPerceptionProtoToIdl(media_perception); | |
116 EXPECT_EQ(*media_perception_result.timestamp, 1); | |
117 ASSERT_TRUE(media_perception_result.frame_perceptions); | |
118 ASSERT_EQ(1u, media_perception_result.frame_perceptions->size()); | |
119 ValidateFramePerceptionResult( | |
120 media_perception_result.frame_perceptions->at(0)); | |
121 } | |
122 | |
123 TEST(MediaPerceptionConversionUtilsTest, DiagnosticsProtoToIdl) { | |
124 const size_t kNumSamples = 3; | |
125 mri::Diagnostics diagnostics; | |
126 for (size_t i = 0; i < kNumSamples; i++) { | |
127 mri::PerceptionSample* perception_sample = | |
128 diagnostics.add_perception_sample(); | |
129 mri::FramePerception* frame_perception = | |
130 perception_sample->mutable_frame_perception(); | |
131 InitializeFakeFramePerception(frame_perception); | |
132 mri::ImageFrame* image_frame = perception_sample->mutable_image_frame(); | |
133 InitializeFakeImageFrameData(i, image_frame); | |
134 } | |
135 media_perception::Diagnostics diagnostics_result = | |
136 media_perception::DiagnosticsProtoToIdl(diagnostics); | |
137 ASSERT_EQ(kNumSamples, diagnostics_result.perception_samples->size()); | |
138 for (size_t i = 0; i < kNumSamples; i++) { | |
139 testing::Message scope_message; | |
140 scope_message << "Sample number: " << i; | |
141 SCOPED_TRACE(scope_message); | |
tbarzic
2017/05/22 17:40:57
SCOPED_TRACE(testing::Message() << "Sample number
Luke Sorenson
2017/05/22 18:40:48
Done.
| |
142 const media_perception::PerceptionSample& perception_sample_result = | |
143 (*diagnostics_result.perception_samples)[i]; | |
tbarzic
2017/05/22 17:40:57
const media_perception::FramePerception* frame_per
Luke Sorenson
2017/05/22 18:40:48
Done.
| |
144 const media_perception::FramePerception& frame_perception_result = | |
145 (*perception_sample_result.frame_perception); | |
146 const media_perception::ImageFrame& image_frame_result = | |
147 (*perception_sample_result.image_frame); | |
148 ValidateFramePerceptionResult(frame_perception_result); | |
149 ValidateFakeImageFrameData(i, image_frame_result); | |
150 } | |
151 } | |
152 | |
153 TEST(MediaPerceptionConversionUtilsTest, StateProtoToIdl) { | |
154 mri::State state; | |
155 state.set_status(mri::State::RUNNING); | |
156 media_perception::State state_result = | |
157 media_perception::StateProtoToIdl(state); | |
158 EXPECT_EQ(state_result.status, media_perception::STATUS_RUNNING); | |
159 | |
160 state.set_status(mri::State::STARTED); | |
161 state.set_device_context(kTestDeviceContext); | |
162 state_result = media_perception::StateProtoToIdl(state); | |
163 EXPECT_EQ(state_result.status, media_perception::STATUS_STARTED); | |
164 EXPECT_EQ(*state_result.device_context, kTestDeviceContext); | |
tbarzic
2017/05/22 17:40:57
ASSERT_TRUE(state_result.device_context)
Luke Sorenson
2017/05/22 18:40:48
Done.
| |
165 } | |
166 | |
167 TEST(MediaPerceptionConversionUtilsTest, StateIdlToProto) { | |
168 media_perception::State state; | |
169 state.status = media_perception::STATUS_UNINITIALIZED; | |
170 mri::State state_proto = StateIdlToProto(state); | |
171 EXPECT_EQ(state_proto.status(), mri::State::UNINITIALIZED); | |
172 EXPECT_FALSE(state_proto.has_device_context()); | |
173 | |
174 state.status = media_perception::STATUS_SUSPENDED; | |
175 state.device_context = base::MakeUnique<std::string>(kTestDeviceContext); | |
176 state_proto = StateIdlToProto(state); | |
177 EXPECT_EQ(state_proto.status(), mri::State::SUSPENDED); | |
178 EXPECT_EQ(state_proto.device_context(), kTestDeviceContext); | |
179 } | |
180 | |
181 } // namespace extensions | |
OLD | NEW |