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_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" 12 13 using message_center::MessageCenter; 14 15 namespace chromeos { 16 17 namespace { 18 19 const char* kNotificationId = 20 NetworkPortalNotificationController::kNotificationId; 21 22 bool HasNotification() { 23 MessageCenter* message_center = MessageCenter::Get(); 24 return message_center->FindVisibleNotificationById(kNotificationId); 25 } 26 27 class NotificationObserver : public message_center::MessageCenterObserver { 28 public: 29 NotificationObserver() : add_count_(0), remove_count_(0), update_count_(0) {} 30 31 // Overridden from message_center::MessageCenterObserver: 32 virtual void OnNotificationAdded( 33 const std::string& notification_id) OVERRIDE { 34 if (notification_id == kNotificationId) 35 ++add_count_; 36 } 37 38 virtual void OnNotificationRemoved(const std::string& notification_id, 39 bool /* by_user */) OVERRIDE { 40 if (notification_id == kNotificationId) 41 ++remove_count_; 42 } 43 44 virtual void OnNotificationUpdated( 45 const std::string& notification_id) OVERRIDE { 46 if (notification_id == kNotificationId) 47 ++update_count_; 48 } 49 50 unsigned add_count() const { return add_count_; } 51 unsigned remove_count() const { return remove_count_; } 52 unsigned update_count() const { return update_count_; } 53 54 private: 55 unsigned add_count_; 56 unsigned remove_count_; 57 unsigned update_count_; 58 59 DISALLOW_COPY_AND_ASSIGN(NotificationObserver); 60 }; 61 62 } // namespace 63 64 class NetworkPortalNotificationControllerTest : public testing::Test { 65 public: 66 NetworkPortalNotificationControllerTest() {} 67 virtual ~NetworkPortalNotificationControllerTest() {} 68 69 virtual void SetUp() OVERRIDE { 70 CommandLine* cl = CommandLine::ForCurrentProcess(); 71 cl->AppendSwitch(switches::kEnableNetworkPortalNotification); 72 MessageCenter::Initialize(); 73 MessageCenter::Get()->AddObserver(&observer_); 74 } 75 76 virtual void TearDown() OVERRIDE { 77 MessageCenter::Get()->RemoveObserver(&observer_); 78 MessageCenter::Shutdown(); 79 } 80 81 protected: 82 void OnPortalDetectionCompleted( 83 const NetworkState* network, 84 const NetworkPortalDetector::CaptivePortalState& state) { 85 controller_.OnPortalDetectionCompleted(network, state); 86 } 87 88 NotificationObserver& observer() { return observer_; } 89 90 private: 91 NetworkPortalNotificationController controller_; 92 NotificationObserver observer_; 93 94 DISALLOW_COPY_AND_ASSIGN(NetworkPortalNotificationControllerTest); 95 }; 96 97 TEST_F(NetworkPortalNotificationControllerTest, NetworkStateChanged) { 98 NetworkState wifi("wifi"); 99 NetworkPortalDetector::CaptivePortalState wifi_state; 100 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()); 106 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()); 112 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()); 118 } 119 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()); 127 128 MessageCenter::Get()->RemoveNotification(kNotificationId, true /* by_user */); 129 ASSERT_FALSE(HasNotification()); 130 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()); 135 136 NetworkState wifi2("wifi2"); 137 NetworkPortalDetector::CaptivePortalState wifi2_state; 138 wifi2_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; 139 wifi2_state.response_code = 204; 140 141 // Second network is in online state, so there shouldn't be any 142 // notifications. 143 OnPortalDetectionCompleted(&wifi2, wifi2_state); 144 ASSERT_FALSE(HasNotification()); 145 146 // User switches back to the first network, so notification should 147 // be displayed. 148 OnPortalDetectionCompleted(&wifi1, wifi1_state); 149 ASSERT_TRUE(HasNotification()); 150 } 151 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; 156 157 // First network is behind a captive portal, so notification should 158 // be displayed. 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()); 165 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()); 174 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()); 181 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()); 190 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()); 198 } 199 200 } // namespace chromeos 201