EME test page application.
[chromium-blink-merge.git] / chrome / browser / chromeos / net / network_portal_notification_controller_unittest.cc
blobb502658d5b456b5bad28bfa64903a7cb20b0e1d2
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;
15 namespace chromeos {
17 namespace {
19 const char* kNotificationId =
20 NetworkPortalNotificationController::kNotificationId;
22 bool HasNotification() {
23 return MessageCenter::Get()->HasNotification(kNotificationId);
26 class NotificationObserver : public message_center::MessageCenterObserver {
27 public:
28 NotificationObserver() : add_count_(0), remove_count_(0), update_count_(0) {}
30 // Overridden from message_center::MessageCenterObserver:
31 virtual void OnNotificationAdded(
32 const std::string& notification_id) OVERRIDE {
33 if (notification_id == kNotificationId)
34 ++add_count_;
37 virtual void OnNotificationRemoved(const std::string& notification_id,
38 bool /* by_user */) OVERRIDE {
39 if (notification_id == kNotificationId)
40 ++remove_count_;
43 virtual void OnNotificationUpdated(
44 const std::string& notification_id) OVERRIDE {
45 if (notification_id == kNotificationId)
46 ++update_count_;
49 unsigned add_count() const { return add_count_; }
50 unsigned remove_count() const { return remove_count_; }
51 unsigned update_count() const { return update_count_; }
53 private:
54 unsigned add_count_;
55 unsigned remove_count_;
56 unsigned update_count_;
58 DISALLOW_COPY_AND_ASSIGN(NotificationObserver);
61 } // namespace
63 class NetworkPortalNotificationControllerTest : public testing::Test {
64 public:
65 NetworkPortalNotificationControllerTest() {}
66 virtual ~NetworkPortalNotificationControllerTest() {}
68 virtual void SetUp() OVERRIDE {
69 CommandLine* cl = CommandLine::ForCurrentProcess();
70 cl->AppendSwitch(switches::kEnableNetworkPortalNotification);
71 MessageCenter::Initialize();
72 MessageCenter::Get()->AddObserver(&observer_);
75 virtual void TearDown() OVERRIDE {
76 MessageCenter::Get()->RemoveObserver(&observer_);
77 MessageCenter::Shutdown();
80 protected:
81 void OnPortalDetectionCompleted(
82 const NetworkState* network,
83 const NetworkPortalDetector::CaptivePortalState& state) {
84 controller_.OnPortalDetectionCompleted(network, state);
87 NotificationObserver& observer() { return observer_; }
89 private:
90 NetworkPortalNotificationController controller_;
91 NotificationObserver observer_;
93 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerTest);
96 TEST_F(NetworkPortalNotificationControllerTest, NetworkStateChanged) {
97 NetworkState wifi("wifi");
98 NetworkPortalDetector::CaptivePortalState wifi_state;
100 // Notification is not displayed for online state.
101 wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
102 wifi_state.response_code = 204;
103 OnPortalDetectionCompleted(&wifi, wifi_state);
104 ASSERT_FALSE(HasNotification());
106 // Notification is displayed for portal state
107 wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
108 wifi_state.response_code = 200;
109 OnPortalDetectionCompleted(&wifi, wifi_state);
110 ASSERT_TRUE(HasNotification());
112 // Notification is closed for online state.
113 wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
114 wifi_state.response_code = 204;
115 OnPortalDetectionCompleted(&wifi, wifi_state);
116 ASSERT_FALSE(HasNotification());
119 TEST_F(NetworkPortalNotificationControllerTest, NetworkChanged) {
120 NetworkState wifi1("wifi1");
121 NetworkPortalDetector::CaptivePortalState wifi1_state;
122 wifi1_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
123 wifi1_state.response_code = 200;
124 OnPortalDetectionCompleted(&wifi1, wifi1_state);
125 ASSERT_TRUE(HasNotification());
127 MessageCenter::Get()->RemoveNotification(kNotificationId, true /* by_user */);
128 ASSERT_FALSE(HasNotification());
130 // User already closed notification about portal state for this network,
131 // so notification shouldn't be displayed second time.
132 OnPortalDetectionCompleted(&wifi1, wifi1_state);
133 ASSERT_FALSE(HasNotification());
135 NetworkState wifi2("wifi2");
136 NetworkPortalDetector::CaptivePortalState wifi2_state;
137 wifi2_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
138 wifi2_state.response_code = 204;
140 // Second network is in online state, so there shouldn't be any
141 // notifications.
142 OnPortalDetectionCompleted(&wifi2, wifi2_state);
143 ASSERT_FALSE(HasNotification());
145 // User switches back to the first network, so notification should
146 // be displayed.
147 OnPortalDetectionCompleted(&wifi1, wifi1_state);
148 ASSERT_TRUE(HasNotification());
151 TEST_F(NetworkPortalNotificationControllerTest, NotificationUpdated) {
152 NetworkPortalDetector::CaptivePortalState portal_state;
153 portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
154 portal_state.response_code = 200;
156 // First network is behind a captive portal, so notification should
157 // be displayed.
158 NetworkState wifi1("wifi1");
159 OnPortalDetectionCompleted(&wifi1, portal_state);
160 ASSERT_TRUE(HasNotification());
161 EXPECT_EQ(1u, observer().add_count());
162 EXPECT_EQ(0u, observer().remove_count());
163 EXPECT_EQ(0u, observer().update_count());
165 // Second network is also behind a captive portal, so notification
166 // should be updated.
167 NetworkState wifi2("wifi2");
168 OnPortalDetectionCompleted(&wifi2, portal_state);
169 ASSERT_TRUE(HasNotification());
170 EXPECT_EQ(1u, observer().add_count());
171 EXPECT_EQ(0u, observer().remove_count());
172 EXPECT_EQ(1u, observer().update_count());
174 // User closes the notification.
175 MessageCenter::Get()->RemoveNotification(kNotificationId, true /* by_user */);
176 ASSERT_FALSE(HasNotification());
177 EXPECT_EQ(1u, observer().add_count());
178 EXPECT_EQ(1u, observer().remove_count());
179 EXPECT_EQ(1u, observer().update_count());
181 // Portal detector notified that second network is still behind captive
182 // portal, but user already closed the notification, so there should
183 // not be any notifications.
184 OnPortalDetectionCompleted(&wifi2, portal_state);
185 ASSERT_FALSE(HasNotification());
186 EXPECT_EQ(1u, observer().add_count());
187 EXPECT_EQ(1u, observer().remove_count());
188 EXPECT_EQ(1u, observer().update_count());
190 // Network was switched (by shill or by user) to wifi1. Notification
191 // should be displayed.
192 OnPortalDetectionCompleted(&wifi1, portal_state);
193 ASSERT_TRUE(HasNotification());
194 EXPECT_EQ(2u, observer().add_count());
195 EXPECT_EQ(1u, observer().remove_count());
196 EXPECT_EQ(1u, observer().update_count());
199 } // namespace chromeos