Commit e2675a6d authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

Avoid repeatedly starting RepeatedEventHandler

Also adds a test for stopping promptly when asked.

AX-Relnotes: n/a.
Bug: None.
Change-Id: Id0dc61f10a7c08aa01658b98c0b429eb95ef753b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2380585
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Reviewed-by: default avatarDavid Tseng <dtseng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802817}
parent 5ac381fe
......@@ -63,6 +63,9 @@ class RepeatedEventHandler {
/** Starts listening or handling events. */
startListening() {
if (this.listening_) {
return;
}
this.listening_ = true;
for (const node of this.nodes_) {
node.addEventListener(this.type_, this.handler_, this.capture_);
......@@ -71,6 +74,9 @@ class RepeatedEventHandler {
/** Stops listening or handling future events. */
stopListening() {
if (!this.listening_) {
return;
}
this.listening_ = false;
for (const node of this.nodes_) {
node.removeEventListener(this.type_, this.handler_, this.capture_);
......
......@@ -26,6 +26,30 @@ TEST_F('RepeatedEventHandlerTest', 'RepeatedEventHandledOnce', function() {
repeatedHandler.onEvent_();
// Yield before verify how many times the handler was called.
setTimeout(() => assertEquals(this.handlerCallCount, 1), 0);
setTimeout(
this.newCallback(() => assertEquals(this.handlerCallCount, 1)), 0);
});
});
TEST_F(
'RepeatedEventHandlerTest', 'NoEventsHandledAfterStopListening',
function() {
this.runWithLoadedTree('', (root) => {
this.handlerCallCount = 0;
const handler = () => this.handlerCallCount++;
const repeatedHandler =
new RepeatedEventHandler(root, 'focus', handler);
// Simulate events being fired.
repeatedHandler.onEvent_();
repeatedHandler.onEvent_();
repeatedHandler.onEvent_();
repeatedHandler.stopListening();
// Yield before verifying how many times the handler was called.
setTimeout(
this.newCallback(() => assertEquals(this.handlerCallCount, 0)), 0);
});
});
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