OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "chrome/browser/chromeos/arc/notification/arc_provision_notification_se rvice.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/command_line.h" | |
12 #include "base/files/scoped_temp_dir.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/chromeos/arc/arc_auth_notification.h" | |
16 #include "chrome/browser/chromeos/arc/arc_optin_uma.h" | |
17 #include "chrome/browser/chromeos/arc/arc_session_manager.h" | |
18 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
19 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
20 #include "chrome/common/pref_names.h" | |
21 #include "chrome/test/base/testing_profile.h" | |
22 #include "chromeos/dbus/dbus_thread_manager.h" | |
23 #include "chromeos/dbus/fake_session_manager_client.h" | |
24 #include "components/arc/arc_bridge_service.h" | |
25 #include "components/arc/arc_service_manager.h" | |
26 #include "components/arc/arc_util.h" | |
27 #include "components/arc/test/fake_arc_session.h" | |
28 #include "components/prefs/pref_service.h" | |
29 #include "components/sync_preferences/testing_pref_service_syncable.h" | |
30 #include "components/user_manager/user_manager.h" | |
31 #include "content/public/test/test_browser_thread_bundle.h" | |
32 #include "testing/gmock/include/gmock/gmock.h" | |
33 #include "testing/gtest/include/gtest/gtest.h" | |
34 | |
35 using testing::AnyNumber; | |
36 using testing::AtLeast; | |
37 using testing::Mock; | |
38 using testing::StrictMock; | |
39 | |
40 namespace arc { | |
41 | |
42 namespace { | |
43 | |
44 class MockArcProvisionNotificationServiceDelegate | |
45 : public ArcProvisionNotificationService::Delegate { | |
46 public: | |
47 MOCK_METHOD0(ShowManagedProvisionNotification, void()); | |
48 MOCK_METHOD0(RemoveManagedProvisionNotification, void()); | |
49 }; | |
50 | |
51 class ArcProvisionNotificationServiceTest : public testing::Test { | |
52 protected: | |
53 ArcProvisionNotificationServiceTest() | |
54 : user_manager_enabler_(new chromeos::FakeChromeUserManager()) {} | |
55 | |
56 void SetUp() override { | |
57 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( | |
58 base::MakeUnique<chromeos::FakeSessionManagerClient>()); | |
59 chromeos::DBusThreadManager::Initialize(); | |
60 | |
61 SetArcAvailableCommandLineForTesting( | |
62 base::CommandLine::ForCurrentProcess()); | |
63 ArcSessionManager::DisableUIForTesting(); | |
64 ArcAuthNotification::DisableForTesting(); | |
65 | |
66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
67 TestingProfile::Builder profile_builder; | |
68 profile_builder.SetProfileName("user@gmail.com"); | |
69 profile_builder.SetPath(temp_dir_.GetPath().AppendASCII("TestArcProfile")); | |
70 profile_ = profile_builder.Build(); | |
71 | |
72 arc_service_manager_ = base::MakeUnique<ArcServiceManager>(nullptr); | |
hidehiko
2017/03/13 05:10:46
nit: not necessary.
emaxx
2017/03/16 18:32:12
Not sure I got it right, but I think ArcServiceMan
| |
73 arc_session_manager_ = base::MakeUnique<ArcSessionManager>( | |
74 base::MakeUnique<ArcSessionRunner>(base::Bind(FakeArcSession::Create))); | |
75 std::unique_ptr<MockArcProvisionNotificationServiceDelegate> | |
76 mock_arc_provision_notification_service_delegate = base::MakeUnique< | |
77 StrictMock<MockArcProvisionNotificationServiceDelegate>>(); | |
78 arc_provision_notification_service_delegate_ = | |
79 mock_arc_provision_notification_service_delegate.get(); | |
80 arc_provision_notification_service_ = | |
81 base::MakeUnique<ArcProvisionNotificationService>( | |
82 arc_bridge_service(), | |
83 std::move(mock_arc_provision_notification_service_delegate)); | |
84 | |
85 const AccountId account_id(AccountId::FromUserEmailGaiaId( | |
86 profile()->GetProfileUserName(), "1234567890")); | |
87 GetFakeUserManager()->AddUser(account_id); | |
88 GetFakeUserManager()->LoginUser(account_id); | |
89 } | |
90 | |
91 void TearDown() override { | |
92 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
93 RemoveManagedProvisionNotification()) | |
94 .Times(AtLeast(1)); | |
95 arc_provision_notification_service_.reset(); | |
96 arc_provision_notification_service_delegate_ = nullptr; | |
97 arc_session_manager_.reset(); | |
98 arc_service_manager_.reset(); | |
99 profile_.reset(); | |
100 chromeos::DBusThreadManager::Shutdown(); | |
101 } | |
102 | |
103 TestingProfile* profile() { return profile_.get(); } | |
104 ArcServiceManager* arc_service_manager() { | |
105 return arc_service_manager_.get(); | |
106 } | |
107 ArcBridgeService* arc_bridge_service() { | |
108 return arc_service_manager_->arc_bridge_service(); | |
109 } | |
110 ArcSessionManager* arc_session_manager() { | |
111 return arc_session_manager_.get(); | |
112 } | |
113 MockArcProvisionNotificationServiceDelegate* | |
114 arc_provision_notification_service_delegate() { | |
115 return arc_provision_notification_service_delegate_; | |
116 } | |
117 ArcProvisionNotificationService* arc_provision_notification_service() { | |
118 return arc_provision_notification_service_.get(); | |
119 } | |
120 chromeos::FakeChromeUserManager* GetFakeUserManager() { | |
121 return static_cast<chromeos::FakeChromeUserManager*>( | |
122 user_manager::UserManager::Get()); | |
123 } | |
124 | |
125 private: | |
126 content::TestBrowserThreadBundle thread_bundle_; | |
127 chromeos::ScopedUserManagerEnabler user_manager_enabler_; | |
128 base::ScopedTempDir temp_dir_; | |
129 std::unique_ptr<TestingProfile> profile_; | |
130 std::unique_ptr<ArcServiceManager> arc_service_manager_; | |
131 std::unique_ptr<ArcSessionManager> arc_session_manager_; | |
132 std::unique_ptr<ArcProvisionNotificationService> | |
133 arc_provision_notification_service_; | |
134 MockArcProvisionNotificationServiceDelegate* | |
135 arc_provision_notification_service_delegate_ = nullptr; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(ArcProvisionNotificationServiceTest); | |
138 }; | |
139 | |
140 } // namespace | |
141 | |
142 // The managed provision notification is displayed from the beginning of the | |
143 // silent opt-in till its successful finish. | |
144 TEST_F(ArcProvisionNotificationServiceTest, | |
145 ManagedProvisionNotification_Basic) { | |
146 // Set up managed ARC and assign managed values to all OptIn prefs. | |
147 profile()->GetTestingPrefService()->SetManagedPref(prefs::kArcEnabled, | |
148 new base::Value(true)); | |
149 profile()->GetTestingPrefService()->SetManagedPref( | |
150 prefs::kArcBackupRestoreEnabled, new base::Value(false)); | |
151 profile()->GetTestingPrefService()->SetManagedPref( | |
152 prefs::kArcLocationServiceEnabled, new base::Value(false)); | |
153 | |
154 arc_session_manager()->SetProfile(profile()); | |
155 | |
156 // Trigger opt-in flow. The notification gets shown. | |
157 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
158 ShowManagedProvisionNotification()); | |
159 arc_session_manager()->RequestEnable(); | |
160 EXPECT_EQ(ArcSessionManager::State::ACTIVE, arc_session_manager()->state()); | |
161 Mock::VerifyAndClearExpectations( | |
162 arc_provision_notification_service_delegate()); | |
163 | |
164 // Emulate successful provisioning. The notification gets removed. | |
165 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
166 RemoveManagedProvisionNotification()) | |
167 .Times(AtLeast(1)); | |
168 arc_session_manager()->OnProvisioningFinished(ProvisioningResult::SUCCESS); | |
169 Mock::VerifyAndClearExpectations( | |
170 arc_provision_notification_service_delegate()); | |
171 } | |
172 | |
173 // The managed provision notification is not displayed after the restart if the | |
174 // provisioning was successful. | |
175 TEST_F(ArcProvisionNotificationServiceTest, | |
176 ManagedProvisionNotification_Restart) { | |
177 // No notifications are expected to be shown in this test, but the delegate | |
178 // method that removes the notification is allowed to be called. | |
179 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
180 RemoveManagedProvisionNotification()) | |
181 .Times(AnyNumber()); | |
182 | |
183 // Set up managed ARC and assign managed values to all OptIn prefs. | |
184 profile()->GetTestingPrefService()->SetManagedPref(prefs::kArcEnabled, | |
185 new base::Value(true)); | |
186 profile()->GetTestingPrefService()->SetManagedPref( | |
187 prefs::kArcBackupRestoreEnabled, new base::Value(false)); | |
188 profile()->GetTestingPrefService()->SetManagedPref( | |
189 prefs::kArcLocationServiceEnabled, new base::Value(false)); | |
190 // Set the pref that indicates that signing into ARC has already been | |
191 // performed. | |
192 profile()->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); | |
193 | |
194 arc_session_manager()->SetProfile(profile()); | |
195 | |
196 // Enable ARC. The opt-in flow doesn't take place, and no notification is | |
197 // shown. | |
198 arc_session_manager()->RequestEnable(); | |
199 EXPECT_EQ(ArcSessionManager::State::ACTIVE, arc_session_manager()->state()); | |
200 arc_session_manager()->OnProvisioningFinished(ProvisioningResult::SUCCESS); | |
201 } | |
202 | |
203 // The managed provision notification is displayed from the beginning of the | |
204 // silent opt-in till the failure of the provision. | |
205 TEST_F(ArcProvisionNotificationServiceTest, | |
206 ManagedProvisionNotification_Failure) { | |
207 // Set up managed ARC and assign managed values to all OptIn prefs. | |
208 profile()->GetTestingPrefService()->SetManagedPref(prefs::kArcEnabled, | |
209 new base::Value(true)); | |
210 profile()->GetTestingPrefService()->SetManagedPref( | |
211 prefs::kArcBackupRestoreEnabled, new base::Value(false)); | |
212 profile()->GetTestingPrefService()->SetManagedPref( | |
213 prefs::kArcLocationServiceEnabled, new base::Value(false)); | |
214 | |
215 arc_session_manager()->SetProfile(profile()); | |
216 | |
217 // Trigger opt-in flow. The notification gets shown. | |
218 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
219 ShowManagedProvisionNotification()); | |
220 arc_session_manager()->RequestEnable(); | |
221 EXPECT_EQ(ArcSessionManager::State::ACTIVE, arc_session_manager()->state()); | |
222 Mock::VerifyAndClearExpectations( | |
223 arc_provision_notification_service_delegate()); | |
224 | |
225 // Emulate provisioning failure that leads to stopping ARC. The notification | |
226 // gets removed. | |
227 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
228 RemoveManagedProvisionNotification()) | |
229 .Times(AtLeast(1)); | |
230 arc_session_manager()->OnProvisioningFinished( | |
231 ProvisioningResult::CHROME_SERVER_COMMUNICATION_ERROR); | |
232 Mock::VerifyAndClearExpectations( | |
233 arc_provision_notification_service_delegate()); | |
234 } | |
235 | |
236 // The managed provision notification is displayed from the beginning of the | |
237 // silent opt-in till the failure of the provision. | |
238 TEST_F(ArcProvisionNotificationServiceTest, | |
239 ManagedProvisionNotification_FailureNotStopping) { | |
240 // Set up managed ARC and assign managed values to all OptIn prefs. | |
241 profile()->GetTestingPrefService()->SetManagedPref(prefs::kArcEnabled, | |
242 new base::Value(true)); | |
243 profile()->GetTestingPrefService()->SetManagedPref( | |
244 prefs::kArcBackupRestoreEnabled, new base::Value(false)); | |
245 profile()->GetTestingPrefService()->SetManagedPref( | |
246 prefs::kArcLocationServiceEnabled, new base::Value(false)); | |
247 | |
248 arc_session_manager()->SetProfile(profile()); | |
249 | |
250 // Trigger opt-in flow. The notification gets shown. | |
251 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
252 ShowManagedProvisionNotification()); | |
253 arc_session_manager()->RequestEnable(); | |
254 EXPECT_EQ(ArcSessionManager::State::ACTIVE, arc_session_manager()->state()); | |
255 Mock::VerifyAndClearExpectations( | |
256 arc_provision_notification_service_delegate()); | |
257 | |
258 // Emulate provisioning failure that leads to showing an error screen without | |
259 // shutting ARC down. The notification gets removed. | |
260 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
261 RemoveManagedProvisionNotification()) | |
262 .Times(AtLeast(1)); | |
263 arc_session_manager()->OnProvisioningFinished( | |
264 ProvisioningResult::NO_NETWORK_CONNECTION); | |
265 Mock::VerifyAndClearExpectations( | |
266 arc_provision_notification_service_delegate()); | |
267 } | |
268 | |
269 // The managed provision notification is not displayed when opt-in prefs are not | |
270 // managed. | |
271 TEST_F(ArcProvisionNotificationServiceTest, | |
272 ManagedProvisionNotification_NotSilent) { | |
273 // No notifications are expected to be shown in this test, but the delegate | |
274 // method that removes the notification is allowed to be called. | |
275 EXPECT_CALL(*arc_provision_notification_service_delegate(), | |
276 RemoveManagedProvisionNotification()) | |
277 .Times(AnyNumber()); | |
278 | |
279 // Set ARC to be managed. | |
280 profile()->GetTestingPrefService()->SetManagedPref(prefs::kArcEnabled, | |
281 new base::Value(true)); | |
282 | |
283 arc_session_manager()->SetProfile(profile()); | |
284 | |
285 // Trigger opt-in flow. The notification is not shown. | |
286 arc_session_manager()->RequestEnable(); | |
287 EXPECT_EQ(ArcSessionManager::State::NEGOTIATING_TERMS_OF_SERVICE, | |
288 arc_session_manager()->state()); | |
289 | |
290 // Emulate accepting the terms of service. | |
291 profile()->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, true); | |
292 arc_session_manager()->StartArcForTesting(); | |
293 EXPECT_EQ(ArcSessionManager::State::ACTIVE, arc_session_manager()->state()); | |
294 | |
295 // Emulate successful provisioning. | |
296 arc_session_manager()->OnProvisioningFinished(ProvisioningResult::SUCCESS); | |
297 } | |
298 | |
299 } // namespace arc | |
OLD | NEW |