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