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/net/network_portal_notification_controller.h"
7 #include "chromeos/chromeos_switches.h"
8 #include "chromeos/network/network_state.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/message_center/message_center.h"
11 #include "ui/message_center/message_center_observer.h"
13 using message_center::MessageCenter
;
19 const char* const kNotificationId
=
20 NetworkPortalNotificationController::kNotificationId
;
22 bool HasNotification() {
23 MessageCenter
* message_center
= MessageCenter::Get();
24 return message_center
->FindVisibleNotificationById(kNotificationId
);
27 class NotificationObserver
: public message_center::MessageCenterObserver
{
29 NotificationObserver() : add_count_(0), remove_count_(0), update_count_(0) {}
31 // Overridden from message_center::MessageCenterObserver:
32 virtual void OnNotificationAdded(
33 const std::string
& notification_id
) override
{
34 if (notification_id
== kNotificationId
)
38 virtual void OnNotificationRemoved(const std::string
& notification_id
,
39 bool /* by_user */) override
{
40 if (notification_id
== kNotificationId
)
44 virtual void OnNotificationUpdated(
45 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 virtual ~NetworkPortalNotificationControllerTest() {}
69 virtual void SetUp() override
{
70 CommandLine
* cl
= CommandLine::ForCurrentProcess();
71 cl
->AppendSwitch(switches::kEnableNetworkPortalNotification
);
72 MessageCenter::Initialize();
73 MessageCenter::Get()->AddObserver(&observer_
);
76 virtual void TearDown() override
{
77 MessageCenter::Get()->RemoveObserver(&observer_
);
78 MessageCenter::Shutdown();
82 void OnPortalDetectionCompleted(
83 const NetworkState
* network
,
84 const NetworkPortalDetector::CaptivePortalState
& state
) {
85 controller_
.OnPortalDetectionCompleted(network
, state
);
88 NotificationObserver
& observer() { return observer_
; }
91 NetworkPortalNotificationController controller_
;
92 NotificationObserver observer_
;
94 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerTest
);
97 TEST_F(NetworkPortalNotificationControllerTest
, NetworkStateChanged
) {
98 NetworkState
wifi("wifi");
99 NetworkPortalDetector::CaptivePortalState wifi_state
;
101 // Notification is not displayed for online state.
102 wifi_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
103 wifi_state
.response_code
= 204;
104 OnPortalDetectionCompleted(&wifi
, wifi_state
);
105 ASSERT_FALSE(HasNotification());
107 // Notification is displayed for portal state
108 wifi_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
109 wifi_state
.response_code
= 200;
110 OnPortalDetectionCompleted(&wifi
, wifi_state
);
111 ASSERT_TRUE(HasNotification());
113 // Notification is closed for online state.
114 wifi_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
115 wifi_state
.response_code
= 204;
116 OnPortalDetectionCompleted(&wifi
, wifi_state
);
117 ASSERT_FALSE(HasNotification());
120 TEST_F(NetworkPortalNotificationControllerTest
, NetworkChanged
) {
121 NetworkState
wifi1("wifi1");
122 NetworkPortalDetector::CaptivePortalState wifi1_state
;
123 wifi1_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
124 wifi1_state
.response_code
= 200;
125 OnPortalDetectionCompleted(&wifi1
, wifi1_state
);
126 ASSERT_TRUE(HasNotification());
128 MessageCenter::Get()->RemoveNotification(kNotificationId
, true /* by_user */);
129 ASSERT_FALSE(HasNotification());
131 // User already closed notification about portal state for this network,
132 // so notification shouldn't be displayed second time.
133 OnPortalDetectionCompleted(&wifi1
, wifi1_state
);
134 ASSERT_FALSE(HasNotification());
136 NetworkState
wifi2("wifi2");
137 NetworkPortalDetector::CaptivePortalState wifi2_state
;
138 wifi2_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
139 wifi2_state
.response_code
= 204;
141 // Second network is in online state, so there shouldn't be any
143 OnPortalDetectionCompleted(&wifi2
, wifi2_state
);
144 ASSERT_FALSE(HasNotification());
146 // User switches back to the first network, so notification should
148 OnPortalDetectionCompleted(&wifi1
, wifi1_state
);
149 ASSERT_TRUE(HasNotification());
152 TEST_F(NetworkPortalNotificationControllerTest
, NotificationUpdated
) {
153 NetworkPortalDetector::CaptivePortalState portal_state
;
154 portal_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
155 portal_state
.response_code
= 200;
157 // First network is behind a captive portal, so notification should
159 NetworkState
wifi1("wifi1");
160 OnPortalDetectionCompleted(&wifi1
, portal_state
);
161 ASSERT_TRUE(HasNotification());
162 EXPECT_EQ(1u, observer().add_count());
163 EXPECT_EQ(0u, observer().remove_count());
164 EXPECT_EQ(0u, observer().update_count());
166 // Second network is also behind a captive portal, so notification
167 // should be updated.
168 NetworkState
wifi2("wifi2");
169 OnPortalDetectionCompleted(&wifi2
, portal_state
);
170 ASSERT_TRUE(HasNotification());
171 EXPECT_EQ(1u, observer().add_count());
172 EXPECT_EQ(0u, observer().remove_count());
173 EXPECT_EQ(1u, observer().update_count());
175 // User closes the notification.
176 MessageCenter::Get()->RemoveNotification(kNotificationId
, true /* by_user */);
177 ASSERT_FALSE(HasNotification());
178 EXPECT_EQ(1u, observer().add_count());
179 EXPECT_EQ(1u, observer().remove_count());
180 EXPECT_EQ(1u, observer().update_count());
182 // Portal detector notified that second network is still behind captive
183 // portal, but user already closed the notification, so there should
184 // not be any notifications.
185 OnPortalDetectionCompleted(&wifi2
, portal_state
);
186 ASSERT_FALSE(HasNotification());
187 EXPECT_EQ(1u, observer().add_count());
188 EXPECT_EQ(1u, observer().remove_count());
189 EXPECT_EQ(1u, observer().update_count());
191 // Network was switched (by shill or by user) to wifi1. Notification
192 // should be displayed.
193 OnPortalDetectionCompleted(&wifi1
, portal_state
);
194 ASSERT_TRUE(HasNotification());
195 EXPECT_EQ(2u, observer().add_count());
196 EXPECT_EQ(1u, observer().remove_count());
197 EXPECT_EQ(1u, observer().update_count());
200 } // namespace chromeos