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

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: 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/first_run/goodies_displayer.h"
stevenjb 2016/07/22 16:14:54 needed?
malaykeshav 2016/07/22 20:03:09 Removed
7 #include "chrome/browser/chromeos/hats/hats_dialog.h"
8 #include "chrome/browser/chromeos/hats/hats_notification_controller.h"
9 #include "chrome/browser/notifications/message_center_notification_manager.h"
10 #include "chrome/browser/notifications/notification.h"
11 #include "chrome/browser/notifications/notification_ui_manager.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/browser_with_test_window_test.h"
14 #include "chrome/test/base/testing_browser_process.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "chrome/test/base/testing_profile_manager.h"
17 #include "chromeos/network/network_state.h"
18 #include "chromeos/network/portal_detector/network_portal_detector.h"
19 #include "components/prefs/pref_service.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "content/public/test/test_utils.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/message_center/fake_message_center_tray_delegate.h"
25 #include "ui/message_center/message_center.h"
stevenjb 2016/07/22 16:14:54 This is a lot of includes, are they all needed?
malaykeshav 2016/07/22 20:03:09 Done
26
27 using testing::_;
28 using testing::AtLeast;
29 using testing::Return;
30 using testing::SaveArg;
31 using testing::StrictMock;
32
33 namespace chromeos {
34
35 namespace {
36
37 class MockNetworkPortalDetector : public NetworkPortalDetector {
38 public:
39 MockNetworkPortalDetector() {}
40 ~MockNetworkPortalDetector() override {}
41
42 MOCK_METHOD1(AddObserver,
43 void(chromeos::NetworkPortalDetector::Observer* observer));
44 MOCK_METHOD1(RemoveObserver,
45 void(chromeos::NetworkPortalDetector::Observer* observer));
46 MOCK_METHOD1(AddAndFireObserver,
47 void(chromeos::NetworkPortalDetector::Observer* observer));
48 MOCK_METHOD1(GetCaptivePortalState,
49 chromeos::NetworkPortalDetector::CaptivePortalState(
50 const std::string& service_path));
51 MOCK_METHOD0(IsEnabled, bool());
52 MOCK_METHOD1(Enable, void(bool start_detection));
53 MOCK_METHOD0(StartDetectionIfIdle, bool());
54 MOCK_METHOD1(SetStrategy,
55 void(chromeos::PortalDetectorStrategy::StrategyId id));
56 MOCK_METHOD0(OnLockScreenRequest, void());
57
58 private:
59 DISALLOW_COPY_AND_ASSIGN(MockNetworkPortalDetector);
60 };
61
62 } // namespace
63
64 class HatsNotificationControllerTest : public BrowserWithTestWindowTest {
65 public:
66 HatsNotificationControllerTest() {}
67 ~HatsNotificationControllerTest() override {}
68
69 void SetUp() override {
70 BrowserWithTestWindowTest::SetUp();
71
72 profile_manager_.reset(
73 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
74 ASSERT_TRUE(profile_manager_->SetUp());
75
76 MessageCenterNotificationManager* manager =
77 static_cast<MessageCenterNotificationManager*>(
78 g_browser_process->notification_ui_manager());
79 manager->SetMessageCenterTrayDelegateForTest(
80 new message_center::FakeMessageCenterTrayDelegate(
81 message_center::MessageCenter::Get()));
82
83 network_portal_detector::InitializeForTesting(
84 &mock_network_portal_detector_);
85 }
86
87 void TearDown() override {
88 g_browser_process->notification_ui_manager()->CancelAll();
89 profile_manager_.reset();
90 network_portal_detector::InitializeForTesting(nullptr);
91 BrowserWithTestWindowTest::TearDown();
92 }
93
94 TestingProfile profile_;
95 StrictMock<MockNetworkPortalDetector> mock_network_portal_detector_;
96
97 private:
98 std::unique_ptr<TestingProfileManager> profile_manager_;
99
100 DISALLOW_COPY_AND_ASSIGN(HatsNotificationControllerTest);
101 };
102
103 TEST_F(HatsNotificationControllerTest, NewDevice_ShouldNotInitialize) {
104 int64_t now_timestamp = base::Time::Now().ToInternalValue();
105 PrefService* pref_service = profile_.GetPrefs();
106 pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp, now_timestamp);
107
108 // The initialization will fail since the function IsNewDevice() will return
109 // true.
stevenjb 2016/07/22 16:14:54 How are we testing that initialization fails?
malaykeshav 2016/07/22 20:03:09 When initialization fails, the timestamp for kHats
stevenjb 2016/07/22 20:13:53 That is not at all obvious, we should add a commen
malaykeshav 2016/07/22 22:48:55 Done
110 scoped_refptr<HatsNotificationController> hats_notification_controller =
111 new HatsNotificationController(&profile_);
112
113 // HatsController::IsNewDevice() is run on a blocking thread.
114 content::RunAllBlockingPoolTasksUntilIdle();
115
116 int64_t new_timestamp =
117 pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);
118
119 // The flag should be updated to a new timestamp.
120 ASSERT_TRUE(base::Time::FromInternalValue(new_timestamp) >
121 base::Time::FromInternalValue(now_timestamp));
122
123 // Destructor for HatsController removes self from observer list.
124 EXPECT_CALL(mock_network_portal_detector_,
125 RemoveObserver(hats_notification_controller.get()))
126 .Times(1);
127 }
128
129 TEST_F(HatsNotificationControllerTest, OldDevice_ShouldInitialize) {
130 scoped_refptr<HatsNotificationController> hats_notification_controller =
131 new HatsNotificationController(&profile_);
132 // HatsController::IsNewDevice() is run on a blocking thread.
133 content::RunAllBlockingPoolTasksUntilIdle();
stevenjb 2016/07/22 16:14:53 Does it make sense to group the above into a helpe
malaykeshav 2016/07/22 20:03:09 Done
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);
stevenjb 2016/07/22 16:14:54 So, are we relying entirely on AddAndFireObserve g
malaykeshav 2016/07/22 20:03:09 Its more of a starting with the initialization. It
stevenjb 2016/07/22 20:13:53 My point is, it is not at all clear that anything
malaykeshav 2016/07/22 22:48:55 Combined this with the test below to check for whe
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 scoped_refptr<HatsNotificationController> hats_notification_controller =
151 new HatsNotificationController(&profile_);
152
153 // Upon destruction HatsNotificationController removes itself as an observer
154 // from NetworkPortalDetector.
155 EXPECT_CALL(mock_network_portal_detector_,
156 RemoveObserver(hats_notification_controller.get()))
157 .Times(1);
158
159 NetworkPortalDetector::CaptivePortalState online_state;
160 hats_notification_controller->OnPortalDetectionCompleted(
161 new NetworkState(std::string()), online_state);
162
163 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
164 hats_notification_controller->OnPortalDetectionCompleted(
165 new NetworkState(std::string()), online_state);
166
167 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
168 hats_notification_controller->OnPortalDetectionCompleted(
169 new NetworkState(std::string()), online_state);
170
171 online_state.status =
172 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED;
173 hats_notification_controller->OnPortalDetectionCompleted(
174 new NetworkState(std::string()), online_state);
stevenjb 2016/07/22 16:14:54 It is not clear here where we are actually testing
malaykeshav 2016/07/22 20:03:09 Added the final check condition
175 }
176
177 TEST_F(HatsNotificationControllerTest, InternetConnected_ShowNotification) {
178 scoped_refptr<HatsNotificationController> hats_notification_controller =
179 new HatsNotificationController(&profile_);
180
181 EXPECT_CALL(mock_network_portal_detector_,
182 RemoveObserver(hats_notification_controller.get()))
183 .Times(AtLeast(1));
184
185 NetworkPortalDetector::CaptivePortalState online_state;
186 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
187 hats_notification_controller->OnPortalDetectionCompleted(
188 new NetworkState(std::string()), online_state);
189
190 MessageCenterNotificationManager* manager =
191 static_cast<MessageCenterNotificationManager*>(
192 g_browser_process->notification_ui_manager());
stevenjb 2016/07/22 16:14:54 Do we need the static cast? Also, no need for a lo
malaykeshav 2016/07/22 20:03:09 Removed
193 const Notification* notification =
194 manager->FindById(HatsNotificationController::kDelegateId, &profile_);
195 EXPECT_TRUE(notification != nullptr);
196 }
197
198 TEST_F(HatsNotificationControllerTest, DismissNotification_ShouldUpdatePref) {
199 int64_t now_timestamp = base::Time::Now().ToInternalValue();
200 PrefService* pref_service = profile_.GetPrefs();
201 pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp, now_timestamp);
202
203 scoped_refptr<HatsNotificationController> hats_notification_controller =
204 new HatsNotificationController(&profile_);
205
206 hats_notification_controller->Close(true);
207
208 content::RunAllBlockingPoolTasksUntilIdle();
209
210 int64_t new_timestamp =
211 pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);
212 // The flag should be updated to a new timestamp.
213 ASSERT_TRUE(base::Time::FromInternalValue(new_timestamp) >
214 base::Time::FromInternalValue(now_timestamp));
215
216 // Destructor for HatsController removes self from observer list.
217 EXPECT_CALL(mock_network_portal_detector_,
218 RemoveObserver(hats_notification_controller.get()))
219 .Times(1);
220 }
221
222 } // 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