Move modifierPressed() method from mixin to service

Google-Bug-Id: b/199305453
Change-Id: I5202c808fad6ffd89b436dcca51171dd81f98750
diff --git a/polygerrit-ui/app/elements/change-list/gr-change-list/gr-change-list.ts b/polygerrit-ui/app/elements/change-list/gr-change-list/gr-change-list.ts
index ac44908..a3db699 100644
--- a/polygerrit-ui/app/elements/change-list/gr-change-list/gr-change-list.ts
+++ b/polygerrit-ui/app/elements/change-list/gr-change-list/gr-change-list.ts
@@ -407,7 +407,7 @@
   }
 
   _nextChange(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -419,7 +419,7 @@
   }
 
   _prevChange(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -431,7 +431,7 @@
   }
 
   _openChange(e: IronKeyboardEvent) {
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
     this.openChange(e.detail.keyboardEvent);
   }
 
@@ -445,7 +445,7 @@
   _nextPage(e: IronKeyboardEvent) {
     if (
       this.shortcuts.shouldSuppress(e) ||
-      (this.modifierPressed(e) && !isShiftPressed(e))
+      (this.shortcuts.modifierPressed(e) && !isShiftPressed(e))
     ) {
       return;
     }
@@ -457,7 +457,7 @@
   _prevPage(e: IronKeyboardEvent) {
     if (
       this.shortcuts.shouldSuppress(e) ||
-      (this.modifierPressed(e) && !isShiftPressed(e))
+      (this.shortcuts.modifierPressed(e) && !isShiftPressed(e))
     ) {
       return;
     }
@@ -472,7 +472,7 @@
   }
 
   _toggleChangeReviewed(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -500,7 +500,7 @@
   }
 
   _toggleChangeStar(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
diff --git a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.ts b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.ts
index fc2cbe5..f8bdbef 100644
--- a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.ts
+++ b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.ts
@@ -752,7 +752,7 @@
   }
 
   _handleToggleDiffMode(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -1496,7 +1496,7 @@
   }
 
   _handleOpenReplyDialog(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
     this._getLoggedIn().then(isLoggedIn => {
@@ -1511,7 +1511,7 @@
   }
 
   _handleOpenDownloadDialogShortcut(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -1520,7 +1520,7 @@
   }
 
   _handleEditTopic(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -1672,7 +1672,7 @@
   }
 
   _handleToggleChangeStar(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
     e.preventDefault();
@@ -1680,7 +1680,7 @@
   }
 
   _handleUpToDashboard(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -1689,7 +1689,7 @@
   }
 
   _handleExpandAllMessages(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -1700,7 +1700,7 @@
   }
 
   _handleCollapseAllMessages(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -1711,7 +1711,7 @@
   }
 
   _handleOpenDiffPrefsShortcut(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
diff --git a/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list.ts b/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list.ts
index b78c78f..74c079b 100644
--- a/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list.ts
+++ b/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list.ts
@@ -902,7 +902,7 @@
   _handleToggleInlineDiff(e: IronKeyboardEvent) {
     if (
       this.shortcuts.shouldSuppress(e) ||
-      this.modifierPressed(e) ||
+      this.shortcuts.modifierPressed(e) ||
       e.detail?.keyboardEvent?.repeat ||
       this.fileCursor.index === -1
     ) {
@@ -923,7 +923,7 @@
   }
 
   _handleToggleHideAllCommentThreads(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -932,7 +932,7 @@
   }
 
   _handleCursorNext(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -952,7 +952,7 @@
   }
 
   _handleCursorPrev(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -972,7 +972,7 @@
   }
 
   _handleNewComment(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
     e.preventDefault();
@@ -1001,7 +1001,7 @@
   }
 
   _handleOpenFile(e: IronKeyboardEvent) {
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
     this.handleOpenFile(e.detail.keyboardEvent);
   }
 
@@ -1022,7 +1022,7 @@
   _handleNextChunk(e: IronKeyboardEvent) {
     if (
       this.shortcuts.shouldSuppress(e) ||
-      (this.modifierPressed(e) && !isShiftPressed(e)) ||
+      (this.shortcuts.modifierPressed(e) && !isShiftPressed(e)) ||
       this._noDiffsExpanded()
     ) {
       return;
@@ -1039,7 +1039,7 @@
   _handlePrevChunk(e: IronKeyboardEvent) {
     if (
       this.shortcuts.shouldSuppress(e) ||
-      (this.modifierPressed(e) && !isShiftPressed(e)) ||
+      (this.shortcuts.modifierPressed(e) && !isShiftPressed(e)) ||
       this._noDiffsExpanded()
     ) {
       return;
@@ -1054,7 +1054,7 @@
   }
 
   _handleToggleFileReviewed(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -1543,7 +1543,7 @@
   }
 
   _handleEscKey(e: IronKeyboardEvent) {
-    if (this.shortcuts.shouldSuppress(e) || this.modifierPressed(e)) {
+    if (this.shortcuts.shouldSuppress(e) || this.shortcuts.modifierPressed(e)) {
       return;
     }
     e.preventDefault();
diff --git a/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list_test.js b/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list_test.js
index f4064da..062d6a2 100644
--- a/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list_test.js
+++ b/polygerrit-ui/app/elements/change/gr-file-list/gr-file-list_test.js
@@ -597,7 +597,6 @@
         let interact;
 
         setup(() => {
-          sinon.stub(element, 'modifierPressed').returns(false);
           const openCursorStub = sinon.stub(element, '_openCursorFile');
           const openSelectedStub = sinon.stub(element, '_openSelectedFile');
           const expandStub = sinon.stub(element, '_toggleFileExpanded');
@@ -1708,12 +1707,15 @@
     });
 
     test('_displayLine', () => {
-      sinon.stub(element, 'modifierPressed')
-          .callsFake(() => false);
       element.filesExpanded = FilesExpandedState.ALL;
       const mockEvent = {
         preventDefault() {},
         composedPath() { return []; },
+        detail: {
+          keyboardEvent: {
+            composedPath() { return []; },
+          },
+        },
       };
 
       element._displayLine = false;
diff --git a/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar.ts b/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar.ts
index aaeb924..dd1ebd1 100644
--- a/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar.ts
+++ b/polygerrit-ui/app/elements/core/gr-search-bar/gr-search-bar.ts
@@ -400,7 +400,7 @@
     const keyboardEvent = e.detail.keyboardEvent;
     if (
       this.shortcuts.shouldSuppress(e) ||
-      (this.modifierPressed(e) && !keyboardEvent.shiftKey)
+      (this.shortcuts.modifierPressed(e) && !keyboardEvent.shiftKey)
     ) {
       return;
     }
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
index 5b48345..9d754fe 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
+++ b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
@@ -524,7 +524,7 @@
 
   _handleToggleFileReviewed(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
 
     e.preventDefault();
     this._setReviewed(!this.$.reviewed.checked);
@@ -532,7 +532,7 @@
 
   _handleEscKey(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
 
     e.preventDefault();
     this.$.diffHost.displayLine = false;
@@ -563,7 +563,7 @@
       this._moveToPreviousFileWithComment();
       return;
     }
-    if (this.modifierPressed(e)) {
+    if (this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -594,7 +594,7 @@
       this._moveToNextFileWithComment();
       return;
     }
-    if (this.modifierPressed(e)) {
+    if (this.shortcuts.modifierPressed(e)) {
       return;
     }
 
@@ -644,7 +644,7 @@
 
   _handleNewComment(ike: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(ike)) return;
-    if (this.modifierPressed(ike)) return;
+    if (this.shortcuts.modifierPressed(ike)) return;
 
     ike.preventDefault();
     this.classList.remove('hideComments');
@@ -685,7 +685,7 @@
         this._navigateToNextFileWithCommentThread();
       }
     } else {
-      if (this.modifierPressed(e)) return;
+      if (this.shortcuts.modifierPressed(e)) return;
       const result = this.cursor.moveToNextChunk();
       // navigate to next file if key is not being held down
       if (
@@ -743,7 +743,7 @@
     if (e.detail.keyboardEvent?.shiftKey) {
       this.cursor.moveToPreviousCommentThread();
     } else {
-      if (this.modifierPressed(e)) return;
+      if (this.shortcuts.modifierPressed(e)) return;
       this.cursor.moveToPreviousChunk();
       if (!e.detail.keyboardEvent?.repeat && this.cursor.isAtStart()) {
         this.showToastAndNavigateFile('previous', 'p');
@@ -754,7 +754,7 @@
   // Similar to gr-change-view._handleOpenReplyDialog
   _handleOpenReplyDialog(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
     this._getLoggedIn().then(isLoggedIn => {
       if (!isLoggedIn) {
         fireEvent(this, 'show-auth-required');
@@ -777,7 +777,7 @@
 
   _handleOpenDownloadDialog(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
 
     this.set('changeViewState.showDownloadDialog', true);
     e.preventDefault();
@@ -786,7 +786,7 @@
 
   _handleUpToChange(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
 
     e.preventDefault();
     this._navToChangeView();
@@ -794,7 +794,7 @@
 
   _handleCommaKey(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
     if (this._diffPrefsDisabled) return;
 
     e.preventDefault();
@@ -803,7 +803,7 @@
 
   _handleToggleDiffMode(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
 
     e.preventDefault();
     if (this._getDiffViewMode() === DiffViewMode.SIDE_BY_SIDE) {
@@ -1699,21 +1699,21 @@
 
   _handleToggleBlame(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
 
     this._toggleBlame();
   }
 
   _handleToggleHideAllCommentThreads(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
 
     toggleClass(this, 'hideComments');
   }
 
   _handleOpenFileList(e: IronKeyboardEvent) {
     if (this.shortcuts.shouldSuppress(e)) return;
-    if (this.modifierPressed(e)) return;
+    if (this.shortcuts.modifierPressed(e)) return;
     this.$.dropdown.open();
   }
 
diff --git a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts
index f1b74ac..a4ff9d0 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts
+++ b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts
@@ -509,7 +509,7 @@
     if (e.detail.keyboardEvent?.shiftKey) {
       this._expandCollapseComments(true);
     } else {
-      if (this.modifierPressed(e)) {
+      if (this.shortcuts.modifierPressed(e)) {
         return;
       }
       this._expandCollapseComments(false);
diff --git a/polygerrit-ui/app/mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin.ts b/polygerrit-ui/app/mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin.ts
index 29bfa05..02cfa9f 100644
--- a/polygerrit-ui/app/mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin.ts
+++ b/polygerrit-ui/app/mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin.ts
@@ -88,10 +88,6 @@
     /** Are shortcuts currently enabled? True only when element is visible. */
     private bindingsEnabled = false;
 
-    modifierPressed(e: IronKeyboardEvent) {
-      return this.shortcuts.modifierPressed(e);
-    }
-
     _addOwnKeyBindings(shortcut: Shortcut, handler: string) {
       const bindings = this.shortcuts.getBindingsForShortcut(shortcut);
       if (!bindings) {
@@ -241,10 +237,7 @@
     }
   }
 
-  return Mixin as T &
-    Constructor<
-      KeyboardShortcutMixinInterface & KeyboardShortcutMixinInterfaceTesting
-    >;
+  return Mixin as T & Constructor<KeyboardShortcutMixinInterface>;
 };
 
 // The following doesn't work (IronA11yKeysBehavior crashes):
@@ -257,10 +250,7 @@
 // This is a workaround
 export const KeyboardShortcutMixin = <T extends Constructor<PolymerElement>>(
   superClass: T
-): T &
-  Constructor<
-    KeyboardShortcutMixinInterface & KeyboardShortcutMixinInterfaceTesting
-  > =>
+): T & Constructor<KeyboardShortcutMixinInterface> =>
   InternalKeyboardShortcutMixin(
     // TODO(TS): mixinBehaviors in some lib is returning: `new () => T` instead
     // which will fail the type check due to missing IronA11yKeysBehavior interface
@@ -271,11 +261,4 @@
 /** The interface corresponding to KeyboardShortcutMixin */
 export interface KeyboardShortcutMixinInterface {
   keyboardShortcuts(): {[key: string]: string | null};
-  modifierPressed(event: IronKeyboardEvent): boolean;
-}
-
-export interface KeyboardShortcutMixinInterfaceTesting {
-  _shortcut_go_table: Map<string, string>;
-  _shortcut_v_table: Map<string, string>;
-  _handleGoAction: (e: IronKeyboardEvent) => void;
 }
diff --git a/polygerrit-ui/app/mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin_test.ts b/polygerrit-ui/app/mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin_test.ts
deleted file mode 100644
index d34c825..0000000
--- a/polygerrit-ui/app/mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin_test.ts
+++ /dev/null
@@ -1,83 +0,0 @@
-/**
- * @license
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-import '../../test/common-test-setup-karma';
-import {KeyboardShortcutMixin} from './keyboard-shortcut-mixin';
-import {PolymerElement} from '@polymer/polymer/polymer-element';
-import '../../elements/shared/gr-overlay/gr-overlay';
-import * as MockInteractions from '@polymer/iron-test-helpers/mock-interactions';
-
-class GrKeyboardShortcutMixinTestElement extends KeyboardShortcutMixin(
-  PolymerElement
-) {
-  static get is() {
-    return 'keyboard-shortcut-mixin-test-element';
-  }
-
-  get keyBindings() {
-    return {
-      k: '_handleKey',
-      enter: '_handleKey',
-    };
-  }
-
-  _handleKey(_: any) {}
-
-  _handleA(_: any) {}
-}
-
-declare global {
-  interface HTMLElementTagNameMap {
-    'keyboard-shortcut-mixin-test-element': GrKeyboardShortcutMixinTestElement;
-  }
-}
-
-customElements.define(
-  GrKeyboardShortcutMixinTestElement.is,
-  GrKeyboardShortcutMixinTestElement
-);
-
-const basicFixture = fixtureFromElement('keyboard-shortcut-mixin-test-element');
-
-suite('keyboard-shortcut-mixin tests', () => {
-  let element: GrKeyboardShortcutMixinTestElement;
-
-  setup(async () => {
-    element = basicFixture.instantiate();
-    await flush();
-  });
-
-  test('modifierPressed returns accurate values', () => {
-    const spy = sinon.spy(element, 'modifierPressed');
-    element._handleKey = e => {
-      element.modifierPressed(e);
-    };
-    MockInteractions.keyDownOn(element, 75, 'shift', 'k');
-    assert.isTrue(spy.lastCall.returnValue);
-    MockInteractions.keyDownOn(element, 75, null, 'k');
-    assert.isFalse(spy.lastCall.returnValue);
-    MockInteractions.keyDownOn(element, 75, 'ctrl', 'k');
-    assert.isTrue(spy.lastCall.returnValue);
-    MockInteractions.keyDownOn(element, 75, null, 'k');
-    assert.isFalse(spy.lastCall.returnValue);
-    MockInteractions.keyDownOn(element, 75, 'meta', 'k');
-    assert.isTrue(spy.lastCall.returnValue);
-    MockInteractions.keyDownOn(element, 75, null, 'k');
-    assert.isFalse(spy.lastCall.returnValue);
-    MockInteractions.keyDownOn(element, 75, 'alt', 'k');
-    assert.isTrue(spy.lastCall.returnValue);
-  });
-});