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