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..efc2440ef075162c4e3130deefb4122c9dad5fd7 |
--- /dev/null |
+++ b/chrome/browser/chromeos/arc/arc_auth_service_browsertest.cc |
@@ -0,0 +1,188 @@ |
+// 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 <string> |
+ |
+#include "base/command_line.h" |
+#include "base/files/scoped_temp_dir.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/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 "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 state_ of ArcAuthService. |
+class ArcAuthServiceStateObserver : public ArcAuthService::Observer { |
+ public: |
+ explicit ArcAuthServiceStateObserver(ArcAuthService::State state) |
+ : running_loop_(false), state_(state) { |
+ ArcAuthService::Get()->AddObserver(this); |
+ } |
+ |
+ void OnOptInChanged(ArcAuthService::State state) override { |
xiyuan
2016/04/20 18:21:42
nit: // ArcAuthService::Observer:
Polina Bondarenko
2016/04/25 19:59:50
Done.
|
+ if (!running_loop_) |
+ return; |
+ |
+ if (state == state_) { |
+ run_loop_->Quit(); |
+ running_loop_ = false; |
+ ArcAuthService::Get()->RemoveObserver(this); |
xiyuan
2016/04/20 18:21:42
nit: RemoveObserver in dtor.
Polina Bondarenko
2016/04/25 19:59:50
Done.
|
+ } |
+ } |
+ |
+ void Wait() { |
xiyuan
2016/04/20 18:21:41
To be more correct, we should check the current st
Polina Bondarenko
2016/04/25 19:59:50
Changed the observer and it waits only for the nex
|
+ running_loop_ = true; |
+ run_loop_.reset(new base::RunLoop); |
+ run_loop_->Run(); |
+ } |
+ |
+ private: |
+ bool running_loop_; |
xiyuan
2016/04/20 18:21:41
nit: Prefer to use the following to init member:
Polina Bondarenko
2016/04/25 19:59:49
Done.
|
+ ArcAuthService::State state_; |
xiyuan
2016/04/20 18:21:41
nit: const ArcAuthService::State state_;
Polina Bondarenko
2016/04/25 19:59:49
Removed state_ at all.
|
+ std::unique_ptr<base::RunLoop> run_loop_; |
xiyuan
2016/04/20 18:21:41
nit: #include <memory>
Polina Bondarenko
2016/04/25 19:59:49
Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceStateObserver); |
xiyuan
2016/04/20 18:21:42
nit: #include "base/macros.h"
Polina Bondarenko
2016/04/25 19:59:50
Done.
|
+}; |
+ |
+class ArcAuthServiceTest : public InProcessBrowserTest { |
+ protected: |
+ ArcAuthServiceTest() {} |
+ ~ArcAuthServiceTest() override {} |
+ |
+ void SetUpInProcessBrowserTestFixture() override { |
xiyuan
2016/04/20 18:21:41
nit: // InProcessBrowserTest:
Polina Bondarenko
2016/04/25 19:59:49
Done.
|
+ // 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 SetUpTest(const std::string& username, const std::string& auth_token) { |
+ // Init ArcAuthService for testing. |
+ ArcAuthService::DisableUIForTesting(); |
+ ArcAuthService::EnableCheckAndroidManagementForTesting(); |
+ ArcAuthService::Get()->set_auth_token_for_testing(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(); |
+ 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() { |
xiyuan
2016/04/20 18:21:41
Can we override TearDown and move the boy there so
Polina Bondarenko
2016/04/25 19:59:49
No, ArcAuthService should be stopped for each test
|
+ ArcAuthService::Get()->Shutdown(); |
+ profile_.reset(); |
+ } |
+ |
+ 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_; |
+ base::ScopedTempDir temp_dir_; |
+ std::unique_ptr<TestingProfile> profile_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest); |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ConsumerAccountTest) { |
xiyuan
2016/04/20 18:21:41
nit: ConsumerAccountTest -> ConsumerAccount, Test
Polina Bondarenko
2016/04/25 19:59:49
Done.
|
+ SetUpTest("test@example", kUnmanagedAuthToken); |
xiyuan
2016/04/20 18:21:41
Should the username be "test@example.com"?
Polina Bondarenko
2016/04/25 19:59:49
Done.
|
+ |
+ PrefService* pref = profile()->GetPrefs(); |
+ |
+ pref->SetBoolean(prefs::kArcEnabled, true); |
+ ArcAuthServiceStateObserver observer(ArcAuthService::State::ACTIVE); |
+ observer.Wait(); |
+ ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); |
+ TearDownTest(); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, WellKnownConsumerAccountTest) { |
+ 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, ManagedChromeAccountTest) { |
+ 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, ManagedAndroidAccountTest) { |
+ SetUpTest("test@example.com", kManagedAuthToken); |
+ |
+ PrefService* pref = profile()->GetPrefs(); |
+ |
+ pref->SetBoolean(prefs::kArcEnabled, true); |
+ ArcAuthServiceStateObserver observer(ArcAuthService::State::STOPPED); |
+ observer.Wait(); |
+ ASSERT_EQ(ArcAuthService::State::STOPPED, ArcAuthService::Get()->state()); |
+ TearDownTest(); |
+} |
+ |
+} // namespace arc |