| 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 <queue> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 13 #include "chromeos/dbus/fake_media_analytics_client.h" | |
| 14 #include "chromeos/dbus/fake_upstart_client.h" | |
| 15 #include "chromeos/dbus/media_analytics_client.h" | |
| 16 #include "chromeos/dbus/upstart_client.h" | |
| 17 #include "content/public/test/test_browser_context.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace media_perception = extensions::api::media_perception_private; | |
| 22 | |
| 23 namespace chromeos { | |
| 24 namespace { | |
| 25 | |
| 26 class TestUpstartClient : public FakeUpstartClient { | |
| 27 public: | |
| 28 TestUpstartClient() : enqueue_requests_(false) {} | |
| 29 | |
| 30 ~TestUpstartClient() override{}; | |
| 31 | |
| 32 // Overrides behavior to queue start requests. | |
| 33 void StartMediaAnalytics(const UpstartCallback& callback) override { | |
| 34 pending_upstart_request_callbacks_.push(callback); | |
| 35 if (!enqueue_requests_) { | |
| 36 HandleNextUpstartRequest(true); | |
| 37 return; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 // Triggers the next queue'd start request to succeed or fail. | |
| 42 bool HandleNextUpstartRequest(bool should_succeed) { | |
| 43 if (pending_upstart_request_callbacks_.empty()) | |
| 44 return false; | |
| 45 | |
| 46 UpstartCallback callback = pending_upstart_request_callbacks_.front(); | |
| 47 pending_upstart_request_callbacks_.pop(); | |
| 48 | |
| 49 if (!should_succeed) { | |
| 50 callback.Run(false); | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 FakeUpstartClient::StartMediaAnalytics(callback); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 void set_enqueue_requests(bool enqueue_requests) { | |
| 59 enqueue_requests_ = enqueue_requests; | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 std::queue<UpstartCallback> pending_upstart_request_callbacks_; | |
| 64 | |
| 65 bool enqueue_requests_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(TestUpstartClient); | |
| 68 }; | |
| 69 | |
| 70 } // namespace | |
| 71 } // namespace chromeos | |
| 72 | |
| 73 namespace extensions { | |
| 74 | |
| 75 using CallbackStatus = MediaPerceptionAPIManager::CallbackStatus; | |
| 76 | |
| 77 namespace { | |
| 78 | |
| 79 void RecordStatusAndRunClosure(base::Closure quit_run_loop, | |
| 80 CallbackStatus* status, | |
| 81 CallbackStatus result_status, | |
| 82 media_perception::State result_state) { | |
| 83 *status = result_status; | |
| 84 quit_run_loop.Run(); | |
| 85 } | |
| 86 | |
| 87 CallbackStatus SetStateAndWaitForResponse( | |
| 88 MediaPerceptionAPIManager* manager, | |
| 89 const media_perception::State& state) { | |
| 90 base::RunLoop run_loop; | |
| 91 CallbackStatus status; | |
| 92 manager->SetState(state, base::Bind(&RecordStatusAndRunClosure, | |
| 93 run_loop.QuitClosure(), &status)); | |
| 94 run_loop.Run(); | |
| 95 return status; | |
| 96 } | |
| 97 | |
| 98 CallbackStatus GetStateAndWaitForResponse(MediaPerceptionAPIManager* manager) { | |
| 99 base::RunLoop run_loop; | |
| 100 CallbackStatus status; | |
| 101 manager->GetState( | |
| 102 base::Bind(&RecordStatusAndRunClosure, run_loop.QuitClosure(), &status)); | |
| 103 run_loop.Run(); | |
| 104 return status; | |
| 105 } | |
| 106 | |
| 107 } // namespace | |
| 108 | |
| 109 class MediaPerceptionAPIManagerTest : public testing::Test { | |
| 110 public: | |
| 111 MediaPerceptionAPIManagerTest() | |
| 112 : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {} | |
| 113 | |
| 114 void SetUp() override { | |
| 115 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = | |
| 116 chromeos::DBusThreadManager::GetSetterForTesting(); | |
| 117 auto media_analytics_client = | |
| 118 base::MakeUnique<chromeos::FakeMediaAnalyticsClient>(); | |
| 119 media_analytics_client_ = media_analytics_client.get(); | |
| 120 dbus_setter->SetMediaAnalyticsClient(std::move(media_analytics_client)); | |
| 121 | |
| 122 auto upstart_client = base::MakeUnique<chromeos::TestUpstartClient>(); | |
| 123 upstart_client_ = upstart_client.get(); | |
| 124 dbus_setter->SetUpstartClient(std::move(upstart_client)); | |
| 125 | |
| 126 manager_ = base::MakeUnique<MediaPerceptionAPIManager>(&browser_context_); | |
| 127 } | |
| 128 | |
| 129 void TearDown() override { | |
| 130 // Need to make sure that the MediaPerceptionAPIManager is destructed before | |
| 131 // the DbusThreadManager. | |
| 132 manager_.reset(); | |
| 133 chromeos::DBusThreadManager::Shutdown(); | |
| 134 } | |
| 135 | |
| 136 std::unique_ptr<MediaPerceptionAPIManager> manager_; | |
| 137 | |
| 138 // Ownership of both is passed on to chromeos::DbusThreadManager. | |
| 139 chromeos::FakeMediaAnalyticsClient* media_analytics_client_; | |
| 140 chromeos::TestUpstartClient* upstart_client_; | |
| 141 | |
| 142 private: | |
| 143 content::TestBrowserContext browser_context_; | |
| 144 content::TestBrowserThreadBundle thread_bundle_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(MediaPerceptionAPIManagerTest); | |
| 147 }; | |
| 148 | |
| 149 TEST_F(MediaPerceptionAPIManagerTest, UpstartFailure) { | |
| 150 upstart_client_->set_enqueue_requests(true); | |
| 151 media_perception::State state; | |
| 152 state.status = media_perception::STATUS_RUNNING; | |
| 153 | |
| 154 base::RunLoop run_loop; | |
| 155 CallbackStatus status; | |
| 156 manager_->SetState(state, base::Bind(&RecordStatusAndRunClosure, | |
| 157 run_loop.QuitClosure(), &status)); | |
| 158 EXPECT_TRUE(upstart_client_->HandleNextUpstartRequest(false)); | |
| 159 run_loop.Run(); | |
| 160 EXPECT_EQ(CallbackStatus::PROCESS_IDLE_ERROR, status); | |
| 161 | |
| 162 // Check that after a failed request, setState RUNNING will go through. | |
| 163 upstart_client_->set_enqueue_requests(false); | |
| 164 EXPECT_EQ(CallbackStatus::SUCCESS, | |
| 165 SetStateAndWaitForResponse(manager_.get(), state)); | |
| 166 } | |
| 167 | |
| 168 TEST_F(MediaPerceptionAPIManagerTest, UpstartStall) { | |
| 169 upstart_client_->set_enqueue_requests(true); | |
| 170 media_perception::State state; | |
| 171 state.status = media_perception::STATUS_RUNNING; | |
| 172 | |
| 173 base::RunLoop run_loop; | |
| 174 CallbackStatus status; | |
| 175 manager_->SetState(state, base::Bind(&RecordStatusAndRunClosure, | |
| 176 run_loop.QuitClosure(), &status)); | |
| 177 | |
| 178 EXPECT_EQ(CallbackStatus::PROCESS_LAUNCHING_ERROR, | |
| 179 GetStateAndWaitForResponse(manager_.get())); | |
| 180 EXPECT_EQ(CallbackStatus::PROCESS_LAUNCHING_ERROR, | |
| 181 SetStateAndWaitForResponse(manager_.get(), state)); | |
| 182 EXPECT_TRUE(upstart_client_->HandleNextUpstartRequest(true)); | |
| 183 run_loop.Run(); | |
| 184 EXPECT_EQ(CallbackStatus::SUCCESS, status); | |
| 185 | |
| 186 // Verify that after the slow start, things works as normal. | |
| 187 upstart_client_->set_enqueue_requests(false); | |
| 188 EXPECT_EQ(CallbackStatus::SUCCESS, | |
| 189 GetStateAndWaitForResponse(manager_.get())); | |
| 190 state.status = media_perception::STATUS_SUSPENDED; | |
| 191 EXPECT_EQ(CallbackStatus::SUCCESS, | |
| 192 SetStateAndWaitForResponse(manager_.get(), state)); | |
| 193 } | |
| 194 | |
| 195 TEST_F(MediaPerceptionAPIManagerTest, MediaAnalyticsDbusError) { | |
| 196 media_perception::State state; | |
| 197 state.status = media_perception::STATUS_RUNNING; | |
| 198 EXPECT_EQ(CallbackStatus::SUCCESS, | |
| 199 SetStateAndWaitForResponse(manager_.get(), state)); | |
| 200 // Disable the functionality of the fake process. | |
| 201 media_analytics_client_->set_process_running(false); | |
| 202 EXPECT_EQ(CallbackStatus::DBUS_ERROR, | |
| 203 GetStateAndWaitForResponse(manager_.get())); | |
| 204 EXPECT_EQ(CallbackStatus::DBUS_ERROR, | |
| 205 SetStateAndWaitForResponse(manager_.get(), state)); | |
| 206 } | |
| 207 | |
| 208 } // namespace extensions | |
| OLD | NEW |