Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / easy_unlock_service_browsertest_chromeos.cc
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.
4
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"
30
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;
37 using testing::_;
38 using testing::Return;
39
40 namespace {
41
42 const char kTestUser1[] = "primary.user@example.com";
43 const char kTestUser2[] = "secondary.user@example.com";
44
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);
52 }
53 #endif
54
55 void SetUpBluetoothMock(
56     scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter,
57     bool is_present) {
58   device::BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter);
59
60   EXPECT_CALL(*mock_adapter, IsPresent())
61       .WillRepeatedly(testing::Return(is_present));
62
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()));
69 }
70
71 }  // namespace
72
73 class EasyUnlockServiceTest : public InProcessBrowserTest {
74  public:
75   EasyUnlockServiceTest() : is_bluetooth_adapter_present_(true) {}
76   virtual ~EasyUnlockServiceTest() {}
77
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),
84                NULL);
85     provider_.UpdateChromePolicy(policy);
86     base::RunLoop().RunUntilIdle();
87   }
88
89 #if defined(GOOGLE_CHROME_BUILD)
90   bool HasEasyUnlockApp() const {
91     return HasEasyUnlockAppForProfile(profile());
92   }
93 #endif
94
95   // InProcessBrowserTest:
96   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
97     EXPECT_CALL(provider_, IsInitializationComplete(_))
98         .WillRepeatedly(Return(true));
99     policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
100
101     mock_adapter_ = new testing::NiceMock<MockBluetoothAdapter>();
102     SetUpBluetoothMock(mock_adapter_, is_bluetooth_adapter_present_);
103   }
104
105   Profile* profile() const { return browser()->profile(); }
106
107   EasyUnlockService* service() const {
108     return EasyUnlockService::Get(profile());
109   }
110
111   void set_is_bluetooth_adapter_present(bool is_present) {
112     is_bluetooth_adapter_present_ = is_present;
113   }
114
115  private:
116   policy::MockConfigurationPolicyProvider provider_;
117   scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter_;
118   bool is_bluetooth_adapter_present_;
119
120   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceTest);
121 };
122
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());
128 #endif
129 }
130
131 class EasyUnlockServiceNoBluetoothTest : public EasyUnlockServiceTest {
132  public:
133   EasyUnlockServiceNoBluetoothTest() {}
134   virtual ~EasyUnlockServiceNoBluetoothTest() {}
135
136   // InProcessBrowserTest:
137   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
138     set_is_bluetooth_adapter_present(false);
139     EasyUnlockServiceTest::SetUpInProcessBrowserTestFixture();
140   }
141
142  private:
143   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceNoBluetoothTest);
144 };
145
146 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceNoBluetoothTest, NoService) {
147   EXPECT_FALSE(service()->IsAllowed());
148 #if defined(GOOGLE_CHROME_BUILD)
149   EXPECT_FALSE(HasEasyUnlockApp());
150 #endif
151 }
152
153 class EasyUnlockServiceFinchEnabledTest : public EasyUnlockServiceTest {
154  public:
155   EasyUnlockServiceFinchEnabledTest() {}
156   virtual ~EasyUnlockServiceFinchEnabledTest() {}
157
158   // InProcessBrowserTest:
159   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
160     command_line->AppendSwitchASCII(switches::kForceFieldTrials,
161                                     "EasyUnlock/Enable/");
162   }
163
164  private:
165   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchEnabledTest);
166 };
167
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());
173 #endif
174
175   // Overridden by policy.
176   SetEasyUnlockAllowedPolicy(false);
177   EXPECT_FALSE(service()->IsAllowed());
178 #if defined(GOOGLE_CHROME_BUILD)
179   EXPECT_FALSE(HasEasyUnlockApp());
180 #endif
181
182   SetEasyUnlockAllowedPolicy(true);
183   EXPECT_TRUE(service()->IsAllowed());
184 #if defined(GOOGLE_CHROME_BUILD)
185   EXPECT_TRUE(HasEasyUnlockApp());
186 #endif
187 }
188
189 class EasyUnlockServiceFinchDisabledTest : public EasyUnlockServiceTest {
190  public:
191   EasyUnlockServiceFinchDisabledTest() {}
192   virtual ~EasyUnlockServiceFinchDisabledTest() {}
193
194   // InProcessBrowserTest:
195   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
196     command_line->AppendSwitchASCII(switches::kForceFieldTrials,
197                                     "EasyUnlock/Disable/");
198   }
199
200  private:
201   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchDisabledTest);
202 };
203
204 // Tests that easy unlock is off when finch is disabled and policy overrides
205 // finch.
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());
211 #endif
212
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());
218 #endif
219 }
220
221 class EasyUnlockServiceMultiProfileTest : public LoginManagerTest {
222  public:
223   EasyUnlockServiceMultiProfileTest() : LoginManagerTest(false) {}
224   virtual ~EasyUnlockServiceMultiProfileTest() {}
225
226   // InProcessBrowserTest:
227   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
228     LoginManagerTest::SetUpInProcessBrowserTestFixture();
229
230     mock_adapter_ = new testing::NiceMock<MockBluetoothAdapter>();
231     SetUpBluetoothMock(mock_adapter_, true);
232   }
233
234  private:
235   scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter_;
236   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceMultiProfileTest);
237 };
238
239 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest,
240                        PRE_DisallowedOnSecondaryProfile) {
241   RegisterUser(kTestUser1);
242   RegisterUser(kTestUser2);
243   StartupUtils::MarkOobeCompleted();
244 }
245
246 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest,
247                        DisallowedOnSecondaryProfile) {
248   LoginUser(kTestUser1);
249   chromeos::UserAddingScreen::Get()->Start();
250   base::RunLoop().RunUntilIdle();
251   AddUser(kTestUser2);
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);
256
257   Profile* primary_profile = ProfileHelper::Get()->GetProfileByUserIdHash(
258       primary_user->username_hash());
259   Profile* secondary_profile = ProfileHelper::Get()->GetProfileByUserIdHash(
260       secondary_user->username_hash());
261
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));
267 #endif
268 }