OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" |
| 11 #include "chromeos/dbus/fake_media_analytics_client.h" |
| 12 #include "chromeos/dbus/fake_upstart_client.h" |
| 13 #include "chromeos/dbus/media_analytics_client.h" |
| 14 #include "chromeos/dbus/upstart_client.h" |
| 15 #include "content/public/test/test_browser_context.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace media_perception = extensions::api::media_perception_private; |
| 20 |
| 21 namespace extensions { |
| 22 |
| 23 using CallbackStatus = MediaPerceptionAPIManager::CallbackStatus; |
| 24 |
| 25 namespace { |
| 26 |
| 27 void StallStateCallback(CallbackStatus result_status, |
| 28 media_perception::State result_state) {} |
| 29 |
| 30 void RecordStatusAndRunClosure(base::Closure quit_run_loop, |
| 31 CallbackStatus* status, |
| 32 CallbackStatus result_status, |
| 33 media_perception::State result_state) { |
| 34 *status = result_status; |
| 35 quit_run_loop.Run(); |
| 36 } |
| 37 |
| 38 CallbackStatus SetState(MediaPerceptionAPIManager* manager, |
| 39 const media_perception::State& state) { |
| 40 base::RunLoop run_loop; |
| 41 CallbackStatus status; |
| 42 manager->SetState(state, base::Bind(&RecordStatusAndRunClosure, |
| 43 run_loop.QuitClosure(), &status)); |
| 44 run_loop.Run(); |
| 45 return status; |
| 46 } |
| 47 |
| 48 CallbackStatus GetState(MediaPerceptionAPIManager* manager) { |
| 49 base::RunLoop run_loop; |
| 50 CallbackStatus status; |
| 51 manager->GetState( |
| 52 base::Bind(&RecordStatusAndRunClosure, run_loop.QuitClosure(), &status)); |
| 53 run_loop.Run(); |
| 54 return status; |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 class MediaPerceptionAPIManagerTest : public testing::Test { |
| 60 public: |
| 61 MediaPerceptionAPIManagerTest() |
| 62 : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {} |
| 63 |
| 64 void SetUp() override { |
| 65 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = |
| 66 chromeos::DBusThreadManager::GetSetterForTesting(); |
| 67 auto media_analytics_client = |
| 68 base::MakeUnique<chromeos::FakeMediaAnalyticsClient>(); |
| 69 media_analytics_client_ = media_analytics_client.get(); |
| 70 dbus_setter->SetMediaAnalyticsClient(std::move(media_analytics_client)); |
| 71 |
| 72 auto upstart_client = base::MakeUnique<chromeos::FakeUpstartClient>(); |
| 73 upstart_client_ = upstart_client.get(); |
| 74 dbus_setter->SetUpstartClient(std::move(upstart_client)); |
| 75 |
| 76 manager_ = base::MakeUnique<MediaPerceptionAPIManager>(&browser_context_); |
| 77 } |
| 78 |
| 79 void TearDown() override { |
| 80 // Need to make sure that the MediaPerceptionAPIManager is destructed before |
| 81 // the DbusThreadManager. |
| 82 manager_.reset(); |
| 83 chromeos::DBusThreadManager::Shutdown(); |
| 84 } |
| 85 |
| 86 std::unique_ptr<MediaPerceptionAPIManager> manager_; |
| 87 content::TestBrowserContext browser_context_; |
| 88 content::TestBrowserThreadBundle thread_bundle_; |
| 89 |
| 90 // Ownership of both is passed on to chromeos::DbusThreadManager. |
| 91 chromeos::FakeMediaAnalyticsClient* media_analytics_client_; |
| 92 chromeos::FakeUpstartClient* upstart_client_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(MediaPerceptionAPIManagerTest); |
| 95 }; |
| 96 |
| 97 TEST_F(MediaPerceptionAPIManagerTest, UpstartFailure) { |
| 98 upstart_client_->SetStartMediaAnalyticsWillSucceed(false); |
| 99 media_perception::State state; |
| 100 state.status = media_perception::STATUS_RUNNING; |
| 101 EXPECT_EQ(CallbackStatus::PROCESS_IDLE_ERROR, |
| 102 SetState(manager_.get(), state)); |
| 103 } |
| 104 |
| 105 TEST_F(MediaPerceptionAPIManagerTest, UpstartStall) { |
| 106 upstart_client_->SetStartMediaAnalyticsWillStall(true); |
| 107 media_perception::State state; |
| 108 state.status = media_perception::STATUS_RUNNING; |
| 109 // This call will stall so we don't wait for it to get callback. |
| 110 manager_->SetState(state, base::Bind(&StallStateCallback)); |
| 111 EXPECT_EQ(CallbackStatus::PROCESS_LAUNCHING_ERROR, GetState(manager_.get())); |
| 112 EXPECT_EQ(CallbackStatus::PROCESS_LAUNCHING_ERROR, |
| 113 SetState(manager_.get(), state)); |
| 114 } |
| 115 |
| 116 TEST_F(MediaPerceptionAPIManagerTest, MediaAnalyticsDbusError) { |
| 117 media_perception::State state; |
| 118 state.status = media_perception::STATUS_RUNNING; |
| 119 EXPECT_EQ(CallbackStatus::SUCCESS, SetState(manager_.get(), state)); |
| 120 // Disable the functionality of the fake process. |
| 121 media_analytics_client_->set_process_running(false); |
| 122 EXPECT_EQ(CallbackStatus::DBUS_ERROR, GetState(manager_.get())); |
| 123 EXPECT_EQ(CallbackStatus::DBUS_ERROR, SetState(manager_.get(), state)); |
| 124 } |
| 125 |
| 126 } // namespace extensions |
OLD | NEW |