1 // Copyright 2014 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 "base/command_line.h"
6 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
7 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
8 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h"
9 #include "chromeos/chromeos_switches.h"
10 #include "chromeos/network/network_state.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/message_center/message_center.h"
13 #include "ui/message_center/message_center_observer.h"
15 using message_center::MessageCenter
;
21 const char* const kNotificationId
=
22 NetworkPortalNotificationController::kNotificationId
;
24 bool HasNotification() {
25 MessageCenter
* message_center
= MessageCenter::Get();
26 return message_center
->FindVisibleNotificationById(kNotificationId
);
29 class NotificationObserver
: public message_center::MessageCenterObserver
{
31 NotificationObserver() : add_count_(0), remove_count_(0), update_count_(0) {}
33 // Overridden from message_center::MessageCenterObserver:
34 void OnNotificationAdded(const std::string
& notification_id
) override
{
35 if (notification_id
== kNotificationId
)
39 void OnNotificationRemoved(const std::string
& notification_id
,
40 bool /* by_user */) override
{
41 if (notification_id
== kNotificationId
)
45 void OnNotificationUpdated(const std::string
& notification_id
) override
{
46 if (notification_id
== kNotificationId
)
50 unsigned add_count() const { return add_count_
; }
51 unsigned remove_count() const { return remove_count_
; }
52 unsigned update_count() const { return update_count_
; }
56 unsigned remove_count_
;
57 unsigned update_count_
;
59 DISALLOW_COPY_AND_ASSIGN(NotificationObserver
);
64 class NetworkPortalNotificationControllerTest
: public testing::Test
{
66 NetworkPortalNotificationControllerTest()
67 : user_manager_enabler_(new chromeos::FakeChromeUserManager()) {}
68 ~NetworkPortalNotificationControllerTest() override
{}
70 void SetUp() override
{
71 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
72 cl
->AppendSwitch(switches::kEnableNetworkPortalNotification
);
73 MessageCenter::Initialize();
74 MessageCenter::Get()->AddObserver(&observer_
);
77 void TearDown() override
{
78 MessageCenter::Get()->RemoveObserver(&observer_
);
79 MessageCenter::Shutdown();
83 void OnPortalDetectionCompleted(
84 const NetworkState
* network
,
85 const NetworkPortalDetector::CaptivePortalState
& state
) {
86 controller_
.OnPortalDetectionCompleted(network
, state
);
89 NotificationObserver
& observer() { return observer_
; }
92 ScopedUserManagerEnabler user_manager_enabler_
;
93 NetworkPortalNotificationController controller_
;
94 NotificationObserver observer_
;
96 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerTest
);
99 TEST_F(NetworkPortalNotificationControllerTest
, NetworkStateChanged
) {
100 NetworkState
wifi("wifi");
101 NetworkPortalDetector::CaptivePortalState wifi_state
;
103 // Notification is not displayed for online state.
104 wifi_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
105 wifi_state
.response_code
= 204;
106 OnPortalDetectionCompleted(&wifi
, wifi_state
);
107 ASSERT_FALSE(HasNotification());
109 // Notification is displayed for portal state
110 wifi_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
111 wifi_state
.response_code
= 200;
112 OnPortalDetectionCompleted(&wifi
, wifi_state
);
113 ASSERT_TRUE(HasNotification());
115 // Notification is closed for online state.
116 wifi_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
117 wifi_state
.response_code
= 204;
118 OnPortalDetectionCompleted(&wifi
, wifi_state
);
119 ASSERT_FALSE(HasNotification());
122 TEST_F(NetworkPortalNotificationControllerTest
, NetworkChanged
) {
123 NetworkState
wifi1("wifi1");
124 NetworkPortalDetector::CaptivePortalState wifi1_state
;
125 wifi1_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
126 wifi1_state
.response_code
= 200;
127 OnPortalDetectionCompleted(&wifi1
, wifi1_state
);
128 ASSERT_TRUE(HasNotification());
130 MessageCenter::Get()->RemoveNotification(kNotificationId
, true /* by_user */);
131 ASSERT_FALSE(HasNotification());
133 // User already closed notification about portal state for this network,
134 // so notification shouldn't be displayed second time.
135 OnPortalDetectionCompleted(&wifi1
, wifi1_state
);
136 ASSERT_FALSE(HasNotification());
138 NetworkState
wifi2("wifi2");
139 NetworkPortalDetector::CaptivePortalState wifi2_state
;
140 wifi2_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
141 wifi2_state
.response_code
= 204;
143 // Second network is in online state, so there shouldn't be any
145 OnPortalDetectionCompleted(&wifi2
, wifi2_state
);
146 ASSERT_FALSE(HasNotification());
148 // User switches back to the first network, so notification should
150 OnPortalDetectionCompleted(&wifi1
, wifi1_state
);
151 ASSERT_TRUE(HasNotification());
154 TEST_F(NetworkPortalNotificationControllerTest
, NotificationUpdated
) {
155 NetworkPortalDetector::CaptivePortalState portal_state
;
156 portal_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
157 portal_state
.response_code
= 200;
159 // First network is behind a captive portal, so notification should
161 NetworkState
wifi1("wifi1");
162 OnPortalDetectionCompleted(&wifi1
, portal_state
);
163 ASSERT_TRUE(HasNotification());
164 EXPECT_EQ(1u, observer().add_count());
165 EXPECT_EQ(0u, observer().remove_count());
166 EXPECT_EQ(0u, observer().update_count());
168 // Second network is also behind a captive portal, so notification
169 // should be updated.
170 NetworkState
wifi2("wifi2");
171 OnPortalDetectionCompleted(&wifi2
, portal_state
);
172 ASSERT_TRUE(HasNotification());
173 EXPECT_EQ(1u, observer().add_count());
174 EXPECT_EQ(0u, observer().remove_count());
175 EXPECT_EQ(1u, observer().update_count());
177 // User closes the notification.
178 MessageCenter::Get()->RemoveNotification(kNotificationId
, true /* by_user */);
179 ASSERT_FALSE(HasNotification());
180 EXPECT_EQ(1u, observer().add_count());
181 EXPECT_EQ(1u, observer().remove_count());
182 EXPECT_EQ(1u, observer().update_count());
184 // Portal detector notified that second network is still behind captive
185 // portal, but user already closed the notification, so there should
186 // not be any notifications.
187 OnPortalDetectionCompleted(&wifi2
, portal_state
);
188 ASSERT_FALSE(HasNotification());
189 EXPECT_EQ(1u, observer().add_count());
190 EXPECT_EQ(1u, observer().remove_count());
191 EXPECT_EQ(1u, observer().update_count());
193 // Network was switched (by shill or by user) to wifi1. Notification
194 // should be displayed.
195 OnPortalDetectionCompleted(&wifi1
, portal_state
);
196 ASSERT_TRUE(HasNotification());
197 EXPECT_EQ(2u, observer().add_count());
198 EXPECT_EQ(1u, observer().remove_count());
199 EXPECT_EQ(1u, observer().update_count());
202 } // namespace chromeos