1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/touch/touch_devices_controller.h"
#include "ash/accelerators/debug_commands.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/ash_prefs.h"
#include "ash/session/session_controller_impl.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "base/test/metrics/histogram_tester.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
namespace ash {
namespace {
constexpr char kUser1Email[] = "user1@test.com";
constexpr char kUser2Email[] = "user2@test.com";
bool GetUserPrefTouchpadEnabled() {
PrefService* prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
return prefs && prefs->GetBoolean(prefs::kTouchpadEnabled);
}
bool GetGlobalTouchpadEnabled() {
return Shell::Get()->touch_devices_controller()->GetTouchpadEnabled(
TouchDeviceEnabledSource::GLOBAL);
}
bool GetUserPrefTouchscreenEnabled() {
return Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
TouchDeviceEnabledSource::USER_PREF);
}
bool GetGlobalTouchscreenEnabled() {
return Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
TouchDeviceEnabledSource::GLOBAL);
}
void SetTapDraggingEnabled(bool enabled) {
PrefService* prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
prefs->SetBoolean(prefs::kTapDraggingEnabled, enabled);
prefs->CommitPendingWrite();
}
class TouchDevicesControllerSigninTest : public NoSessionAshTestBase {
public:
TouchDevicesControllerSigninTest() = default;
TouchDevicesControllerSigninTest(const TouchDevicesControllerSigninTest&) =
delete;
TouchDevicesControllerSigninTest& operator=(
const TouchDevicesControllerSigninTest&) = delete;
~TouchDevicesControllerSigninTest() override = default;
// NoSessionAshTestBase:
void SetUp() override {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kAshDebugShortcuts);
NoSessionAshTestBase::SetUp();
CreateTestUserSessions();
// Simulate user 1 login.
SwitchActiveUser(kUser1Email);
ASSERT_TRUE(debug::DebugAcceleratorsEnabled());
}
void CreateTestUserSessions() {
GetSessionControllerClient()->Reset();
GetSessionControllerClient()->AddUserSession(kUser1Email);
GetSessionControllerClient()->AddUserSession(kUser2Email);
}
void SwitchActiveUser(const std::string& email) {
GetSessionControllerClient()->SwitchActiveUser(
AccountId::FromUserEmail(email));
}
};
TEST_F(TouchDevicesControllerSigninTest, PrefsAreRegistered) {
PrefService* prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
EXPECT_TRUE(prefs->FindPreference(prefs::kTapDraggingEnabled));
EXPECT_TRUE(prefs->FindPreference(prefs::kTouchpadEnabled));
EXPECT_TRUE(prefs->FindPreference(prefs::kTouchscreenEnabled));
}
TEST_F(TouchDevicesControllerSigninTest, SetTapDraggingEnabled) {
auto* controller = Shell::Get()->touch_devices_controller();
ASSERT_FALSE(controller->tap_dragging_enabled_for_test());
SetTapDraggingEnabled(true);
EXPECT_TRUE(controller->tap_dragging_enabled_for_test());
// Switch to user 2 and switch back.
SwitchActiveUser(kUser2Email);
EXPECT_FALSE(controller->tap_dragging_enabled_for_test());
SwitchActiveUser(kUser1Email);
EXPECT_TRUE(controller->tap_dragging_enabled_for_test());
SetTapDraggingEnabled(false);
EXPECT_FALSE(controller->tap_dragging_enabled_for_test());
}
// Tests that touchpad enabled user pref works properly under debug accelerator.
TEST_F(TouchDevicesControllerSigninTest, ToggleTouchpad) {
ASSERT_TRUE(GetUserPrefTouchpadEnabled());
debug::PerformDebugActionIfEnabled(AcceleratorAction::kDebugToggleTouchPad);
EXPECT_FALSE(GetUserPrefTouchpadEnabled());
// Switch to user 2 and switch back.
SwitchActiveUser(kUser2Email);
EXPECT_TRUE(GetUserPrefTouchpadEnabled());
SwitchActiveUser(kUser1Email);
EXPECT_FALSE(GetUserPrefTouchpadEnabled());
debug::PerformDebugActionIfEnabled(AcceleratorAction::kDebugToggleTouchPad);
EXPECT_TRUE(GetUserPrefTouchpadEnabled());
}
TEST_F(TouchDevicesControllerSigninTest, SetTouchpadEnabled) {
ASSERT_TRUE(GetUserPrefTouchpadEnabled());
ASSERT_TRUE(GetGlobalTouchpadEnabled());
Shell::Get()->touch_devices_controller()->SetTouchpadEnabled(
false, TouchDeviceEnabledSource::GLOBAL);
ASSERT_TRUE(GetUserPrefTouchpadEnabled());
ASSERT_FALSE(GetGlobalTouchpadEnabled());
Shell::Get()->touch_devices_controller()->SetTouchpadEnabled(
false, TouchDeviceEnabledSource::USER_PREF);
ASSERT_FALSE(GetUserPrefTouchpadEnabled());
ASSERT_FALSE(GetGlobalTouchpadEnabled());
Shell::Get()->touch_devices_controller()->SetTouchpadEnabled(
true, TouchDeviceEnabledSource::GLOBAL);
ASSERT_FALSE(GetUserPrefTouchpadEnabled());
ASSERT_TRUE(GetGlobalTouchpadEnabled());
}
// Tests that touchscreen enabled user pref works properly under debug
// accelerator.
TEST_F(TouchDevicesControllerSigninTest, SetTouchscreenEnabled) {
ASSERT_TRUE(GetGlobalTouchscreenEnabled());
ASSERT_TRUE(GetUserPrefTouchscreenEnabled());
debug::PerformDebugActionIfEnabled(
AcceleratorAction::kDebugToggleTouchScreen);
EXPECT_TRUE(GetGlobalTouchscreenEnabled());
EXPECT_FALSE(GetUserPrefTouchscreenEnabled());
// Switch to user 2 and switch back.
SwitchActiveUser(kUser2Email);
EXPECT_TRUE(GetUserPrefTouchscreenEnabled());
SwitchActiveUser(kUser1Email);
EXPECT_TRUE(GetGlobalTouchscreenEnabled());
EXPECT_FALSE(GetUserPrefTouchscreenEnabled());
debug::PerformDebugActionIfEnabled(
AcceleratorAction::kDebugToggleTouchScreen);
EXPECT_TRUE(GetUserPrefTouchscreenEnabled());
EXPECT_TRUE(GetGlobalTouchscreenEnabled());
// The global setting should be preserved when switching users.
Shell::Get()->touch_devices_controller()->SetTouchscreenEnabled(
false, TouchDeviceEnabledSource::GLOBAL);
EXPECT_FALSE(GetGlobalTouchscreenEnabled());
SwitchActiveUser(kUser2Email);
EXPECT_FALSE(GetGlobalTouchscreenEnabled());
}
using TouchDevicesControllerPrefsTest = NoSessionAshTestBase;
// Tests that "Touchpad.TapDragging.Started" is recorded on user session added
// and pref service is ready and "Touchpad.TapDragging.Changed" is recorded each
// time pref changes.
TEST_F(TouchDevicesControllerPrefsTest, RecordUma) {
auto* controller = Shell::Get()->touch_devices_controller();
ASSERT_FALSE(controller->tap_dragging_enabled_for_test());
TestSessionControllerClient* session = GetSessionControllerClient();
// Disable auto-provision of PrefService.
constexpr bool kProvidePrefService = false;
// Add and switch to |kUser1Email|, but user pref service is not ready.
session->AddUserSession(kUser1Email, user_manager::USER_TYPE_REGULAR,
kProvidePrefService);
const AccountId kUserAccount1 = AccountId::FromUserEmail(kUser1Email);
session->SwitchActiveUser(kUserAccount1);
base::HistogramTester histogram_tester;
histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Started", 0);
histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Changed", 0);
// Simulate active user pref service is changed.
auto pref_service = std::make_unique<TestingPrefServiceSimple>();
RegisterUserProfilePrefs(pref_service->registry(), /*country=*/"",
true /* for_test */);
GetSessionControllerClient()->SetUserPrefService(kUserAccount1,
std::move(pref_service));
histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Started", 1);
histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Changed", 0);
EXPECT_FALSE(controller->tap_dragging_enabled_for_test());
SetTapDraggingEnabled(true);
histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Started", 1);
histogram_tester.ExpectTotalCount("Touchpad.TapDragging.Changed", 1);
}
} // namespace
} // namespace ash
|