bluetooth_power_controller_unittest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Copyright 2017 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. #include "ash/system/bluetooth/bluetooth_power_controller.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/constants/ash_pref_names.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/shell.h"
  9. #include "ash/system/bluetooth/bluetooth_power_controller.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "ash/test_shell_delegate.h"
  12. #include "base/callback_helpers.h"
  13. #include "base/run_loop.h"
  14. #include "base/test/metrics/histogram_tester.h"
  15. #include "base/test/scoped_feature_list.h"
  16. #include "components/prefs/pref_registry_simple.h"
  17. #include "components/prefs/testing_pref_service.h"
  18. #include "device/bluetooth/dbus/bluez_dbus_manager.h"
  19. #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
  20. namespace ash {
  21. namespace {
  22. constexpr char kUser1Email[] = "user1@bluetooth";
  23. constexpr bool kUserFirstLogin = true;
  24. void SetupBluetoothAdapter() {
  25. // Set Bluetooth discovery simulation delay to 0 so the test doesn't have to
  26. // wait or use timers.
  27. bluez::FakeBluetoothAdapterClient* adapter_client =
  28. static_cast<bluez::FakeBluetoothAdapterClient*>(
  29. bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient());
  30. adapter_client->SetSimulationIntervalMs(0);
  31. // Makes sure we get the callback from BluetoothAdapterFactory::GetAdapter
  32. // first before running the remaining test.
  33. base::RunLoop().RunUntilIdle();
  34. }
  35. BluetoothPowerController* GetController() {
  36. return Shell::Get()->bluetooth_power_controller();
  37. }
  38. device::BluetoothAdapter* GetBluetoothAdapter() {
  39. return GetController()->bluetooth_adapter_for_test();
  40. }
  41. } // namespace
  42. class BluetoothPowerControllerTest : public AshTestBase {
  43. public:
  44. BluetoothPowerControllerTest() {
  45. feature_list_.InitAndDisableFeature(features::kBluetoothRevamp);
  46. // Manually register local state prefs because AshTestBase attempts to
  47. // register local state prefs before this constructor is called when
  48. // the kBluetoothRevamp flag is still enabled.
  49. BluetoothPowerController::RegisterLocalStatePrefs(
  50. local_state()->registry());
  51. BluetoothPowerController::RegisterProfilePrefs(
  52. active_user_prefs_.registry());
  53. }
  54. BluetoothPowerControllerTest(const BluetoothPowerControllerTest&) = delete;
  55. BluetoothPowerControllerTest& operator=(const BluetoothPowerControllerTest&) =
  56. delete;
  57. ~BluetoothPowerControllerTest() override = default;
  58. void SetUp() override {
  59. AshTestBase::SetUp();
  60. SetupBluetoothAdapter();
  61. }
  62. void AddUserSessionAndStartWatchingPrefsChanges(
  63. const std::string& display_email,
  64. user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR,
  65. bool is_new_profile = false) {
  66. GetSessionControllerClient()->AddUserSession(
  67. display_email, user_type, false /* provide_pref_service */,
  68. is_new_profile);
  69. GetController()->active_user_pref_service_ = &active_user_prefs_;
  70. GetController()->StartWatchingActiveUserPrefsChanges();
  71. }
  72. void StartWatchingLocalStatePrefsChanges() {
  73. GetController()->StartWatchingLocalStatePrefsChanges();
  74. }
  75. protected:
  76. void ApplyBluetoothLocalStatePref() {
  77. GetController()->ApplyBluetoothLocalStatePref();
  78. }
  79. void ApplyBluetoothPrimaryUserPref() {
  80. GetController()->ApplyBluetoothPrimaryUserPref();
  81. }
  82. const base::queue<BluetoothPowerController::BluetoothTask>&
  83. GetPendingBluetoothTasks() {
  84. return GetController()->pending_bluetooth_tasks_;
  85. }
  86. // Pretends that the controller is busy doing bluetooth work. This is needed
  87. // to test the behavior when multiple power change requests are queued when
  88. // the controller is busy.
  89. void SimulateControllerBusy(bool is_busy) {
  90. GetController()->pending_tasks_busy_ = is_busy;
  91. if (!is_busy)
  92. GetController()->TriggerRunPendingBluetoothTasks();
  93. }
  94. TestingPrefServiceSimple active_user_prefs_;
  95. base::HistogramTester histogram_tester;
  96. private:
  97. base::test::ScopedFeatureList feature_list_;
  98. };
  99. class BluetoothPowerControllerNoSessionTest
  100. : public BluetoothPowerControllerTest {
  101. public:
  102. BluetoothPowerControllerNoSessionTest() { set_start_session(false); }
  103. };
  104. // This "integration" test aims to provide a closer simulation of production
  105. // logic behavior (see http://crbug.com/762567) using the PrefService objects
  106. // provided by AshTestBase and TestSessionControllerClient. However, we do have
  107. // to manually register some prefs since this test needs the kBluetoothRevamp
  108. // feature flag to be disabled. For more details see SetUp().
  109. class BluetoothPowerControllerIntegrationTest : public NoSessionAshTestBase {
  110. public:
  111. BluetoothPowerControllerIntegrationTest() = default;
  112. BluetoothPowerControllerIntegrationTest(
  113. const BluetoothPowerControllerIntegrationTest&) = delete;
  114. BluetoothPowerControllerIntegrationTest& operator=(
  115. const BluetoothPowerControllerIntegrationTest&) = delete;
  116. void SetUp() override {
  117. // These tests should only be run with the kBluetoothRevamp feature flag is
  118. // disabled, and so we force it off here and ensure that the local state
  119. // prefs that would have been registered had the feature flag been off are
  120. // registered.
  121. if (ash::features::IsBluetoothRevampEnabled()) {
  122. feature_list_.InitAndDisableFeature(features::kBluetoothRevamp);
  123. BluetoothPowerController::RegisterLocalStatePrefs(
  124. local_state()->registry());
  125. }
  126. NoSessionAshTestBase::SetUp();
  127. }
  128. private:
  129. base::test::ScopedFeatureList feature_list_;
  130. };
  131. // Tests toggling Bluetooth setting on and off.
  132. TEST_F(BluetoothPowerControllerNoSessionTest, ToggleBluetoothEnabled) {
  133. // Initially power state is set to default value.
  134. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  135. 1);
  136. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  137. 0);
  138. histogram_tester.ExpectBucketCount(
  139. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 1);
  140. histogram_tester.ExpectBucketCount(
  141. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 0);
  142. // Toggling bluetooth on/off when there is no user session should affect
  143. // local state prefs.
  144. EXPECT_FALSE(
  145. local_state()->GetBoolean(prefs::kSystemBluetoothAdapterEnabled));
  146. GetController()->SetBluetoothEnabled(true);
  147. EXPECT_TRUE(local_state()->GetBoolean(prefs::kSystemBluetoothAdapterEnabled));
  148. GetController()->SetBluetoothEnabled(false);
  149. EXPECT_FALSE(
  150. local_state()->GetBoolean(prefs::kSystemBluetoothAdapterEnabled));
  151. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  152. 2);
  153. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  154. 1);
  155. histogram_tester.ExpectBucketCount(
  156. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 2);
  157. histogram_tester.ExpectBucketCount(
  158. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 1);
  159. // Toggling bluetooth on/off when there is user session should affect
  160. // user prefs.
  161. AddUserSessionAndStartWatchingPrefsChanges(kUser1Email);
  162. EXPECT_FALSE(
  163. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  164. GetController()->SetBluetoothEnabled(true);
  165. EXPECT_TRUE(
  166. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  167. GetController()->SetBluetoothEnabled(false);
  168. EXPECT_FALSE(
  169. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  170. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  171. 3);
  172. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  173. 2);
  174. histogram_tester.ExpectBucketCount(
  175. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 3);
  176. histogram_tester.ExpectBucketCount(
  177. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 2);
  178. }
  179. // Tests that BluetoothPowerController listens to local state pref changes
  180. // and applies the changes to bluetooth device.
  181. TEST_F(BluetoothPowerControllerTest, ListensPrefChangesLocalState) {
  182. // Initially power state is set to default value .
  183. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  184. 1);
  185. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  186. 0);
  187. histogram_tester.ExpectBucketCount(
  188. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 1);
  189. histogram_tester.ExpectBucketCount(
  190. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 0);
  191. StartWatchingLocalStatePrefsChanges();
  192. // Makes sure we start with bluetooth power off.
  193. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  194. EXPECT_FALSE(
  195. local_state()->GetBoolean(prefs::kSystemBluetoothAdapterEnabled));
  196. // Power should be turned on when pref changes to enabled.
  197. local_state()->SetBoolean(prefs::kSystemBluetoothAdapterEnabled, true);
  198. EXPECT_TRUE(GetBluetoothAdapter()->IsPowered());
  199. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  200. 1);
  201. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  202. 1);
  203. histogram_tester.ExpectBucketCount(
  204. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 1);
  205. histogram_tester.ExpectBucketCount(
  206. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 1);
  207. // Power should be turned off when pref changes to disabled.
  208. local_state()->SetBoolean(prefs::kSystemBluetoothAdapterEnabled, false);
  209. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  210. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  211. 2);
  212. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  213. 1);
  214. histogram_tester.ExpectBucketCount(
  215. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 2);
  216. histogram_tester.ExpectBucketCount(
  217. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 1);
  218. }
  219. // Tests that BluetoothPowerController listens to active user pref changes
  220. // and applies the changes to bluetooth device.
  221. TEST_F(BluetoothPowerControllerTest, ListensPrefChangesActiveUser) {
  222. AddUserSessionAndStartWatchingPrefsChanges(kUser1Email);
  223. // Makes sure we start with bluetooth power off.
  224. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  225. EXPECT_FALSE(
  226. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  227. // Power should be turned on when pref changes to enabled.
  228. active_user_prefs_.SetBoolean(prefs::kUserBluetoothAdapterEnabled, true);
  229. EXPECT_TRUE(GetBluetoothAdapter()->IsPowered());
  230. // Power should be turned off when pref changes to disabled.
  231. active_user_prefs_.SetBoolean(prefs::kUserBluetoothAdapterEnabled, false);
  232. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  233. }
  234. // Tests that BluetoothPowerController listens to multiple active user pref
  235. // changes and applies the changes to bluetooth device. The queued multiple
  236. // power change tasks shouldn't be executed all but rather only the last request
  237. // is executed.
  238. TEST_F(BluetoothPowerControllerTest, ListensPrefChangesLongQueue) {
  239. // Initially power state is set to default value.
  240. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  241. 1);
  242. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  243. 0);
  244. histogram_tester.ExpectBucketCount(
  245. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 1);
  246. histogram_tester.ExpectBucketCount(
  247. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 0);
  248. AddUserSessionAndStartWatchingPrefsChanges(kUser1Email);
  249. // Makes sure we start with bluetooth power off.
  250. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  251. EXPECT_FALSE(
  252. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  253. // Multiple power change requests come in when the controller is busy.
  254. SimulateControllerBusy(true);
  255. active_user_prefs_.SetBoolean(prefs::kUserBluetoothAdapterEnabled, true);
  256. active_user_prefs_.SetBoolean(prefs::kUserBluetoothAdapterEnabled, false);
  257. active_user_prefs_.SetBoolean(prefs::kUserBluetoothAdapterEnabled, true);
  258. active_user_prefs_.SetBoolean(prefs::kUserBluetoothAdapterEnabled, false);
  259. active_user_prefs_.SetBoolean(prefs::kUserBluetoothAdapterEnabled, true);
  260. // The controller should execute only the last request.
  261. EXPECT_EQ(1u, GetPendingBluetoothTasks().size());
  262. // Flush the queue to be executed.
  263. SimulateControllerBusy(false);
  264. // The power state should represent the last request in the queue.
  265. EXPECT_TRUE(GetBluetoothAdapter()->IsPowered());
  266. // Since controller executes only last request only power on is recorded.
  267. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  268. 1);
  269. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  270. 1);
  271. histogram_tester.ExpectBucketCount(
  272. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 1);
  273. histogram_tester.ExpectBucketCount(
  274. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 1);
  275. }
  276. // Tests how BluetoothPowerController applies the local state pref when
  277. // the pref hasn't been set before.
  278. TEST_F(BluetoothPowerControllerTest, ApplyBluetoothLocalStatePrefDefault) {
  279. // Makes sure pref hasn't been set before.
  280. local_state()->RemoveUserPref(prefs::kSystemBluetoothAdapterEnabled);
  281. EXPECT_TRUE(local_state()
  282. ->FindPreference(prefs::kSystemBluetoothAdapterEnabled)
  283. ->IsDefaultValue());
  284. // Start with bluetooth power on.
  285. GetBluetoothAdapter()->SetPowered(true, base::DoNothing(), base::DoNothing());
  286. EXPECT_TRUE(GetBluetoothAdapter()->IsPowered());
  287. ApplyBluetoothLocalStatePref();
  288. // Pref should now contain the current bluetooth adapter state (on).
  289. EXPECT_FALSE(local_state()
  290. ->FindPreference(prefs::kSystemBluetoothAdapterEnabled)
  291. ->IsDefaultValue());
  292. EXPECT_TRUE(local_state()->GetBoolean(prefs::kSystemBluetoothAdapterEnabled));
  293. }
  294. // Tests how BluetoothPowerController applies the local state pref when
  295. // the pref has been set before.
  296. TEST_F(BluetoothPowerControllerTest, ApplyBluetoothLocalStatePrefOn) {
  297. // Initially power state is set to default value .
  298. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  299. 1);
  300. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  301. 0);
  302. histogram_tester.ExpectBucketCount(
  303. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 1);
  304. histogram_tester.ExpectBucketCount(
  305. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 0);
  306. // Set the pref to true.
  307. local_state()->SetBoolean(prefs::kSystemBluetoothAdapterEnabled, true);
  308. EXPECT_FALSE(local_state()
  309. ->FindPreference(prefs::kSystemBluetoothAdapterEnabled)
  310. ->IsDefaultValue());
  311. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  312. 1);
  313. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  314. 1);
  315. histogram_tester.ExpectBucketCount(
  316. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 1);
  317. histogram_tester.ExpectBucketCount(
  318. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 1);
  319. // Start with bluetooth power off.
  320. GetBluetoothAdapter()->SetPowered(false, base::DoNothing(),
  321. base::DoNothing());
  322. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  323. ApplyBluetoothLocalStatePref();
  324. // Bluetooth power setting should be applied (on), and pref value unchanged.
  325. EXPECT_TRUE(local_state()->GetBoolean(prefs::kSystemBluetoothAdapterEnabled));
  326. EXPECT_TRUE(GetBluetoothAdapter()->IsPowered());
  327. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", false,
  328. 1);
  329. histogram_tester.ExpectBucketCount("Bluetooth.ChromeOS.PoweredState", true,
  330. 2);
  331. histogram_tester.ExpectBucketCount(
  332. "Bluetooth.ChromeOS.PoweredState.Disable.Result", true, 1);
  333. histogram_tester.ExpectBucketCount(
  334. "Bluetooth.ChromeOS.PoweredState.Enable.Result", true, 2);
  335. }
  336. // Tests how BluetoothPowerController applies the user pref when
  337. // the pref hasn't been set before.
  338. TEST_F(BluetoothPowerControllerTest, ApplyBluetoothPrimaryUserPrefDefault) {
  339. AddUserSessionAndStartWatchingPrefsChanges(kUser1Email);
  340. // Makes sure pref hasn't been set before.
  341. EXPECT_TRUE(
  342. active_user_prefs_.FindPreference(prefs::kUserBluetoothAdapterEnabled)
  343. ->IsDefaultValue());
  344. // Start with bluetooth power off.
  345. GetBluetoothAdapter()->SetPowered(false, base::DoNothing(),
  346. base::DoNothing());
  347. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  348. ApplyBluetoothPrimaryUserPref();
  349. // Pref should now contain the current bluetooth adapter state (off).
  350. EXPECT_FALSE(
  351. active_user_prefs_.FindPreference(prefs::kUserBluetoothAdapterEnabled)
  352. ->IsDefaultValue());
  353. EXPECT_FALSE(
  354. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  355. }
  356. // Tests how BluetoothPowerController applies the user pref when
  357. // the pref hasn't been set before, and it's a first-login user.
  358. TEST_F(BluetoothPowerControllerNoSessionTest,
  359. ApplyBluetoothPrimaryUserPrefDefaultNew) {
  360. AddUserSessionAndStartWatchingPrefsChanges(
  361. kUser1Email, user_manager::USER_TYPE_REGULAR, kUserFirstLogin);
  362. // Makes sure pref hasn't been set before.
  363. EXPECT_TRUE(
  364. active_user_prefs_.FindPreference(prefs::kUserBluetoothAdapterEnabled)
  365. ->IsDefaultValue());
  366. // Start with bluetooth power off.
  367. GetBluetoothAdapter()->SetPowered(false, base::DoNothing(),
  368. base::DoNothing());
  369. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  370. ApplyBluetoothPrimaryUserPref();
  371. // Pref should be set to true for first-login users, and this will also
  372. // trigger the bluetooth power on.
  373. EXPECT_FALSE(
  374. active_user_prefs_.FindPreference(prefs::kUserBluetoothAdapterEnabled)
  375. ->IsDefaultValue());
  376. EXPECT_TRUE(
  377. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  378. EXPECT_TRUE(GetBluetoothAdapter()->IsPowered());
  379. }
  380. // Tests how BluetoothPowerController applies the user pref when
  381. // the pref hasn't been set before, but not a regular user (e.g. kiosk).
  382. TEST_F(BluetoothPowerControllerNoSessionTest,
  383. ApplyBluetoothKioskUserPrefDefault) {
  384. AddUserSessionAndStartWatchingPrefsChanges(kUser1Email,
  385. user_manager::USER_TYPE_KIOSK_APP);
  386. // Makes sure pref hasn't been set before.
  387. EXPECT_TRUE(
  388. active_user_prefs_.FindPreference(prefs::kUserBluetoothAdapterEnabled)
  389. ->IsDefaultValue());
  390. // Start with bluetooth power off.
  391. GetBluetoothAdapter()->SetPowered(false, base::DoNothing(),
  392. base::DoNothing());
  393. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  394. ApplyBluetoothPrimaryUserPref();
  395. // For non-regular user, do not apply the bluetooth setting and no need
  396. // to set the pref.
  397. EXPECT_TRUE(
  398. active_user_prefs_.FindPreference(prefs::kUserBluetoothAdapterEnabled)
  399. ->IsDefaultValue());
  400. EXPECT_FALSE(
  401. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  402. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  403. }
  404. // Tests how BluetoothPowerController applies the user pref when
  405. // the pref has been set before.
  406. TEST_F(BluetoothPowerControllerTest, ApplyBluetoothPrimaryUserPrefOn) {
  407. AddUserSessionAndStartWatchingPrefsChanges(kUser1Email);
  408. // Set the pref to true.
  409. active_user_prefs_.SetBoolean(prefs::kUserBluetoothAdapterEnabled, true);
  410. EXPECT_FALSE(
  411. active_user_prefs_.FindPreference(prefs::kUserBluetoothAdapterEnabled)
  412. ->IsDefaultValue());
  413. // Start with bluetooth power off.
  414. GetBluetoothAdapter()->SetPowered(false, base::DoNothing(),
  415. base::DoNothing());
  416. EXPECT_FALSE(GetBluetoothAdapter()->IsPowered());
  417. ApplyBluetoothPrimaryUserPref();
  418. // Pref should be applied to trigger the bluetooth power on, and the pref
  419. // value should be unchanged..
  420. EXPECT_TRUE(
  421. active_user_prefs_.GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  422. EXPECT_TRUE(GetBluetoothAdapter()->IsPowered());
  423. }
  424. TEST_F(BluetoothPowerControllerIntegrationTest, Basics) {
  425. SetupBluetoothAdapter();
  426. device::BluetoothAdapter* adapter = GetBluetoothAdapter();
  427. // Verify toggling bluetooth before login.
  428. GetController()->SetBluetoothEnabled(true);
  429. EXPECT_TRUE(local_state()->GetBoolean(prefs::kSystemBluetoothAdapterEnabled));
  430. EXPECT_TRUE(adapter->IsPowered());
  431. GetController()->SetBluetoothEnabled(false);
  432. EXPECT_FALSE(
  433. local_state()->GetBoolean(prefs::kSystemBluetoothAdapterEnabled));
  434. EXPECT_FALSE(adapter->IsPowered());
  435. // Verify toggling bluetooth after login.
  436. SimulateUserLogin(kUser1Email);
  437. PrefService* user_prefs =
  438. Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  439. GetController()->SetBluetoothEnabled(true);
  440. EXPECT_TRUE(user_prefs->GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  441. EXPECT_TRUE(adapter->IsPowered());
  442. GetController()->SetBluetoothEnabled(false);
  443. EXPECT_FALSE(user_prefs->GetBoolean(prefs::kUserBluetoothAdapterEnabled));
  444. EXPECT_FALSE(adapter->IsPowered());
  445. }
  446. } // namespace ash