| OLD | NEW |
| 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/pref_service.h" |
| 12 #include "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/browser_process_platform_part.h" | 15 #include "chrome/browser/browser_process_platform_part.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" | 17 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" |
| 18 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 18 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 19 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 19 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 20 #include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h" | 20 #include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h" |
| 21 #include "chrome/browser/chromeos/policy/device_policy_builder.h" |
| 21 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" | 22 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" |
| 22 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h" | 23 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h" |
| 24 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" |
| 23 #include "chrome/browser/notifications/notification_ui_manager.h" | 25 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 27 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 26 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | 28 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
| 27 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 29 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 28 #include "chrome/browser/signin/signin_manager_factory.h" | 30 #include "chrome/browser/signin/signin_manager_factory.h" |
| 29 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| 30 #include "chrome/test/base/browser_with_test_window_test.h" | 32 #include "chrome/test/base/browser_with_test_window_test.h" |
| 33 #include "chrome/test/base/scoped_testing_local_state.h" |
| 31 #include "chrome/test/base/testing_browser_process.h" | 34 #include "chrome/test/base/testing_browser_process.h" |
| 32 #include "chrome/test/base/testing_profile.h" | 35 #include "chrome/test/base/testing_profile.h" |
| 33 #include "chrome/test/base/testing_profile_manager.h" | 36 #include "chrome/test/base/testing_profile_manager.h" |
| 34 #include "chromeos/dbus/cryptohome/rpc.pb.h" | 37 #include "chromeos/dbus/cryptohome/rpc.pb.h" |
| 35 #include "chromeos/dbus/cryptohome_client.h" | 38 #include "chromeos/dbus/cryptohome_client.h" |
| 36 #include "chromeos/dbus/mock_cryptohome_client.h" | 39 #include "chromeos/dbus/mock_cryptohome_client.h" |
| 37 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 40 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 38 #include "components/signin/core/browser/signin_manager_base.h" | 41 #include "components/signin/core/browser/signin_manager_base.h" |
| 39 #include "content/public/browser/notification_details.h" | 42 #include "content/public/browser/notification_details.h" |
| 40 #include "content/public/browser/notification_source.h" | 43 #include "content/public/browser/notification_source.h" |
| 41 #include "google_apis/gaia/google_service_auth_error.h" | 44 #include "google_apis/gaia/google_service_auth_error.h" |
| 45 #include "policy/proto/device_management_backend.pb.h" |
| 42 #include "testing/gmock/include/gmock/gmock.h" | 46 #include "testing/gmock/include/gmock/gmock.h" |
| 43 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
| 44 | 48 |
| 45 using testing::Invoke; | 49 using testing::Invoke; |
| 46 using testing::NiceMock; | 50 using testing::NiceMock; |
| 47 using testing::Return; | 51 using testing::Return; |
| 48 using testing::_; | 52 using testing::_; |
| 49 | 53 |
| 54 namespace em = enterprise_management; |
| 55 |
| 50 namespace { | 56 namespace { |
| 51 const char* kAttributeOwnerId = "consumer_management.owner_id"; | 57 const char* kAttributeOwnerId = "consumer_management.owner_id"; |
| 52 const char* kTestOwner = "test@chromium.org.test"; | 58 const char* kTestOwner = "test@chromium.org.test"; |
| 53 } | 59 } |
| 54 | 60 |
| 55 namespace policy { | 61 namespace policy { |
| 56 | 62 |
| 57 class ConsumerManagementServiceTest : public BrowserWithTestWindowTest { | 63 class ConsumerManagementServiceTest : public BrowserWithTestWindowTest { |
| 58 public: | 64 public: |
| 59 ConsumerManagementServiceTest() | 65 ConsumerManagementServiceTest() |
| (...skipping 12 matching lines...) Expand all Loading... |
| 72 MockFlushAndSignBootAttributes)); | 78 MockFlushAndSignBootAttributes)); |
| 73 } | 79 } |
| 74 | 80 |
| 75 virtual void SetUp() OVERRIDE { | 81 virtual void SetUp() OVERRIDE { |
| 76 BrowserWithTestWindowTest::SetUp(); | 82 BrowserWithTestWindowTest::SetUp(); |
| 77 | 83 |
| 78 testing_profile_manager_.reset(new TestingProfileManager( | 84 testing_profile_manager_.reset(new TestingProfileManager( |
| 79 TestingBrowserProcess::GetGlobal())); | 85 TestingBrowserProcess::GetGlobal())); |
| 80 ASSERT_TRUE(testing_profile_manager_->SetUp()); | 86 ASSERT_TRUE(testing_profile_manager_->SetUp()); |
| 81 | 87 |
| 82 service_.reset(new ConsumerManagementService(&mock_cryptohome_client_)); | 88 service_.reset(new ConsumerManagementService(&mock_cryptohome_client_, |
| 89 NULL)); |
| 83 } | 90 } |
| 84 | 91 |
| 85 virtual void TearDown() OVERRIDE { | 92 virtual void TearDown() OVERRIDE { |
| 86 testing_profile_manager_.reset(); | 93 testing_profile_manager_.reset(); |
| 87 | 94 |
| 88 service_.reset(); | 95 service_.reset(); |
| 89 | 96 |
| 90 BrowserWithTestWindowTest::TearDown(); | 97 BrowserWithTestWindowTest::TearDown(); |
| 91 } | 98 } |
| 92 | 99 |
| 93 ConsumerManagementService::ConsumerEnrollmentState GetEnrollmentState() { | 100 ConsumerManagementService::EnrollmentStage GetEnrollmentStage() { |
| 94 return static_cast<ConsumerManagementService::ConsumerEnrollmentState>( | 101 return static_cast<ConsumerManagementService::EnrollmentStage>( |
| 95 g_browser_process->local_state()->GetInteger( | 102 g_browser_process->local_state()->GetInteger( |
| 96 prefs::kConsumerManagementEnrollmentState)); | 103 prefs::kConsumerManagementEnrollmentStage)); |
| 97 } | 104 } |
| 98 | 105 |
| 99 void SetEnrollmentState( | 106 void SetEnrollmentStage(ConsumerManagementService::EnrollmentStage stage) { |
| 100 ConsumerManagementService::ConsumerEnrollmentState state) { | |
| 101 g_browser_process->local_state()->SetInteger( | 107 g_browser_process->local_state()->SetInteger( |
| 102 prefs::kConsumerManagementEnrollmentState, state); | 108 prefs::kConsumerManagementEnrollmentStage, stage); |
| 103 } | 109 } |
| 104 | 110 |
| 105 void MockGetBootAttribute( | 111 void MockGetBootAttribute( |
| 106 const cryptohome::GetBootAttributeRequest& request, | 112 const cryptohome::GetBootAttributeRequest& request, |
| 107 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) { | 113 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) { |
| 108 get_boot_attribute_request_ = request; | 114 get_boot_attribute_request_ = request; |
| 109 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_); | 115 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_); |
| 110 } | 116 } |
| 111 | 117 |
| 112 void MockSetBootAttribute( | 118 void MockSetBootAttribute( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 123 } | 129 } |
| 124 | 130 |
| 125 void OnGetOwnerDone(const std::string& owner) { | 131 void OnGetOwnerDone(const std::string& owner) { |
| 126 owner_ = owner; | 132 owner_ = owner; |
| 127 } | 133 } |
| 128 | 134 |
| 129 void OnSetOwnerDone(bool status) { | 135 void OnSetOwnerDone(bool status) { |
| 130 set_owner_status_ = status; | 136 set_owner_status_ = status; |
| 131 } | 137 } |
| 132 | 138 |
| 139 // Variables for building the service. |
| 133 NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_; | 140 NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_; |
| 134 scoped_ptr<ConsumerManagementService> service_; | 141 scoped_ptr<ConsumerManagementService> service_; |
| 135 | 142 |
| 136 scoped_ptr<TestingProfileManager> testing_profile_manager_; | 143 scoped_ptr<TestingProfileManager> testing_profile_manager_; |
| 144 |
| 145 // Variables for setting the return value or catching the arguments of mock |
| 146 // functions. |
| 137 chromeos::DBusMethodCallStatus cryptohome_status_; | 147 chromeos::DBusMethodCallStatus cryptohome_status_; |
| 138 bool cryptohome_result_; | 148 bool cryptohome_result_; |
| 139 cryptohome::BaseReply cryptohome_reply_; | 149 cryptohome::BaseReply cryptohome_reply_; |
| 140 cryptohome::GetBootAttributeRequest get_boot_attribute_request_; | 150 cryptohome::GetBootAttributeRequest get_boot_attribute_request_; |
| 141 cryptohome::SetBootAttributeRequest set_boot_attribute_request_; | 151 cryptohome::SetBootAttributeRequest set_boot_attribute_request_; |
| 142 | |
| 143 std::string owner_; | 152 std::string owner_; |
| 144 bool set_owner_status_; | 153 bool set_owner_status_; |
| 145 }; | 154 }; |
| 146 | 155 |
| 147 TEST_F(ConsumerManagementServiceTest, CanGetEnrollmentState) { | 156 TEST_F(ConsumerManagementServiceTest, CanGetEnrollmentStage) { |
| 148 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, | 157 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_NONE, |
| 149 service_->GetEnrollmentState()); | 158 service_->GetEnrollmentStage()); |
| 150 | 159 |
| 151 SetEnrollmentState(ConsumerManagementService::ENROLLMENT_REQUESTED); | 160 SetEnrollmentStage(ConsumerManagementService::ENROLLMENT_STAGE_REQUESTED); |
| 152 | 161 |
| 153 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_REQUESTED, | 162 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_REQUESTED, |
| 154 service_->GetEnrollmentState()); | 163 service_->GetEnrollmentStage()); |
| 155 } | 164 } |
| 156 | 165 |
| 157 TEST_F(ConsumerManagementServiceTest, CanSetEnrollmentState) { | 166 TEST_F(ConsumerManagementServiceTest, CanSetEnrollmentStage) { |
| 158 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, GetEnrollmentState()); | 167 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_NONE, |
| 168 GetEnrollmentStage()); |
| 159 | 169 |
| 160 service_->SetEnrollmentState(ConsumerManagementService::ENROLLMENT_REQUESTED); | 170 service_->SetEnrollmentStage( |
| 171 ConsumerManagementService::ENROLLMENT_STAGE_REQUESTED); |
| 161 | 172 |
| 162 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_REQUESTED, | 173 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_REQUESTED, |
| 163 GetEnrollmentState()); | 174 GetEnrollmentStage()); |
| 164 } | 175 } |
| 165 | 176 |
| 166 TEST_F(ConsumerManagementServiceTest, CanGetOwner) { | 177 TEST_F(ConsumerManagementServiceTest, CanGetOwner) { |
| 167 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_SUCCESS; | 178 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_SUCCESS; |
| 168 cryptohome_result_ = true; | 179 cryptohome_result_ = true; |
| 169 cryptohome_reply_.MutableExtension(cryptohome::GetBootAttributeReply::reply)-> | 180 cryptohome_reply_.MutableExtension(cryptohome::GetBootAttributeReply::reply)-> |
| 170 set_value(kTestOwner); | 181 set_value(kTestOwner); |
| 171 | 182 |
| 172 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone, | 183 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone, |
| 173 base::Unretained(this))); | 184 base::Unretained(this))); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 public: | 228 public: |
| 218 ConsumerManagementServiceEnrollmentTest() | 229 ConsumerManagementServiceEnrollmentTest() |
| 219 : mock_user_manager_(new NiceMock<chromeos::MockUserManager>()), | 230 : mock_user_manager_(new NiceMock<chromeos::MockUserManager>()), |
| 220 scoped_user_manager_enabler_(mock_user_manager_), | 231 scoped_user_manager_enabler_(mock_user_manager_), |
| 221 fake_initializer_(new FakeDeviceCloudPolicyInitializer()), | 232 fake_initializer_(new FakeDeviceCloudPolicyInitializer()), |
| 222 enrollment_status_(EnrollmentStatus::ForStatus( | 233 enrollment_status_(EnrollmentStatus::ForStatus( |
| 223 EnrollmentStatus::STATUS_SUCCESS)) { | 234 EnrollmentStatus::STATUS_SUCCESS)) { |
| 224 // Set up MockUserManager. The first user will be the owner. | 235 // Set up MockUserManager. The first user will be the owner. |
| 225 mock_user_manager_->AddUser(kTestOwner); | 236 mock_user_manager_->AddUser(kTestOwner); |
| 226 | 237 |
| 227 // Return false for IsCurrentUserOwner() so that the enrollment state is not | 238 // Return false for IsCurrentUserOwner() so that the enrollment stage is not |
| 228 // reset. | 239 // reset. |
| 229 ON_CALL(*mock_user_manager_, IsCurrentUserOwner()) | 240 ON_CALL(*mock_user_manager_, IsCurrentUserOwner()) |
| 230 .WillByDefault(Return(false)); | 241 .WillByDefault(Return(false)); |
| 231 | 242 |
| 232 // Inject FakeDeviceCloudPolicyInitializer. | 243 // Inject FakeDeviceCloudPolicyInitializer. |
| 233 BrowserPolicyConnectorChromeOS* connector = | 244 BrowserPolicyConnectorChromeOS* connector = |
| 234 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | 245 g_browser_process->platform_part()->browser_policy_connector_chromeos(); |
| 235 connector->SetDeviceCloudPolicyInitializerForTesting( | 246 connector->SetDeviceCloudPolicyInitializerForTesting( |
| 236 scoped_ptr<DeviceCloudPolicyInitializer>(fake_initializer_)); | 247 scoped_ptr<DeviceCloudPolicyInitializer>(fake_initializer_)); |
| 237 } | 248 } |
| 238 | 249 |
| 239 virtual void SetUp() OVERRIDE { | 250 virtual void SetUp() OVERRIDE { |
| 240 ConsumerManagementServiceTest::SetUp(); | 251 ConsumerManagementServiceTest::SetUp(); |
| 241 | 252 |
| 242 // Set up the testing profile. | 253 // Set up the testing profile. |
| 243 profile()->set_profile_name(kTestOwner); | 254 profile()->set_profile_name(kTestOwner); |
| 244 | 255 |
| 245 // Set up FakeProfileOAuth2TokenService and issue a fake refresh token. | 256 // Set up FakeProfileOAuth2TokenService and issue a fake refresh token. |
| 246 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( | 257 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( |
| 247 profile(), &BuildAutoIssuingFakeProfileOAuth2TokenService); | 258 profile(), &BuildAutoIssuingFakeProfileOAuth2TokenService); |
| 248 GetFakeProfileOAuth2TokenService()-> | 259 GetFakeProfileOAuth2TokenService()-> |
| 249 IssueRefreshTokenForUser(kTestOwner, "fake_token"); | 260 IssueRefreshTokenForUser(kTestOwner, "fake_token"); |
| 250 | 261 |
| 251 // Set up the authenticated user name and ID. | 262 // Set up the authenticated user name and ID. |
| 252 SigninManagerFactory::GetForProfile(profile())-> | 263 SigninManagerFactory::GetForProfile(profile())-> |
| 253 SetAuthenticatedUsername(kTestOwner); | 264 SetAuthenticatedUsername(kTestOwner); |
| 254 | 265 |
| 255 // The service should continue the enrollment process if the state is | 266 // The service should continue the enrollment process if the stage is |
| 256 // ENROLLMENT_OWNER_STORED. | 267 // ENROLLMENT_STAGE_OWNER_STORED. |
| 257 SetEnrollmentState(ConsumerManagementService::ENROLLMENT_OWNER_STORED); | 268 SetEnrollmentStage( |
| 269 ConsumerManagementService::ENROLLMENT_STAGE_OWNER_STORED); |
| 258 } | 270 } |
| 259 | 271 |
| 260 virtual void TearDown() OVERRIDE { | 272 virtual void TearDown() OVERRIDE { |
| 261 g_browser_process->notification_ui_manager()->CancelAll(); | 273 g_browser_process->notification_ui_manager()->CancelAll(); |
| 262 | 274 |
| 263 ConsumerManagementServiceTest::TearDown(); | 275 ConsumerManagementServiceTest::TearDown(); |
| 264 } | 276 } |
| 265 | 277 |
| 266 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() { | 278 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() { |
| 267 return static_cast<FakeProfileOAuth2TokenService*>( | 279 return static_cast<FakeProfileOAuth2TokenService*>( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 286 FakeDeviceCloudPolicyInitializer* fake_initializer_; | 298 FakeDeviceCloudPolicyInitializer* fake_initializer_; |
| 287 EnrollmentStatus enrollment_status_; | 299 EnrollmentStatus enrollment_status_; |
| 288 }; | 300 }; |
| 289 | 301 |
| 290 TEST_F(ConsumerManagementServiceEnrollmentTest, EnrollsSuccessfully) { | 302 TEST_F(ConsumerManagementServiceEnrollmentTest, EnrollsSuccessfully) { |
| 291 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | 303 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); |
| 292 | 304 |
| 293 RunEnrollmentTest(); | 305 RunEnrollmentTest(); |
| 294 | 306 |
| 295 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); | 307 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); |
| 296 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_SUCCESS, | 308 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS, |
| 297 GetEnrollmentState()); | 309 GetEnrollmentStage()); |
| 298 EXPECT_FALSE(HasEnrollmentNotification()); | 310 EXPECT_FALSE(HasEnrollmentNotification()); |
| 299 } | 311 } |
| 300 | 312 |
| 301 TEST_F(ConsumerManagementServiceEnrollmentTest, | 313 TEST_F(ConsumerManagementServiceEnrollmentTest, |
| 302 ShowsDesktopNotificationAndResetsEnrollmentStateIfCurrentUserIsOwner) { | 314 ShowsDesktopNotificationAndResetsEnrollmentStageIfCurrentUserIsOwner) { |
| 303 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_OWNER_STORED, | 315 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_OWNER_STORED, |
| 304 GetEnrollmentState()); | 316 GetEnrollmentStage()); |
| 305 EXPECT_FALSE(HasEnrollmentNotification()); | 317 EXPECT_FALSE(HasEnrollmentNotification()); |
| 306 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner()) | 318 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner()) |
| 307 .WillOnce(Return(true)); | 319 .WillOnce(Return(true)); |
| 308 | 320 |
| 309 RunEnrollmentTest(); | 321 RunEnrollmentTest(); |
| 310 | 322 |
| 311 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, GetEnrollmentState()); | 323 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_NONE, |
| 324 GetEnrollmentStage()); |
| 312 EXPECT_TRUE(HasEnrollmentNotification()); | 325 EXPECT_TRUE(HasEnrollmentNotification()); |
| 313 } | 326 } |
| 314 | 327 |
| 315 TEST_F(ConsumerManagementServiceEnrollmentTest, FailsToGetAccessToken) { | 328 TEST_F(ConsumerManagementServiceEnrollmentTest, FailsToGetAccessToken) { |
| 316 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for | 329 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for |
| 317 // the access token to be available. | 330 // the access token to be available. |
| 318 GetFakeProfileOAuth2TokenService()-> | 331 GetFakeProfileOAuth2TokenService()-> |
| 319 set_auto_post_fetch_response_on_message_loop(false); | 332 set_auto_post_fetch_response_on_message_loop(false); |
| 320 | 333 |
| 321 RunEnrollmentTest(); | 334 RunEnrollmentTest(); |
| 322 | 335 |
| 323 // The service should have a pending token request. | 336 // The service should have a pending token request. |
| 324 OAuth2TokenService::Request* token_request = | 337 OAuth2TokenService::Request* token_request = |
| 325 service_->GetTokenRequestForTesting(); | 338 service_->GetTokenRequestForTesting(); |
| 326 EXPECT_TRUE(token_request); | 339 EXPECT_TRUE(token_request); |
| 327 | 340 |
| 328 // Tell the service that the access token is not available because of some | 341 // Tell the service that the access token is not available because of some |
| 329 // backend issue. | 342 // backend issue. |
| 330 service_->OnGetTokenFailure( | 343 service_->OnGetTokenFailure( |
| 331 token_request, | 344 token_request, |
| 332 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); | 345 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR)); |
| 333 | 346 |
| 334 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | 347 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); |
| 335 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_GET_TOKEN_FAILED, | 348 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_GET_TOKEN_FAILED, |
| 336 GetEnrollmentState()); | 349 GetEnrollmentStage()); |
| 337 } | 350 } |
| 338 | 351 |
| 339 TEST_F(ConsumerManagementServiceEnrollmentTest, FailsToRegister) { | 352 TEST_F(ConsumerManagementServiceEnrollmentTest, FailsToRegister) { |
| 340 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | 353 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); |
| 341 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus( | 354 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus( |
| 342 EnrollmentStatus::STATUS_REGISTRATION_FAILED)); | 355 EnrollmentStatus::STATUS_REGISTRATION_FAILED)); |
| 343 | 356 |
| 344 RunEnrollmentTest(); | 357 RunEnrollmentTest(); |
| 345 | 358 |
| 346 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); | 359 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called()); |
| 347 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_DM_SERVER_FAILED, | 360 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_DM_SERVER_FAILED, |
| 348 GetEnrollmentState()); | 361 GetEnrollmentStage()); |
| 349 } | 362 } |
| 350 | 363 |
| 351 TEST_F(ConsumerManagementServiceEnrollmentTest, | 364 TEST_F(ConsumerManagementServiceEnrollmentTest, |
| 352 ShowsDesktopNotificationOnlyIfEnrollmentIsAlreadyCompleted) { | 365 ShowsDesktopNotificationOnlyIfEnrollmentIsAlreadyCompleted) { |
| 353 SetEnrollmentState(ConsumerManagementService::ENROLLMENT_CANCELED); | 366 SetEnrollmentStage(ConsumerManagementService::ENROLLMENT_STAGE_CANCELED); |
| 354 EXPECT_FALSE(HasEnrollmentNotification()); | 367 EXPECT_FALSE(HasEnrollmentNotification()); |
| 355 | 368 |
| 356 RunEnrollmentTest(); | 369 RunEnrollmentTest(); |
| 357 | 370 |
| 358 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); | 371 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called()); |
| 359 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_NONE, GetEnrollmentState()); | 372 EXPECT_EQ(ConsumerManagementService::ENROLLMENT_STAGE_NONE, |
| 373 GetEnrollmentStage()); |
| 360 EXPECT_TRUE(HasEnrollmentNotification()); | 374 EXPECT_TRUE(HasEnrollmentNotification()); |
| 361 } | 375 } |
| 362 | 376 |
| 377 class ConsumerManagementServiceStatusTest |
| 378 : public chromeos::DeviceSettingsTestBase { |
| 379 public: |
| 380 ConsumerManagementServiceStatusTest() |
| 381 : testing_local_state_(TestingBrowserProcess::GetGlobal()), |
| 382 service_(NULL, &device_settings_service_) { |
| 383 } |
| 384 |
| 385 void SetEnrollmentStage(ConsumerManagementService::EnrollmentStage stage) { |
| 386 testing_local_state_.Get()->SetInteger( |
| 387 prefs::kConsumerManagementEnrollmentStage, stage); |
| 388 } |
| 389 |
| 390 void SetManagementMode(em::PolicyData::ManagementMode mode) { |
| 391 device_policy_.policy_data().set_management_mode(mode); |
| 392 device_policy_.Build(); |
| 393 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); |
| 394 ReloadDeviceSettings(); |
| 395 } |
| 396 |
| 397 ScopedTestingLocalState testing_local_state_; |
| 398 ConsumerManagementService service_; |
| 399 }; |
| 400 |
| 401 TEST_F(ConsumerManagementServiceStatusTest, GetStatusAndGetStatusStringWork) { |
| 402 EXPECT_EQ(ConsumerManagementService::STATUS_UNKNOWN, service_.GetStatus()); |
| 403 EXPECT_EQ("STATUS_UNKNOWN", service_.GetStatusString()); |
| 404 |
| 405 SetManagementMode(em::PolicyData::NOT_MANAGED); |
| 406 SetEnrollmentStage(ConsumerManagementService::ENROLLMENT_STAGE_NONE); |
| 407 |
| 408 EXPECT_EQ(ConsumerManagementService::STATUS_UNENROLLED, service_.GetStatus()); |
| 409 EXPECT_EQ("STATUS_UNENROLLED", service_.GetStatusString()); |
| 410 |
| 411 SetEnrollmentStage(ConsumerManagementService::ENROLLMENT_STAGE_REQUESTED); |
| 412 |
| 413 EXPECT_EQ(ConsumerManagementService::STATUS_ENROLLING, service_.GetStatus()); |
| 414 EXPECT_EQ("STATUS_ENROLLING", service_.GetStatusString()); |
| 415 |
| 416 SetManagementMode(em::PolicyData::CONSUMER_MANAGED); |
| 417 SetEnrollmentStage(ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS); |
| 418 |
| 419 EXPECT_EQ(ConsumerManagementService::STATUS_ENROLLED, service_.GetStatus()); |
| 420 EXPECT_EQ("STATUS_ENROLLED", service_.GetStatusString()); |
| 421 |
| 422 // TODO(davidyu): Test for STATUS_UNENROLLING when it is implemented. |
| 423 // http://crbug.com/353050. |
| 424 } |
| 425 |
| 363 } // namespace policy | 426 } // namespace policy |
| OLD | NEW |