OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "base/message_loop/message_loop.h" | |
6 #include "chrome/browser/media/router/issues_observer.h" | |
7 #include "chrome/browser/media/router/media_router_factory.h" | |
8 #include "chrome/browser/media/router/media_router_mojo_impl.h" | |
9 #include "chrome/browser/media/router/media_router_type_converters.h" | |
10 #include "chrome/browser/media/router/media_source_helper.h" | |
11 #include "chrome/browser/media/router/test_helper.h" | |
12 #include "chrome/browser/ui/browser.h" | |
13 #include "chrome/browser/ui/toolbar/media_router_action.h" | |
14 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | |
15 #include "chrome/browser/ui/webui/media_router/media_router_test.h" | |
16 #include "chrome/grit/generated_resources.h" | |
17 #include "chrome/test/base/test_browser_window.h" | |
18 #include "chrome/test/base/testing_profile.h" | |
19 #include "content/public/test/browser_test_utils.h" | |
20 #include "content/public/test/test_utils.h" | |
21 #include "extensions/browser/process_manager.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 #include "ui/base/l10n/l10n_util.h" | |
24 #include "ui/base/resource/resource_bundle.h" | |
25 | |
26 class TestActionIssuesObserver : public ActionIssuesObserver { | |
27 public: | |
28 explicit TestActionIssuesObserver(media_router::MediaRouter* router, | |
29 MediaRouterAction* action) | |
30 : ActionIssuesObserver(router, action) {} | |
31 ~TestActionIssuesObserver() override {}; | |
32 | |
33 // IssuesObserver: | |
34 void OnIssueUpdated(const media_router::Issue* issue) override { | |
35 ActionIssuesObserver::OnIssueUpdated(issue); | |
Kevin M
2015/07/31 18:18:40
If this method just delegates to the superclass, t
apacible
2015/08/01 02:05:29
Removed.
| |
36 } | |
37 }; | |
38 | |
39 class TestActionMediaRoutesObserver : public ActionMediaRoutesObserver { | |
40 public: | |
41 explicit TestActionMediaRoutesObserver(media_router::MediaRouter* router, | |
42 MediaRouterAction* action) | |
43 : ActionMediaRoutesObserver(router, action) {} | |
44 ~TestActionMediaRoutesObserver() override {}; | |
45 | |
46 // MediaRoutesObserver: | |
47 void OnRoutesUpdated(const std::vector<media_router::MediaRoute>& routes) | |
48 override { | |
49 ActionMediaRoutesObserver::OnRoutesUpdated(routes); | |
50 } | |
51 }; | |
52 | |
53 class TestMediaRouterAction : public MediaRouterAction { | |
Kevin M
2015/07/31 18:18:40
It would be better to test this class directly ins
apacible
2015/08/01 02:05:29
I need a valid MediaRouter* so I subclassed it to
| |
54 public: | |
55 TestMediaRouterAction(Browser* browser, | |
56 extensions::ProcessManager* process_manager) | |
57 : MediaRouterAction(browser), | |
58 mock_media_router_( | |
59 new media_router::MediaRouterMojoImpl(process_manager)) { | |
60 } | |
61 ~TestMediaRouterAction() override {}; | |
62 | |
63 // ToolbarActionViewController: | |
64 void SetDelegate(ToolbarActionViewDelegate* delegate) override { | |
65 delegate_ = delegate; | |
66 | |
67 if (delegate_) { | |
68 issues_observer_.reset(new TestActionIssuesObserver( | |
69 GetMediaRouter(), this)); | |
70 routes_observer_.reset(new TestActionMediaRoutesObserver( | |
71 GetMediaRouter(), this)); | |
72 } else { | |
73 issues_observer_.reset(); | |
74 routes_observer_.reset(); | |
75 } | |
76 } | |
77 | |
78 // MediaRouterAction: | |
79 media_router::MediaRouter* GetMediaRouter() override { | |
80 return mock_media_router_; | |
81 } | |
82 | |
83 private: | |
84 media_router::MediaRouterMojoImpl* mock_media_router_; | |
85 | |
86 friend class MediaRouterActionUnitTest; | |
87 FRIEND_TEST_ALL_PREFIXES(MediaRouterActionUnitTest, Initialization); | |
88 FRIEND_TEST_ALL_PREFIXES(MediaRouterActionUnitTest, UpdateIssues); | |
89 FRIEND_TEST_ALL_PREFIXES(MediaRouterActionUnitTest, UpdateRoutes); | |
90 FRIEND_TEST_ALL_PREFIXES(MediaRouterActionUnitTest, UpdateIssuesAndRoutes); | |
91 }; | |
92 | |
93 class TestToolbarActionViewDelegate : public ToolbarActionViewDelegate { | |
94 public: | |
95 TestToolbarActionViewDelegate() {} | |
96 | |
97 // ToolbarActionViewDelegate: | |
98 ToolbarActionViewController* GetPreferredPopupViewController() { | |
99 return nullptr; | |
100 } | |
101 content::WebContents* GetCurrentWebContents() const override { | |
102 return nullptr; | |
103 } | |
104 void UpdateState() override {}; | |
105 bool IsMenuRunning() const override { return false; }; | |
106 void OnPopupShown(bool by_user) override {}; | |
107 void OnPopupClosed() override {}; | |
108 | |
109 protected: | |
110 ~TestToolbarActionViewDelegate() override {}; | |
111 }; | |
112 | |
113 class MediaRouterActionUnitTest : public MediaRouterTest { | |
114 public: | |
115 MediaRouterActionUnitTest() | |
116 : fake_issue_notification_(media_router::Issue("title notification", | |
117 "message notification", | |
118 media_router::IssueAction(media_router::IssueAction::TYPE_OK), | |
119 std::vector<media_router::IssueAction>(), | |
120 "route_id", | |
121 media_router::Issue::NOTIFICATION, | |
122 false, "")), | |
123 fake_issue_warning_(media_router::Issue("title warning", | |
124 "message warning", | |
125 media_router::IssueAction( | |
126 media_router::IssueAction::TYPE_LEARN_MORE), | |
127 std::vector<media_router::IssueAction>(), | |
128 "route_id", | |
129 media_router::Issue::WARNING, | |
130 false, | |
131 "www.google.com")), | |
132 fake_issue_fatal_(media_router::Issue("title fatal", | |
133 "message fatal", | |
134 media_router::IssueAction(media_router::IssueAction::TYPE_OK), | |
135 std::vector<media_router::IssueAction>(), | |
136 "route_id", | |
137 media_router::Issue::FATAL, | |
138 true, | |
139 "")), | |
140 fake_sink1_("fakeSink1", "Fake Sink 1"), | |
141 fake_sink2_("fakeSink2", "Fake Sink 2"), | |
142 fake_source1_("fakeSource1"), | |
143 fake_source2_("fakeSource2"), | |
144 fake_route_local_("route1", fake_source1_, fake_sink1_, "desc1", | |
145 true, "path.html"), | |
146 fake_route_remote_("route2", fake_source2_, fake_sink2_, "desc2", | |
147 false, "path.html") { | |
148 } | |
149 | |
150 ~MediaRouterActionUnitTest() override {} | |
151 | |
152 // BrowserWithTestWindowTest: | |
153 void SetUp() override { | |
154 BrowserWithTestWindowTest::SetUp(); | |
155 | |
156 extensions::ProcessManager* process_manager = | |
157 extensions::ProcessManager::Get(profile()); | |
158 action_.reset(new TestMediaRouterAction(browser(), process_manager)); | |
159 } | |
160 | |
161 protected: | |
162 scoped_ptr<TestMediaRouterAction> action_; | |
163 | |
164 // Fake Issues. | |
165 media_router::Issue fake_issue_notification_; | |
166 media_router::Issue fake_issue_warning_; | |
167 media_router::Issue fake_issue_fatal_; | |
168 | |
169 // Fake Sinks. | |
170 media_router::MediaSink fake_sink1_; | |
171 media_router::MediaSink fake_sink2_; | |
172 | |
173 // Fake Sources. | |
174 media_router::MediaSource fake_source1_; | |
175 media_router::MediaSource fake_source2_; | |
176 | |
177 // Fake Routes. | |
178 media_router::MediaRoute fake_route_local_; | |
179 media_router::MediaRoute fake_route_remote_; | |
180 | |
181 | |
182 private: | |
183 DISALLOW_COPY_AND_ASSIGN(MediaRouterActionUnitTest); | |
184 }; | |
185 | |
186 // Tests the initial state of MediaRouterAction after construction. | |
187 TEST_F(MediaRouterActionUnitTest, Initialization) { | |
188 EXPECT_EQ("media_router_action", action_->id_); | |
189 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_TITLE), action_->name_); | |
190 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
191 EXPECT_FALSE(action_->issue_); | |
192 EXPECT_FALSE(action_->local_active_route_exists_); | |
193 EXPECT_FALSE(action_->delegate_); | |
194 EXPECT_FALSE(action_->issues_observer_); | |
195 EXPECT_FALSE(action_->routes_observer_); | |
196 } | |
197 | |
198 // Tests the state of the MediaRouterAction based on updates to issues. | |
199 TEST_F(MediaRouterActionUnitTest, UpdateIssues) { | |
200 // Set the delegate to register the observers. | |
201 action_->SetDelegate(new TestToolbarActionViewDelegate()); | |
202 media_router::IssuesObserver* issues_observer = | |
203 action_->issues_observer_.get(); | |
204 EXPECT_TRUE(issues_observer); | |
205 | |
206 // Initially, there are no issues. | |
207 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
208 | |
209 // Don't |state_| since the issue is only a notification. | |
210 issues_observer->OnIssueUpdated(&fake_issue_notification_); | |
211 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
212 | |
213 // Update |state_| since the issue is a warning. | |
214 issues_observer->OnIssueUpdated(&fake_issue_warning_); | |
215 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_WARNING, | |
216 action_->state_); | |
217 | |
218 // Update |state_| since the issue is fatal. | |
219 issues_observer->OnIssueUpdated(&fake_issue_fatal_); | |
220 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_ERROR, | |
221 action_->state_); | |
222 | |
223 // Clear the issue. | |
224 issues_observer->OnIssueUpdated(nullptr); | |
225 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
226 } | |
227 | |
228 // Tests the state of the MediaRouterAction based on updates to routes. | |
229 TEST_F(MediaRouterActionUnitTest, UpdateRoutes) { | |
230 // Set the delegate to register the observers. | |
231 action_->SetDelegate(new TestToolbarActionViewDelegate()); | |
232 media_router::MediaRoutesObserver* routes_observer = | |
233 action_->routes_observer_.get(); | |
234 EXPECT_TRUE(routes_observer); | |
235 | |
236 // Initially, there are no routes. | |
237 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
238 | |
239 std::vector<media_router::MediaRoute> routes = | |
240 { fake_route_local_, fake_route_remote_ }; | |
241 | |
242 // Update |state_| since there is a local route. | |
243 routes_observer->OnRoutesUpdated(routes); | |
244 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_ACTIVE, | |
245 action_->state_); | |
246 | |
247 // Update |state_| since there are no more local routes. | |
248 routes = { fake_route_remote_ }; | |
249 routes_observer->OnRoutesUpdated(routes); | |
250 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
251 | |
252 // |state_| stays the same if there are no local routes or no routes. | |
253 routes = {}; | |
254 routes_observer->OnRoutesUpdated(routes); | |
255 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
256 } | |
257 | |
258 // Tests the state of the MediaRouterAction based on updates to both issues | |
259 // and routes. | |
260 TEST_F(MediaRouterActionUnitTest, UpdateIssuesAndRoutes) { | |
261 // Set the delegate to register the observers. | |
262 action_->SetDelegate(new TestToolbarActionViewDelegate()); | |
263 media_router::MediaRoutesObserver* routes_observer = | |
264 action_->routes_observer_.get(); | |
265 EXPECT_TRUE(routes_observer); | |
266 media_router::IssuesObserver* issues_observer = | |
267 action_->issues_observer_.get(); | |
268 EXPECT_TRUE(issues_observer); | |
269 | |
270 // Initially, there are no issues or routes. | |
271 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
272 | |
273 // There is no change in |state_| since notification issues do not update | |
274 // the state. | |
275 issues_observer->OnIssueUpdated(&fake_issue_notification_); | |
276 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
277 | |
278 // Non-local routes also do not have an effect on |state_|. | |
279 std::vector<media_router::MediaRoute> routes = { fake_route_remote_ }; | |
280 routes_observer->OnRoutesUpdated(routes); | |
281 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
282 | |
283 // Update |state_| since there is a local route. | |
284 routes = { fake_route_local_ }; | |
285 routes_observer->OnRoutesUpdated(routes); | |
286 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_ACTIVE, | |
287 action_->state_); | |
288 | |
289 // Update |state_|, with a priority to reflect the warning issue rather than | |
290 // local route. | |
291 issues_observer->OnIssueUpdated(&fake_issue_warning_); | |
292 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_WARNING, | |
293 action_->state_); | |
294 | |
295 // Swapping the local route for a non-local one makes no difference to the | |
296 // |state_|. | |
297 routes = { fake_route_remote_ }; | |
298 routes_observer->OnRoutesUpdated(routes); | |
299 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_WARNING, | |
300 action_->state_); | |
301 | |
302 // Update |state_| since the issue has been updated to fatal. | |
303 issues_observer->OnIssueUpdated(&fake_issue_fatal_); | |
304 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_ERROR, | |
305 action_->state_); | |
306 | |
307 // Fatal issues still take precedent over local routes. | |
308 routes = { fake_route_local_ }; | |
309 routes_observer->OnRoutesUpdated(routes); | |
310 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_ERROR, | |
311 action_->state_); | |
312 | |
313 // When the fatal issue is dismissed, |state_| reflects the existing local | |
314 // route. | |
315 issues_observer->OnIssueUpdated(nullptr); | |
316 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_ACTIVE, | |
317 action_->state_); | |
318 | |
319 // Update |state_| when the local route is swapped out for a non-local route. | |
320 routes = { fake_route_remote_ }; | |
321 routes_observer->OnRoutesUpdated(routes); | |
322 EXPECT_EQ(MediaRouterActionState::MEDIA_ROUTER_ACTION_IDLE, action_->state_); | |
323 } | |
OLD | NEW |