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 const char kTestDeviceContext[] = "Video camera"; | |
17 } // namespace | |
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); | |
tbarzic
2017/05/17 21:04:42
move helper methods to namespace {}
Luke Sorenson
2017/05/17 23:07:48
Done.
| |
23 frame_perception->set_timestamp(5); | |
24 mri::Entity* e_one = frame_perception->add_entity(); | |
25 e_one->set_id(6); | |
26 e_one->set_type(mri::Entity::FACE); | |
27 e_one->set_confidence(7); | |
28 mri::Entity* e_two = frame_perception->add_entity(); | |
29 e_two->set_id(8); | |
30 e_two->set_type(mri::Entity::PERSON); | |
31 e_two->set_confidence(9); | |
32 mri::BoundingBox* b_one = e_one->mutable_bounding_box(); | |
33 b_one->mutable_top_left()->set_x(10); | |
34 b_one->mutable_top_left()->set_y(11); | |
tbarzic
2017/05/17 21:04:41
can you tests cases where some of these are null?
Luke Sorenson
2017/05/17 23:07:48
Done.
| |
35 b_one->mutable_bottom_right()->set_x(12); | |
36 b_one->mutable_bottom_right()->set_y(13); | |
37 b_one->set_normalized(false); | |
38 mri::BoundingBox* b_two = e_two->mutable_bounding_box(); | |
39 b_two->mutable_top_left()->set_x(14); | |
40 b_two->mutable_top_left()->set_y(15); | |
41 b_two->mutable_bottom_right()->set_x(16); | |
42 b_two->mutable_bottom_right()->set_y(17); | |
43 b_two->set_normalized(true); | |
44 } | |
45 | |
46 void ValidateFramePerceptionResult( | |
47 const media_perception::FramePerception& fp_result) { | |
48 EXPECT_EQ(*fp_result.frame_id, 2); | |
49 EXPECT_EQ(*fp_result.frame_width_in_px, 3); | |
50 EXPECT_EQ(*fp_result.frame_height_in_px, 4); | |
51 EXPECT_EQ(*fp_result.timestamp, 5); | |
52 const auto& e_result_one = (*fp_result.entities)[0]; | |
53 EXPECT_EQ(*e_result_one.id, 6); | |
54 EXPECT_EQ(*e_result_one.confidence, 7); | |
55 EXPECT_EQ(e_result_one.type, media_perception::ENTITY_TYPE_FACE); | |
56 const auto& e_result_two = (*fp_result.entities)[1]; | |
57 EXPECT_EQ(*e_result_two.id, 8); | |
58 EXPECT_EQ(*e_result_two.confidence, 9); | |
59 EXPECT_EQ(e_result_two.type, media_perception::ENTITY_TYPE_PERSON); | |
60 const auto& b_result_one = *e_result_one.bounding_box; | |
61 EXPECT_EQ(*(*b_result_one.top_left).x, 10); | |
62 EXPECT_EQ(*(*b_result_one.top_left).y, 11); | |
63 EXPECT_EQ(*(*b_result_one.bottom_right).x, 12); | |
64 EXPECT_EQ(*(*b_result_one.bottom_right).y, 13); | |
65 EXPECT_FALSE(*b_result_one.normalized); | |
66 const auto& b_result_two = *e_result_two.bounding_box; | |
67 EXPECT_EQ(*(*b_result_two.top_left).x, 14); | |
68 EXPECT_EQ(*(*b_result_two.top_left).y, 15); | |
69 EXPECT_EQ(*(*b_result_two.bottom_right).x, 16); | |
70 EXPECT_EQ(*(*b_result_two.bottom_right).y, 17); | |
71 EXPECT_TRUE(*b_result_two.normalized); | |
72 } | |
73 | |
74 void InitializeFakeImageFrameData(mri::ImageFrame* image_frame) { | |
75 image_frame->set_width(1); | |
76 image_frame->set_height(2); | |
77 image_frame->set_data_length(3); | |
78 image_frame->set_pixel_data(" "); | |
79 image_frame->set_format(mri::ImageFrame::JPEG); | |
80 } | |
81 | |
82 void ValidateFakeImageFrameData(const media_perception::ImageFrame& if_result) { | |
83 EXPECT_EQ(*if_result.width, 1); | |
84 EXPECT_EQ(*if_result.height, 2); | |
85 EXPECT_EQ(*if_result.data_length, 3); | |
86 EXPECT_EQ((*if_result.frame).size(), 4ul); | |
87 EXPECT_EQ(if_result.format, media_perception::IMAGE_FORMAT_JPEG); | |
88 } | |
89 | |
90 // Verifies that the data is converted successfully and as expected in each of | |
91 // these cases. | |
92 | |
93 TEST(ConversionUtilsTest, MediaPerceptionProtoToIdl) { | |
tbarzic
2017/05/17 21:04:41
rename tests to MediaPerceptionConversionUtilsTest
Luke Sorenson
2017/05/17 23:07:49
Done.
| |
94 mri::MediaPerception media_perception; | |
95 // Fill in fake values for the media_perception proto. | |
96 media_perception.set_timestamp(1); | |
97 mri::FramePerception* frame_perception = | |
98 media_perception.add_frame_perception(); | |
99 InitializeFakeFramePerception(frame_perception); | |
100 media_perception::MediaPerception mp_result = | |
tbarzic
2017/05/17 21:04:41
add a new line after initialization is done
also,
Luke Sorenson
2017/05/17 23:07:48
Done.
| |
101 MediaPerceptionProtoToIdl(media_perception); | |
102 EXPECT_EQ(*mp_result.timestamp, 1); | |
103 const auto& fp_result = (*mp_result.frame_perceptions)[0]; | |
tbarzic
2017/05/17 21:04:41
assert that frame perception is set and its size i
Luke Sorenson
2017/05/17 23:07:48
Done.
| |
104 ValidateFramePerceptionResult(fp_result); | |
105 } | |
106 | |
107 TEST(ConversionUtilsTest, DiagnosticsProtoToIdl) { | |
108 const int kNumSamples = 3; | |
109 mri::Diagnostics diagnostics; | |
110 for (int i = 0; i < kNumSamples; i++) { | |
111 mri::PerceptionSample* perception_sample = | |
112 diagnostics.add_perception_sample(); | |
113 mri::FramePerception* frame_perception = | |
114 perception_sample->mutable_frame_perception(); | |
115 InitializeFakeFramePerception(frame_perception); | |
116 mri::ImageFrame* image_frame = perception_sample->mutable_image_frame(); | |
117 InitializeFakeImageFrameData(image_frame); | |
118 } | |
119 media_perception::Diagnostics d_result = DiagnosticsProtoToIdl(diagnostics); | |
120 ASSERT_THAT(*d_result.perception_samples, testing::SizeIs(kNumSamples)); | |
tbarzic
2017/05/17 21:04:41
ASSERT_EQ(kNumSamples, d_result.perception_samples
Luke Sorenson
2017/05/17 23:07:48
For testing the size of stl objects, I think I am
tbarzic
2017/05/18 19:28:01
testing::SizeIs is not really common in Chrome - i
Luke Sorenson
2017/05/18 21:24:16
Done.
| |
121 for (int i = 0; i < kNumSamples; i++) { | |
122 const auto& ps_result = (*d_result.perception_samples)[i]; | |
123 const auto& fp_result = (*ps_result.frame_perception); | |
124 const auto& if_result = (*ps_result.image_frame); | |
125 ValidateFramePerceptionResult(fp_result); | |
126 ValidateFakeImageFrameData(if_result); | |
127 } | |
128 } | |
129 | |
130 TEST(ConversionUtilsTest, StateProtoToIdl) { | |
131 mri::State state; | |
132 state.set_status(mri::State::RUNNING); | |
133 media_perception::State s_result = StateProtoToIdl(state); | |
134 EXPECT_EQ(s_result.status, media_perception::STATUS_RUNNING); | |
135 | |
136 state.set_status(mri::State::STARTED); | |
137 state.set_device_context(kTestDeviceContext); | |
138 s_result = StateProtoToIdl(state); | |
139 EXPECT_EQ(s_result.status, media_perception::STATUS_STARTED); | |
140 EXPECT_EQ(*s_result.device_context, kTestDeviceContext); | |
141 } | |
142 | |
143 TEST(ConversionUtilsTest, StateIdlToProto) { | |
144 media_perception::State state; | |
145 state.status = media_perception::STATUS_UNINITIALIZED; | |
146 mri::State s_proto = StateIdlToProto(state); | |
147 EXPECT_EQ(s_proto.status(), mri::State::UNINITIALIZED); | |
148 EXPECT_FALSE(s_proto.has_device_context()); | |
149 | |
150 state.status = media_perception::STATUS_SUSPENDED; | |
151 state.device_context.reset(new std::string(kTestDeviceContext)); | |
152 s_proto = StateIdlToProto(state); | |
153 EXPECT_EQ(s_proto.status(), mri::State::SUSPENDED); | |
154 EXPECT_EQ(s_proto.device_context(), kTestDeviceContext); | |
155 } | |
156 | |
157 } // namespace extensions | |
OLD | NEW |