Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/browser/chromeos/hats/hats_notification_controller_unittest.cc

Issue 2169223002: Implements unittests for hats notification controller (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implements unittests for hats notification controller Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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::Return;
28 using testing::SaveArg;
29 using testing::StrictMock;
30
31 namespace chromeos {
32
33 namespace {
34
35 class MockNetworkPortalDetector : public NetworkPortalDetector {
36 public:
37 MockNetworkPortalDetector() {}
38 ~MockNetworkPortalDetector() override {}
39
40 MOCK_METHOD1(AddObserver,
41 void(chromeos::NetworkPortalDetector::Observer* observer));
42 MOCK_METHOD1(RemoveObserver,
43 void(chromeos::NetworkPortalDetector::Observer* observer));
44 MOCK_METHOD1(AddAndFireObserver,
45 void(chromeos::NetworkPortalDetector::Observer* observer));
46 MOCK_METHOD1(GetCaptivePortalState,
47 chromeos::NetworkPortalDetector::CaptivePortalState(
48 const std::string& service_path));
49 MOCK_METHOD0(IsEnabled, bool());
50 MOCK_METHOD1(Enable, void(bool start_detection));
51 MOCK_METHOD0(StartDetectionIfIdle, bool());
52 MOCK_METHOD1(SetStrategy,
53 void(chromeos::PortalDetectorStrategy::StrategyId id));
54 MOCK_METHOD0(OnLockScreenRequest, void());
55
56 private:
57 DISALLOW_COPY_AND_ASSIGN(MockNetworkPortalDetector);
58 };
59
60 } // namespace
61
62 class HatsNotificationControllerTest : public BrowserWithTestWindowTest {
63 public:
64 HatsNotificationControllerTest() {}
65 ~HatsNotificationControllerTest() override {}
66
67 void SetUp() override {
68 BrowserWithTestWindowTest::SetUp();
69
70 profile_manager_.reset(
71 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
72 ASSERT_TRUE(profile_manager_->SetUp());
73
74 MessageCenterNotificationManager* manager =
75 static_cast<MessageCenterNotificationManager*>(
76 g_browser_process->notification_ui_manager());
77 manager->SetMessageCenterTrayDelegateForTest(
78 new message_center::FakeMessageCenterTrayDelegate(
79 message_center::MessageCenter::Get()));
80
81 network_portal_detector::InitializeForTesting(
82 &mock_network_portal_detector_);
83 }
84
85 void TearDown() override {
86 g_browser_process->notification_ui_manager()->CancelAll();
87 profile_manager_.reset();
88 network_portal_detector::InitializeForTesting(nullptr);
89 BrowserWithTestWindowTest::TearDown();
90 }
91
92 scoped_refptr<HatsNotificationController> InstantiateHatsController() {
93 // The initialization will fail since the function IsNewDevice() will return
94 // true.
95 scoped_refptr<HatsNotificationController> hats_notification_controller =
96 new HatsNotificationController(&profile_);
97
98 // HatsController::IsNewDevice() is run on a blocking thread.
99 content::RunAllBlockingPoolTasksUntilIdle();
100 return hats_notification_controller;
101 }
102
103 TestingProfile profile_;
104 StrictMock<MockNetworkPortalDetector> mock_network_portal_detector_;
105
106 private:
107 std::unique_ptr<TestingProfileManager> profile_manager_;
108
109 DISALLOW_COPY_AND_ASSIGN(HatsNotificationControllerTest);
110 };
111
112 TEST_F(HatsNotificationControllerTest, NewDevice_ShouldNotStartInitialization) {
113 int64_t now_timestamp = base::Time::Now().ToInternalValue();
114 PrefService* pref_service = profile_.GetPrefs();
115 pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp, now_timestamp);
116
117 auto hats_notification_controller = InstantiateHatsController();
118
119 int64_t new_timestamp =
120 pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);
121
122 // The flag should be updated to a new timestamp.
123 ASSERT_TRUE(base::Time::FromInternalValue(new_timestamp) >
124 base::Time::FromInternalValue(now_timestamp));
125
126 // Destructor for HatsController removes self from observer list.
127 EXPECT_CALL(mock_network_portal_detector_,
128 RemoveObserver(hats_notification_controller.get()))
129 .Times(1);
130 }
131
132 TEST_F(HatsNotificationControllerTest, OldDevice_ShouldStartInitialization) {
133 auto hats_notification_controller = InstantiateHatsController();
134
135 // On initialization, HatsNotificationController adds itself as an observer to
136 // NetworkPortalDetector.
137 EXPECT_CALL(mock_network_portal_detector_,
138 AddAndFireObserver(hats_notification_controller.get()))
139 .Times(1);
140
141 hats_notification_controller->Initialize(false);
142
143 // Destructor for HatsNotificationController removes self from observer list.
144 EXPECT_CALL(mock_network_portal_detector_,
145 RemoveObserver(hats_notification_controller.get()))
146 .Times(1);
147 }
148
149 TEST_F(HatsNotificationControllerTest, NoInternet_DoNotShowNotification) {
150 auto hats_notification_controller = InstantiateHatsController();
151
152 // Upon destruction HatsNotificationController removes itself as an observer
153 // from NetworkPortalDetector. This will only be called once from the
154 EXPECT_CALL(mock_network_portal_detector_,
155 RemoveObserver(hats_notification_controller.get()))
156 .Times(1);
157
158 NetworkPortalDetector::CaptivePortalState online_state;
159 hats_notification_controller->OnPortalDetectionCompleted(
160 new NetworkState(std::string()), online_state);
161
162 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
163 hats_notification_controller->OnPortalDetectionCompleted(
164 new NetworkState(std::string()), online_state);
165
166 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
167 hats_notification_controller->OnPortalDetectionCompleted(
168 new NetworkState(std::string()), online_state);
169
170 online_state.status =
171 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED;
172 hats_notification_controller->OnPortalDetectionCompleted(
173 new NetworkState(std::string()), online_state);
174
175 const Notification* notification =
176 g_browser_process->notification_ui_manager()->FindById(
177 HatsNotificationController::kDelegateId, &profile_);
178 EXPECT_TRUE(notification == nullptr);
stevenjb 2016/07/22 20:13:53 I believe that EXPECT_FALSE(notification) should w
malaykeshav 2016/07/22 22:48:56 Done
179 }
180
181 TEST_F(HatsNotificationControllerTest, InternetConnected_ShowNotification) {
182 auto hats_notification_controller = InstantiateHatsController();
183
184 EXPECT_CALL(mock_network_portal_detector_,
185 RemoveObserver(hats_notification_controller.get()))
186 .Times(AtLeast(1));
187
188 NetworkPortalDetector::CaptivePortalState online_state;
189 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
190 hats_notification_controller->OnPortalDetectionCompleted(
191 new NetworkState(std::string()), online_state);
192
193 const Notification* notification =
194 g_browser_process->notification_ui_manager()->FindById(
195 HatsNotificationController::kDelegateId, &profile_);
196 EXPECT_TRUE(notification != nullptr);
197 }
198
199 TEST_F(HatsNotificationControllerTest, DismissNotification_ShouldUpdatePref) {
200 int64_t now_timestamp = base::Time::Now().ToInternalValue();
201 PrefService* pref_service = profile_.GetPrefs();
202 pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp, now_timestamp);
203
204 auto hats_notification_controller = InstantiateHatsController();
205
206 hats_notification_controller->Close(true);
207
208 int64_t new_timestamp =
209 pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);
210 // The flag should be updated to a new timestamp.
211 ASSERT_TRUE(base::Time::FromInternalValue(new_timestamp) >
212 base::Time::FromInternalValue(now_timestamp));
213
214 // Destructor for HatsController removes self from observer list.
215 EXPECT_CALL(mock_network_portal_detector_,
216 RemoveObserver(hats_notification_controller.get()))
217 .Times(1);
218 }
219
220 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/hats/hats_notification_controller.h ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698