Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_auth_service_browsertest.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_auth_service_browsertest.cc b/chrome/browser/chromeos/arc/arc_auth_service_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d9b2a0000c58a5d8876c9299f60f58694f07b597 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/arc_auth_service_browsertest.cc |
| @@ -0,0 +1,213 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <memory> |
| +#include <string> |
| + |
| +#include "base/command_line.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| +#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| +#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| +#include "chrome/browser/policy/profile_policy_connector.h" |
| +#include "chrome/browser/policy/profile_policy_connector_factory.h" |
| +#include "chrome/browser/policy/test/local_policy_test_server.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/fake_session_manager_client.h" |
| +#include "components/arc/arc_bridge_service_impl.h" |
| +#include "components/arc/arc_service_manager.h" |
| +#include "components/arc/test/fake_arc_bridge_bootstrap.h" |
| +#include "components/arc/test/fake_arc_bridge_instance.h" |
| +#include "components/policy/core/common/policy_switches.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/signin/core/account_id/account_id.h" |
| +#include "components/user_manager/user_manager.h" |
| +#include "url/gurl.h" |
| + |
| +namespace arc { |
| + |
| +// Set fake auth token for Chrome managed and well-known consumer accounts. |
| +const char kFakeAuthToken[] = "fake-auth-token"; |
| +// Set managed auth token for Android managed accounts. |
| +const char kManagedAuthToken[] = "managed-auth-token"; |
| +// Set unmanaged auth token for other Android unmanaged accounts. |
| +const char kUnmanagedAuthToken[] = "unmanaged-auth-token"; |
| + |
| +// Waits for the next ArcAuthService state change. |
| +class ArcAuthServiceStateObserver : public ArcAuthService::Observer { |
| + public: |
| + ArcAuthServiceStateObserver() { ArcAuthService::Get()->AddObserver(this); } |
| + |
| + // ArcAuthService::Observer: |
| + ~ArcAuthServiceStateObserver() override { |
| + run_loop_.reset(); |
| + ArcAuthService::Get()->RemoveObserver(this); |
| + } |
| + |
| + void OnOptInChanged(ArcAuthService::State state) override { |
| + if (!running_loop_) |
| + return; |
| + |
| + run_loop_->Quit(); |
| + running_loop_ = false; |
| + } |
| + |
| + void Wait() { |
| + running_loop_ = true; |
|
xiyuan
2016/04/26 17:48:10
running_loop_ seems redundant. Can we check whethe
Polina Bondarenko
2016/04/27 15:19:57
Done.
|
| + run_loop_.reset(new base::RunLoop); |
| + run_loop_->Run(); |
| + } |
| + |
| + private: |
| + bool running_loop_ = false; |
| + std::unique_ptr<base::RunLoop> run_loop_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceStateObserver); |
| +}; |
| + |
| +class ArcAuthServiceTest : public InProcessBrowserTest { |
| + protected: |
| + ArcAuthServiceTest() {} |
| + |
| + // InProcessBrowserTest: |
| + ~ArcAuthServiceTest() override {} |
| + |
| + void SetUpInProcessBrowserTestFixture() override { |
| + // Start test device management server. |
| + test_server_.reset(new policy::LocalPolicyTestServer()); |
| + ASSERT_TRUE(test_server_->Start()); |
| + |
| + // Specify device management server URL. |
| + std::string url = test_server_->GetServiceURL().spec(); |
| + base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| + command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, |
| + url); |
| + |
| + // Enable ARC. |
| + fake_session_manager_client_ = new chromeos::FakeSessionManagerClient; |
| + fake_session_manager_client_->set_arc_available(true); |
| + chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| + std::unique_ptr<chromeos::SessionManagerClient>( |
| + fake_session_manager_client_)); |
| + |
| + // Mock out ARC bridge. |
| + fake_arc_bridge_instance_.reset(new FakeArcBridgeInstance); |
| + ArcServiceManager::SetArcBridgeServiceForTesting( |
| + base::WrapUnique(new ArcBridgeServiceImpl(base::WrapUnique( |
| + new FakeArcBridgeBootstrap(fake_arc_bridge_instance_.get()))))); |
| + } |
| + |
| + void SetUpOnMainThread() override { |
| + user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler( |
| + new chromeos::FakeChromeUserManager)); |
| + } |
| + |
| + void TearDownOnMainThread() override { user_manager_enabler_.reset(); } |
| + |
| + void SetUpTest(const std::string& username, const char* auth_token) { |
| + // Init ArcAuthService for testing. |
| + ArcAuthService::DisableUIForTesting(); |
| + ArcAuthService::EnableCheckAndroidManagementForTesting(auth_token); |
| + |
| + EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + |
| + // Create test profile. |
| + TestingProfile::Builder profile_builder; |
| + profile_builder.SetPath(temp_dir_.path().AppendASCII("TestArcProfile")); |
| + profile_builder.SetProfileName(username); |
| + profile_ = profile_builder.Build(); |
| + |
| + const AccountId account_id( |
| + AccountId::FromUserEmailGaiaId(username, "1234567890")); |
| + GetFakeUserManager()->AddUser(account_id); |
| + GetFakeUserManager()->LoginUser(account_id); |
| + |
| + profile()->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); |
| + |
| + // Setup ARC for test profile. |
| + ArcServiceManager::Get()->OnPrimaryUserProfilePrepared( |
| + multi_user_util::GetAccountIdFromProfile(profile())); |
| + ArcAuthService::Get()->OnPrimaryUserProfilePrepared(profile()); |
| + } |
| + |
| + void TearDownTest() { |
| + ArcAuthService::Get()->Shutdown(); |
| + profile_.reset(); |
| + } |
| + |
| + chromeos::FakeChromeUserManager* GetFakeUserManager() const { |
| + return static_cast<chromeos::FakeChromeUserManager*>( |
| + user_manager::UserManager::Get()); |
| + } |
| + |
| + Profile* profile() { return profile_.get(); } |
| + |
| + private: |
| + std::unique_ptr<policy::LocalPolicyTestServer> test_server_; |
| + chromeos::FakeSessionManagerClient* fake_session_manager_client_; |
| + std::unique_ptr<FakeArcBridgeInstance> fake_arc_bridge_instance_; |
| + scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
| + base::ScopedTempDir temp_dir_; |
| + std::unique_ptr<TestingProfile> profile_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest); |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ConsumerAccount) { |
| + SetUpTest("test@example.com", kUnmanagedAuthToken); |
| + |
| + PrefService* pref = profile()->GetPrefs(); |
| + |
| + pref->SetBoolean(prefs::kArcEnabled, true); |
| + ArcAuthServiceStateObserver observer; |
| + observer.Wait(); |
| + ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); |
| + TearDownTest(); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, WellKnownConsumerAccount) { |
| + SetUpTest("test@gmail.com", kFakeAuthToken); |
| + |
| + PrefService* pref = profile()->GetPrefs(); |
| + |
| + pref->SetBoolean(prefs::kArcEnabled, true); |
| + ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); |
| + TearDownTest(); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedChromeAccount) { |
| + SetUpTest("test@managedchrome.com", kFakeAuthToken); |
| + policy::ProfilePolicyConnector* const connector = |
| + policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile()); |
| + connector->OverrideIsManagedForTesting(true); |
| + |
| + PrefService* pref = profile()->GetPrefs(); |
| + |
| + pref->SetBoolean(prefs::kArcEnabled, true); |
| + ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); |
| + TearDownTest(); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedAndroidAccount) { |
| + SetUpTest("test@example.com", kManagedAuthToken); |
| + |
| + PrefService* pref = profile()->GetPrefs(); |
| + |
| + pref->SetBoolean(prefs::kArcEnabled, true); |
| + ArcAuthServiceStateObserver observer; |
| + observer.Wait(); |
| + ASSERT_EQ(ArcAuthService::State::STOPPED, ArcAuthService::Get()->state()); |
| + TearDownTest(); |
| +} |
| + |
| +} // namespace arc |