OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "chrome/browser/chromeos/net/network_portal_detector_notification.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" | |
12 | |
13 using message_center::MessageCenter; | |
14 | |
15 namespace chromeos { | |
16 | |
17 namespace { | |
18 | |
19 const char* kNotificationId = | |
20 NetworkPortalDetectorNotification::kNotificationId; | |
21 | |
22 bool HasNotification() { | |
23 return MessageCenter::Get()->HasNotification(kNotificationId); | |
24 } | |
25 | |
26 class NotificationObserver : public message_center::MessageCenterObserver { | |
27 public: | |
28 NotificationObserver() : add_count_(0), remove_count_(0), update_count_(0) {} | |
29 | |
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_; | |
35 } | |
36 | |
37 virtual void OnNotificationRemoved(const std::string& notification_id, | |
38 bool /* by_user */) OVERRIDE { | |
39 if (notification_id == kNotificationId) | |
40 ++remove_count_; | |
41 } | |
42 | |
43 virtual void OnNotificationUpdated( | |
44 const std::string& notification_id) OVERRIDE { | |
45 if (notification_id == kNotificationId) | |
46 ++update_count_; | |
47 } | |
48 | |
49 unsigned add_count() const { return add_count_; } | |
oshima
2014/02/19 18:59:41
size_t
ygorshenin1
2014/02/20 16:31:39
size_t values are usually arch-dependent sizes of
| |
50 unsigned remove_count() const { return remove_count_; } | |
51 unsigned update_count() const { return update_count_; } | |
52 | |
53 private: | |
54 unsigned add_count_; | |
55 unsigned remove_count_; | |
56 unsigned update_count_; | |
oshima
2014/02/19 18:59:41
DISALLOW_COPY_AND_ASSIGN
ygorshenin1
2014/02/20 16:31:39
Done.
| |
57 }; | |
58 | |
59 } // namespace | |
60 | |
61 class NetworkPortalDetectorNotificationTest : public testing::Test { | |
62 public: | |
63 NetworkPortalDetectorNotificationTest() {} | |
64 virtual ~NetworkPortalDetectorNotificationTest() {} | |
65 | |
66 virtual void SetUp() OVERRIDE { | |
67 CommandLine* cl = CommandLine::ForCurrentProcess(); | |
68 cl->AppendSwitch(switches::kEnableNetworkPortalNotification); | |
69 MessageCenter::Initialize(); | |
70 } | |
71 | |
72 virtual void TearDown() OVERRIDE { MessageCenter::Shutdown(); } | |
73 | |
74 void OnPortalDetectionCompleted( | |
75 const NetworkState* network, | |
76 const NetworkPortalDetector::CaptivePortalState& state) { | |
77 notification_.OnPortalDetectionCompleted(network, state); | |
78 } | |
79 | |
80 private: | |
81 NetworkPortalDetectorNotification notification_; | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorNotificationTest); | |
84 }; | |
85 | |
86 TEST_F(NetworkPortalDetectorNotificationTest, NetworkStateChanged) { | |
87 NetworkState wifi("wifi"); | |
88 NetworkPortalDetector::CaptivePortalState wifi_state; | |
89 | |
90 // Notification is not displayed for online state. | |
91 wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; | |
92 wifi_state.response_code = 204; | |
93 OnPortalDetectionCompleted(&wifi, wifi_state); | |
94 ASSERT_FALSE(HasNotification()); | |
95 | |
96 // Notification is displayed for portal state | |
97 wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL; | |
98 wifi_state.response_code = 200; | |
99 OnPortalDetectionCompleted(&wifi, wifi_state); | |
100 ASSERT_TRUE(HasNotification()); | |
101 | |
102 // Notification is closed for online state. | |
103 wifi_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; | |
104 wifi_state.response_code = 204; | |
105 OnPortalDetectionCompleted(&wifi, wifi_state); | |
106 ASSERT_FALSE(HasNotification()); | |
107 } | |
108 | |
109 TEST_F(NetworkPortalDetectorNotificationTest, NetworkChanged) { | |
110 NetworkState wifi1("wifi1"); | |
111 NetworkPortalDetector::CaptivePortalState wifi1_state; | |
112 wifi1_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL; | |
113 wifi1_state.response_code = 200; | |
114 OnPortalDetectionCompleted(&wifi1, wifi1_state); | |
115 ASSERT_TRUE(HasNotification()); | |
116 | |
117 MessageCenter::Get()->RemoveNotification(kNotificationId, true /* by_user */); | |
118 ASSERT_FALSE(HasNotification()); | |
119 | |
120 // User already closed notification about portal state for this network, | |
121 // so notification shouldn't be displayed second time. | |
122 OnPortalDetectionCompleted(&wifi1, wifi1_state); | |
123 ASSERT_FALSE(HasNotification()); | |
124 | |
125 NetworkState wifi2("wifi2"); | |
126 NetworkPortalDetector::CaptivePortalState wifi2_state; | |
127 wifi2_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; | |
128 wifi2_state.response_code = 204; | |
129 | |
130 // Second network is in online state, so there shouldn't be any | |
131 // notifications. | |
132 OnPortalDetectionCompleted(&wifi2, wifi2_state); | |
133 ASSERT_FALSE(HasNotification()); | |
134 | |
135 // User switches back to the first network, so notification should | |
136 // be displayed. | |
137 OnPortalDetectionCompleted(&wifi1, wifi1_state); | |
138 ASSERT_TRUE(HasNotification()); | |
139 } | |
140 | |
141 TEST_F(NetworkPortalDetectorNotificationTest, NotificationUpdated) { | |
142 NotificationObserver observer; | |
143 MessageCenter::Get()->AddObserver(&observer); | |
Jun Mukai
2014/02/19 19:17:25
I personally like to put this 'AddObserver/RemoveO
ygorshenin1
2014/02/20 16:31:39
Done.
| |
144 | |
145 NetworkPortalDetector::CaptivePortalState portal_state; | |
146 portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL; | |
147 portal_state.response_code = 200; | |
148 | |
149 // First network is behind a captive portal, so notification should | |
150 // be displayed. | |
151 NetworkState wifi1("wifi1"); | |
152 OnPortalDetectionCompleted(&wifi1, portal_state); | |
153 ASSERT_TRUE(HasNotification()); | |
154 EXPECT_EQ(1u, observer.add_count()); | |
155 EXPECT_EQ(0u, observer.remove_count()); | |
156 EXPECT_EQ(0u, observer.update_count()); | |
157 | |
158 // Second network is also behind a captive portal, so notification | |
159 // should be updated. | |
160 NetworkState wifi2("wifi2"); | |
161 OnPortalDetectionCompleted(&wifi2, portal_state); | |
162 ASSERT_TRUE(HasNotification()); | |
163 EXPECT_EQ(1u, observer.add_count()); | |
164 EXPECT_EQ(0u, observer.remove_count()); | |
165 EXPECT_EQ(1u, observer.update_count()); | |
166 | |
167 // User closes the notification. | |
168 MessageCenter::Get()->RemoveNotification(kNotificationId, true /* by_user */); | |
169 ASSERT_FALSE(HasNotification()); | |
170 EXPECT_EQ(1u, observer.add_count()); | |
171 EXPECT_EQ(1u, observer.remove_count()); | |
172 EXPECT_EQ(1u, observer.update_count()); | |
173 | |
174 // Portal detector notified that second network is still behind captive | |
175 // portal, but user already closed the notification, so there should | |
176 // not be any notifications. | |
177 OnPortalDetectionCompleted(&wifi2, portal_state); | |
178 ASSERT_FALSE(HasNotification()); | |
179 EXPECT_EQ(1u, observer.add_count()); | |
180 EXPECT_EQ(1u, observer.remove_count()); | |
181 EXPECT_EQ(1u, observer.update_count()); | |
182 | |
183 // Network was switched (by shill or by user) to wifi1. Notification | |
184 // should be displayed. | |
185 OnPortalDetectionCompleted(&wifi1, portal_state); | |
186 ASSERT_TRUE(HasNotification()); | |
187 EXPECT_EQ(2u, observer.add_count()); | |
188 EXPECT_EQ(1u, observer.remove_count()); | |
189 EXPECT_EQ(1u, observer.update_count()); | |
190 | |
191 MessageCenter::Get()->RemoveObserver(&observer); | |
192 } | |
193 | |
194 } // namespace chromeos | |
OLD | NEW |