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/media_perception_api_m
anager.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); |
| 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); |
| 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 // Verifies that the data is converted successfully and as expected in each of |
| 74 // these cases. |
| 75 |
| 76 TEST(MediaPerceptionAPIManagerTest, MediaPerceptionProtoToIdl) { |
| 77 mri::MediaPerception media_perception; |
| 78 // Fill in fake values for the media_perception proto. |
| 79 media_perception.set_timestamp(1); |
| 80 mri::FramePerception* frame_perception = |
| 81 media_perception.add_frame_perception(); |
| 82 InitializeFakeFramePerception(frame_perception); |
| 83 media_perception::MediaPerception mp_result = |
| 84 MediaPerceptionProtoToIdl(media_perception); |
| 85 EXPECT_EQ(*mp_result.timestamp, 1); |
| 86 const auto& fp_result = (*mp_result.frame_perceptions)[0]; |
| 87 ValidateFramePerceptionResult(fp_result); |
| 88 } |
| 89 |
| 90 TEST(MediaPerceptionAPIManagerTest, DiagnosticsProtoToIdl) { |
| 91 const int kNumSamples = 3; |
| 92 mri::Diagnostics diagnostics; |
| 93 for (int i = 0; i < kNumSamples; i++) { |
| 94 mri::PerceptionSample* perception_sample = |
| 95 diagnostics.add_perception_sample(); |
| 96 mri::FramePerception* frame_perception = |
| 97 perception_sample->mutable_frame_perception(); |
| 98 InitializeFakeFramePerception(frame_perception); |
| 99 // TODO(lasoren): Test with PerceptionSample protos that have image data. |
| 100 } |
| 101 media_perception::Diagnostics d_result = DiagnosticsProtoToIdl(diagnostics); |
| 102 ASSERT_THAT(*d_result.perception_samples, testing::SizeIs(kNumSamples)); |
| 103 for (int i = 0; i < kNumSamples; i++) { |
| 104 const auto& ps_result = (*d_result.perception_samples)[i]; |
| 105 const auto& fp_result = (*ps_result.frame_perception); |
| 106 ValidateFramePerceptionResult(fp_result); |
| 107 } |
| 108 } |
| 109 |
| 110 TEST(MediaPerceptionAPIManagerTest, StateProtoToIdl) { |
| 111 mri::State state; |
| 112 state.set_status(mri::State::RUNNING); |
| 113 media_perception::State s_result = StateProtoToIdl(state); |
| 114 EXPECT_EQ(s_result.status, media_perception::STATUS_RUNNING); |
| 115 |
| 116 state.set_status(mri::State::STARTED); |
| 117 state.set_device_context(kTestDeviceContext); |
| 118 s_result = StateProtoToIdl(state); |
| 119 EXPECT_EQ(s_result.status, media_perception::STATUS_STARTED); |
| 120 EXPECT_EQ(*s_result.device_context, kTestDeviceContext); |
| 121 } |
| 122 |
| 123 TEST(MediaPerceptionAPIManagerTest, StateIdlToProto) { |
| 124 media_perception::State state; |
| 125 state.status = media_perception::STATUS_UNINITIALIZED; |
| 126 mri::State s_proto = StateIdlToProto(state); |
| 127 EXPECT_EQ(s_proto.status(), mri::State::UNINITIALIZED); |
| 128 EXPECT_FALSE(s_proto.has_device_context()); |
| 129 |
| 130 state.status = media_perception::STATUS_SUSPENDED; |
| 131 state.device_context.reset(new std::string(kTestDeviceContext)); |
| 132 s_proto = StateIdlToProto(state); |
| 133 EXPECT_EQ(s_proto.status(), mri::State::SUSPENDED); |
| 134 EXPECT_EQ(s_proto.device_context(), kTestDeviceContext); |
| 135 } |
| 136 |
| 137 } // namespace extensions |
OLD | NEW |