8da28d33e3ccbf4f1a16d5449b26f324677549a2
[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 "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"
32
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;
42 using testing::_;
43 using testing::Return;
44
45 namespace {
46
47 const char kTestUser1[] = "primary.user@example.com";
48 const char kTestUser2[] = "secondary.user@example.com";
49
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);
57 }
58 #endif
59
60 void SetUpBluetoothMock(
61     scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter,
62     bool is_present) {
63   device::BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter);
64
65   EXPECT_CALL(*mock_adapter, IsPresent())
66       .WillRepeatedly(testing::Return(is_present));
67
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()));
74 }
75
76 }  // namespace
77
78 class EasyUnlockServiceTest : public InProcessBrowserTest {
79  public:
80   EasyUnlockServiceTest() : is_bluetooth_adapter_present_(true) {}
81   virtual ~EasyUnlockServiceTest() {}
82
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),
89                NULL);
90     provider_.UpdateChromePolicy(policy);
91     base::RunLoop().RunUntilIdle();
92   }
93
94 #if defined(GOOGLE_CHROME_BUILD)
95   bool HasEasyUnlockApp() const {
96     return HasEasyUnlockAppForProfile(profile());
97   }
98 #endif
99
100   // InProcessBrowserTest:
101   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
102     EXPECT_CALL(provider_, IsInitializationComplete(_))
103         .WillRepeatedly(Return(true));
104     policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
105
106     mock_adapter_ = new testing::NiceMock<MockBluetoothAdapter>();
107     SetUpBluetoothMock(mock_adapter_, is_bluetooth_adapter_present_);
108
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_));
114   }
115
116   Profile* profile() const { return browser()->profile(); }
117
118   EasyUnlockService* service() const {
119     return EasyUnlockService::Get(profile());
120   }
121
122   void set_is_bluetooth_adapter_present(bool is_present) {
123     is_bluetooth_adapter_present_ = is_present;
124   }
125
126   FakePowerManagerClient* power_manager_client() {
127     return power_manager_client_;
128   }
129
130  private:
131   policy::MockConfigurationPolicyProvider provider_;
132   scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter_;
133   bool is_bluetooth_adapter_present_;
134   FakePowerManagerClient* power_manager_client_;
135
136   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceTest);
137 };
138
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());
144 #endif
145 }
146
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());
154 }
155 #endif
156
157 class EasyUnlockServiceNoBluetoothTest : public EasyUnlockServiceTest {
158  public:
159   EasyUnlockServiceNoBluetoothTest() {}
160   virtual ~EasyUnlockServiceNoBluetoothTest() {}
161
162   // InProcessBrowserTest:
163   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
164     set_is_bluetooth_adapter_present(false);
165     EasyUnlockServiceTest::SetUpInProcessBrowserTestFixture();
166   }
167
168  private:
169   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceNoBluetoothTest);
170 };
171
172 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceNoBluetoothTest, NoService) {
173   EXPECT_FALSE(service()->IsAllowed());
174 #if defined(GOOGLE_CHROME_BUILD)
175   EXPECT_FALSE(HasEasyUnlockApp());
176 #endif
177 }
178
179 class EasyUnlockServiceFinchEnabledTest : public EasyUnlockServiceTest {
180  public:
181   EasyUnlockServiceFinchEnabledTest() {}
182   virtual ~EasyUnlockServiceFinchEnabledTest() {}
183
184   // InProcessBrowserTest:
185   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
186     command_line->AppendSwitchASCII(switches::kForceFieldTrials,
187                                     "EasyUnlock/Enable/");
188   }
189
190  private:
191   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchEnabledTest);
192 };
193
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());
199 #endif
200
201   // Overridden by policy.
202   SetEasyUnlockAllowedPolicy(false);
203   EXPECT_FALSE(service()->IsAllowed());
204 #if defined(GOOGLE_CHROME_BUILD)
205   EXPECT_FALSE(HasEasyUnlockApp());
206 #endif
207
208   SetEasyUnlockAllowedPolicy(true);
209   EXPECT_TRUE(service()->IsAllowed());
210 #if defined(GOOGLE_CHROME_BUILD)
211   EXPECT_TRUE(HasEasyUnlockApp());
212 #endif
213 }
214
215 class EasyUnlockServiceFinchDisabledTest : public EasyUnlockServiceTest {
216  public:
217   EasyUnlockServiceFinchDisabledTest() {}
218   virtual ~EasyUnlockServiceFinchDisabledTest() {}
219
220   // InProcessBrowserTest:
221   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
222     command_line->AppendSwitchASCII(switches::kForceFieldTrials,
223                                     "EasyUnlock/Disable/");
224   }
225
226  private:
227   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceFinchDisabledTest);
228 };
229
230 // Tests that easy unlock is off when finch is disabled and policy overrides
231 // finch.
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());
237 #endif
238
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());
244 #endif
245 }
246
247 class EasyUnlockServiceMultiProfileTest : public LoginManagerTest {
248  public:
249   EasyUnlockServiceMultiProfileTest() : LoginManagerTest(false) {}
250   virtual ~EasyUnlockServiceMultiProfileTest() {}
251
252   // InProcessBrowserTest:
253   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
254     LoginManagerTest::SetUpInProcessBrowserTestFixture();
255
256     mock_adapter_ = new testing::NiceMock<MockBluetoothAdapter>();
257     SetUpBluetoothMock(mock_adapter_, true);
258   }
259
260  private:
261   scoped_refptr<testing::NiceMock<MockBluetoothAdapter> > mock_adapter_;
262   DISALLOW_COPY_AND_ASSIGN(EasyUnlockServiceMultiProfileTest);
263 };
264
265 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest,
266                        PRE_DisallowedOnSecondaryProfile) {
267   RegisterUser(kTestUser1);
268   RegisterUser(kTestUser2);
269   StartupUtils::MarkOobeCompleted();
270 }
271
272 IN_PROC_BROWSER_TEST_F(EasyUnlockServiceMultiProfileTest,
273                        DisallowedOnSecondaryProfile) {
274   LoginUser(kTestUser1);
275   chromeos::UserAddingScreen::Get()->Start();
276   base::RunLoop().RunUntilIdle();
277   AddUser(kTestUser2);
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);
282
283   Profile* primary_profile = ProfileHelper::Get()->GetProfileByUserIdHash(
284       primary_user->username_hash());
285   Profile* secondary_profile = ProfileHelper::Get()->GetProfileByUserIdHash(
286       secondary_user->username_hash());
287
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));
293 #endif
294 }