Replace some flush() callbacks in js tests with await flush()

Change-Id: Ifbeca501a8b78c0bead5aaf718bb3fbcc79bf871
diff --git a/polygerrit-ui/app/elements/shared/gr-account-list/gr-account-list_test.js b/polygerrit-ui/app/elements/shared/gr-account-list/gr-account-list_test.js
index 78c8b9f..ebfd18e 100644
--- a/polygerrit-ui/app/elements/shared/gr-account-list/gr-account-list_test.js
+++ b/polygerrit-ui/app/elements/shared/gr-account-list/gr-account-list_test.js
@@ -349,7 +349,7 @@
     assert.isTrue(element.$.entry.hasAttribute('hidden'));
   });
 
-  test('enter text calls suggestions provider', done => {
+  test('enter text calls suggestions provider', async () => {
     const suggestions = [
       {
         email: 'abc@example.com',
@@ -373,16 +373,13 @@
     input.text = 'newTest';
     MockInteractions.focus(input.$.input);
     input.noDebounce = true;
-    flush();
-    flush(() => {
-      assert.isTrue(getSuggestionsStub.calledOnce);
-      assert.equal(getSuggestionsStub.lastCall.args[0], 'newTest');
-      assert.equal(makeSuggestionItemStub.getCalls().length, 2);
-      done();
-    });
+    await flush();
+    assert.isTrue(getSuggestionsStub.calledOnce);
+    assert.equal(getSuggestionsStub.lastCall.args[0], 'newTest');
+    assert.equal(makeSuggestionItemStub.getCalls().length, 2);
   });
 
-  test('suggestion on empty', done => {
+  test('suggestion on empty', async () => {
     element.skipSuggestOnEmpty = false;
     const suggestions = [
       {
@@ -407,16 +404,13 @@
     input.text = '';
     MockInteractions.focus(input.$.input);
     input.noDebounce = true;
-    flush();
-    flush(() => {
-      assert.isTrue(getSuggestionsStub.calledOnce);
-      assert.equal(getSuggestionsStub.lastCall.args[0], '');
-      assert.equal(makeSuggestionItemStub.getCalls().length, 2);
-      done();
-    });
+    await flush();
+    assert.isTrue(getSuggestionsStub.calledOnce);
+    assert.equal(getSuggestionsStub.lastCall.args[0], '');
+    assert.equal(makeSuggestionItemStub.getCalls().length, 2);
   });
 
-  test('skip suggestion on empty', done => {
+  test('skip suggestion on empty', async () => {
     element.skipSuggestOnEmpty = true;
     const getSuggestionsStub =
         sinon.stub(suggestionsProvider, 'getSuggestions')
@@ -427,11 +421,8 @@
     input.text = '';
     MockInteractions.focus(input.$.input);
     input.noDebounce = true;
-    flush();
-    flush(() => {
-      assert.isTrue(getSuggestionsStub.notCalled);
-      done();
-    });
+    await flush();
+    assert.isTrue(getSuggestionsStub.notCalled);
   });
 
   suite('allowAnyInput', () => {
@@ -468,68 +459,62 @@
   });
 
   suite('keyboard interactions', () => {
-    test('backspace at text input start removes last account', done => {
+    test('backspace at text input start removes last account', async () => {
       const input = element.$.entry.$.input;
       sinon.stub(input, '_updateSuggestions');
       sinon.stub(element, '_computeRemovable').returns(true);
-      flush(() => {
-        // Next line is a workaround for Firefox not moving cursor
-        // on input field update
-        assert.equal(
-            element._getNativeInput(input.$.input).selectionStart, 0);
-        input.text = 'test';
-        MockInteractions.focus(input.$.input);
-        flush();
-        assert.equal(element.accounts.length, 2);
-        MockInteractions.pressAndReleaseKeyOn(
-            element._getNativeInput(input.$.input), 8); // Backspace
-        assert.equal(element.accounts.length, 2);
-        input.text = '';
-        MockInteractions.pressAndReleaseKeyOn(
-            element._getNativeInput(input.$.input), 8); // Backspace
-        flush();
-        assert.equal(element.accounts.length, 1);
-        done();
-      });
+      await flush();
+      // Next line is a workaround for Firefox not moving cursor
+      // on input field update
+      assert.equal(
+          element._getNativeInput(input.$.input).selectionStart, 0);
+      input.text = 'test';
+      MockInteractions.focus(input.$.input);
+      flush();
+      assert.equal(element.accounts.length, 2);
+      MockInteractions.pressAndReleaseKeyOn(
+          element._getNativeInput(input.$.input), 8); // Backspace
+      assert.equal(element.accounts.length, 2);
+      input.text = '';
+      MockInteractions.pressAndReleaseKeyOn(
+          element._getNativeInput(input.$.input), 8); // Backspace
+      flush();
+      assert.equal(element.accounts.length, 1);
     });
 
-    test('arrow key navigation', done => {
+    test('arrow key navigation', async () => {
       const input = element.$.entry.$.input;
       input.text = '';
       element.accounts = [makeAccount(), makeAccount()];
-      flush(() => {
-        MockInteractions.focus(input.$.input);
-        flush();
-        const chips = element.accountChips;
-        const chipsOneSpy = sinon.spy(chips[1], 'focus');
-        MockInteractions.pressAndReleaseKeyOn(input.$.input, 37); // Left
-        assert.isTrue(chipsOneSpy.called);
-        const chipsZeroSpy = sinon.spy(chips[0], 'focus');
-        MockInteractions.pressAndReleaseKeyOn(chips[1], 37); // Left
-        assert.isTrue(chipsZeroSpy.called);
-        MockInteractions.pressAndReleaseKeyOn(chips[0], 37); // Left
-        assert.isTrue(chipsZeroSpy.calledOnce);
-        MockInteractions.pressAndReleaseKeyOn(chips[0], 39); // Right
-        assert.isTrue(chipsOneSpy.calledTwice);
-        done();
-      });
+      flush();
+      MockInteractions.focus(input.$.input);
+      await flush();
+      const chips = element.accountChips;
+      const chipsOneSpy = sinon.spy(chips[1], 'focus');
+      MockInteractions.pressAndReleaseKeyOn(input.$.input, 37); // Left
+      assert.isTrue(chipsOneSpy.called);
+      const chipsZeroSpy = sinon.spy(chips[0], 'focus');
+      MockInteractions.pressAndReleaseKeyOn(chips[1], 37); // Left
+      assert.isTrue(chipsZeroSpy.called);
+      MockInteractions.pressAndReleaseKeyOn(chips[0], 37); // Left
+      assert.isTrue(chipsZeroSpy.calledOnce);
+      MockInteractions.pressAndReleaseKeyOn(chips[0], 39); // Right
+      assert.isTrue(chipsOneSpy.calledTwice);
     });
 
-    test('delete', done => {
+    test('delete', () => {
       element.accounts = [makeAccount(), makeAccount()];
-      flush(() => {
-        const focusSpy = sinon.spy(element.accountChips[1], 'focus');
-        const removeSpy = sinon.spy(element, 'removeAccount');
-        MockInteractions.pressAndReleaseKeyOn(
-            element.accountChips[0], 8); // Backspace
-        assert.isTrue(focusSpy.called);
-        assert.isTrue(removeSpy.calledOnce);
+      flush();
+      const focusSpy = sinon.spy(element.accountChips[1], 'focus');
+      const removeSpy = sinon.spy(element, 'removeAccount');
+      MockInteractions.pressAndReleaseKeyOn(
+          element.accountChips[0], 8); // Backspace
+      assert.isTrue(focusSpy.called);
+      assert.isTrue(removeSpy.calledOnce);
 
-        MockInteractions.pressAndReleaseKeyOn(
-            element.accountChips[1], 46); // Delete
-        assert.isTrue(removeSpy.calledTwice);
-        done();
-      });
+      MockInteractions.pressAndReleaseKeyOn(
+          element.accountChips[1], 46); // Delete
+      assert.isTrue(removeSpy.calledTwice);
     });
   });
 });
diff --git a/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete_test.js b/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete_test.js
index 3c30c4e..f1cbea9 100644
--- a/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete_test.js
+++ b/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete_test.js
@@ -68,19 +68,17 @@
     });
   });
 
-  test('selectAll', done => {
-    flush(() => {
-      const nativeInput = element._nativeInput;
-      const selectionStub = sinon.stub(nativeInput, 'setSelectionRange');
+  test('selectAll', async () => {
+    await flush();
+    const nativeInput = element._nativeInput;
+    const selectionStub = sinon.stub(nativeInput, 'setSelectionRange');
 
-      element.selectAll();
-      assert.isFalse(selectionStub.called);
+    element.selectAll();
+    assert.isFalse(selectionStub.called);
 
-      element.$.input.value = 'test';
-      element.selectAll();
-      assert.isTrue(selectionStub.called);
-      done();
-    });
+    element.$.input.value = 'test';
+    element.selectAll();
+    assert.isTrue(selectionStub.called);
   });
 
   test('esc key behavior', done => {
@@ -349,28 +347,24 @@
     assert.isTrue(element._focused);
   });
 
-  test('_focused flag properly triggered', done => {
-    flush(() => {
-      assert.isFalse(element._focused);
-      const input = element.shadowRoot
-          .querySelector('paper-input').inputElement;
-      MockInteractions.focus(input);
-      assert.isTrue(element._focused);
-      done();
-    });
+  test('_focused flag properly triggered', () => {
+    flush();
+    assert.isFalse(element._focused);
+    const input = element.shadowRoot
+        .querySelector('paper-input').inputElement;
+    MockInteractions.focus(input);
+    assert.isTrue(element._focused);
   });
 
-  test('search icon shows with showSearchIcon property', done => {
-    flush(() => {
-      assert.equal(getComputedStyle(element.shadowRoot
-          .querySelector('iron-icon')).display,
-      'none');
-      element.showSearchIcon = true;
-      assert.notEqual(getComputedStyle(element.shadowRoot
-          .querySelector('iron-icon')).display,
-      'none');
-      done();
-    });
+  test('search icon shows with showSearchIcon property', () => {
+    flush();
+    assert.equal(getComputedStyle(element.shadowRoot
+        .querySelector('iron-icon')).display,
+    'none');
+    element.showSearchIcon = true;
+    assert.notEqual(getComputedStyle(element.shadowRoot
+        .querySelector('iron-icon')).display,
+    'none');
   });
 
   test('vertical offset overridden by param if it exists', () => {
diff --git a/polygerrit-ui/app/elements/shared/gr-dialog/gr-dialog_test.js b/polygerrit-ui/app/elements/shared/gr-dialog/gr-dialog_test.js
index 346031d..b60b2e2 100644
--- a/polygerrit-ui/app/elements/shared/gr-dialog/gr-dialog_test.js
+++ b/polygerrit-ui/app/elements/shared/gr-dialog/gr-dialog_test.js
@@ -73,12 +73,10 @@
       assert.isNull(element.$.confirm.getAttribute('has-tooltip'));
     });
 
-    test('tooltip added if confirm tooltip is passed', done => {
+    test('tooltip added if confirm tooltip is passed', () => {
       element.confirmTooltip = 'confirm tooltip';
-      flush(() => {
-        assert(element.$.confirm.getAttribute('has-tooltip'));
-        done();
-      });
+      flush();
+      assert(element.$.confirm.getAttribute('has-tooltip'));
     });
   });
 
diff --git a/polygerrit-ui/app/elements/shared/gr-editable-label/gr-editable-label_test.js b/polygerrit-ui/app/elements/shared/gr-editable-label/gr-editable-label_test.js
index 8c04aed..7260018 100644
--- a/polygerrit-ui/app/elements/shared/gr-editable-label/gr-editable-label_test.js
+++ b/polygerrit-ui/app/elements/shared/gr-editable-label/gr-editable-label_test.js
@@ -43,18 +43,14 @@
   let input;
   let label;
 
-  setup(done => {
+  setup(async () => {
     element = basicFixture.instantiate();
     elementNoPlaceholder = noPlaceholderFixture.instantiate();
+    label = element.shadowRoot.querySelector('label');
 
-    label = element.shadowRoot
-        .querySelector('label');
-
-    flush(() => {
-      // In Polymer 2 inputElement isn't nativeInput anymore
-      input = element.$.input.$.nativeInput || element.$.input.inputElement;
-      done();
-    });
+    await flush();
+    // In Polymer 2 inputElement isn't nativeInput anymore
+    input = element.$.input.$.nativeInput || element.$.input.inputElement;
   });
 
   test('element render', () => {
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-action-context_test.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-action-context_test.js
index 27c9c28..e764bf8 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-action-context_test.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-action-context_test.js
@@ -33,7 +33,7 @@
     instance = new GrPluginActionContext(plugin);
   });
 
-  test('popup() and hide()', done => {
+  test('popup() and hide()', async () => {
     const popupApiStub = {
       _getElement: sinon.stub().returns(document.createElement('div')),
       close: sinon.stub(),
@@ -41,12 +41,10 @@
     sinon.stub(plugin, 'popup').returns(Promise.resolve(popupApiStub));
     const el = document.createElement('span');
     instance.popup(el);
-    flush(() => {
-      assert.isTrue(popupApiStub._getElement.called);
-      instance.hide();
-      assert.isTrue(popupApiStub.close.called);
-      done();
-    });
+    await flush();
+    assert.isTrue(popupApiStub._getElement.called);
+    instance.hide();
+    assert.isTrue(popupApiStub.close.called);
   });
 
   test('textfield', () => {
@@ -84,13 +82,11 @@
       document.body.appendChild(button);
     });
 
-    test('click', done => {
+    test('click', () => {
       MockInteractions.tap(button);
-      flush(() => {
-        assert.isTrue(clickStub.called);
-        assert.equal(button.textContent, 'foo');
-        done();
-      });
+      flush();
+      assert.isTrue(clickStub.called);
+      assert.equal(button.textContent, 'foo');
     });
 
     teardown(() => {
@@ -130,7 +126,7 @@
         'METHOD', '/changes/1/revisions/2/foo~bar', payload));
   });
 
-  test('call error', done => {
+  test('call error', async () => {
     instance.action = {
       method: 'METHOD',
       __key: 'key',
@@ -143,12 +139,10 @@
     const errorStub = sinon.stub();
     document.addEventListener('show-alert', errorStub);
     instance.call();
-    flush(() => {
-      assert.isTrue(errorStub.calledOnce);
-      assert.equal(errorStub.args[0][0].detail.message,
-          'Plugin network error: Error: boom');
-      done();
-    });
+    await flush();
+    assert.isTrue(errorStub.calledOnce);
+    assert.equal(errorStub.args[0][0].detail.message,
+        'Plugin network error: Error: boom');
   });
 });
 
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-loader_test.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-loader_test.js
index 5accc68..06c75e3 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-loader_test.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-loader_test.js
@@ -83,18 +83,16 @@
     assert(callback.notCalled);
   });
 
-  test('report pluginsLoaded', done => {
+  test('report pluginsLoaded', async () => {
     const pluginsLoadedStub = sinon.stub(pluginLoader._getReporting(),
         'pluginsLoaded');
     pluginsLoadedStub.reset();
     window.Gerrit._loadPlugins([]);
-    flush(() => {
-      assert.isTrue(pluginsLoadedStub.called);
-      done();
-    });
+    await flush();
+    assert.isTrue(pluginsLoadedStub.called);
   });
 
-  test('arePluginsLoaded', done => {
+  test('arePluginsLoaded', () => {
     assert.isFalse(pluginLoader.arePluginsLoaded());
     const plugins = [
       'http://test.com/plugins/foo/static/test.js',
@@ -106,13 +104,11 @@
     // Timeout on loading plugins
     window.clock.tick(PLUGIN_LOADING_TIMEOUT_MS * 2);
 
-    flush(() => {
-      assert.isTrue(pluginLoader.arePluginsLoaded());
-      done();
-    });
+    flush();
+    assert.isTrue(pluginLoader.arePluginsLoaded());
   });
 
-  test('plugins installed successfully', done => {
+  test('plugins installed successfully', async () => {
     sinon.stub(pluginLoader, '_loadJsPlugin').callsFake( url => {
       pluginApi.install(() => void 0, undefined, url);
     });
@@ -125,14 +121,12 @@
     ];
     pluginLoader.loadPlugins(plugins);
 
-    flush(() => {
-      assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo', 'bar']));
-      assert.isTrue(pluginLoader.arePluginsLoaded());
-      done();
-    });
+    await flush();
+    assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo', 'bar']));
+    assert.isTrue(pluginLoader.arePluginsLoaded());
   });
 
-  test('isPluginEnabled and isPluginLoaded', done => {
+  test('isPluginEnabled and isPluginLoaded', () => {
     sinon.stub(pluginLoader, '_loadJsPlugin').callsFake( url => {
       pluginApi.install(() => void 0, undefined, url);
     });
@@ -147,17 +141,14 @@
         plugins.every(plugin => pluginLoader.isPluginEnabled(plugin))
     );
 
-    flush(() => {
-      assert.isTrue(pluginLoader.arePluginsLoaded());
-      assert.isTrue(
-          plugins.every(plugin => pluginLoader.isPluginLoaded(plugin))
-      );
-
-      done();
-    });
+    flush();
+    assert.isTrue(pluginLoader.arePluginsLoaded());
+    assert.isTrue(
+        plugins.every(plugin => pluginLoader.isPluginLoaded(plugin))
+    );
   });
 
-  test('plugins installed mixed result, 1 fail 1 succeed', done => {
+  test('plugins installed mixed result, 1 fail 1 succeed', async () => {
     const plugins = [
       'http://test.com/plugins/foo/static/test.js',
       'http://test.com/plugins/bar/static/test.js',
@@ -179,15 +170,13 @@
 
     pluginLoader.loadPlugins(plugins);
 
-    flush(() => {
-      assert.isTrue(pluginsLoadedStub.calledWithExactly(['bar']));
-      assert.isTrue(pluginLoader.arePluginsLoaded());
-      assert.isTrue(alertStub.calledOnce);
-      done();
-    });
+    await flush();
+    assert.isTrue(pluginsLoadedStub.calledWithExactly(['bar']));
+    assert.isTrue(pluginLoader.arePluginsLoaded());
+    assert.isTrue(alertStub.calledOnce);
   });
 
-  test('isPluginEnabled and isPluginLoaded for mixed results', done => {
+  test('isPluginEnabled and isPluginLoaded for mixed results', async () => {
     const plugins = [
       'http://test.com/plugins/foo/static/test.js',
       'http://test.com/plugins/bar/static/test.js',
@@ -212,17 +201,15 @@
         plugins.every(plugin => pluginLoader.isPluginEnabled(plugin))
     );
 
-    flush(() => {
-      assert.isTrue(pluginsLoadedStub.calledWithExactly(['bar']));
-      assert.isTrue(pluginLoader.arePluginsLoaded());
-      assert.isTrue(alertStub.calledOnce);
-      assert.isTrue(pluginLoader.isPluginLoaded(plugins[1]));
-      assert.isFalse(pluginLoader.isPluginLoaded(plugins[0]));
-      done();
-    });
+    await flush();
+    assert.isTrue(pluginsLoadedStub.calledWithExactly(['bar']));
+    assert.isTrue(pluginLoader.arePluginsLoaded());
+    assert.isTrue(alertStub.calledOnce);
+    assert.isTrue(pluginLoader.isPluginLoaded(plugins[1]));
+    assert.isFalse(pluginLoader.isPluginLoaded(plugins[0]));
   });
 
-  test('plugins installed all failed', done => {
+  test('plugins installed all failed', async () => {
     const plugins = [
       'http://test.com/plugins/foo/static/test.js',
       'http://test.com/plugins/bar/static/test.js',
@@ -242,15 +229,13 @@
 
     pluginLoader.loadPlugins(plugins);
 
-    flush(() => {
-      assert.isTrue(pluginsLoadedStub.calledWithExactly([]));
-      assert.isTrue(pluginLoader.arePluginsLoaded());
-      assert.isTrue(alertStub.calledTwice);
-      done();
-    });
+    await flush();
+    assert.isTrue(pluginsLoadedStub.calledWithExactly([]));
+    assert.isTrue(pluginLoader.arePluginsLoaded());
+    assert.isTrue(alertStub.calledTwice);
   });
 
-  test('plugins installed failed becasue of wrong version', done => {
+  test('plugins installed failed becasue of wrong version', async () => {
     const plugins = [
       'http://test.com/plugins/foo/static/test.js',
       'http://test.com/plugins/bar/static/test.js',
@@ -269,15 +254,13 @@
 
     pluginLoader.loadPlugins(plugins);
 
-    flush(() => {
-      assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo']));
-      assert.isTrue(pluginLoader.arePluginsLoaded());
-      assert.isTrue(alertStub.calledOnce);
-      done();
-    });
+    await flush();
+    assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo']));
+    assert.isTrue(pluginLoader.arePluginsLoaded());
+    assert.isTrue(alertStub.calledOnce);
   });
 
-  test('multiple assets for same plugin installed successfully', done => {
+  test('multiple assets for same plugin installed successfully', async () => {
     sinon.stub(pluginLoader, '_loadJsPlugin').callsFake( url => {
       pluginApi.install(() => void 0, undefined, url);
     });
@@ -291,11 +274,9 @@
     ];
     pluginLoader.loadPlugins(plugins);
 
-    flush(() => {
-      assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo', 'bar']));
-      assert.isTrue(pluginLoader.arePluginsLoaded());
-      done();
-    });
+    await flush();
+    assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo', 'bar']));
+    assert.isTrue(pluginLoader.arePluginsLoaded());
   });
 
   suite('plugin path and url', () => {