Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/strings/string_split.h" | |
| 6 #include "chrome/browser/chromeos/hats/hats_notification_controller.h" | |
| 7 #include "chrome/browser/notifications/message_center_notification_manager.h" | |
| 8 #include "chrome/browser/notifications/notification.h" | |
| 9 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 10 #include "chrome/common/pref_names.h" | |
| 11 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 12 #include "chrome/test/base/testing_browser_process.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "chrome/test/base/testing_profile_manager.h" | |
| 15 #include "chromeos/network/network_state.h" | |
| 16 #include "chromeos/network/portal_detector/network_portal_detector.h" | |
| 17 #include "components/prefs/pref_service.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "content/public/test/test_utils.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 #include "ui/message_center/fake_message_center_tray_delegate.h" | |
| 23 #include "ui/message_center/message_center.h" | |
| 24 | |
| 25 using testing::_; | |
| 26 using testing::AtLeast; | |
| 27 using testing::Invoke; | |
| 28 using testing::Return; | |
| 29 using testing::SaveArg; | |
| 30 using testing::StrictMock; | |
| 31 | |
| 32 namespace chromeos { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 class MockNetworkPortalDetector : public NetworkPortalDetector { | |
| 37 public: | |
| 38 MockNetworkPortalDetector() {} | |
| 39 ~MockNetworkPortalDetector() override {} | |
| 40 | |
| 41 MOCK_METHOD1(AddObserver, | |
| 42 void(chromeos::NetworkPortalDetector::Observer* observer)); | |
| 43 MOCK_METHOD1(RemoveObserver, | |
| 44 void(chromeos::NetworkPortalDetector::Observer* observer)); | |
| 45 MOCK_METHOD1(AddAndFireObserver, | |
| 46 void(chromeos::NetworkPortalDetector::Observer* observer)); | |
| 47 MOCK_METHOD1(GetCaptivePortalState, | |
| 48 chromeos::NetworkPortalDetector::CaptivePortalState( | |
| 49 const std::string& service_path)); | |
| 50 MOCK_METHOD0(IsEnabled, bool()); | |
| 51 MOCK_METHOD1(Enable, void(bool start_detection)); | |
| 52 MOCK_METHOD0(StartDetectionIfIdle, bool()); | |
| 53 MOCK_METHOD1(SetStrategy, | |
| 54 void(chromeos::PortalDetectorStrategy::StrategyId id)); | |
| 55 MOCK_METHOD0(OnLockScreenRequest, void()); | |
| 56 | |
| 57 private: | |
| 58 DISALLOW_COPY_AND_ASSIGN(MockNetworkPortalDetector); | |
| 59 }; | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 class HatsNotificationControllerTest : public BrowserWithTestWindowTest { | |
| 64 public: | |
| 65 HatsNotificationControllerTest() {} | |
| 66 ~HatsNotificationControllerTest() override {} | |
| 67 | |
| 68 void SetUp() override { | |
| 69 BrowserWithTestWindowTest::SetUp(); | |
| 70 | |
| 71 profile_manager_.reset( | |
| 72 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | |
| 73 ASSERT_TRUE(profile_manager_->SetUp()); | |
| 74 | |
| 75 MessageCenterNotificationManager* manager = | |
| 76 static_cast<MessageCenterNotificationManager*>( | |
| 77 g_browser_process->notification_ui_manager()); | |
| 78 manager->SetMessageCenterTrayDelegateForTest( | |
| 79 new message_center::FakeMessageCenterTrayDelegate( | |
| 80 message_center::MessageCenter::Get())); | |
| 81 | |
| 82 network_portal_detector::InitializeForTesting( | |
| 83 &mock_network_portal_detector_); | |
| 84 } | |
| 85 | |
| 86 void TearDown() override { | |
| 87 g_browser_process->notification_ui_manager()->CancelAll(); | |
| 88 profile_manager_.reset(); | |
| 89 network_portal_detector::InitializeForTesting(nullptr); | |
| 90 BrowserWithTestWindowTest::TearDown(); | |
| 91 } | |
| 92 | |
| 93 scoped_refptr<HatsNotificationController> InstantiateHatsController() { | |
| 94 // The initialization will fail since the function IsNewDevice() will return | |
| 95 // true. | |
| 96 scoped_refptr<HatsNotificationController> hats_notification_controller = | |
| 97 new HatsNotificationController(&profile_); | |
| 98 | |
| 99 // HatsController::IsNewDevice() is run on a blocking thread. | |
| 100 content::RunAllBlockingPoolTasksUntilIdle(); | |
| 101 | |
| 102 // Send a callback to the observer to simulate internet connectivity is | |
| 103 // present on device. | |
| 104 ON_CALL(mock_network_portal_detector_, | |
| 105 AddAndFireObserver(hats_notification_controller.get())) | |
| 106 .WillByDefault(Invoke( | |
| 107 [&hats_notification_controller](NetworkPortalDetector::Observer*) { | |
| 108 NetworkPortalDetector::CaptivePortalState online_state; | |
| 109 online_state.status = | |
| 110 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; | |
| 111 hats_notification_controller->OnPortalDetectionCompleted( | |
| 112 new NetworkState(std::string()), online_state); | |
|
stevenjb
2016/07/25 16:04:18
This will leak. Even though it's a test, we should
malaykeshav
2016/07/25 20:49:25
Done
| |
| 113 })); | |
| 114 | |
| 115 return hats_notification_controller; | |
| 116 } | |
| 117 | |
| 118 TestingProfile profile_; | |
| 119 StrictMock<MockNetworkPortalDetector> mock_network_portal_detector_; | |
| 120 | |
| 121 private: | |
| 122 std::unique_ptr<TestingProfileManager> profile_manager_; | |
| 123 | |
| 124 DISALLOW_COPY_AND_ASSIGN(HatsNotificationControllerTest); | |
| 125 }; | |
| 126 | |
| 127 TEST_F(HatsNotificationControllerTest, NewDevice_ShouldNotShowNotification) { | |
| 128 int64_t initial_timestamp = base::Time::Now().ToInternalValue(); | |
| 129 PrefService* pref_service = profile_.GetPrefs(); | |
| 130 pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp, | |
| 131 initial_timestamp); | |
| 132 | |
| 133 auto hats_notification_controller = InstantiateHatsController(); | |
| 134 hats_notification_controller->Initialize(true); | |
| 135 | |
| 136 int64_t current_timestamp = | |
| 137 pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp); | |
| 138 | |
| 139 // When the device is new, the controller does not begin initialization and | |
| 140 // simply updates the timestamp to Time::Now(). | |
| 141 ASSERT_TRUE(base::Time::FromInternalValue(current_timestamp) > | |
| 142 base::Time::FromInternalValue(initial_timestamp)); | |
| 143 | |
| 144 // Destructor for HatsController removes self from observer list. | |
| 145 EXPECT_CALL(mock_network_portal_detector_, | |
| 146 RemoveObserver(hats_notification_controller.get())) | |
| 147 .Times(1); | |
| 148 | |
| 149 const Notification* notification = | |
| 150 g_browser_process->notification_ui_manager()->FindById( | |
| 151 HatsNotificationController::kDelegateId, &profile_); | |
| 152 EXPECT_FALSE(notification); | |
| 153 } | |
| 154 | |
| 155 TEST_F(HatsNotificationControllerTest, OldDevice_ShouldShowNotification) { | |
| 156 auto hats_notification_controller = InstantiateHatsController(); | |
| 157 | |
| 158 // On initialization, HatsNotificationController adds itself as an observer to | |
| 159 // NetworkPortalDetector to detect internet connectivity. | |
| 160 EXPECT_CALL(mock_network_portal_detector_, | |
| 161 AddAndFireObserver(hats_notification_controller.get())) | |
| 162 .Times(1); | |
| 163 // Observer is removed if an internet connection is detected. It is called | |
| 164 // a second time when hats_notification_controller is destroyed. | |
| 165 EXPECT_CALL(mock_network_portal_detector_, | |
| 166 RemoveObserver(hats_notification_controller.get())) | |
| 167 .Times(2); | |
| 168 | |
| 169 hats_notification_controller->Initialize(false); | |
| 170 | |
| 171 // Finally check if notification was launched to confirm initialization. | |
| 172 const Notification* notification = | |
| 173 g_browser_process->notification_ui_manager()->FindById( | |
| 174 HatsNotificationController::kDelegateId, &profile_); | |
| 175 EXPECT_TRUE(notification != nullptr); | |
| 176 } | |
| 177 | |
| 178 TEST_F(HatsNotificationControllerTest, NoInternet_DoNotShowNotification) { | |
| 179 auto hats_notification_controller = InstantiateHatsController(); | |
| 180 | |
| 181 // Upon destruction HatsNotificationController removes itself as an observer | |
| 182 // from NetworkPortalDetector. This will only be called once from the | |
| 183 EXPECT_CALL(mock_network_portal_detector_, | |
| 184 RemoveObserver(hats_notification_controller.get())) | |
| 185 .Times(1); | |
| 186 | |
| 187 NetworkPortalDetector::CaptivePortalState online_state; | |
| 188 hats_notification_controller->OnPortalDetectionCompleted( | |
| 189 new NetworkState(std::string()), online_state); | |
|
stevenjb
2016/07/25 16:04:18
All of these should pass a pointer to a local Netw
malaykeshav
2016/07/25 20:49:25
Done
| |
| 190 | |
| 191 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE; | |
| 192 hats_notification_controller->OnPortalDetectionCompleted( | |
| 193 new NetworkState(std::string()), online_state); | |
| 194 | |
| 195 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL; | |
| 196 hats_notification_controller->OnPortalDetectionCompleted( | |
| 197 new NetworkState(std::string()), online_state); | |
| 198 | |
| 199 online_state.status = | |
| 200 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED; | |
| 201 hats_notification_controller->OnPortalDetectionCompleted( | |
| 202 new NetworkState(std::string()), online_state); | |
| 203 | |
| 204 const Notification* notification = | |
| 205 g_browser_process->notification_ui_manager()->FindById( | |
| 206 HatsNotificationController::kDelegateId, &profile_); | |
| 207 EXPECT_FALSE(notification); | |
| 208 } | |
| 209 | |
| 210 TEST_F(HatsNotificationControllerTest, DismissNotification_ShouldUpdatePref) { | |
| 211 int64_t now_timestamp = base::Time::Now().ToInternalValue(); | |
| 212 PrefService* pref_service = profile_.GetPrefs(); | |
| 213 pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp, now_timestamp); | |
| 214 | |
| 215 auto hats_notification_controller = InstantiateHatsController(); | |
| 216 | |
| 217 // Simulate closing notification via user interaction. | |
| 218 hats_notification_controller->Close(true); | |
| 219 | |
| 220 int64_t new_timestamp = | |
| 221 pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp); | |
| 222 // The flag should be updated to a new timestamp. | |
| 223 ASSERT_TRUE(base::Time::FromInternalValue(new_timestamp) > | |
| 224 base::Time::FromInternalValue(now_timestamp)); | |
| 225 | |
| 226 // Destructor for HatsController removes self from observer list. | |
| 227 EXPECT_CALL(mock_network_portal_detector_, | |
| 228 RemoveObserver(hats_notification_controller.get())) | |
| 229 .Times(1); | |
| 230 } | |
| 231 | |
| 232 } // namespace chromeos | |
| OLD | NEW |