Commit 3a586091 authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

[Switch Access] Clean up AutoScanManager

There have been a few major refactors of Switch Access recently. This
change looks at AutoScanManager and cleans up things that are now
overly complex, or need adjusting.

Bug: None
Change-Id: Ic77fb49a143ce91ff90099f773f613bd2f9e55ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2056502Reviewed-by: default avatarKatie Dektar <katie@chromium.org>
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Cr-Commit-Position: refs/heads/master@{#742777}
parent 066452e4
...@@ -6,38 +6,63 @@ ...@@ -6,38 +6,63 @@
* Class to handle auto-scan behavior. * Class to handle auto-scan behavior.
*/ */
class AutoScanManager { class AutoScanManager {
/** @private */
constructor() {
/**
* Auto-scan interval ID.
* @private {number|undefined}
*/
this.intervalID_;
/**
* Length of the auto-scan interval for most contexts, in milliseconds.
* @private {number}
*/
this.primaryScanTime_ = AutoScanManager.NOT_INITIALIZED;
/**
* Length of auto-scan interval for the on-screen keyboard in milliseconds.
* @private {number}
*/
this.keyboardScanTime_ = AutoScanManager.NOT_INITIALIZED;
/**
* Whether auto-scanning is enabled.
* @private {boolean}
*/
this.isEnabled_ = false;
/**
* Whether the current node is within the virtual keyboard.
* @private {boolean}
*/
this.inKeyboard_ = false;
}
// ============== Static Methods ================ // ============== Static Methods ================
static initialize() { static initialize() {
AutoScanManager.instance = new AutoScanManager(); AutoScanManager.instance = new AutoScanManager();
} }
/**
* Return true if auto-scan is currently running. Otherwise return false.
* @return {boolean}
*/
static isRunning() {
return AutoScanManager.instance.isEnabled_;
}
/** /**
* Restart auto-scan under the current settings if it is currently running. * Restart auto-scan under the current settings if it is currently running.
*/ */
static restartIfRunning() { static restartIfRunning() {
if (AutoScanManager.isRunning()) { if (AutoScanManager.instance.isRunning_()) {
AutoScanManager.instance.stop_(); AutoScanManager.instance.stop_();
AutoScanManager.instance.start_(); AutoScanManager.instance.start_();
} }
} }
/** /**
* Update this.defaultScanTime_ to |scanTime|. Then, if auto-scan is currently * Update this.primaryScanTime_ to |scanTime|. Then, if auto-scan is currently
* running, restart it. * running, restart it.
* *
* @param {number} scanTime Auto-scan interval time in milliseconds. * @param {number} scanTime Auto-scan interval time in milliseconds.
*/ */
static setDefaultScanTime(scanTime) { static setPrimaryScanTime(scanTime) {
AutoScanManager.instance.defaultScanTime_ = scanTime; AutoScanManager.instance.primaryScanTime_ = scanTime;
AutoScanManager.restartIfRunning(); AutoScanManager.restartIfRunning();
} }
...@@ -48,7 +73,7 @@ class AutoScanManager { ...@@ -48,7 +73,7 @@ class AutoScanManager {
* @param {boolean} enabled * @param {boolean} enabled
*/ */
static setEnabled(enabled) { static setEnabled(enabled) {
if (AutoScanManager.isRunning()) { if (AutoScanManager.instance.isRunning_()) {
AutoScanManager.instance.stop_(); AutoScanManager.instance.stop_();
} }
AutoScanManager.instance.isEnabled_ = enabled; AutoScanManager.instance.isEnabled_ = enabled;
...@@ -79,54 +104,31 @@ class AutoScanManager { ...@@ -79,54 +104,31 @@ class AutoScanManager {
// ============== Private Methods ================ // ============== Private Methods ================
/** @private */ /**
constructor() { * Return true if auto-scan is currently running. Otherwise return false.
/** * @return {boolean}
* Auto-scan interval ID. * @private
* @private {number|undefined} */
*/ isRunning_() {
this.intervalID_; return AutoScanManager.instance.isEnabled_;
/**
* Length of the default auto-scan interval (used wherever there is not
* a more specific scan time set) in milliseconds.
* @private {number}
*/
this.defaultScanTime_ = AutoScanManager.NOT_INITIALIZED;
/**
* Length of auto-scan interval for the on-screen keyboard in milliseconds.
* @private {number}
*/
this.keyboardScanTime_ = AutoScanManager.NOT_INITIALIZED;
/**
* Whether auto-scanning is enabled.
* @private {boolean}
*/
this.isEnabled_ = false;
/**
* Whether the current node is within the virtual keyboard.
* @private {boolean}
*/
this.inKeyboard_ = false;
} }
/** /**
* Set the window to move to the next node at an interval in milliseconds * Set the window to move to the next node at an interval in milliseconds
* depending on where the user is navigating. Currently, * depending on where the user is navigating. Currently,
* this.keyboardScanTime_ is used as the interval if the user is * this.keyboardScanTime_ is used as the interval if the user is
* navigating in the virtual keyboard, and this.defaultScanTime_ is used * navigating in the virtual keyboard, and this.primaryScanTime_ is used
* otherwise. * otherwise. Does not do anything if AutoScanManager is already scanning.
*
* @private * @private
*/ */
start_() { start_() {
if (this.defaultScanTime_ === AutoScanManager.NOT_INITIALIZED) { if (this.primaryScanTime_ === AutoScanManager.NOT_INITIALIZED ||
this.intervalID_) {
return; return;
} }
let currentScanTime = this.defaultScanTime_; let currentScanTime = this.primaryScanTime_;
if (SwitchAccess.instance.improvedTextInputEnabled() && this.inKeyboard_ && if (SwitchAccess.instance.improvedTextInputEnabled() && this.inKeyboard_ &&
this.keyboardScanTime_ !== AutoScanManager.NOT_INITIALIZED) { this.keyboardScanTime_ !== AutoScanManager.NOT_INITIALIZED) {
......
...@@ -57,7 +57,7 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabled', function() { ...@@ -57,7 +57,7 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabled', function() {
this.runWithLoadedTree('', (desktop) => { this.runWithLoadedTree('', (desktop) => {
console.error('RUNNING TEST'); console.error('RUNNING TEST');
assertFalse( assertFalse(
AutoScanManager.isRunning(), AutoScanManager.instance.isRunning_(),
'Auto scan manager is running prematurely'); 'Auto scan manager is running prematurely');
assertEquals( assertEquals(
0, NavigationManager.moveForwardCount, 0, NavigationManager.moveForwardCount,
...@@ -67,7 +67,8 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabled', function() { ...@@ -67,7 +67,8 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabled', function() {
NavigationManager.instance NavigationManager.instance
.onMoveForwardForTesting_ = this.newCallback(() => { .onMoveForwardForTesting_ = this.newCallback(() => {
assertTrue( assertTrue(
AutoScanManager.isRunning(), 'Auto scan manager has stopped running'); AutoScanManager.instance.isRunning_(),
'Auto scan manager has stopped running');
assertGT( assertGT(
NavigationManager.moveForwardCount, 0, NavigationManager.moveForwardCount, 0,
'Switch Access has not moved forward'); 'Switch Access has not moved forward');
...@@ -76,7 +77,9 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabled', function() { ...@@ -76,7 +77,9 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabled', function() {
}); });
AutoScanManager.setEnabled(true); AutoScanManager.setEnabled(true);
assertTrue(AutoScanManager.isRunning(), 'Auto scan manager is not running'); assertTrue(
AutoScanManager.instance.isRunning_(),
'Auto scan manager is not running');
assertEquals(1, intervalCount, 'There is not exactly 1 interval'); assertEquals(1, intervalCount, 'There is not exactly 1 interval');
}); });
}); });
...@@ -84,7 +87,7 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabled', function() { ...@@ -84,7 +87,7 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabled', function() {
TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabledMultiple', function() { TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabledMultiple', function() {
this.runWithLoadedTree('', (desktop) => { this.runWithLoadedTree('', (desktop) => {
assertFalse( assertFalse(
AutoScanManager.isRunning(), AutoScanManager.instance.isRunning_(),
'Auto scan manager is running prematurely'); 'Auto scan manager is running prematurely');
assertEquals(0, intervalCount, 'Incorrect initialization of intervalCount'); assertEquals(0, intervalCount, 'Incorrect initialization of intervalCount');
...@@ -92,7 +95,9 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabledMultiple', function() { ...@@ -92,7 +95,9 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabledMultiple', function() {
AutoScanManager.setEnabled(true); AutoScanManager.setEnabled(true);
AutoScanManager.setEnabled(true); AutoScanManager.setEnabled(true);
assertTrue(AutoScanManager.isRunning(), 'Auto scan manager is not running'); assertTrue(
AutoScanManager.instance.isRunning_(),
'Auto scan manager is not running');
assertEquals(1, intervalCount, 'There is not exactly 1 interval'); assertEquals(1, intervalCount, 'There is not exactly 1 interval');
}); });
}); });
...@@ -100,17 +105,20 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabledMultiple', function() { ...@@ -100,17 +105,20 @@ TEST_F('SwitchAccessAutoScanManagerTest', 'SetEnabledMultiple', function() {
TEST_F('SwitchAccessAutoScanManagerTest', 'EnableAndDisable', function() { TEST_F('SwitchAccessAutoScanManagerTest', 'EnableAndDisable', function() {
this.runWithLoadedTree('', (desktop) => { this.runWithLoadedTree('', (desktop) => {
assertFalse( assertFalse(
AutoScanManager.isRunning(), AutoScanManager.instance.isRunning_(),
'Auto scan manager is running prematurely'); 'Auto scan manager is running prematurely');
assertEquals(0, intervalCount, 'Incorrect initialization of intervalCount'); assertEquals(0, intervalCount, 'Incorrect initialization of intervalCount');
AutoScanManager.setEnabled(true); AutoScanManager.setEnabled(true);
assertTrue(AutoScanManager.isRunning(), 'Auto scan manager is not running'); assertTrue(
AutoScanManager.instance.isRunning_(),
'Auto scan manager is not running');
assertEquals(1, intervalCount, 'There is not exactly 1 interval'); assertEquals(1, intervalCount, 'There is not exactly 1 interval');
AutoScanManager.setEnabled(false); AutoScanManager.setEnabled(false);
assertFalse( assertFalse(
AutoScanManager.isRunning(), 'Auto scan manager did not stop running'); AutoScanManager.instance.isRunning_(),
'Auto scan manager did not stop running');
assertEquals(0, intervalCount, 'Interval was not removed'); assertEquals(0, intervalCount, 'Interval was not removed');
}); });
}); });
...@@ -119,7 +127,7 @@ TEST_F( ...@@ -119,7 +127,7 @@ TEST_F(
'SwitchAccessAutoScanManagerTest', 'RestartIfRunningMultiple', function() { 'SwitchAccessAutoScanManagerTest', 'RestartIfRunningMultiple', function() {
this.runWithLoadedTree('', (desktop) => { this.runWithLoadedTree('', (desktop) => {
assertFalse( assertFalse(
AutoScanManager.isRunning(), AutoScanManager.instance.isRunning_(),
'Auto scan manager is running prematurely'); 'Auto scan manager is running prematurely');
assertEquals( assertEquals(
0, NavigationManager.moveForwardCount, 0, NavigationManager.moveForwardCount,
...@@ -133,7 +141,8 @@ TEST_F( ...@@ -133,7 +141,8 @@ TEST_F(
AutoScanManager.restartIfRunning(); AutoScanManager.restartIfRunning();
assertTrue( assertTrue(
AutoScanManager.isRunning(), 'Auto scan manager is not running'); AutoScanManager.instance.isRunning_(),
'Auto scan manager is not running');
assertEquals(1, intervalCount, 'There is not exactly 1 interval'); assertEquals(1, intervalCount, 'There is not exactly 1 interval');
}); });
}); });
...@@ -142,46 +151,47 @@ TEST_F( ...@@ -142,46 +151,47 @@ TEST_F(
'SwitchAccessAutoScanManagerTest', 'RestartIfRunningWhenOff', function() { 'SwitchAccessAutoScanManagerTest', 'RestartIfRunningWhenOff', function() {
this.runWithLoadedTree('', (desktop) => { this.runWithLoadedTree('', (desktop) => {
assertFalse( assertFalse(
AutoScanManager.isRunning(), AutoScanManager.instance.isRunning_(),
'Auto scan manager is running at start.'); 'Auto scan manager is running at start.');
AutoScanManager.restartIfRunning(); AutoScanManager.restartIfRunning();
assertFalse( assertFalse(
AutoScanManager.isRunning(), AutoScanManager.instance.isRunning_(),
'Auto scan manager enabled by restartIfRunning'); 'Auto scan manager enabled by restartIfRunning');
}); });
}); });
TEST_F('SwitchAccessAutoScanManagerTest', 'SetDefaultScanTime', function() { TEST_F('SwitchAccessAutoScanManagerTest', 'SetPrimaryScanTime', function() {
this.runWithLoadedTree('', (desktop) => { this.runWithLoadedTree('', (desktop) => {
assertFalse( assertFalse(
AutoScanManager.isRunning(), AutoScanManager.instance.isRunning_(),
'Auto scan manager is running prematurely'); 'Auto scan manager is running prematurely');
assertEquals( assertEquals(
UNDEFINED_INTERVAL_DELAY, intervalDelay, UNDEFINED_INTERVAL_DELAY, intervalDelay,
'Interval delay improperly initialized'); 'Interval delay improperly initialized');
AutoScanManager.setDefaultScanTime(2); AutoScanManager.setPrimaryScanTime(2);
assertFalse( assertFalse(
AutoScanManager.isRunning(), AutoScanManager.instance.isRunning_(),
'Setting default scan time started auto-scanning'); 'Setting default scan time started auto-scanning');
assertEquals( assertEquals(
2, AutoScanManager.instance.defaultScanTime_, 2, AutoScanManager.instance.primaryScanTime_,
'Default scan time set improperly'); 'Default scan time set improperly');
assertEquals( assertEquals(
UNDEFINED_INTERVAL_DELAY, intervalDelay, UNDEFINED_INTERVAL_DELAY, intervalDelay,
'Interval delay set prematurely'); 'Interval delay set prematurely');
AutoScanManager.setEnabled(true); AutoScanManager.setEnabled(true);
assertTrue(AutoScanManager.isRunning(), 'Auto scan did not start'); assertTrue(
AutoScanManager.instance.isRunning_(), 'Auto scan did not start');
assertEquals( assertEquals(
2, AutoScanManager.instance.defaultScanTime_, 2, AutoScanManager.instance.primaryScanTime_,
'Default scan time has changed'); 'Default scan time has changed');
assertEquals(2, intervalDelay, 'Interval delay not set'); assertEquals(2, intervalDelay, 'Interval delay not set');
AutoScanManager.setDefaultScanTime(5); AutoScanManager.setPrimaryScanTime(5);
assertTrue(AutoScanManager.isRunning(), 'Auto scan stopped'); assertTrue(AutoScanManager.instance.isRunning_(), 'Auto scan stopped');
assertEquals( assertEquals(
5, AutoScanManager.instance.defaultScanTime_, 5, AutoScanManager.instance.primaryScanTime_,
'Default scan time did not change when set a second time'); 'Default scan time did not change when set a second time');
assertEquals(5, intervalDelay, 'Interval delay did not update'); assertEquals(5, intervalDelay, 'Interval delay did not update');
}); });
......
...@@ -102,6 +102,7 @@ class KeyboardRootNode extends RootNodeWrapper { ...@@ -102,6 +102,7 @@ class KeyboardRootNode extends RootNodeWrapper {
/** @override */ /** @override */
onExit() { onExit() {
chrome.accessibilityPrivate.setVirtualKeyboardVisible(false); chrome.accessibilityPrivate.setVirtualKeyboardVisible(false);
AutoScanManager.setInKeyboard(false);
} }
// ================= Static methods ================= // ================= Static methods =================
...@@ -113,6 +114,7 @@ class KeyboardRootNode extends RootNodeWrapper { ...@@ -113,6 +114,7 @@ class KeyboardRootNode extends RootNodeWrapper {
*/ */
static buildTree(desktop) { static buildTree(desktop) {
KeyboardRootNode.loadKeyboard_(); KeyboardRootNode.loadKeyboard_();
AutoScanManager.setInKeyboard(true);
const keyboardContainer = const keyboardContainer =
desktop.find({role: chrome.automation.RoleType.KEYBOARD}); desktop.find({role: chrome.automation.RoleType.KEYBOARD});
......
...@@ -92,7 +92,7 @@ class SwitchAccessPreferences { ...@@ -92,7 +92,7 @@ class SwitchAccessPreferences {
break; break;
case SAConstants.Preference.AUTO_SCAN_TIME: case SAConstants.Preference.AUTO_SCAN_TIME:
if (pref.type === chrome.settingsPrivate.PrefType.NUMBER) { if (pref.type === chrome.settingsPrivate.PrefType.NUMBER) {
AutoScanManager.setDefaultScanTime( AutoScanManager.setPrimaryScanTime(
/** @type {number} */ (pref.value)); /** @type {number} */ (pref.value));
} }
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment