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