blob: 570a42392e2686b4ea0f60e5e9b3d4d483b31fba [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2017 The Chromium Authors
Qiang Xuf1400a362017-09-14 02:28:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/touch/touch_devices_controller.h"
6
7#include "ash/accelerators/debug_commands.h"
Henrique Ferreirof9d1cb22021-07-13 01:32:478#include "ash/constants/ash_pref_names.h"
Henrique Ferreiro54c89f82021-06-15 15:41:289#include "ash/constants/ash_switches.h"
Xiyuan Xia5a8c4172019-05-13 16:23:4810#include "ash/public/cpp/ash_prefs.h"
Xiyuan Xiae7b19542019-05-06 23:05:1811#include "ash/session/session_controller_impl.h"
Qiang Xuf1400a362017-09-14 02:28:3012#include "ash/session/test_session_controller_client.h"
13#include "ash/shell.h"
14#include "ash/test/ash_test_base.h"
15#include "base/command_line.h"
Devlin Cronin7d338412018-06-05 01:17:0316#include "base/test/metrics/histogram_tester.h"
Qiang Xuf1400a362017-09-14 02:28:3017#include "components/prefs/pref_service.h"
Qiang Xu7e7dd6b2018-03-06 05:27:5118#include "components/prefs/testing_pref_service.h"
Qiang Xuf1400a362017-09-14 02:28:3019
20namespace ash {
21
22namespace {
23
24constexpr char kUser1Email[] = "[email protected]";
25constexpr char kUser2Email[] = "[email protected]";
26
Jun Mukaic82ae4e72018-04-03 03:08:1327bool GetUserPrefTouchpadEnabled() {
Qiang Xuf1400a362017-09-14 02:28:3028 PrefService* prefs =
29 Shell::Get()->session_controller()->GetLastActiveUserPrefService();
30 return prefs && prefs->GetBoolean(prefs::kTouchpadEnabled);
31}
32
Jun Mukaic82ae4e72018-04-03 03:08:1333bool GetGlobalTouchpadEnabled() {
34 return Shell::Get()->touch_devices_controller()->GetTouchpadEnabled(
35 TouchDeviceEnabledSource::GLOBAL);
36}
37
Qiang Xuf1400a362017-09-14 02:28:3038bool GetUserPrefTouchscreenEnabled() {
39 return Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
Jun Mukaic82ae4e72018-04-03 03:08:1340 TouchDeviceEnabledSource::USER_PREF);
Qiang Xuf1400a362017-09-14 02:28:3041}
42
43bool GetGlobalTouchscreenEnabled() {
44 return Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
Jun Mukaic82ae4e72018-04-03 03:08:1345 TouchDeviceEnabledSource::GLOBAL);
Qiang Xuf1400a362017-09-14 02:28:3046}
47
James Cook827225bb2018-03-22 19:28:4448void SetTapDraggingEnabled(bool enabled) {
49 PrefService* prefs =
50 Shell::Get()->session_controller()->GetLastActiveUserPrefService();
51 prefs->SetBoolean(prefs::kTapDraggingEnabled, enabled);
52 prefs->CommitPendingWrite();
53}
54
Qiang Xu7e7dd6b2018-03-06 05:27:5155class TouchDevicesControllerSigninTest : public NoSessionAshTestBase {
Qiang Xuf1400a362017-09-14 02:28:3056 public:
Qiang Xu7e7dd6b2018-03-06 05:27:5157 TouchDevicesControllerSigninTest() = default;
Peter Boströmec31a042021-09-16 23:37:3458
59 TouchDevicesControllerSigninTest(const TouchDevicesControllerSigninTest&) =
60 delete;
61 TouchDevicesControllerSigninTest& operator=(
62 const TouchDevicesControllerSigninTest&) = delete;
63
Qiang Xu7e7dd6b2018-03-06 05:27:5164 ~TouchDevicesControllerSigninTest() override = default;
Qiang Xuf1400a362017-09-14 02:28:3065
66 // NoSessionAshTestBase:
67 void SetUp() override {
68 base::CommandLine::ForCurrentProcess()->AppendSwitch(
69 switches::kAshDebugShortcuts);
70 NoSessionAshTestBase::SetUp();
71 CreateTestUserSessions();
72
73 // Simulate user 1 login.
74 SwitchActiveUser(kUser1Email);
75
76 ASSERT_TRUE(debug::DebugAcceleratorsEnabled());
77 }
78
79 void CreateTestUserSessions() {
80 GetSessionControllerClient()->Reset();
81 GetSessionControllerClient()->AddUserSession(kUser1Email);
82 GetSessionControllerClient()->AddUserSession(kUser2Email);
83 }
84
85 void SwitchActiveUser(const std::string& email) {
86 GetSessionControllerClient()->SwitchActiveUser(
87 AccountId::FromUserEmail(email));
88 }
Qiang Xuf1400a362017-09-14 02:28:3089};
90
Qiang Xu7e7dd6b2018-03-06 05:27:5191TEST_F(TouchDevicesControllerSigninTest, PrefsAreRegistered) {
92 PrefService* prefs =
93 Shell::Get()->session_controller()->GetLastActiveUserPrefService();
94 EXPECT_TRUE(prefs->FindPreference(prefs::kTapDraggingEnabled));
95 EXPECT_TRUE(prefs->FindPreference(prefs::kTouchpadEnabled));
96 EXPECT_TRUE(prefs->FindPreference(prefs::kTouchscreenEnabled));
97}
98
99TEST_F(TouchDevicesControllerSigninTest, SetTapDraggingEnabled) {
100 auto* controller = Shell::Get()->touch_devices_controller();
James Cook827225bb2018-03-22 19:28:44101 ASSERT_FALSE(controller->tap_dragging_enabled_for_test());
102 SetTapDraggingEnabled(true);
103 EXPECT_TRUE(controller->tap_dragging_enabled_for_test());
Qiang Xu7e7dd6b2018-03-06 05:27:51104
105 // Switch to user 2 and switch back.
106 SwitchActiveUser(kUser2Email);
James Cook827225bb2018-03-22 19:28:44107 EXPECT_FALSE(controller->tap_dragging_enabled_for_test());
Qiang Xu7e7dd6b2018-03-06 05:27:51108 SwitchActiveUser(kUser1Email);
James Cook827225bb2018-03-22 19:28:44109 EXPECT_TRUE(controller->tap_dragging_enabled_for_test());
Qiang Xu7e7dd6b2018-03-06 05:27:51110
James Cook827225bb2018-03-22 19:28:44111 SetTapDraggingEnabled(false);
112 EXPECT_FALSE(controller->tap_dragging_enabled_for_test());
Qiang Xu7e7dd6b2018-03-06 05:27:51113}
114
Qiang Xuf1400a362017-09-14 02:28:30115// Tests that touchpad enabled user pref works properly under debug accelerator.
Qiang Xu7e7dd6b2018-03-06 05:27:51116TEST_F(TouchDevicesControllerSigninTest, ToggleTouchpad) {
Jun Mukaic82ae4e72018-04-03 03:08:13117 ASSERT_TRUE(GetUserPrefTouchpadEnabled());
xiangdong konge368fac2023-05-08 18:12:56118 debug::PerformDebugActionIfEnabled(AcceleratorAction::kDebugToggleTouchPad);
Jun Mukaic82ae4e72018-04-03 03:08:13119 EXPECT_FALSE(GetUserPrefTouchpadEnabled());
Qiang Xuf1400a362017-09-14 02:28:30120
121 // Switch to user 2 and switch back.
122 SwitchActiveUser(kUser2Email);
Jun Mukaic82ae4e72018-04-03 03:08:13123 EXPECT_TRUE(GetUserPrefTouchpadEnabled());
Qiang Xuf1400a362017-09-14 02:28:30124 SwitchActiveUser(kUser1Email);
Jun Mukaic82ae4e72018-04-03 03:08:13125 EXPECT_FALSE(GetUserPrefTouchpadEnabled());
Qiang Xuf1400a362017-09-14 02:28:30126
xiangdong konge368fac2023-05-08 18:12:56127 debug::PerformDebugActionIfEnabled(AcceleratorAction::kDebugToggleTouchPad);
Jun Mukaic82ae4e72018-04-03 03:08:13128 EXPECT_TRUE(GetUserPrefTouchpadEnabled());
129}
130
131TEST_F(TouchDevicesControllerSigninTest, SetTouchpadEnabled) {
132 ASSERT_TRUE(GetUserPrefTouchpadEnabled());
133 ASSERT_TRUE(GetGlobalTouchpadEnabled());
134
135 Shell::Get()->touch_devices_controller()->SetTouchpadEnabled(
136 false, TouchDeviceEnabledSource::GLOBAL);
137 ASSERT_TRUE(GetUserPrefTouchpadEnabled());
138 ASSERT_FALSE(GetGlobalTouchpadEnabled());
139
140 Shell::Get()->touch_devices_controller()->SetTouchpadEnabled(
141 false, TouchDeviceEnabledSource::USER_PREF);
142 ASSERT_FALSE(GetUserPrefTouchpadEnabled());
143 ASSERT_FALSE(GetGlobalTouchpadEnabled());
144
145 Shell::Get()->touch_devices_controller()->SetTouchpadEnabled(
146 true, TouchDeviceEnabledSource::GLOBAL);
147 ASSERT_FALSE(GetUserPrefTouchpadEnabled());
148 ASSERT_TRUE(GetGlobalTouchpadEnabled());
Qiang Xuf1400a362017-09-14 02:28:30149}
150
151// Tests that touchscreen enabled user pref works properly under debug
152// accelerator.
Qiang Xu7e7dd6b2018-03-06 05:27:51153TEST_F(TouchDevicesControllerSigninTest, SetTouchscreenEnabled) {
Daniel Erat37fd6d72017-10-26 19:28:52154 ASSERT_TRUE(GetGlobalTouchscreenEnabled());
Qiang Xuf1400a362017-09-14 02:28:30155 ASSERT_TRUE(GetUserPrefTouchscreenEnabled());
Daniel Erat37fd6d72017-10-26 19:28:52156
xiangdong konge368fac2023-05-08 18:12:56157 debug::PerformDebugActionIfEnabled(
158 AcceleratorAction::kDebugToggleTouchScreen);
Daniel Erat37fd6d72017-10-26 19:28:52159 EXPECT_TRUE(GetGlobalTouchscreenEnabled());
Qiang Xuf1400a362017-09-14 02:28:30160 EXPECT_FALSE(GetUserPrefTouchscreenEnabled());
Qiang Xuf1400a362017-09-14 02:28:30161
162 // Switch to user 2 and switch back.
163 SwitchActiveUser(kUser2Email);
164 EXPECT_TRUE(GetUserPrefTouchscreenEnabled());
165 SwitchActiveUser(kUser1Email);
Daniel Erat37fd6d72017-10-26 19:28:52166 EXPECT_TRUE(GetGlobalTouchscreenEnabled());
Qiang Xuf1400a362017-09-14 02:28:30167 EXPECT_FALSE(GetUserPrefTouchscreenEnabled());
Qiang Xuf1400a362017-09-14 02:28:30168
xiangdong konge368fac2023-05-08 18:12:56169 debug::PerformDebugActionIfEnabled(
170 AcceleratorAction::kDebugToggleTouchScreen);
Qiang Xuf1400a362017-09-14 02:28:30171 EXPECT_TRUE(GetUserPrefTouchscreenEnabled());
Daniel Erat37fd6d72017-10-26 19:28:52172 EXPECT_TRUE(GetGlobalTouchscreenEnabled());
173
174 // The global setting should be preserved when switching users.
175 Shell::Get()->touch_devices_controller()->SetTouchscreenEnabled(
Jun Mukaic82ae4e72018-04-03 03:08:13176 false, TouchDeviceEnabledSource::GLOBAL);
Daniel Erat37fd6d72017-10-26 19:28:52177 EXPECT_FALSE(GetGlobalTouchscreenEnabled());
178 SwitchActiveUser(kUser2Email);
179 EXPECT_FALSE(GetGlobalTouchscreenEnabled());
Qiang Xuf1400a362017-09-14 02:28:30180}
181
Qiang Xu7e7dd6b2018-03-06 05:27:51182using TouchDevicesControllerPrefsTest = NoSessionAshTestBase;
183
184// Tests that "Touchpad.TapDragging.Started" is recorded on user session added
185// and pref service is ready and "Touchpad.TapDragging.Changed" is recorded each
186// time pref changes.
187TEST_F(TouchDevicesControllerPrefsTest, RecordUma) {
188 auto* controller = Shell::Get()->touch_devices_controller();
James Cook827225bb2018-03-22 19:28:44189 ASSERT_FALSE(controller->tap_dragging_enabled_for_test());
Qiang Xu7e7dd6b2018-03-06 05:27:51190
191 TestSessionControllerClient* session = GetSessionControllerClient();
192 // Disable auto-provision of PrefService.
Qiang Xu7e7dd6b2018-03-06 05:27:51193 constexpr bool kProvidePrefService = false;
194 // Add and switch to |kUser1Email|, but user pref service is not ready.
195 session->AddUserSession(kUser1Email, user_manager::USER_TYPE_REGULAR,
Denis Kuznetsove3400c12020-11-11 10:45:54196 kProvidePrefService);
Qiang Xu7e7dd6b2018-03-06 05:27:51197 const AccountId kUserAccount1 = AccountId::FromUserEmail(kUser1Email);
198 session->SwitchActiveUser(kUserAccount1);
199
200 base::HistogramTester histogram_tester;
201 histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Started", 0);
202 histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Changed", 0);
203
204 // Simulate active user pref service is changed.
205 auto pref_service = std::make_unique<TestingPrefServiceSimple>();
Xiyuan Xia5a8c4172019-05-13 16:23:48206 RegisterUserProfilePrefs(pref_service->registry(), true /* for_test */);
207 GetSessionControllerClient()->SetUserPrefService(kUserAccount1,
208 std::move(pref_service));
Qiang Xu7e7dd6b2018-03-06 05:27:51209 histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Started", 1);
210 histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Changed", 0);
211
James Cook827225bb2018-03-22 19:28:44212 EXPECT_FALSE(controller->tap_dragging_enabled_for_test());
213 SetTapDraggingEnabled(true);
Qiang Xu7e7dd6b2018-03-06 05:27:51214 histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Started", 1);
215 histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Changed", 1);
216}
217
James Cookbd0b7792017-11-17 03:24:26218} // namespace
Qiang Xuf1400a362017-09-14 02:28:30219} // namespace ash