1 // Copyright 2015 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 <memory>
6 
7 #include "base/memory/ptr_util.h"
8 #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
9 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
10 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
11 #include "chrome/browser/chromeos/ownership/fake_owner_settings_service.h"
12 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "components/account_id/account_id.h"
15 #include "components/user_manager/scoped_user_manager.h"
16 #include "content/public/test/browser_test.h"
17 #include "extensions/common/switches.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 
20 namespace {
21 const char kTestingAppId[] = "pjdjhejcdkeebjehnokfbfnjmgmgdjlc";
22 }  // namespace
23 
24 namespace extensions {
25 namespace {
26 
27 // This class contains chrome.bluetoothLowEnergy API tests.
28 class BluetoothLowEnergyApiTestChromeOs : public PlatformAppBrowserTest {
29  public:
BluetoothLowEnergyApiTestChromeOs()30   BluetoothLowEnergyApiTestChromeOs()
31       : fake_user_manager_(nullptr), settings_helper_(false) {}
~BluetoothLowEnergyApiTestChromeOs()32   ~BluetoothLowEnergyApiTestChromeOs() override {}
33 
SetUpOnMainThread()34   void SetUpOnMainThread() override {
35     PlatformAppBrowserTest::SetUpOnMainThread();
36     settings_helper_.ReplaceDeviceSettingsProviderWithStub();
37     owner_settings_service_ =
38         settings_helper_.CreateOwnerSettingsService(browser()->profile());
39   }
40 
TearDownOnMainThread()41   void TearDownOnMainThread() override {
42     owner_settings_service_.reset();
43     settings_helper_.RestoreRealDeviceSettingsProvider();
44     PlatformAppBrowserTest::TearDownOnMainThread();
45     user_manager_enabler_.reset();
46     fake_user_manager_ = nullptr;
47   }
48 
49  protected:
EnterKioskSession()50   void EnterKioskSession() {
51     fake_user_manager_ = new chromeos::FakeChromeUserManager();
52     user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
53         base::WrapUnique(fake_user_manager_));
54 
55     const AccountId kiosk_account_id(
56         AccountId::FromUserEmail("kiosk@foobar.com"));
57     fake_user_manager_->AddKioskAppUser(kiosk_account_id);
58     fake_user_manager_->LoginUser(kiosk_account_id);
59   }
60 
SetAutoLaunchApp()61   void SetAutoLaunchApp() {
62     manager()->AddApp(kTestingAppId, owner_settings_service_.get());
63     manager()->SetAutoLaunchApp(kTestingAppId, owner_settings_service_.get());
64     manager()->SetAppWasAutoLaunchedWithZeroDelay(kTestingAppId);
65   }
66 
manager() const67   chromeos::KioskAppManager* manager() const {
68     return chromeos::KioskAppManager::Get();
69   }
70 
71   chromeos::FakeChromeUserManager* fake_user_manager_;
72   std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_;
73 
74   chromeos::ScopedCrosSettingsTestHelper settings_helper_;
75   std::unique_ptr<chromeos::FakeOwnerSettingsService> owner_settings_service_;
76 };
77 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,RegisterAdvertisement_Flag)78 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
79                        RegisterAdvertisement_Flag) {
80   base::CommandLine::ForCurrentProcess()->AppendSwitch(
81       switches::kEnableBLEAdvertising);
82   ASSERT_TRUE(RunPlatformAppTest(
83       "api_test/bluetooth_low_energy/register_advertisement_flag"))
84       << message_;
85 }
86 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,RegisterAdvertisement_NotKioskSession)87 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
88                        RegisterAdvertisement_NotKioskSession) {
89   ASSERT_TRUE(RunPlatformAppTest(
90       "api_test/bluetooth_low_energy/register_advertisement_no_kiosk_mode"))
91       << message_;
92 }
93 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,RegisterAdvertisement_KioskSessionOnly)94 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
95                        RegisterAdvertisement_KioskSessionOnly) {
96   EnterKioskSession();
97   ASSERT_TRUE(
98       RunPlatformAppTest("api_test/bluetooth_low_energy/"
99                          "register_advertisement_kiosk_session_only"))
100       << message_;
101 }
102 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,RegisterAdvertisement)103 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
104                        RegisterAdvertisement) {
105   EnterKioskSession();
106   SetAutoLaunchApp();
107   ASSERT_TRUE(RunPlatformAppTest(
108       "api_test/bluetooth_low_energy/register_advertisement"))
109       << message_;
110 }
111 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,ResetAdvertising)112 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs, ResetAdvertising) {
113   EnterKioskSession();
114   SetAutoLaunchApp();
115   ASSERT_TRUE(RunPlatformAppTest(
116       "api_test/bluetooth_low_energy/reset_all_advertisements"))
117       << message_;
118 }
119 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,SetAdvertisingInterval)120 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
121                        SetAdvertisingInterval) {
122   EnterKioskSession();
123   SetAutoLaunchApp();
124   ASSERT_TRUE(
125       RunPlatformAppTest("api_test/bluetooth_low_energy/"
126                          "set_advertising_interval"))
127       << message_;
128 }
129 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,CreateService)130 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs, CreateService) {
131   EnterKioskSession();
132   SetAutoLaunchApp();
133   ASSERT_TRUE(
134       RunPlatformAppTest("api_test/bluetooth_low_energy/"
135                          "create_service"))
136       << message_;
137 }
138 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,CreateService_Flag)139 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs, CreateService_Flag) {
140   base::CommandLine::ForCurrentProcess()->AppendSwitch(
141       switches::kEnableBLEAdvertising);
142   ASSERT_TRUE(
143       RunPlatformAppTest("api_test/bluetooth_low_energy/create_service_flag"))
144       << message_;
145 }
146 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,CreateService_NotKioskSession)147 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
148                        CreateService_NotKioskSession) {
149   ASSERT_TRUE(RunPlatformAppTest(
150       "api_test/bluetooth_low_energy/create_service_no_kiosk_mode"))
151       << message_;
152 }
153 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,CreateService_KioskSessionOnly)154 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
155                        CreateService_KioskSessionOnly) {
156   EnterKioskSession();
157   ASSERT_TRUE(
158       RunPlatformAppTest("api_test/bluetooth_low_energy/"
159                          "create_service_kiosk_session_only"))
160       << message_;
161 }
162 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,CreateCharacteristic)163 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
164                        CreateCharacteristic) {
165   EnterKioskSession();
166   SetAutoLaunchApp();
167   ASSERT_TRUE(
168       RunPlatformAppTest("api_test/bluetooth_low_energy/"
169                          "create_characteristic"))
170       << message_;
171 }
172 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,CreateDescriptor)173 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs, CreateDescriptor) {
174   EnterKioskSession();
175   SetAutoLaunchApp();
176   ASSERT_TRUE(
177       RunPlatformAppTest("api_test/bluetooth_low_energy/"
178                          "create_descriptor"))
179       << message_;
180 }
181 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,RegisterService)182 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs, RegisterService) {
183   EnterKioskSession();
184   SetAutoLaunchApp();
185   ASSERT_TRUE(
186       RunPlatformAppTest("api_test/bluetooth_low_energy/"
187                          "register_service"))
188       << message_;
189 }
190 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,UnregisterService)191 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs, UnregisterService) {
192   EnterKioskSession();
193   SetAutoLaunchApp();
194   ASSERT_TRUE(
195       RunPlatformAppTest("api_test/bluetooth_low_energy/"
196                          "unregister_service"))
197       << message_;
198 }
199 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,RemoveService)200 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs, RemoveService) {
201   EnterKioskSession();
202   SetAutoLaunchApp();
203   ASSERT_TRUE(
204       RunPlatformAppTest("api_test/bluetooth_low_energy/"
205                          "remove_service"))
206       << message_;
207 }
208 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,NotifyCharacteristicValueChanged)209 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
210                        NotifyCharacteristicValueChanged) {
211   EnterKioskSession();
212   SetAutoLaunchApp();
213   ASSERT_TRUE(
214       RunPlatformAppTest("api_test/bluetooth_low_energy/"
215                          "notify_characteristic_value_changed"))
216       << message_;
217 }
218 
IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,NotifyCharacteristicValueChanged_ErrorConditions)219 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTestChromeOs,
220                        NotifyCharacteristicValueChanged_ErrorConditions) {
221   EnterKioskSession();
222   SetAutoLaunchApp();
223   ASSERT_TRUE(RunPlatformAppTest(
224       "api_test/bluetooth_low_energy/"
225       "notify_characteristic_value_changed_error_conditions"))
226       << message_;
227 }
228 
229 // TODO(rkc): Figure out how to integrate with BluetoothTestBlueZ and write
230 // comprehensive tests for GATT server events. See http://crbug.com/607395 for
231 // details.
232 
233 }  // namespace
234 }  // namespace extensions
235