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.
5 #include "chrome/browser/ui/toolbar/media_router_action.h"
6 #include "chrome/browser/ui/webui/media_router/media_router_test.h"
7 #include "chrome/grit/generated_resources.h"
8 #include "content/public/test/test_utils.h"
9 #include "grit/theme_resources.h"
10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/image/image_unittest_util.h"
14 class MediaRouterActionUnitTest
: public MediaRouterTest
{
16 MediaRouterActionUnitTest()
17 : fake_issue_notification_(media_router::Issue(
19 "message notification",
20 media_router::IssueAction(media_router::IssueAction::TYPE_DISMISS
),
21 std::vector
<media_router::IssueAction
>(),
23 media_router::Issue::NOTIFICATION
,
24 false, std::string())),
25 fake_issue_warning_(media_router::Issue(
28 media_router::IssueAction(
29 media_router::IssueAction::TYPE_LEARN_MORE
),
30 std::vector
<media_router::IssueAction
>(),
32 media_router::Issue::WARNING
,
35 fake_issue_fatal_(media_router::Issue(
38 media_router::IssueAction(media_router::IssueAction::TYPE_DISMISS
),
39 std::vector
<media_router::IssueAction
>(),
41 media_router::Issue::FATAL
,
44 fake_sink1_("fakeSink1", "Fake Sink 1"),
45 fake_sink2_("fakeSink2", "Fake Sink 2"),
46 fake_source1_("fakeSource1"),
47 fake_source2_("fakeSource2"),
48 fake_route_local_("route1", fake_source1_
, fake_sink1_
, "desc1",
50 fake_route_remote_("route2", fake_source2_
, fake_sink2_
, "desc2",
52 active_icon_(ui::ResourceBundle::GetSharedInstance().
53 GetImageNamed(IDR_MEDIA_ROUTER_ACTIVE_ICON
)),
54 error_icon_(ui::ResourceBundle::GetSharedInstance().
55 GetImageNamed(IDR_MEDIA_ROUTER_ERROR_ICON
)),
56 idle_icon_(ui::ResourceBundle::GetSharedInstance().
57 GetImageNamed(IDR_MEDIA_ROUTER_IDLE_ICON
)),
58 warning_icon_(ui::ResourceBundle::GetSharedInstance().
59 GetImageNamed(IDR_MEDIA_ROUTER_WARNING_ICON
)) {
62 ~MediaRouterActionUnitTest() override
{}
64 // BrowserWithTestWindowTest:
65 void SetUp() override
{
66 BrowserWithTestWindowTest::SetUp();
67 action_
.reset(new MediaRouterAction(browser()));
70 void TearDown() override
{
72 BrowserWithTestWindowTest::TearDown();
75 MediaRouterAction
* action() { return action_
.get(); }
76 const media_router::Issue
* fake_issue_notification() {
77 return &fake_issue_notification_
;
79 const media_router::Issue
* fake_issue_warning() {
80 return &fake_issue_warning_
;
82 const media_router::Issue
* fake_issue_fatal() {
83 return &fake_issue_fatal_
;
85 const media_router::MediaRoute
fake_route_local() {
86 return fake_route_local_
;
88 const media_router::MediaRoute
fake_route_remote() {
89 return fake_route_remote_
;
91 const gfx::Image
active_icon() { return active_icon_
; }
92 const gfx::Image
error_icon() { return error_icon_
; }
93 const gfx::Image
idle_icon() { return idle_icon_
; }
94 const gfx::Image
warning_icon() { return warning_icon_
; }
97 scoped_ptr
<MediaRouterAction
> action_
;
100 const media_router::Issue fake_issue_notification_
;
101 const media_router::Issue fake_issue_warning_
;
102 const media_router::Issue fake_issue_fatal_
;
104 // Fake Sinks and Sources, used for the Routes.
105 const media_router::MediaSink fake_sink1_
;
106 const media_router::MediaSink fake_sink2_
;
107 const media_router::MediaSource fake_source1_
;
108 const media_router::MediaSource fake_source2_
;
111 const media_router::MediaRoute fake_route_local_
;
112 const media_router::MediaRoute fake_route_remote_
;
115 const gfx::Image active_icon_
;
116 const gfx::Image error_icon_
;
117 const gfx::Image idle_icon_
;
118 const gfx::Image warning_icon_
;
120 DISALLOW_COPY_AND_ASSIGN(MediaRouterActionUnitTest
);
123 // Tests the initial state of MediaRouterAction after construction.
124 TEST_F(MediaRouterActionUnitTest
, Initialization
) {
125 EXPECT_EQ("media_router_action", action()->GetId());
126 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_TITLE
),
127 action()->GetActionName());
128 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
129 action()->GetIcon(nullptr, gfx::Size())));
132 // Tests the MediaRouterAction icon based on updates to issues.
133 TEST_F(MediaRouterActionUnitTest
, UpdateIssues
) {
134 // Initially, there are no issues.
135 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
136 action()->GetIcon(nullptr, gfx::Size())));
138 // Don't update |current_icon_| since the issue is only a notification.
139 action()->OnIssueUpdated(fake_issue_notification());
140 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
141 action()->GetIcon(nullptr, gfx::Size())));
143 // Update |current_icon_| since the issue is a warning.
144 action()->OnIssueUpdated(fake_issue_warning());
145 EXPECT_TRUE(gfx::test::IsEqual(warning_icon(),
146 action()->GetIcon(nullptr, gfx::Size())));
148 // Update |current_icon_| since the issue is fatal.
149 action()->OnIssueUpdated(fake_issue_fatal());
150 EXPECT_TRUE(gfx::test::IsEqual(error_icon(),
151 action()->GetIcon(nullptr, gfx::Size())));
154 action()->OnIssueUpdated(nullptr);
155 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
156 action()->GetIcon(nullptr, gfx::Size())));
159 // Tests the MediaRouterAction state based on updates to routes.
160 TEST_F(MediaRouterActionUnitTest
, UpdateRoutes
) {
161 scoped_ptr
<std::vector
<media_router::MediaRoute
>> routes(
162 new std::vector
<media_router::MediaRoute
>());
164 // Initially, there are no routes.
165 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
166 action()->GetIcon(nullptr, gfx::Size())));
168 // Update |current_icon_| since there is a local route.
169 routes
->push_back(fake_route_local());
170 routes
->push_back(fake_route_remote());
171 action()->OnRoutesUpdated(*routes
.get());
172 EXPECT_TRUE(gfx::test::IsEqual(active_icon(),
173 action()->GetIcon(nullptr, gfx::Size())));
175 // Update |current_icon_| since there are no more local routes.
177 routes
->push_back(fake_route_remote());
178 action()->OnRoutesUpdated(*routes
.get());
179 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
180 action()->GetIcon(nullptr, gfx::Size())));
182 // |current_icon_| stays the same if there are no local routes or no routes.
184 action()->OnRoutesUpdated(*routes
.get());
185 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
186 action()->GetIcon(nullptr, gfx::Size())));
189 // Tests the MediaRouterAction icon based on updates to both issues and routes.
190 TEST_F(MediaRouterActionUnitTest
, UpdateIssuesAndRoutes
) {
191 scoped_ptr
<std::vector
<media_router::MediaRoute
>> routes(
192 new std::vector
<media_router::MediaRoute
>());
194 // Initially, there are no issues or routes.
195 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
196 action()->GetIcon(nullptr, gfx::Size())));
198 // There is no change in |current_icon_| since notification issues do not
200 action()->OnIssueUpdated(fake_issue_notification());
201 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
202 action()->GetIcon(nullptr, gfx::Size())));
204 // Non-local routes also do not have an effect on |current_icon_|.
205 routes
->push_back(fake_route_remote());
206 action()->OnRoutesUpdated(*routes
.get());
207 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
208 action()->GetIcon(nullptr, gfx::Size())));
210 // Update |current_icon_| since there is a local route.
212 routes
->push_back(fake_route_local());
213 action()->OnRoutesUpdated(*routes
.get());
214 EXPECT_TRUE(gfx::test::IsEqual(active_icon(),
215 action()->GetIcon(nullptr, gfx::Size())));
217 // Update |current_icon_|, with a priority to reflect the warning issue
218 // rather than the local route.
219 action()->OnIssueUpdated(fake_issue_warning());
220 EXPECT_TRUE(gfx::test::IsEqual(warning_icon(),
221 action()->GetIcon(nullptr, gfx::Size())));
223 // Swapping the local route for a non-local one makes no difference to the
226 routes
->push_back(fake_route_remote());
227 action()->OnRoutesUpdated(*routes
.get());
228 EXPECT_TRUE(gfx::test::IsEqual(warning_icon(),
229 action()->GetIcon(nullptr, gfx::Size())));
231 // Update |current_icon_| since the issue has been updated to fatal.
232 action()->OnIssueUpdated(fake_issue_fatal());
233 EXPECT_TRUE(gfx::test::IsEqual(error_icon(),
234 action()->GetIcon(nullptr, gfx::Size())));
236 // Fatal issues still take precedent over local routes.
238 routes
->push_back(fake_route_local());
239 action()->OnRoutesUpdated(*routes
.get());
240 EXPECT_TRUE(gfx::test::IsEqual(error_icon(),
241 action()->GetIcon(nullptr, gfx::Size())));
243 // When the fatal issue is dismissed, |current_icon_| reflects the existing
245 action()->OnIssueUpdated(nullptr);
246 EXPECT_TRUE(gfx::test::IsEqual(active_icon(),
247 action()->GetIcon(nullptr, gfx::Size())));
249 // Update |current_icon_| when the local route is swapped out for a non-local
252 routes
->push_back(fake_route_remote());
253 action()->OnRoutesUpdated(*routes
.get());
254 EXPECT_TRUE(gfx::test::IsEqual(idle_icon(),
255 action()->GetIcon(nullptr, gfx::Size())));