| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "chrome/test/base/testing_browser_process.h" | 25 #include "chrome/test/base/testing_browser_process.h" |
| 26 #include "chrome/test/base/testing_pref_service_syncable.h" | 26 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 27 #include "chrome/test/base/testing_profile.h" | 27 #include "chrome/test/base/testing_profile.h" |
| 28 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
| 29 #include "content/public/browser/notification_details.h" | 29 #include "content/public/browser/notification_details.h" |
| 30 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| 31 #include "content/public/browser/notification_source.h" | 31 #include "content/public/browser/notification_source.h" |
| 32 #include "content/public/test/test_browser_thread_bundle.h" | 32 #include "content/public/test/test_browser_thread_bundle.h" |
| 33 #include "google_apis/gaia/gaia_constants.h" | 33 #include "google_apis/gaia/gaia_constants.h" |
| 34 #include "google_apis/gaia/google_service_auth_error.h" | 34 #include "google_apis/gaia/google_service_auth_error.h" |
| 35 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | |
| 36 #include "net/http/http_status_code.h" | 35 #include "net/http/http_status_code.h" |
| 37 #include "net/url_request/test_url_fetcher_factory.h" | 36 #include "net/url_request/test_url_fetcher_factory.h" |
| 38 #include "net/url_request/url_request_context_getter.h" | 37 #include "net/url_request/url_request_context_getter.h" |
| 39 #include "net/url_request/url_request_status.h" | 38 #include "net/url_request/url_request_status.h" |
| 40 #include "testing/gmock/include/gmock/gmock.h" | 39 #include "testing/gmock/include/gmock/gmock.h" |
| 41 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
| 42 | 41 |
| 43 #if defined(OS_ANDROID) | 42 #if defined(OS_ANDROID) |
| 44 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h" | 43 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h" |
| 45 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" | 44 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 bool HasPendingRequest() const { | 135 bool HasPendingRequest() const { |
| 137 return pending_request_; | 136 return pending_request_; |
| 138 } | 137 } |
| 139 | 138 |
| 140 private: | 139 private: |
| 141 base::WeakPtr<RequestImpl> pending_request_; | 140 base::WeakPtr<RequestImpl> pending_request_; |
| 142 }; | 141 }; |
| 143 | 142 |
| 144 #endif | 143 #endif |
| 145 | 144 |
| 146 } // namespace | |
| 147 | |
| 148 class UserPolicySigninServiceTest : public testing::Test { | 145 class UserPolicySigninServiceTest : public testing::Test { |
| 149 public: | 146 public: |
| 150 UserPolicySigninServiceTest() | 147 UserPolicySigninServiceTest() |
| 151 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 148 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 152 register_completed_(false) {} | 149 register_completed_(false) {} |
| 153 | 150 |
| 154 MOCK_METHOD1(OnPolicyRefresh, void(bool)); | 151 MOCK_METHOD1(OnPolicyRefresh, void(bool)); |
| 155 | 152 |
| 156 void OnRegisterCompleted(scoped_ptr<CloudPolicyClient> client) { | 153 void OnRegisterCompleted(scoped_ptr<CloudPolicyClient> client) { |
| 157 register_completed_ = true; | 154 register_completed_ = true; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(NULL); | 212 UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(NULL); |
| 216 // Free the profile before we clear out the browser prefs. | 213 // Free the profile before we clear out the browser prefs. |
| 217 profile_.reset(); | 214 profile_.reset(); |
| 218 TestingBrowserProcess* testing_browser_process = | 215 TestingBrowserProcess* testing_browser_process = |
| 219 TestingBrowserProcess::GetGlobal(); | 216 TestingBrowserProcess::GetGlobal(); |
| 220 testing_browser_process->SetLocalState(NULL); | 217 testing_browser_process->SetLocalState(NULL); |
| 221 local_state_.reset(); | 218 local_state_.reset(); |
| 222 testing_browser_process->SetBrowserPolicyConnector(NULL); | 219 testing_browser_process->SetBrowserPolicyConnector(NULL); |
| 223 base::RunLoop run_loop; | 220 base::RunLoop run_loop; |
| 224 run_loop.RunUntilIdle(); | 221 run_loop.RunUntilIdle(); |
| 225 ResetLastFetcherId(); | |
| 226 } | 222 } |
| 227 | 223 |
| 228 #if defined(OS_ANDROID) | 224 #if defined(OS_ANDROID) |
| 229 FakeAndroidProfileOAuth2TokenService* GetTokenService() { | 225 FakeAndroidProfileOAuth2TokenService* GetTokenService() { |
| 230 ProfileOAuth2TokenService* service = | 226 ProfileOAuth2TokenService* service = |
| 231 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | 227 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); |
| 232 return static_cast<FakeAndroidProfileOAuth2TokenService*>(service); | 228 return static_cast<FakeAndroidProfileOAuth2TokenService*>(service); |
| 233 } | 229 } |
| 234 #else | 230 #else |
| 235 FakeProfileOAuth2TokenService* GetTokenService() { | 231 FakeProfileOAuth2TokenService* GetTokenService() { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 ASSERT_TRUE(policy_data.SerializeToString( | 341 ASSERT_TRUE(policy_data.SerializeToString( |
| 346 policy_response->mutable_policy_data())); | 342 policy_response->mutable_policy_data())); |
| 347 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob); | 343 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob); |
| 348 | 344 |
| 349 // Complete the store which should cause the policy fetch callback to be | 345 // Complete the store which should cause the policy fetch callback to be |
| 350 // invoked. | 346 // invoked. |
| 351 mock_store_->NotifyStoreLoaded(); | 347 mock_store_->NotifyStoreLoaded(); |
| 352 Mock::VerifyAndClearExpectations(this); | 348 Mock::VerifyAndClearExpectations(this); |
| 353 } | 349 } |
| 354 | 350 |
| 355 void ResetLastFetcherId() { | |
| 356 OAuth2AccessTokenFetcher::ResetLastFetcherIdForTest(); | |
| 357 } | |
| 358 | |
| 359 scoped_ptr<TestingProfile> profile_; | 351 scoped_ptr<TestingProfile> profile_; |
| 360 // Weak pointer to a MockUserCloudPolicyStore - lifetime is managed by the | 352 // Weak pointer to a MockUserCloudPolicyStore - lifetime is managed by the |
| 361 // UserCloudPolicyManager. | 353 // UserCloudPolicyManager. |
| 362 MockUserCloudPolicyStore* mock_store_; | 354 MockUserCloudPolicyStore* mock_store_; |
| 363 scoped_ptr<UserCloudPolicyManager> manager_; | 355 scoped_ptr<UserCloudPolicyManager> manager_; |
| 364 | 356 |
| 365 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free | 357 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free |
| 366 // various components asynchronously via tasks, so create fake threads here. | 358 // various components asynchronously via tasks, so create fake threads here. |
| 367 content::TestBrowserThreadBundle thread_bundle_; | 359 content::TestBrowserThreadBundle thread_bundle_; |
| 368 | 360 |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 } | 779 } |
| 788 | 780 |
| 789 TEST_F(UserPolicySigninServiceTest, SignOutThenSignInAgain) { | 781 TEST_F(UserPolicySigninServiceTest, SignOutThenSignInAgain) { |
| 790 ASSERT_NO_FATAL_FAILURE(TestSuccessfulSignin()); | 782 ASSERT_NO_FATAL_FAILURE(TestSuccessfulSignin()); |
| 791 | 783 |
| 792 EXPECT_CALL(*mock_store_, Clear()); | 784 EXPECT_CALL(*mock_store_, Clear()); |
| 793 signin_manager_->ForceSignOut(); | 785 signin_manager_->ForceSignOut(); |
| 794 ASSERT_FALSE(manager_->core()->service()); | 786 ASSERT_FALSE(manager_->core()->service()); |
| 795 | 787 |
| 796 // Now sign in again. | 788 // Now sign in again. |
| 797 ResetLastFetcherId(); | |
| 798 ASSERT_NO_FATAL_FAILURE(TestSuccessfulSignin()); | 789 ASSERT_NO_FATAL_FAILURE(TestSuccessfulSignin()); |
| 799 } | 790 } |
| 800 | 791 |
| 801 TEST_F(UserPolicySigninServiceTest, PolicyFetchFailureTemporary) { | 792 TEST_F(UserPolicySigninServiceTest, PolicyFetchFailureTemporary) { |
| 802 ASSERT_NO_FATAL_FAILURE(TestSuccessfulSignin()); | 793 ASSERT_NO_FATAL_FAILURE(TestSuccessfulSignin()); |
| 803 | 794 |
| 804 ASSERT_TRUE(manager_->IsClientRegistered()); | 795 ASSERT_TRUE(manager_->IsClientRegistered()); |
| 805 | 796 |
| 806 // Kick off another policy fetch. | 797 // Kick off another policy fetch. |
| 807 MockDeviceManagementJob* fetch_request = NULL; | 798 MockDeviceManagementJob* fetch_request = NULL; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 EXPECT_CALL(*mock_store_, Clear()); | 836 EXPECT_CALL(*mock_store_, Clear()); |
| 846 fetch_request->SendResponse(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED, | 837 fetch_request->SendResponse(DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED, |
| 847 em::DeviceManagementResponse()); | 838 em::DeviceManagementResponse()); |
| 848 base::RunLoop().RunUntilIdle(); | 839 base::RunLoop().RunUntilIdle(); |
| 849 EXPECT_FALSE(manager_->IsClientRegistered()); | 840 EXPECT_FALSE(manager_->IsClientRegistered()); |
| 850 #if !defined(OS_ANDROID) | 841 #if !defined(OS_ANDROID) |
| 851 EXPECT_FALSE(signin_manager_->IsSignoutProhibited()); | 842 EXPECT_FALSE(signin_manager_->IsSignoutProhibited()); |
| 852 #endif | 843 #endif |
| 853 } | 844 } |
| 854 | 845 |
| 846 } // namespace |
| 847 |
| 855 } // namespace policy | 848 } // namespace policy |
| OLD | NEW |