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 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceTest
, NoFinchNoService
) {
140 EXPECT_FALSE(service()->IsAllowed());
141 #if defined(GOOGLE_CHROME_BUILD)
142 EXPECT_FALSE(HasEasyUnlockApp());
146 class EasyUnlockServiceNoBluetoothTest
: public EasyUnlockServiceTest
{
148 EasyUnlockServiceNoBluetoothTest() {}
149 virtual ~EasyUnlockServiceNoBluetoothTest() {}
151 // InProcessBrowserTest:
152 virtual void SetUpInProcessBrowserTestFixture() override
{
153 set_is_bluetooth_adapter_present(false);
154 EasyUnlockServiceTest::SetUpInProcessBrowserTestFixture();
158 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceNoBluetoothTest
);
161 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceNoBluetoothTest
, NoService
) {
162 EXPECT_FALSE(service()->IsAllowed());
163 #if defined(GOOGLE_CHROME_BUILD)
164 EXPECT_FALSE(HasEasyUnlockApp());
168 class EasyUnlockServiceFinchEnabledTest
: public EasyUnlockServiceTest
{
170 EasyUnlockServiceFinchEnabledTest() {}
171 virtual ~EasyUnlockServiceFinchEnabledTest() {}
173 // InProcessBrowserTest:
174 virtual void SetUpCommandLine(CommandLine
* command_line
) override
{
175 command_line
->AppendSwitchASCII(switches::kForceFieldTrials
,
176 "EasyUnlock/Enable/");
180 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchEnabledTest
);
183 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceFinchEnabledTest
, Enabled
) {
184 EXPECT_TRUE(service()->IsAllowed());
185 #if defined(GOOGLE_CHROME_BUILD)
186 EXPECT_TRUE(HasEasyUnlockApp());
190 #if defined(GOOGLE_CHROME_BUILD)
191 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceFinchEnabledTest
, UnloadsOnSuspend
) {
192 EXPECT_TRUE(HasEasyUnlockApp());
193 power_manager_client()->SendSuspendImminent();
194 EXPECT_FALSE(HasEasyUnlockApp());
195 power_manager_client()->SendSuspendDone();
196 EXPECT_TRUE(HasEasyUnlockApp());
200 // Tests that policy can override finch to turn easy unlock off.
201 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceFinchEnabledTest
, PolicyOveride
) {
202 EXPECT_TRUE(service()->IsAllowed());
203 #if defined(GOOGLE_CHROME_BUILD)
204 EXPECT_TRUE(HasEasyUnlockApp());
207 // Overridden by policy.
208 SetEasyUnlockAllowedPolicy(false);
209 EXPECT_FALSE(service()->IsAllowed());
210 #if defined(GOOGLE_CHROME_BUILD)
211 EXPECT_FALSE(HasEasyUnlockApp());
214 SetEasyUnlockAllowedPolicy(true);
215 EXPECT_TRUE(service()->IsAllowed());
216 #if defined(GOOGLE_CHROME_BUILD)
217 EXPECT_TRUE(HasEasyUnlockApp());
221 class EasyUnlockServiceFinchDisabledTest
: public EasyUnlockServiceTest
{
223 EasyUnlockServiceFinchDisabledTest() {}
224 virtual ~EasyUnlockServiceFinchDisabledTest() {}
226 // InProcessBrowserTest:
227 virtual void SetUpCommandLine(CommandLine
* command_line
) override
{
228 command_line
->AppendSwitchASCII(switches::kForceFieldTrials
,
229 "EasyUnlock/Disable/");
233 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchDisabledTest
);
236 // Tests that easy unlock is off when finch is disabled and policy overrides
238 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceFinchDisabledTest
, PolicyOverride
) {
239 // Finch is disabled.
240 EXPECT_FALSE(service()->IsAllowed());
241 #if defined(GOOGLE_CHROME_BUILD)
242 EXPECT_FALSE(HasEasyUnlockApp());
245 // Policy overrides finch and turns on Easy unlock.
246 SetEasyUnlockAllowedPolicy(true);
247 EXPECT_TRUE(service()->IsAllowed());
248 #if defined(GOOGLE_CHROME_BUILD)
249 EXPECT_TRUE(HasEasyUnlockApp());
253 class EasyUnlockServiceMultiProfileTest
: public LoginManagerTest
{
255 EasyUnlockServiceMultiProfileTest() : LoginManagerTest(false) {}
256 virtual ~EasyUnlockServiceMultiProfileTest() {}
258 // InProcessBrowserTest:
259 virtual void SetUpInProcessBrowserTestFixture() override
{
260 LoginManagerTest::SetUpInProcessBrowserTestFixture();
262 mock_adapter_
= new testing::NiceMock
<MockBluetoothAdapter
>();
263 SetUpBluetoothMock(mock_adapter_
, true);
267 scoped_refptr
<testing::NiceMock
<MockBluetoothAdapter
> > mock_adapter_
;
268 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceMultiProfileTest
);
271 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest
,
272 PRE_DisallowedOnSecondaryProfile
) {
273 RegisterUser(kTestUser1
);
274 RegisterUser(kTestUser2
);
275 StartupUtils::MarkOobeCompleted();
278 // Hangs flakily. See http://crbug.com/421448.
279 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest
,
280 DISABLED_DisallowedOnSecondaryProfile
) {
281 LoginUser(kTestUser1
);
282 chromeos::UserAddingScreen::Get()->Start();
283 base::RunLoop().RunUntilIdle();
285 const user_manager::User
* primary_user
=
286 user_manager::UserManager::Get()->FindUser(kTestUser1
);
287 const user_manager::User
* secondary_user
=
288 user_manager::UserManager::Get()->FindUser(kTestUser2
);
290 Profile
* primary_profile
= ProfileHelper::Get()->GetProfileByUserIdHash(
291 primary_user
->username_hash());
292 Profile
* secondary_profile
= ProfileHelper::Get()->GetProfileByUserIdHash(
293 secondary_user
->username_hash());
295 EXPECT_TRUE(EasyUnlockService::Get(primary_profile
)->IsAllowed());
296 EXPECT_FALSE(EasyUnlockService::Get(secondary_profile
)->IsAllowed());
297 #if defined(GOOGLE_CHROME_BUILD)
298 EXPECT_TRUE(HasEasyUnlockAppForProfile(primary_profile
));
299 EXPECT_FALSE(HasEasyUnlockAppForProfile(secondary_profile
));