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 "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/chromeos/arc/arc_auth_service.h" | |
| 15 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
| 16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
| 17 #include "chrome/browser/policy/profile_policy_connector.h" | |
| 18 #include "chrome/browser/policy/profile_policy_connector_factory.h" | |
| 19 #include "chrome/browser/policy/test/local_policy_test_server.h" | |
| 20 #include "chrome/browser/profiles/profile.h" | |
| 21 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | |
| 22 #include "chrome/common/pref_names.h" | |
| 23 #include "chrome/test/base/in_process_browser_test.h" | |
| 24 #include "chrome/test/base/testing_profile.h" | |
| 25 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 26 #include "chromeos/dbus/fake_session_manager_client.h" | |
| 27 #include "components/arc/arc_bridge_service_impl.h" | |
| 28 #include "components/arc/arc_service_manager.h" | |
| 29 #include "components/arc/test/fake_arc_bridge_bootstrap.h" | |
| 30 #include "components/arc/test/fake_arc_bridge_instance.h" | |
| 31 #include "components/policy/core/common/policy_switches.h" | |
| 32 #include "components/prefs/pref_service.h" | |
| 33 #include "components/signin/core/account_id/account_id.h" | |
| 34 #include "components/user_manager/user_manager.h" | |
| 35 #include "url/gurl.h" | |
| 36 | |
| 37 namespace arc { | |
| 38 | |
| 39 // Set fake auth token for Chrome managed and well-known consumer accounts. | |
| 40 const char kFakeAuthToken[] = "fake-auth-token"; | |
| 41 // Set managed auth token for Android managed accounts. | |
| 42 const char kManagedAuthToken[] = "managed-auth-token"; | |
| 43 // Set unmanaged auth token for other Android unmanaged accounts. | |
| 44 const char kUnmanagedAuthToken[] = "unmanaged-auth-token"; | |
| 45 | |
| 46 // Waits for the next ArcAuthService state change. | |
| 47 class ArcAuthServiceStateObserver : public ArcAuthService::Observer { | |
| 48 public: | |
| 49 ArcAuthServiceStateObserver() { ArcAuthService::Get()->AddObserver(this); } | |
| 50 | |
| 51 // ArcAuthService::Observer: | |
| 52 ~ArcAuthServiceStateObserver() override { | |
| 53 run_loop_.reset(); | |
| 54 ArcAuthService::Get()->RemoveObserver(this); | |
| 55 } | |
| 56 | |
| 57 void OnOptInChanged(ArcAuthService::State state) override { | |
| 58 if (!running_loop_) | |
| 59 return; | |
| 60 | |
| 61 run_loop_->Quit(); | |
| 62 running_loop_ = false; | |
| 63 } | |
| 64 | |
| 65 void Wait() { | |
| 66 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.
| |
| 67 run_loop_.reset(new base::RunLoop); | |
| 68 run_loop_->Run(); | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 bool running_loop_ = false; | |
| 73 std::unique_ptr<base::RunLoop> run_loop_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceStateObserver); | |
| 76 }; | |
| 77 | |
| 78 class ArcAuthServiceTest : public InProcessBrowserTest { | |
| 79 protected: | |
| 80 ArcAuthServiceTest() {} | |
| 81 | |
| 82 // InProcessBrowserTest: | |
| 83 ~ArcAuthServiceTest() override {} | |
| 84 | |
| 85 void SetUpInProcessBrowserTestFixture() override { | |
| 86 // Start test device management server. | |
| 87 test_server_.reset(new policy::LocalPolicyTestServer()); | |
| 88 ASSERT_TRUE(test_server_->Start()); | |
| 89 | |
| 90 // Specify device management server URL. | |
| 91 std::string url = test_server_->GetServiceURL().spec(); | |
| 92 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 93 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | |
| 94 url); | |
| 95 | |
| 96 // Enable ARC. | |
| 97 fake_session_manager_client_ = new chromeos::FakeSessionManagerClient; | |
| 98 fake_session_manager_client_->set_arc_available(true); | |
| 99 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( | |
| 100 std::unique_ptr<chromeos::SessionManagerClient>( | |
| 101 fake_session_manager_client_)); | |
| 102 | |
| 103 // Mock out ARC bridge. | |
| 104 fake_arc_bridge_instance_.reset(new FakeArcBridgeInstance); | |
| 105 ArcServiceManager::SetArcBridgeServiceForTesting( | |
| 106 base::WrapUnique(new ArcBridgeServiceImpl(base::WrapUnique( | |
| 107 new FakeArcBridgeBootstrap(fake_arc_bridge_instance_.get()))))); | |
| 108 } | |
| 109 | |
| 110 void SetUpOnMainThread() override { | |
| 111 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler( | |
| 112 new chromeos::FakeChromeUserManager)); | |
| 113 } | |
| 114 | |
| 115 void TearDownOnMainThread() override { user_manager_enabler_.reset(); } | |
| 116 | |
| 117 void SetUpTest(const std::string& username, const char* auth_token) { | |
| 118 // Init ArcAuthService for testing. | |
| 119 ArcAuthService::DisableUIForTesting(); | |
| 120 ArcAuthService::EnableCheckAndroidManagementForTesting(auth_token); | |
| 121 | |
| 122 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 123 | |
| 124 // Create test profile. | |
| 125 TestingProfile::Builder profile_builder; | |
| 126 profile_builder.SetPath(temp_dir_.path().AppendASCII("TestArcProfile")); | |
| 127 profile_builder.SetProfileName(username); | |
| 128 profile_ = profile_builder.Build(); | |
| 129 | |
| 130 const AccountId account_id( | |
| 131 AccountId::FromUserEmailGaiaId(username, "1234567890")); | |
| 132 GetFakeUserManager()->AddUser(account_id); | |
| 133 GetFakeUserManager()->LoginUser(account_id); | |
| 134 | |
| 135 profile()->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); | |
| 136 | |
| 137 // Setup ARC for test profile. | |
| 138 ArcServiceManager::Get()->OnPrimaryUserProfilePrepared( | |
| 139 multi_user_util::GetAccountIdFromProfile(profile())); | |
| 140 ArcAuthService::Get()->OnPrimaryUserProfilePrepared(profile()); | |
| 141 } | |
| 142 | |
| 143 void TearDownTest() { | |
| 144 ArcAuthService::Get()->Shutdown(); | |
| 145 profile_.reset(); | |
| 146 } | |
| 147 | |
| 148 chromeos::FakeChromeUserManager* GetFakeUserManager() const { | |
| 149 return static_cast<chromeos::FakeChromeUserManager*>( | |
| 150 user_manager::UserManager::Get()); | |
| 151 } | |
| 152 | |
| 153 Profile* profile() { return profile_.get(); } | |
| 154 | |
| 155 private: | |
| 156 std::unique_ptr<policy::LocalPolicyTestServer> test_server_; | |
| 157 chromeos::FakeSessionManagerClient* fake_session_manager_client_; | |
| 158 std::unique_ptr<FakeArcBridgeInstance> fake_arc_bridge_instance_; | |
| 159 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | |
| 160 base::ScopedTempDir temp_dir_; | |
| 161 std::unique_ptr<TestingProfile> profile_; | |
| 162 | |
| 163 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest); | |
| 164 }; | |
| 165 | |
| 166 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ConsumerAccount) { | |
| 167 SetUpTest("test@example.com", kUnmanagedAuthToken); | |
| 168 | |
| 169 PrefService* pref = profile()->GetPrefs(); | |
| 170 | |
| 171 pref->SetBoolean(prefs::kArcEnabled, true); | |
| 172 ArcAuthServiceStateObserver observer; | |
| 173 observer.Wait(); | |
| 174 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
| 175 TearDownTest(); | |
| 176 } | |
| 177 | |
| 178 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, WellKnownConsumerAccount) { | |
| 179 SetUpTest("test@gmail.com", kFakeAuthToken); | |
| 180 | |
| 181 PrefService* pref = profile()->GetPrefs(); | |
| 182 | |
| 183 pref->SetBoolean(prefs::kArcEnabled, true); | |
| 184 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
| 185 TearDownTest(); | |
| 186 } | |
| 187 | |
| 188 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedChromeAccount) { | |
| 189 SetUpTest("test@managedchrome.com", kFakeAuthToken); | |
| 190 policy::ProfilePolicyConnector* const connector = | |
| 191 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile()); | |
| 192 connector->OverrideIsManagedForTesting(true); | |
| 193 | |
| 194 PrefService* pref = profile()->GetPrefs(); | |
| 195 | |
| 196 pref->SetBoolean(prefs::kArcEnabled, true); | |
| 197 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
| 198 TearDownTest(); | |
| 199 } | |
| 200 | |
| 201 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedAndroidAccount) { | |
| 202 SetUpTest("test@example.com", kManagedAuthToken); | |
| 203 | |
| 204 PrefService* pref = profile()->GetPrefs(); | |
| 205 | |
| 206 pref->SetBoolean(prefs::kArcEnabled, true); | |
| 207 ArcAuthServiceStateObserver observer; | |
| 208 observer.Wait(); | |
| 209 ASSERT_EQ(ArcAuthService::State::STOPPED, ArcAuthService::Get()->state()); | |
| 210 TearDownTest(); | |
| 211 } | |
| 212 | |
| 213 } // namespace arc | |
| OLD | NEW |