OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 <memory> | |
6 #include <string> | |
7 | |
8 #include "base/command_line.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/files/scoped_temp_dir.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/time/time.h" | |
15 #include "chrome/browser/chromeos/arc/arc_auth_service.h" | |
16 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
17 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
18 #include "chrome/browser/policy/profile_policy_connector.h" | |
19 #include "chrome/browser/policy/profile_policy_connector_factory.h" | |
20 #include "chrome/browser/policy/test/local_policy_test_server.h" | |
21 #include "chrome/browser/profiles/profile.h" | |
22 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | |
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
24 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | |
25 #include "chrome/common/pref_names.h" | |
26 #include "chrome/test/base/in_process_browser_test.h" | |
27 #include "chrome/test/base/testing_profile.h" | |
28 #include "chromeos/dbus/dbus_thread_manager.h" | |
29 #include "chromeos/dbus/fake_session_manager_client.h" | |
30 #include "chromeos/dbus/session_manager_client.h" | |
31 #include "components/arc/arc_bridge_service_impl.h" | |
32 #include "components/arc/arc_service_manager.h" | |
33 #include "components/arc/test/fake_arc_bridge_bootstrap.h" | |
34 #include "components/arc/test/fake_arc_bridge_instance.h" | |
35 #include "components/policy/core/common/policy_switches.h" | |
36 #include "components/prefs/pref_service.h" | |
37 #include "components/signin/core/account_id/account_id.h" | |
38 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
39 #include "components/user_manager/user_manager.h" | |
40 #include "testing/gtest/include/gtest/gtest.h" | |
41 #include "url/gurl.h" | |
42 | |
43 namespace { | |
44 | |
45 const char kRefreshToken[] = "fake-refresh-token"; | |
46 // Set managed auth token for Android managed accounts. | |
47 const char kManagedAuthToken[] = "managed-auth-token"; | |
48 // Set unmanaged auth token for other Android unmanaged accounts. | |
49 const char kUnmanagedAuthToken[] = "unmanaged-auth-token"; | |
50 const char kWellKnownConsumerName[] = "test@gmail.com"; | |
51 const char kFakeUserName[] = "test@example.com"; | |
52 | |
53 } // namespace | |
54 | |
55 namespace arc { | |
56 | |
57 // Base ArcAuthService observer. | |
58 class ArcAuthServiceObserver : public ArcAuthService::Observer { | |
59 public: | |
60 // ArcAuthService::Observer: | |
61 ~ArcAuthServiceObserver() override { | |
62 ArcAuthService::Get()->RemoveObserver(this); | |
63 } | |
64 | |
65 void Wait() { | |
66 run_loop_.reset(new base::RunLoop); | |
67 run_loop_->Run(); | |
68 run_loop_.reset(); | |
69 } | |
70 | |
71 protected: | |
72 ArcAuthServiceObserver() { ArcAuthService::Get()->AddObserver(this); } | |
73 | |
74 std::unique_ptr<base::RunLoop> run_loop_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceObserver); | |
77 }; | |
78 | |
79 // Observer of ArcAuthService state change. | |
80 class ArcAuthServiceStateObserver : public ArcAuthServiceObserver { | |
81 public: | |
82 ArcAuthServiceStateObserver() : ArcAuthServiceObserver() {} | |
83 | |
84 // ArcAuthService::Observer: | |
85 void OnOptInChanged(ArcAuthService::State state) override { | |
86 if (!run_loop_) | |
87 return; | |
88 run_loop_->Quit(); | |
89 } | |
90 | |
91 protected: | |
92 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceStateObserver); | |
93 }; | |
94 | |
95 // Observer of ARC bridge shutdown. | |
96 class ArcAuthServiceShutdownObserver : public ArcAuthServiceObserver { | |
97 public: | |
98 ArcAuthServiceShutdownObserver() : ArcAuthServiceObserver() {} | |
99 | |
100 // ArcAuthService::Observer: | |
101 void OnShutdownBridge() override { | |
102 if (!run_loop_) | |
103 return; | |
104 run_loop_->Quit(); | |
105 } | |
106 | |
107 protected: | |
108 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceShutdownObserver); | |
109 }; | |
110 | |
111 class ArcAuthServiceTest : public InProcessBrowserTest { | |
112 protected: | |
113 ArcAuthServiceTest() {} | |
114 | |
115 // InProcessBrowserTest: | |
116 ~ArcAuthServiceTest() override {} | |
117 | |
118 void SetUpInProcessBrowserTestFixture() override { | |
119 // Start test device management server. | |
120 test_server_.reset(new policy::LocalPolicyTestServer()); | |
121 ASSERT_TRUE(test_server_->Start()); | |
122 | |
123 // Specify device management server URL. | |
124 std::string url = test_server_->GetServiceURL().spec(); | |
125 base::CommandLine* const command_line = | |
126 base::CommandLine::ForCurrentProcess(); | |
127 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | |
128 url); | |
129 | |
130 // Enable ARC. | |
131 chromeos::FakeSessionManagerClient* fake_session_manager_client( | |
bartfab (slow)
2016/05/03 14:25:18
Nit 1: That is an unusual way to initialize a poin
Polina Bondarenko
2016/05/03 14:55:45
Done.
| |
132 new chromeos::FakeSessionManagerClient); | |
133 fake_session_manager_client->set_arc_available(true); | |
134 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( | |
135 std::unique_ptr<chromeos::SessionManagerClient>( | |
136 fake_session_manager_client)); | |
137 | |
138 // Mock out ARC bridge. | |
139 fake_arc_bridge_instance_.reset(new FakeArcBridgeInstance); | |
140 ArcServiceManager::SetArcBridgeServiceForTesting( | |
141 base::WrapUnique(new ArcBridgeServiceImpl(base::WrapUnique( | |
142 new FakeArcBridgeBootstrap(fake_arc_bridge_instance_.get()))))); | |
143 } | |
144 | |
145 void SetUpOnMainThread() override { | |
146 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler( | |
147 new chromeos::FakeChromeUserManager)); | |
148 // Init ArcAuthService for testing. | |
149 ArcAuthService::DisableUIForTesting(); | |
150 ArcAuthService::EnableCheckAndroidManagementForTesting(); | |
151 | |
152 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
153 | |
154 // Create test profile. | |
155 TestingProfile::Builder profile_builder; | |
156 profile_builder.SetPath(temp_dir_.path().AppendASCII("TestArcProfile")); | |
157 profile_builder.SetProfileName(kFakeUserName); | |
158 profile_builder.AddTestingFactory( | |
159 ProfileOAuth2TokenServiceFactory::GetInstance(), | |
160 BuildFakeProfileOAuth2TokenService); | |
161 profile_ = profile_builder.Build(); | |
162 token_service_ = static_cast<FakeProfileOAuth2TokenService*>( | |
163 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())); | |
164 token_service_->UpdateCredentials("", kRefreshToken); | |
165 | |
166 profile()->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); | |
167 | |
168 const AccountId account_id( | |
169 AccountId::FromUserEmailGaiaId(kFakeUserName, "1234567890")); | |
170 GetFakeUserManager()->AddUser(account_id); | |
171 GetFakeUserManager()->LoginUser(account_id); | |
172 | |
173 // Set up ARC for test profile. | |
174 ArcServiceManager::Get()->OnPrimaryUserProfilePrepared( | |
175 multi_user_util::GetAccountIdFromProfile(profile())); | |
176 ArcAuthService::Get()->OnPrimaryUserProfilePrepared(profile()); | |
177 } | |
178 | |
179 void TearDownOnMainThread() override { | |
180 ArcAuthService::Get()->Shutdown(); | |
181 profile_.reset(); | |
182 user_manager_enabler_.reset(); | |
183 } | |
184 | |
185 chromeos::FakeChromeUserManager* GetFakeUserManager() const { | |
186 return static_cast<chromeos::FakeChromeUserManager*>( | |
187 user_manager::UserManager::Get()); | |
188 } | |
189 | |
190 void set_profile_name(const std::string& username) { | |
191 profile_->set_profile_name(username); | |
192 } | |
193 | |
194 Profile* profile() { return profile_.get(); } | |
195 | |
196 FakeProfileOAuth2TokenService* token_service() { return token_service_; } | |
197 | |
198 private: | |
199 std::unique_ptr<policy::LocalPolicyTestServer> test_server_; | |
200 std::unique_ptr<FakeArcBridgeInstance> fake_arc_bridge_instance_; | |
201 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | |
202 base::ScopedTempDir temp_dir_; | |
203 std::unique_ptr<TestingProfile> profile_; | |
204 FakeProfileOAuth2TokenService* token_service_; | |
205 | |
206 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest); | |
207 }; | |
208 | |
209 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ConsumerAccount) { | |
210 PrefService* const prefs = profile()->GetPrefs(); | |
211 prefs->SetBoolean(prefs::kArcEnabled, true); | |
212 token_service()->IssueTokenForAllPendingRequests(kUnmanagedAuthToken, | |
213 base::Time::Max()); | |
214 ArcAuthServiceStateObserver observer; | |
215 observer.Wait(); | |
216 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
217 } | |
218 | |
219 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, WellKnownConsumerAccount) { | |
220 set_profile_name(kWellKnownConsumerName); | |
221 PrefService* const prefs = profile()->GetPrefs(); | |
222 | |
223 prefs->SetBoolean(prefs::kArcEnabled, true); | |
224 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
225 } | |
226 | |
227 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedChromeAccount) { | |
228 policy::ProfilePolicyConnector* const connector = | |
229 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile()); | |
230 connector->OverrideIsManagedForTesting(true); | |
231 | |
232 PrefService* const pref = profile()->GetPrefs(); | |
233 | |
234 pref->SetBoolean(prefs::kArcEnabled, true); | |
235 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
236 } | |
237 | |
238 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedAndroidAccount) { | |
239 PrefService* const prefs = profile()->GetPrefs(); | |
240 | |
241 prefs->SetBoolean(prefs::kArcEnabled, true); | |
242 token_service()->IssueTokenForAllPendingRequests(kManagedAuthToken, | |
243 base::Time::Max()); | |
244 ArcAuthServiceShutdownObserver observer; | |
245 observer.Wait(); | |
246 ASSERT_EQ(ArcAuthService::State::STOPPED, ArcAuthService::Get()->state()); | |
247 } | |
248 | |
249 } // namespace arc | |
OLD | NEW |