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 "components/policy/core/browser/browser_policy_connector.h"
20 #include "components/policy/core/common/mock_configuration_policy_provider.h"
21 #include "components/policy/core/common/policy_map.h"
22 #include "components/policy/core/common/policy_types.h"
23 #include "components/user_manager/user_manager.h"
24 #include "content/public/common/content_switches.h"
25 #include "device/bluetooth/bluetooth_adapter_factory.h"
26 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
27 #include "extensions/browser/extension_system.h"
28 #include "policy/policy_constants.h"
29 #include "testing/gmock/include/gmock/gmock.h"
31 using chromeos::ProfileHelper;
32 using chromeos::LoginManagerTest;
33 using chromeos::StartupUtils;
34 using chromeos::UserAddingScreen;
35 using user_manager::UserManager;
36 using device::MockBluetoothAdapter;
38 using testing::Return;
42 const char kTestUser1[] = "primary.user@example.com";
43 const char kTestUser2[] = "secondary.user@example.com";
45 #if defined(GOOGLE_CHROME_BUILD)
46 bool HasEasyUnlockAppForProfile(Profile* profile) {
47 extensions::ExtensionSystem* extension_system =
48 extensions::ExtensionSystem::Get(profile);
49 ExtensionService* extension_service = extension_system->extension_service();
50 return !!extension_service->GetExtensionById(
51 extension_misc::kEasyUnlockAppId, false);
55 void SetUpBluetoothMock(
56 scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter,
58 device::BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter);
60 EXPECT_CALL(*mock_adapter, IsPresent())
61 .WillRepeatedly(testing::Return(is_present));
63 // These functions are called from ash system tray. They are speculations of
64 // why flaky gmock errors are seen on bots.
65 EXPECT_CALL(*mock_adapter, IsPowered())
66 .WillRepeatedly(testing::Return(true));
67 EXPECT_CALL(*mock_adapter, GetDevices()).WillRepeatedly(
68 testing::Return(device::BluetoothAdapter::ConstDeviceList()));
73 class EasyUnlockServiceTest : public InProcessBrowserTest {
75 EasyUnlockServiceTest() : is_bluetooth_adapter_present_(true) {}
76 virtual ~EasyUnlockServiceTest() {}
78 void SetEasyUnlockAllowedPolicy(bool allowed) {
79 policy::PolicyMap policy;
80 policy.Set(policy::key::kEasyUnlockAllowed,
81 policy::POLICY_LEVEL_MANDATORY,
82 policy::POLICY_SCOPE_USER,
83 new base::FundamentalValue(allowed),
85 provider_.UpdateChromePolicy(policy);
86 base::RunLoop().RunUntilIdle();
89 #if defined(GOOGLE_CHROME_BUILD)
90 bool HasEasyUnlockApp() const {
91 return HasEasyUnlockAppForProfile(profile());
95 // InProcessBrowserTest:
96 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
97 EXPECT_CALL(provider_, IsInitializationComplete(_))
98 .WillRepeatedly(Return(true));
99 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
101 mock_adapter_ = new testing::NiceMock<MockBluetoothAdapter>();
102 SetUpBluetoothMock(mock_adapter_, is_bluetooth_adapter_present_);
105 Profile* profile() const { return browser()->profile(); }
107 EasyUnlockService* service() const {
108 return EasyUnlockService::Get(profile());
111 void set_is_bluetooth_adapter_present(bool is_present) {
112 is_bluetooth_adapter_present_ = is_present;
116 policy::MockConfigurationPolicyProvider provider_;
117 scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter_;
118 bool is_bluetooth_adapter_present_;
120 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceTest);
123 // Tests that EasyUnlock is on by default.
124 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceTest, DefaultOn) {
125 EXPECT_TRUE(service()->IsAllowed());
126 #if defined(GOOGLE_CHROME_BUILD)
127 EXPECT_TRUE(HasEasyUnlockApp());
131 class EasyUnlockServiceNoBluetoothTest : public EasyUnlockServiceTest {
133 EasyUnlockServiceNoBluetoothTest() {}
134 virtual ~EasyUnlockServiceNoBluetoothTest() {}
136 // InProcessBrowserTest:
137 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
138 set_is_bluetooth_adapter_present(false);
139 EasyUnlockServiceTest::SetUpInProcessBrowserTestFixture();
143 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceNoBluetoothTest);
146 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceNoBluetoothTest, NoService) {
147 EXPECT_FALSE(service()->IsAllowed());
148 #if defined(GOOGLE_CHROME_BUILD)
149 EXPECT_FALSE(HasEasyUnlockApp());
153 class EasyUnlockServiceFinchEnabledTest : public EasyUnlockServiceTest {
155 EasyUnlockServiceFinchEnabledTest() {}
156 virtual ~EasyUnlockServiceFinchEnabledTest() {}
158 // InProcessBrowserTest:
159 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
160 command_line->AppendSwitchASCII(switches::kForceFieldTrials,
161 "EasyUnlock/Enable/");
165 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchEnabledTest);
168 // Tests that policy can override finch to turn easy unlock off.
169 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceFinchEnabledTest, PolicyOveride) {
170 EXPECT_TRUE(service()->IsAllowed());
171 #if defined(GOOGLE_CHROME_BUILD)
172 EXPECT_TRUE(HasEasyUnlockApp());
175 // Overridden by policy.
176 SetEasyUnlockAllowedPolicy(false);
177 EXPECT_FALSE(service()->IsAllowed());
178 #if defined(GOOGLE_CHROME_BUILD)
179 EXPECT_FALSE(HasEasyUnlockApp());
182 SetEasyUnlockAllowedPolicy(true);
183 EXPECT_TRUE(service()->IsAllowed());
184 #if defined(GOOGLE_CHROME_BUILD)
185 EXPECT_TRUE(HasEasyUnlockApp());
189 class EasyUnlockServiceFinchDisabledTest : public EasyUnlockServiceTest {
191 EasyUnlockServiceFinchDisabledTest() {}
192 virtual ~EasyUnlockServiceFinchDisabledTest() {}
194 // InProcessBrowserTest:
195 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
196 command_line->AppendSwitchASCII(switches::kForceFieldTrials,
197 "EasyUnlock/Disable/");
201 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchDisabledTest);
204 // Tests that easy unlock is off when finch is disabled and policy overrides
206 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceFinchDisabledTest, PolicyOverride) {
207 // Finch is disabled.
208 EXPECT_FALSE(service()->IsAllowed());
209 #if defined(GOOGLE_CHROME_BUILD)
210 EXPECT_FALSE(HasEasyUnlockApp());
213 // Policy overrides finch and turns on Easy unlock.
214 SetEasyUnlockAllowedPolicy(true);
215 EXPECT_TRUE(service()->IsAllowed());
216 #if defined(GOOGLE_CHROME_BUILD)
217 EXPECT_TRUE(HasEasyUnlockApp());
221 class EasyUnlockServiceMultiProfileTest : public LoginManagerTest {
223 EasyUnlockServiceMultiProfileTest() : LoginManagerTest(false) {}
224 virtual ~EasyUnlockServiceMultiProfileTest() {}
226 // InProcessBrowserTest:
227 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
228 LoginManagerTest::SetUpInProcessBrowserTestFixture();
230 mock_adapter_ = new testing::NiceMock<MockBluetoothAdapter>();
231 SetUpBluetoothMock(mock_adapter_, true);
235 scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter_;
236 DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceMultiProfileTest);
239 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest,
240 PRE_DisallowedOnSecondaryProfile) {
241 RegisterUser(kTestUser1);
242 RegisterUser(kTestUser2);
243 StartupUtils::MarkOobeCompleted();
246 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest,
247 DisallowedOnSecondaryProfile) {
248 LoginUser(kTestUser1);
249 chromeos::UserAddingScreen::Get()->Start();
250 base::RunLoop().RunUntilIdle();
252 const user_manager::User* primary_user =
253 user_manager::UserManager::Get()->FindUser(kTestUser1);
254 const user_manager::User* secondary_user =
255 user_manager::UserManager::Get()->FindUser(kTestUser2);
257 Profile* primary_profile = ProfileHelper::Get()->GetProfileByUserIdHash(
258 primary_user->username_hash());
259 Profile* secondary_profile = ProfileHelper::Get()->GetProfileByUserIdHash(
260 secondary_user->username_hash());
262 EXPECT_TRUE(EasyUnlockService::Get(primary_profile)->IsAllowed());
263 EXPECT_FALSE(EasyUnlockService::Get(secondary_profile)->IsAllowed());
264 #if defined(GOOGLE_CHROME_BUILD)
265 EXPECT_TRUE(HasEasyUnlockAppForProfile(primary_profile));
266 EXPECT_FALSE(HasEasyUnlockAppForProfile(secondary_profile));