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