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

Side by Side Diff: chrome/browser/chromeos/policy/consumer_management_service_unittest.cc

Issue 468873002: Show a desktop notification when the enrollment is completed or failed. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix broken build and un-inline virtual functions. Created 6 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/chromeos/policy/consumer_management_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/policy/consumer_management_service.h" 5 #include "chrome/browser/chromeos/policy/consumer_management_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/prefs/pref_registry_simple.h" 10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
11 #include "base/prefs/testing_pref_service.h" 12 #include "base/prefs/testing_pref_service.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
13 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/browser_process_platform_part.h" 15 #include "chrome/browser/browser_process_platform_part.h"
15 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" 17 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
17 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" 18 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
18 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 19 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
19 #include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h" 20 #include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h"
20 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" 21 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h"
21 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h" 22 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h"
23 #include "chrome/browser/notifications/notification_ui_manager.h"
22 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" 25 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
24 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" 26 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 27 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
26 #include "chrome/browser/signin/signin_manager_factory.h" 28 #include "chrome/browser/signin/signin_manager_factory.h"
27 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
28 #include "chrome/test/base/scoped_testing_local_state.h" 30 #include "chrome/test/base/browser_with_test_window_test.h"
29 #include "chrome/test/base/testing_browser_process.h" 31 #include "chrome/test/base/testing_browser_process.h"
30 #include "chrome/test/base/testing_profile.h" 32 #include "chrome/test/base/testing_profile.h"
33 #include "chrome/test/base/testing_profile_manager.h"
31 #include "chromeos/dbus/cryptohome/rpc.pb.h" 34 #include "chromeos/dbus/cryptohome/rpc.pb.h"
32 #include "chromeos/dbus/cryptohome_client.h" 35 #include "chromeos/dbus/cryptohome_client.h"
33 #include "chromeos/dbus/mock_cryptohome_client.h" 36 #include "chromeos/dbus/mock_cryptohome_client.h"
34 #include "components/signin/core/browser/profile_oauth2_token_service.h" 37 #include "components/signin/core/browser/profile_oauth2_token_service.h"
35 #include "components/signin/core/browser/signin_manager_base.h" 38 #include "components/signin/core/browser/signin_manager_base.h"
36 #include "content/public/browser/notification_details.h" 39 #include "content/public/browser/notification_details.h"
37 #include "content/public/browser/notification_source.h" 40 #include "content/public/browser/notification_source.h"
38 #include "content/public/test/test_browser_thread_bundle.h"
39 #include "google_apis/gaia/google_service_auth_error.h" 41 #include "google_apis/gaia/google_service_auth_error.h"
40 #include "testing/gmock/include/gmock/gmock.h" 42 #include "testing/gmock/include/gmock/gmock.h"
41 #include "testing/gtest/include/gtest/gtest.h" 43 #include "testing/gtest/include/gtest/gtest.h"
42 44
43 using testing::Invoke; 45 using testing::Invoke;
44 using testing::NiceMock; 46 using testing::NiceMock;
45 using testing::Return; 47 using testing::Return;
46 using testing::_; 48 using testing::_;
47 49
48 namespace { 50 namespace {
49 const char* kAttributeOwnerId = "consumer_management.owner_id"; 51 const char* kAttributeOwnerId = "consumer_management.owner_id";
50 const char* kTestOwner = "test@chromium.org.test"; 52 const char* kTestOwner = "test@chromium.org.test";
51 } 53 }
52 54
53 namespace policy { 55 namespace policy {
54 56
55 class ConsumerManagementServiceTest : public testing::Test { 57 class ConsumerManagementServiceTest : public BrowserWithTestWindowTest {
56 public: 58 public:
57 ConsumerManagementServiceTest() 59 ConsumerManagementServiceTest()
58 : testing_local_state_(TestingBrowserProcess::GetGlobal()), 60 : cryptohome_result_(false),
59 cryptohome_result_(false),
60 set_owner_status_(false) { 61 set_owner_status_(false) {
61 ON_CALL(mock_cryptohome_client_, GetBootAttribute(_, _)) 62 ON_CALL(mock_cryptohome_client_, GetBootAttribute(_, _))
62 .WillByDefault( 63 .WillByDefault(
63 Invoke(this, &ConsumerManagementServiceTest::MockGetBootAttribute)); 64 Invoke(this, &ConsumerManagementServiceTest::MockGetBootAttribute));
64 ON_CALL(mock_cryptohome_client_, SetBootAttribute(_, _)) 65 ON_CALL(mock_cryptohome_client_, SetBootAttribute(_, _))
65 .WillByDefault( 66 .WillByDefault(
66 Invoke(this, &ConsumerManagementServiceTest::MockSetBootAttribute)); 67 Invoke(this, &ConsumerManagementServiceTest::MockSetBootAttribute));
67 ON_CALL(mock_cryptohome_client_, FlushAndSignBootAttributes(_, _)) 68 ON_CALL(mock_cryptohome_client_, FlushAndSignBootAttributes(_, _))
68 .WillByDefault( 69 .WillByDefault(
69 Invoke(this, 70 Invoke(this,
70 &ConsumerManagementServiceTest:: 71 &ConsumerManagementServiceTest::
71 MockFlushAndSignBootAttributes)); 72 MockFlushAndSignBootAttributes));
72 } 73 }
73 74
74 virtual void SetUp() OVERRIDE { 75 virtual void SetUp() OVERRIDE {
76 BrowserWithTestWindowTest::SetUp();
77
78 testing_profile_manager_.reset(new TestingProfileManager(
79 TestingBrowserProcess::GetGlobal()));
80 ASSERT_TRUE(testing_profile_manager_->SetUp());
81
75 service_.reset(new ConsumerManagementService(&mock_cryptohome_client_)); 82 service_.reset(new ConsumerManagementService(&mock_cryptohome_client_));
76 } 83 }
77 84
78 virtual void TearDown() OVERRIDE { 85 virtual void TearDown() OVERRIDE {
86 testing_profile_manager_.reset();
87
79 service_.reset(); 88 service_.reset();
89
90 BrowserWithTestWindowTest::TearDown();
80 } 91 }
81 92
82 ConsumerManagementService::ConsumerEnrollmentState GetEnrollmentState() { 93 ConsumerManagementService::ConsumerEnrollmentState GetEnrollmentState() {
83 return static_cast<ConsumerManagementService::ConsumerEnrollmentState>( 94 return static_cast<ConsumerManagementService::ConsumerEnrollmentState>(
84 testing_local_state_.Get()->GetInteger( 95 g_browser_process->local_state()->GetInteger(
85 prefs::kConsumerManagementEnrollmentState)); 96 prefs::kConsumerManagementEnrollmentState));
86 } 97 }
87 98
88 void SetEnrollmentState( 99 void SetEnrollmentState(
89 ConsumerManagementService::ConsumerEnrollmentState state) { 100 ConsumerManagementService::ConsumerEnrollmentState state) {
90 testing_local_state_.Get()->SetInteger( 101 g_browser_process->local_state()->SetInteger(
91 prefs::kConsumerManagementEnrollmentState, state); 102 prefs::kConsumerManagementEnrollmentState, state);
92 } 103 }
93 104
94 void MockGetBootAttribute( 105 void MockGetBootAttribute(
95 const cryptohome::GetBootAttributeRequest& request, 106 const cryptohome::GetBootAttributeRequest& request,
96 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) { 107 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) {
97 get_boot_attribute_request_ = request; 108 get_boot_attribute_request_ = request;
98 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_); 109 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_);
99 } 110 }
100 111
(...skipping 11 matching lines...) Expand all
112 } 123 }
113 124
114 void OnGetOwnerDone(const std::string& owner) { 125 void OnGetOwnerDone(const std::string& owner) {
115 owner_ = owner; 126 owner_ = owner;
116 } 127 }
117 128
118 void OnSetOwnerDone(bool status) { 129 void OnSetOwnerDone(bool status) {
119 set_owner_status_ = status; 130 set_owner_status_ = status;
120 } 131 }
121 132
122 ScopedTestingLocalState testing_local_state_;
123 NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_; 133 NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_;
124 scoped_ptr<ConsumerManagementService> service_; 134 scoped_ptr<ConsumerManagementService> service_;
125 135
136 scoped_ptr<TestingProfileManager> testing_profile_manager_;
126 chromeos::DBusMethodCallStatus cryptohome_status_; 137 chromeos::DBusMethodCallStatus cryptohome_status_;
127 bool cryptohome_result_; 138 bool cryptohome_result_;
128 cryptohome::BaseReply cryptohome_reply_; 139 cryptohome::BaseReply cryptohome_reply_;
129 cryptohome::GetBootAttributeRequest get_boot_attribute_request_; 140 cryptohome::GetBootAttributeRequest get_boot_attribute_request_;
130 cryptohome::SetBootAttributeRequest set_boot_attribute_request_; 141 cryptohome::SetBootAttributeRequest set_boot_attribute_request_;
131 142
132 std::string owner_; 143 std::string owner_;
133 bool set_owner_status_; 144 bool set_owner_status_;
134 }; 145 };
135 146
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 } 213 }
203 214
204 class ConsumerManagementServiceEnrollmentTest 215 class ConsumerManagementServiceEnrollmentTest
205 : public ConsumerManagementServiceTest { 216 : public ConsumerManagementServiceTest {
206 public: 217 public:
207 ConsumerManagementServiceEnrollmentTest() 218 ConsumerManagementServiceEnrollmentTest()
208 : mock_user_manager_(new NiceMock<chromeos::MockUserManager>()), 219 : mock_user_manager_(new NiceMock<chromeos::MockUserManager>()),
209 scoped_user_manager_enabler_(mock_user_manager_), 220 scoped_user_manager_enabler_(mock_user_manager_),
210 fake_initializer_(new FakeDeviceCloudPolicyInitializer()), 221 fake_initializer_(new FakeDeviceCloudPolicyInitializer()),
211 enrollment_status_(EnrollmentStatus::ForStatus( 222 enrollment_status_(EnrollmentStatus::ForStatus(
212 EnrollmentStatus::STATUS_SUCCESS)){ 223 EnrollmentStatus::STATUS_SUCCESS)) {
213 // Set up MockUserManager. The first user will be the owner. 224 // Set up MockUserManager. The first user will be the owner.
214 mock_user_manager_->AddUser(kTestOwner); 225 mock_user_manager_->AddUser(kTestOwner);
215 226
216 // Return false for IsCurrentUserOwner() so that the enrollment state is not 227 // Return false for IsCurrentUserOwner() so that the enrollment state is not
217 // reset. 228 // reset.
218 ON_CALL(*mock_user_manager_, IsCurrentUserOwner()) 229 ON_CALL(*mock_user_manager_, IsCurrentUserOwner())
219 .WillByDefault(Return(false)); 230 .WillByDefault(Return(false));
220 231
221 // Create test profile and set up FakeProfileOAuth2TokenService.
222 TestingProfile::Builder builder;
223 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
224 &BuildAutoIssuingFakeProfileOAuth2TokenService);
225 testing_owner_profile_ = builder.Build();
226 testing_owner_profile_->set_profile_name(kTestOwner);
227
228 // Set up the authenticated user name and ID.
229 SigninManagerFactory::GetForProfile(testing_owner_profile_.get())->
230 SetAuthenticatedUsername(kTestOwner);
231
232 // Issue a fake refresh token.
233 GetFakeProfileOAuth2TokenService()->
234 IssueRefreshTokenForUser(kTestOwner, "fake_token");
235
236 // Inject FakeDeviceCloudPolicyInitializer. 232 // Inject FakeDeviceCloudPolicyInitializer.
237 BrowserPolicyConnectorChromeOS* connector = 233 BrowserPolicyConnectorChromeOS* connector =
238 g_browser_process->platform_part()->browser_policy_connector_chromeos(); 234 g_browser_process->platform_part()->browser_policy_connector_chromeos();
239 connector->SetDeviceCloudPolicyInitializerForTesting( 235 connector->SetDeviceCloudPolicyInitializerForTesting(
240 scoped_ptr<DeviceCloudPolicyInitializer>(fake_initializer_)); 236 scoped_ptr<DeviceCloudPolicyInitializer>(fake_initializer_));
237 }
238
239 virtual void SetUp() OVERRIDE {
240 ConsumerManagementServiceTest::SetUp();
241
242 // Set up the testing profile.
243 profile()->set_profile_name(kTestOwner);
244
245 // Set up FakeProfileOAuth2TokenService and issue a fake refresh token.
246 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
247 profile(), &BuildAutoIssuingFakeProfileOAuth2TokenService);
248 GetFakeProfileOAuth2TokenService()->
249 IssueRefreshTokenForUser(kTestOwner, "fake_token");
250
251 // Set up the authenticated user name and ID.
252 SigninManagerFactory::GetForProfile(profile())->
253 SetAuthenticatedUsername(kTestOwner);
241 254
242 // The service should continue the enrollment process if the state is 255 // The service should continue the enrollment process if the state is
243 // ENROLLMENT_OWNER_STORED. 256 // ENROLLMENT_OWNER_STORED.
244 SetEnrollmentState(ConsumerManagementService::ENROLLMENT_OWNER_STORED); 257 SetEnrollmentState(ConsumerManagementService::ENROLLMENT_OWNER_STORED);
245 } 258 }
246 259
260 virtual void TearDown() OVERRIDE {
261 g_browser_process->notification_ui_manager()->CancelAll();
262
263 ConsumerManagementServiceTest::TearDown();
264 }
265
247 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() { 266 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() {
248 return static_cast<FakeProfileOAuth2TokenService*>( 267 return static_cast<FakeProfileOAuth2TokenService*>(
249 ProfileOAuth2TokenServiceFactory::GetForProfile( 268 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
250 testing_owner_profile_.get())); 269 }
270
271 bool HasEnrollmentNotification() {
272 return g_browser_process->notification_ui_manager()->
273 FindById("consumer_management.enroll");
251 } 274 }
252 275
253 void RunEnrollmentTest() { 276 void RunEnrollmentTest() {
254 // Send the profile prepared notification to continue the enrollment. 277 // Send the profile prepared notification to continue the enrollment.
255 service_->Observe(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, 278 service_->Observe(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
256 content::Source<void>(NULL), // Not used. 279 content::Source<void>(NULL), // Not used.
257 content::Details<Profile>(testing_owner_profile_.get())); 280 content::Details<Profile>(profile()));
258 base::RunLoop().RunUntilIdle(); 281 base::RunLoop().RunUntilIdle();
259 } 282 }
260 283
261 content::TestBrowserThreadBundle thread_bundle_;
262
263 NiceMock<chromeos::MockUserManager>* mock_user_manager_; 284 NiceMock<chromeos::MockUserManager>* mock_user_manager_;
264 chromeos::ScopedUserManagerEnabler scoped_user_manager_enabler_; 285 chromeos::ScopedUserManagerEnabler scoped_user_manager_enabler_;
265 scoped_ptr<TestingProfile> testing_owner_profile_;
266 FakeDeviceCloudPolicyInitializer* fake_initializer_; 286 FakeDeviceCloudPolicyInitializer* fake_initializer_;
267 EnrollmentStatus enrollment_status_; 287 EnrollmentStatus enrollment_status_;
268 }; 288 };
269 289
270 TEST_F(ConsumerManagementServiceEnrollmentTest, EnrollsSuccessfully) { 290 TEST_F(ConsumerManagementServiceEnrollmentTest, EnrollsSuccessfully) {
271 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); 291 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called());
272 292
273 RunEnrollmentTest(); 293 RunEnrollmentTest();
274 294
275 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); 295 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called());
276 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_SUCCESS, 296 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_SUCCESS,
277 GetEnrollmentState()); 297 GetEnrollmentState());
298 EXPECT_FALSE(HasEnrollmentNotification());
278 } 299 }
279 300
280 TEST_F(ConsumerManagementServiceEnrollmentTest, 301 TEST_F(ConsumerManagementServiceEnrollmentTest,
281 EnrollmentStateIsResetIfCurrentUserIsOwner) { 302 ShowsDesktopNotificationAndResetsEnrollmentStateIfCurrentUserIsOwner) {
282 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_OWNER_STORED, 303 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_OWNER_STORED,
283 GetEnrollmentState()); 304 GetEnrollmentState());
284 305 EXPECT_FALSE(HasEnrollmentNotification());
285 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner()) 306 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner())
286 .WillOnce(Return(true)); 307 .WillOnce(Return(true));
287 308
288 RunEnrollmentTest(); 309 RunEnrollmentTest();
289 310
290 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, GetEnrollmentState()); 311 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, GetEnrollmentState());
312 EXPECT_TRUE(HasEnrollmentNotification());
291 } 313 }
292 314
293 TEST_F(ConsumerManagementServiceEnrollmentTest, FailsToGetAccessToken) { 315 TEST_F(ConsumerManagementServiceEnrollmentTest, FailsToGetAccessToken) {
294 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for 316 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for
295 // the access token to be available. 317 // the access token to be available.
296 GetFakeProfileOAuth2TokenService()-> 318 GetFakeProfileOAuth2TokenService()->
297 set_auto_post_fetch_response_on_message_loop(false); 319 set_auto_post_fetch_response_on_message_loop(false);
298 320
299 RunEnrollmentTest(); 321 RunEnrollmentTest();
300 322
(...skipping 18 matching lines...) Expand all
319 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus( 341 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus(
320 EnrollmentStatus::STATUS_REGISTRATION_FAILED)); 342 EnrollmentStatus::STATUS_REGISTRATION_FAILED));
321 343
322 RunEnrollmentTest(); 344 RunEnrollmentTest();
323 345
324 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); 346 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called());
325 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_DM_SERVER_FAILED, 347 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_DM_SERVER_FAILED,
326 GetEnrollmentState()); 348 GetEnrollmentState());
327 } 349 }
328 350
329 TEST_F(ConsumerManagementServiceEnrollmentTest, ShowsDesktopNotificationOnly) { 351 TEST_F(ConsumerManagementServiceEnrollmentTest,
352 ShowsDesktopNotificationOnlyIfEnrollmentIsAlreadyCompleted) {
330 SetEnrollmentState(ConsumerManagementService::ENROLLMENT_CANCELED); 353 SetEnrollmentState(ConsumerManagementService::ENROLLMENT_CANCELED);
354 EXPECT_FALSE(HasEnrollmentNotification());
331 355
332 RunEnrollmentTest(); 356 RunEnrollmentTest();
333 357
334 // TODO(davidyu): Check that the desktop notification was shown.
335
336 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); 358 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called());
337 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, GetEnrollmentState()); 359 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, GetEnrollmentState());
360 EXPECT_TRUE(HasEnrollmentNotification());
338 } 361 }
339 362
340 } // namespace policy 363 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/policy/consumer_management_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698