1 // Copyright 2014 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.
5 #include "base/command_line.h"
6 #include "base/macros.h"
7 #include "base/run_loop.h"
8 #include "base/values.h"
9 #include "chrome/browser/chromeos/login/login_manager_test.h"
10 #include "chrome/browser/chromeos/login/startup_utils.h"
11 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
12 #include "chrome/browser/chromeos/profiles/profile_helper.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/signin/easy_unlock_service.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/extensions/extension_constants.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chromeos/dbus/dbus_thread_manager.h"
20 #include "chromeos/dbus/fake_power_manager_client.h"
21 #include "components/policy/core/browser/browser_policy_connector.h"
22 #include "components/policy/core/common/mock_configuration_policy_provider.h"
23 #include "components/policy/core/common/policy_map.h"
24 #include "components/policy/core/common/policy_types.h"
25 #include "components/user_manager/user_manager.h"
26 #include "content/public/common/content_switches.h"
27 #include "device/bluetooth/bluetooth_adapter_factory.h"
28 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
29 #include "extensions/browser/extension_system.h"
30 #include "policy/policy_constants.h"
31 #include "testing/gmock/include/gmock/gmock.h"
33 using chromeos::DBusThreadManagerSetter;
34 using chromeos::FakePowerManagerClient;
35 using chromeos::PowerManagerClient;
36 using chromeos::ProfileHelper;
37 using chromeos::LoginManagerTest;
38 using chromeos::StartupUtils;
39 using chromeos::UserAddingScreen;
40 using user_manager::UserManager;
41 using device::MockBluetoothAdapter;
43 using testing::Return;
47 const char kTestUser1[] = "primary.user@example.com";
48 const char kTestUser2[] = "secondary.user@example.com";
50 #if defined(GOOGLE_CHROME_BUILD)
51 bool HasEasyUnlockAppForProfile(Profile* profile) {
52 extensions::ExtensionSystem* extension_system =
53 extensions::ExtensionSystem::Get(profile);
54 ExtensionService* extension_service = extension_system->extension_service();
55 return !!extension_service->GetExtensionById(
56 extension_misc::kEasyUnlockAppId, false);
60 void SetUpBluetoothMock(
61 scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter,
63 device::BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter);
65 EXPECT_CALL(*mock_adapter, IsPresent())
66 .WillRepeatedly(testing::Return(is_present));
68 // These functions are called from ash system tray. They are speculations of
69 // why flaky gmock errors are seen on bots.
70 EXPECT_CALL(*mock_adapter, IsPowered())
71 .WillRepeatedly(testing::Return(true));
72 EXPECT_CALL(*mock_adapter, GetDevices()).WillRepeatedly(
73 testing::Return(device::BluetoothAdapter::ConstDeviceList()));
78 class EasyUnlockServiceTest : public InProcessBrowserTest {
80 EasyUnlockServiceTest() : is_bluetooth_adapter_present_(true) {}
81 virtual ~EasyUnlockServiceTest() {}
83 void SetEasyUnlockAllowedPolicy(bool allowed) {
84 policy::PolicyMap policy;
85 policy.Set(policy::key::kEasyUnlockAllowed,
86 policy::POLICY_LEVEL_MANDATORY,
87 policy::POLICY_SCOPE_USER,
88 new base::FundamentalValue(allowed),
90 provider_.UpdateChromePolicy(policy);
91 base::RunLoop().RunUntilIdle();
94 #if defined(GOOGLE_CHROME_BUILD)
95 bool HasEasyUnlockApp() const {
96 return HasEasyUnlockAppForProfile(profile());
100 // InProcessBrowserTest:
101 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
102 EXPECT_CALL(provider_, IsInitializationComplete(_))
103 .WillRepeatedly(Return(true));
104 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
106 mock_adapter_ = new testing::NiceMock<MockBluetoothAdapter>();
107 SetUpBluetoothMock(mock_adapter_, is_bluetooth_adapter_present_);
109 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
110 chromeos::DBusThreadManager::GetSetterForTesting();
111 power_manager_client_ = new FakePowerManagerClient;
112 dbus_setter->SetPowerManagerClient(
113 scoped_ptr<PowerManagerClient>(power_manager_client_));
116 Profile* profile() const { return browser()->profile(); }
118 EasyUnlockService* service() const {
119 return EasyUnlockService::Get(profile());
122 void set_is_bluetooth_adapter_present(bool is_present) {
123 is_bluetooth_adapter_present_ = is_present;
126 FakePowerManagerClient* power_manager_client() {
127 return power_manager_client_;
131 policy::MockConfigurationPolicyProvider provider_;
132 scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter_;
133 bool is_bluetooth_adapter_present_;
134 FakePowerManagerClient* power_manager_client_;
136 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceTest);
139 // Tests that EasyUnlock is on by default.
140 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceTest, DefaultOn) {
141 EXPECT_TRUE(service()->IsAllowed());
142 #if defined(GOOGLE_CHROME_BUILD)
143 EXPECT_TRUE(HasEasyUnlockApp());
147 #if defined(GOOGLE_CHROME_BUILD)
148 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceTest, UnloadsOnSuspend) {
149 EXPECT_TRUE(HasEasyUnlockApp());
150 power_manager_client()->SendSuspendImminent();
151 EXPECT_FALSE(HasEasyUnlockApp());
152 power_manager_client()->SendSuspendDone();
153 EXPECT_TRUE(HasEasyUnlockApp());
157 class EasyUnlockServiceNoBluetoothTest : public EasyUnlockServiceTest {
159 EasyUnlockServiceNoBluetoothTest() {}
160 virtual ~EasyUnlockServiceNoBluetoothTest() {}
162 // InProcessBrowserTest:
163 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
164 set_is_bluetooth_adapter_present(false);
165 EasyUnlockServiceTest::SetUpInProcessBrowserTestFixture();
169 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceNoBluetoothTest);
172 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceNoBluetoothTest, NoService) {
173 EXPECT_FALSE(service()->IsAllowed());
174 #if defined(GOOGLE_CHROME_BUILD)
175 EXPECT_FALSE(HasEasyUnlockApp());
179 class EasyUnlockServiceFinchEnabledTest : public EasyUnlockServiceTest {
181 EasyUnlockServiceFinchEnabledTest() {}
182 virtual ~EasyUnlockServiceFinchEnabledTest() {}
184 // InProcessBrowserTest:
185 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
186 command_line->AppendSwitchASCII(switches::kForceFieldTrials,
187 "EasyUnlock/Enable/");
191 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchEnabledTest);
194 // Tests that policy can override finch to turn easy unlock off.
195 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceFinchEnabledTest, PolicyOveride) {
196 EXPECT_TRUE(service()->IsAllowed());
197 #if defined(GOOGLE_CHROME_BUILD)
198 EXPECT_TRUE(HasEasyUnlockApp());
201 // Overridden by policy.
202 SetEasyUnlockAllowedPolicy(false);
203 EXPECT_FALSE(service()->IsAllowed());
204 #if defined(GOOGLE_CHROME_BUILD)
205 EXPECT_FALSE(HasEasyUnlockApp());
208 SetEasyUnlockAllowedPolicy(true);
209 EXPECT_TRUE(service()->IsAllowed());
210 #if defined(GOOGLE_CHROME_BUILD)
211 EXPECT_TRUE(HasEasyUnlockApp());
215 class EasyUnlockServiceFinchDisabledTest : public EasyUnlockServiceTest {
217 EasyUnlockServiceFinchDisabledTest() {}
218 virtual ~EasyUnlockServiceFinchDisabledTest() {}
220 // InProcessBrowserTest:
221 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
222 command_line->AppendSwitchASCII(switches::kForceFieldTrials,
223 "EasyUnlock/Disable/");
227 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchDisabledTest);
230 // Tests that easy unlock is off when finch is disabled and policy overrides
232 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceFinchDisabledTest, PolicyOverride) {
233 // Finch is disabled.
234 EXPECT_FALSE(service()->IsAllowed());
235 #if defined(GOOGLE_CHROME_BUILD)
236 EXPECT_FALSE(HasEasyUnlockApp());
239 // Policy overrides finch and turns on Easy unlock.
240 SetEasyUnlockAllowedPolicy(true);
241 EXPECT_TRUE(service()->IsAllowed());
242 #if defined(GOOGLE_CHROME_BUILD)
243 EXPECT_TRUE(HasEasyUnlockApp());
247 class EasyUnlockServiceMultiProfileTest : public LoginManagerTest {
249 EasyUnlockServiceMultiProfileTest() : LoginManagerTest(false) {}
250 virtual ~EasyUnlockServiceMultiProfileTest() {}
252 // InProcessBrowserTest:
253 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
254 LoginManagerTest::SetUpInProcessBrowserTestFixture();
256 mock_adapter_ = new testing::NiceMock<MockBluetoothAdapter>();
257 SetUpBluetoothMock(mock_adapter_, true);
261 scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter_;
262 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceMultiProfileTest);
265 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest,
266 PRE_DisallowedOnSecondaryProfile) {
267 RegisterUser(kTestUser1);
268 RegisterUser(kTestUser2);
269 StartupUtils::MarkOobeCompleted();
272 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest,
273 DisallowedOnSecondaryProfile) {
274 LoginUser(kTestUser1);
275 chromeos::UserAddingScreen::Get()->Start();
276 base::RunLoop().RunUntilIdle();
278 const user_manager::User* primary_user =
279 user_manager::UserManager::Get()->FindUser(kTestUser1);
280 const user_manager::User* secondary_user =
281 user_manager::UserManager::Get()->FindUser(kTestUser2);
283 Profile* primary_profile = ProfileHelper::Get()->GetProfileByUserIdHash(
284 primary_user->username_hash());
285 Profile* secondary_profile = ProfileHelper::Get()->GetProfileByUserIdHash(
286 secondary_user->username_hash());
288 EXPECT_TRUE(EasyUnlockService::Get(primary_profile)->IsAllowed());
289 EXPECT_FALSE(EasyUnlockService::Get(secondary_profile)->IsAllowed());
290 #if defined(GOOGLE_CHROME_BUILD)
291 EXPECT_TRUE(HasEasyUnlockAppForProfile(primary_profile));
292 EXPECT_FALSE(HasEasyUnlockAppForProfile(secondary_profile));