Fix bug in autocomplete focus detection

https://goo.gl/j4Lr5w enabled conditional showing of autocomplete
suggestions based on focus of the autocomplete's child input. One case
in which the input is focused programmatically as opposed to through UI
interaction fell through the cracks.
This change addresses that case.

Change-Id: I24515557c3cf9af164991829746d597a2b769c84
diff --git a/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete.html b/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete.html
index 97a4f3d..32c7c70 100644
--- a/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete.html
+++ b/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete.html
@@ -55,7 +55,8 @@
         bind-value="{{text}}"
         placeholder="[[placeholder]]"
         on-keydown="_handleInputKeydown"
-        on-focus="_updateSuggestions"
+        on-focus="_onInputFocus"
+        on-blur="_onInputBlur"
         autocomplete="off" />
     <div
         id="suggestions"
diff --git a/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete.js b/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete.js
index 45d55eb..16ba150 100644
--- a/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete.js
+++ b/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete.js
@@ -117,14 +117,6 @@
 
     },
 
-    attached: function() {
-      this.listen(document.body, 'click', '_handleBodyClick');
-    },
-
-    detached: function() {
-      this.unlisten(document.body, 'click', '_handleBodyClick');
-    },
-
     get focusStart() {
       return this.$.input;
     },
@@ -147,6 +139,15 @@
       this._disableSuggestions = false;
     },
 
+    _onInputFocus: function() {
+      this._focused = true;
+      this._updateSuggestions();
+    },
+
+    _onInputBlur: function() {
+      this._focused = false;
+    },
+
     _updateSuggestions: function() {
       if (!this.text || this._disableSuggestions) { return; }
       if (this.text.length < this.threshold) {
@@ -228,18 +229,6 @@
       }
     },
 
-    _handleBodyClick: function(e) {
-      var eventPath = Polymer.dom(e).path;
-      for (var i = 0; i < eventPath.length; i++) {
-        if (eventPath[i] == this) {
-          this._focused = true;
-          return;
-        }
-      }
-      this._suggestions = [];
-      this._focused = false;
-    },
-
     _handleSuggestionTap: function(e) {
       this.$.cursor.setCursor(e.target);
       this._commit();
diff --git a/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete_test.html b/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete_test.html
index c87521e..abf052c 100644
--- a/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-autocomplete/gr-autocomplete_test.html
@@ -266,6 +266,15 @@
       assert.isTrue(commitHandler.called);
     });
 
+    test('_focused flag properly triggered', function() {
+      flushAsynchronousOperations();
+      assert.isFalse(element._focused);
+      element.$.input.focus();
+      assert.isTrue(element._focused);
+      element.$.input.blur();
+      assert.isFalse(element._focused);
+    });
+
     test('_focused flag shows/hides the suggestions', function() {
       var suggestions = ['hello', 'its me'];
       assert.isTrue(element._computeSuggestionsHidden(suggestions, false));