OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/message_loop.h" |
| 6 #include "chrome/browser/policy/mock_cloud_policy_store.h" |
| 7 #include "chrome/browser/policy/user_cloud_policy_manager.h" |
| 8 #include "chrome/browser/policy/user_policy_signin_service.h" |
| 9 #include "chrome/browser/policy/user_policy_signin_service_factory.h" |
| 10 #include "chrome/browser/prefs/browser_prefs.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/signin/signin_manager.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "chrome/browser/signin/signin_manager_fake.h" |
| 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/base/testing_browser_process.h" |
| 18 #include "chrome/test/base/testing_pref_service.h" |
| 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/browser/notification_source.h" |
| 23 #include "content/public/test/test_browser_thread.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 27 using testing::_; |
| 28 using testing::Return; |
| 29 |
| 30 namespace policy { |
| 31 |
| 32 namespace { |
| 33 |
| 34 class UserPolicySigninServiceTest : public testing::Test { |
| 35 public: |
| 36 UserPolicySigninServiceTest() |
| 37 : loop_(MessageLoop::TYPE_UI), |
| 38 ui_thread_(content::BrowserThread::UI, &loop_) { |
| 39 } |
| 40 |
| 41 virtual void SetUp() OVERRIDE { |
| 42 local_state_.reset(new TestingPrefService); |
| 43 chrome::RegisterLocalState(local_state_.get()); |
| 44 static_cast<TestingBrowserProcess*>(g_browser_process)->SetLocalState( |
| 45 local_state_.get()); |
| 46 |
| 47 // Create a UserCloudPolicyManager with a MockCloudPolicyStore, and build a |
| 48 // TestingProfile that uses it. |
| 49 mock_store_ = new MockCloudPolicyStore(); |
| 50 mock_store_->NotifyStoreLoaded(); |
| 51 EXPECT_CALL(*mock_store_, Load()); |
| 52 scoped_ptr<UserCloudPolicyManager> manager(new UserCloudPolicyManager( |
| 53 scoped_ptr<CloudPolicyStore>(mock_store_), false)); |
| 54 TestingProfile::Builder builder; |
| 55 builder.SetUserCloudPolicyManager(manager.Pass()); |
| 56 profile_ = builder.Build().Pass(); |
| 57 profile_->GetPrefs()->SetBoolean(prefs::kLoadCloudPolicyOnSignin, true); |
| 58 SigninManagerFactory::GetInstance()->SetTestingFactory( |
| 59 profile_.get(), FakeSigninManager::Build); |
| 60 |
| 61 // Make sure the UserPolicySigninService is created. |
| 62 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); |
| 63 } |
| 64 |
| 65 virtual void TearDown() OVERRIDE { |
| 66 // Free the profile before we clear out the browser prefs. |
| 67 profile_.reset(); |
| 68 static_cast<TestingBrowserProcess*>(g_browser_process)->SetLocalState(NULL); |
| 69 local_state_.reset(); |
| 70 } |
| 71 |
| 72 |
| 73 scoped_ptr<TestingProfile> profile_; |
| 74 // Weak pointer to a MockCloudPolicyStore - lifetime is managed by the |
| 75 // UserCloudPolicyManager. |
| 76 MockCloudPolicyStore* mock_store_; |
| 77 |
| 78 // BrowserPolicyConnector wants to initialize various components |
| 79 // asynchronously via tasks, so create a fake thread here. |
| 80 MessageLoop loop_; |
| 81 content::TestBrowserThread ui_thread_; |
| 82 |
| 83 scoped_ptr<TestingPrefService> local_state_; |
| 84 }; |
| 85 |
| 86 TEST_F(UserPolicySigninServiceTest, InitWhileSignedOut) { |
| 87 // Make sure user is not signed in. |
| 88 ASSERT_TRUE(SigninManagerFactory::GetForProfile(profile_.get())-> |
| 89 GetAuthenticatedUsername().empty()); |
| 90 |
| 91 // Let the SigninService know that the profile has been created. |
| 92 content::NotificationService::current()->Notify( |
| 93 chrome::NOTIFICATION_PROFILE_ADDED, |
| 94 content::Source<Profile>(profile_.get()), |
| 95 content::NotificationService::NoDetails()); |
| 96 |
| 97 // UserCloudPolicyManager should not be initialized. |
| 98 ASSERT_FALSE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); |
| 99 } |
| 100 |
| 101 TEST_F(UserPolicySigninServiceTest, InitWhileSignedIn) { |
| 102 // Set the user as signed in. |
| 103 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| 104 "testuser@test.com"); |
| 105 |
| 106 // Let the SigninService know that the profile has been created. |
| 107 content::NotificationService::current()->Notify( |
| 108 chrome::NOTIFICATION_PROFILE_ADDED, |
| 109 content::Source<Profile>(profile_.get()), |
| 110 content::NotificationService::NoDetails()); |
| 111 |
| 112 // UserCloudPolicyManager should be initialized. |
| 113 ASSERT_TRUE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); |
| 114 } |
| 115 |
| 116 // TODO(atwilson): Enable test for signing in once it is possible to use a |
| 117 // mock TokenService (http://crbug.com/138618). |
| 118 TEST_F(UserPolicySigninServiceTest, DISABLED_SignInAfterInit) { |
| 119 // Let the SigninService know that the profile has been created. |
| 120 content::NotificationService::current()->Notify( |
| 121 chrome::NOTIFICATION_PROFILE_ADDED, |
| 122 content::Source<Profile>(profile_.get()), |
| 123 content::NotificationService::NoDetails()); |
| 124 |
| 125 // UserCloudPolicyManager should not be initialized. |
| 126 ASSERT_FALSE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); |
| 127 |
| 128 // Now sign in the user. |
| 129 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| 130 "testuser@test.com"); |
| 131 |
| 132 // Make oauth token available (needs MockTokenService - see TODO above). |
| 133 |
| 134 // UserCloudPolicyManager should be initialized. |
| 135 ASSERT_TRUE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); |
| 136 } |
| 137 |
| 138 TEST_F(UserPolicySigninServiceTest, SignOutAfterInit) { |
| 139 // Set the user as signed in. |
| 140 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( |
| 141 "testuser@test.com"); |
| 142 |
| 143 // Let the SigninService know that the profile has been created. |
| 144 content::NotificationService::current()->Notify( |
| 145 chrome::NOTIFICATION_PROFILE_ADDED, |
| 146 content::Source<Profile>(profile_.get()), |
| 147 content::NotificationService::NoDetails()); |
| 148 |
| 149 // UserCloudPolicyManager should be initialized. |
| 150 ASSERT_TRUE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); |
| 151 |
| 152 // Now sign out. |
| 153 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); |
| 154 |
| 155 // UserCloudPolicyManager should be shut down. |
| 156 ASSERT_FALSE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); |
| 157 } |
| 158 |
| 159 } // namespace |
| 160 |
| 161 } // namespace policy |
OLD | NEW |