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 // By default, TestingProfile doesn't have a UserCloudPolicyManager. For these | |
35 // tests we use a custom subclass that creates one. | |
36 class TestingProfileWithUserCloudPolicyManager : public TestingProfile { | |
Mattias Nissler (ping if slow)
2012/08/03 12:19:08
Used?
Andrew T Wilson (Slow)
2012/08/04 00:54:41
Good catch - this became obsolete when I added Tes
| |
37 TestingProfileWithUserCloudPolicyManager() | |
38 : TestingProfile() { | |
39 } | |
40 scoped_ptr<UserCloudPolicyManager> manager_; | |
41 }; | |
42 | |
43 class UserPolicySigninServiceTest : public testing::Test { | |
44 public: | |
45 UserPolicySigninServiceTest() | |
46 : loop_(MessageLoop::TYPE_UI), | |
47 ui_thread_(content::BrowserThread::UI, &loop_) { | |
48 } | |
49 | |
50 virtual void SetUp() OVERRIDE { | |
51 local_state_.reset(new TestingPrefService); | |
52 chrome::RegisterLocalState(local_state_.get()); | |
53 ((TestingBrowserProcess*) g_browser_process)->SetLocalState( | |
Mattias Nissler (ping if slow)
2012/08/03 12:19:08
Should use static_cast, but you may actually not n
Andrew T Wilson (Slow)
2012/08/04 00:54:41
Changed to use static_cast.
| |
54 local_state_.get()); | |
55 | |
56 // Create a UserCloudPolicyManager with a MockCloudPolicyStore, and build a | |
57 // TestingProfile that uses it. | |
58 mock_store_ = new MockCloudPolicyStore(); | |
59 mock_store_->NotifyStoreLoaded(); | |
60 EXPECT_CALL(*mock_store_, Load()); | |
61 scoped_ptr<UserCloudPolicyManager> manager(new UserCloudPolicyManager( | |
62 scoped_ptr<CloudPolicyStore>(mock_store_), false)); | |
63 TestingProfile::Builder builder; | |
64 builder.SetUserCloudPolicyManager(manager.Pass()); | |
65 profile_ = builder.Build().Pass(); | |
66 profile_->GetPrefs()->SetBoolean(prefs::kLoadCloudPolicyOnSignin, true); | |
67 SigninManagerFactory::GetInstance()->SetTestingFactory( | |
68 profile_.get(), FakeSigninManager::Build); | |
69 | |
70 // Make sure the UserPolicySigninService is created. | |
71 UserPolicySigninServiceFactory::GetForProfile(profile_.get()); | |
72 } | |
73 | |
74 virtual void TearDown() OVERRIDE { | |
75 // Free the profile before we clear out the browser prefs. | |
76 profile_.reset(); | |
77 static_cast<TestingBrowserProcess*>(g_browser_process)->SetLocalState(NULL); | |
78 local_state_.reset(); | |
79 } | |
80 | |
81 | |
82 scoped_ptr<TestingProfile> profile_; | |
83 // Weak pointer to a MockCloudPolicyStore - lifetime is managed by the | |
84 // UserCloudPolicyManager. | |
85 MockCloudPolicyStore* mock_store_; | |
86 | |
87 // BrowserPolicyConnector wants to initialize various components | |
88 // asynchronously via tasks, so create a fake thread here. | |
89 MessageLoop loop_; | |
90 content::TestBrowserThread ui_thread_; | |
91 | |
92 scoped_ptr<TestingPrefService> local_state_; | |
93 }; | |
94 | |
95 TEST_F(UserPolicySigninServiceTest, InitWhileSignedOut) { | |
96 // Make sure user is not signed in. | |
97 ASSERT_TRUE(SigninManagerFactory::GetForProfile(profile_.get())-> | |
98 GetAuthenticatedUsername().empty()); | |
99 | |
100 // Let the SigninService know that the profile has been created. | |
101 content::NotificationService::current()->Notify( | |
102 chrome::NOTIFICATION_PROFILE_ADDED, | |
103 content::Source<Profile>(profile_.get()), | |
104 content::NotificationService::NoDetails()); | |
105 | |
106 // UserCloudPolicyManager should not be initialized. | |
107 ASSERT_FALSE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); | |
108 } | |
109 | |
110 TEST_F(UserPolicySigninServiceTest, InitWhileSignedIn) { | |
111 // Set the user as signed in. | |
112 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | |
113 "testuser@test.com"); | |
114 | |
115 // Let the SigninService know that the profile has been created. | |
116 content::NotificationService::current()->Notify( | |
117 chrome::NOTIFICATION_PROFILE_ADDED, | |
118 content::Source<Profile>(profile_.get()), | |
119 content::NotificationService::NoDetails()); | |
120 | |
121 // UserCloudPolicyManager should be initialized. | |
122 ASSERT_TRUE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); | |
123 } | |
124 | |
125 // TODO(atwilson): Enable test for signing in once it is possible to use a | |
126 // mock TokenService (http://crbug.com/138618). | |
127 TEST_F(UserPolicySigninServiceTest, DISABLED_SignInAfterInit) { | |
128 // Let the SigninService know that the profile has been created. | |
129 content::NotificationService::current()->Notify( | |
130 chrome::NOTIFICATION_PROFILE_ADDED, | |
131 content::Source<Profile>(profile_.get()), | |
132 content::NotificationService::NoDetails()); | |
133 | |
134 // UserCloudPolicyManager should not be initialized. | |
135 ASSERT_FALSE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); | |
136 | |
137 // Now sign in the user. | |
138 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | |
139 "testuser@test.com"); | |
140 | |
141 // Make oauth token available (needs MockTokenService - see TODO above). | |
142 | |
143 // UserCloudPolicyManager should be initialized. | |
144 ASSERT_TRUE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); | |
145 } | |
146 | |
147 TEST_F(UserPolicySigninServiceTest, SignOutAfterInit) { | |
148 // Set the user as signed in. | |
149 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | |
150 "testuser@test.com"); | |
151 | |
152 // Let the SigninService know that the profile has been created. | |
153 content::NotificationService::current()->Notify( | |
154 chrome::NOTIFICATION_PROFILE_ADDED, | |
155 content::Source<Profile>(profile_.get()), | |
156 content::NotificationService::NoDetails()); | |
157 | |
158 // UserCloudPolicyManager should be initialized. | |
159 ASSERT_TRUE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); | |
160 | |
161 // Now sign out. | |
162 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | |
163 | |
164 // UserCloudPolicyManager should be shut down. | |
165 ASSERT_FALSE(profile_->GetUserCloudPolicyManager()->cloud_policy_service()); | |
166 } | |
167 | |
168 } // namespace | |
169 | |
170 } // namespace policy | |
171 | |
Mattias Nissler (ping if slow)
2012/08/03 12:19:08
remove trailing newline.
Andrew T Wilson (Slow)
2012/08/04 00:54:41
Done.
| |
OLD | NEW |