Bug 1270078 - Fix AutocompleteResults.get_matching_text() for new awesomebar design. r?adw draft
authorHenrik Skupin <mail@hskupin.info>
Wed, 04 May 2016 18:44:51 +0200
changeset 364788 e47db1f00c0c1c7e44e7f1702bfe61b9dcc04cc1
parent 364787 e8256ae1b2672250240077ccdd6a97557b34526b
child 520386 98f294c1e5164002a4a253de192fbaa11e70167b
push id17563
push userbmo:hskupin@gmail.com
push dateMon, 09 May 2016 10:07:30 +0000
reviewersadw
bugs1270078
milestone49.0a1
Bug 1270078 - Fix AutocompleteResults.get_matching_text() for new awesomebar design. r?adw MozReview-Commit-ID: 1kZX35CVpga
testing/firefox-ui/tests/puppeteer/test_toolbars.py
testing/puppeteer/firefox/firefox_puppeteer/ui/browser/toolbars.py
--- a/testing/firefox-ui/tests/puppeteer/test_toolbars.py
+++ b/testing/firefox-ui/tests/puppeteer/test_toolbars.py
@@ -173,35 +173,38 @@ class TestAutoCompleteResults(FirefoxTes
     def test_force_close(self):
         self.browser.navbar.locationbar.urlbar.send_keys('a')
         Wait(self.marionette).until(lambda _: self.autocomplete_results.is_open)
         # The Wait in the library implementation will fail this if this doesn't
         # end up closing.
         self.autocomplete_results.close(force=True)
 
     def test_matching_text(self):
-        # The default profile always has links to mozilla.org. So multiple results
-        # will be found with 'moz'.
-        input_text = 'moz'
-
-        self.browser.navbar.locationbar.urlbar.send_keys(input_text)
-        Wait(self.marionette).until(lambda _: self.autocomplete_results.is_complete)
-        visible_results = self.autocomplete_results.visible_results
-        self.assertTrue(len(visible_results) > 0)
+        # The default profile always has existing bookmarks. So no sites have to
+        # be visited and bookmarked.
+        for input_text in ('about', 'zilla'):
+            self.browser.navbar.locationbar.urlbar.clear()
+            self.browser.navbar.locationbar.urlbar.send_keys(input_text)
+            Wait(self.marionette).until(lambda _: self.autocomplete_results.is_open)
+            Wait(self.marionette).until(lambda _: self.autocomplete_results.is_complete)
+            visible_results = self.autocomplete_results.visible_results
+            self.assertTrue(len(visible_results) > 0)
 
-        for result in visible_results:
-            # check matching text only for results of type bookmark
-            if result.get_attribute('type') != 'bookmark':
-                continue
-            title_matches = self.autocomplete_results.get_matching_text(result, "title")
-            url_matches = self.autocomplete_results.get_matching_text(result, "url")
-            all_matches = title_matches + url_matches
-            self.assertTrue(len(all_matches) > 0)
-            for match_fragment in all_matches:
-                self.assertIn(match_fragment.lower(), input_text)
+            for result in visible_results:
+                # check matching text only for results of type bookmark
+                if result.get_attribute('type') != 'bookmark':
+                    continue
+                title_matches = self.autocomplete_results.get_matching_text(result, "title")
+                url_matches = self.autocomplete_results.get_matching_text(result, "url")
+                all_matches = title_matches + url_matches
+                self.assertTrue(len(all_matches) > 0)
+                for match_fragment in all_matches:
+                    self.assertIn(match_fragment.lower(), input_text)
+
+            self.autocomplete_results.close()
 
 
 class TestIdentityPopup(FirefoxTestCase):
     def setUp(self):
         FirefoxTestCase.setUp(self)
 
         self.locationbar = self.browser.navbar.locationbar
         self.identity_popup = self.locationbar.identity_popup
--- a/testing/puppeteer/firefox/firefox_puppeteer/ui/browser/toolbars.py
+++ b/testing/puppeteer/firefox/firefox_puppeteer/ui/browser/toolbars.py
@@ -327,55 +327,47 @@ class AutocompleteResults(UIBaseLib):
     def get_matching_text(self, result, match_type):
         """Returns an array of strings of the matching text within an autocomplete
         result in the urlbar.
 
         :param result: The result to inspect for matches.
         :param match_type: The type of match to search for (one of `title` or `url`).
         """
 
-        if match_type == 'title':
-            descnode = self.marionette.execute_script("""
-              return arguments[0].boxObject.firstChild.childNodes[1].childNodes[0];
-            """, script_args=[result])
-        elif match_type == 'url':
-            descnode = self.marionette.execute_script("""
-              return arguments[0].boxObject.lastChild.childNodes[2].childNodes[0];
-            """, script_args=[result])
-        else:
+        if match_type not in ('title', 'url'):
             raise ValueError('match_type provided must be one of'
                              '"title" or "url", not %s' % match_type)
 
-        return self.marionette.execute_script("""
-          let rv = [];
-          for (let node of arguments[0].childNodes) {
-            if (node.nodeName == 'span') {
-              rv.push(node.innerHTML);
-            }
-          }
-          return rv;
-        """, script_args=[descnode])
+        # Search for nodes of the given type with emphasized text
+        emphasized_nodes = result.find_elements(
+            By.ANON_ATTRIBUTE,
+            {'class': 'ac-emphasize-text ac-emphasize-text-%s' % match_type}
+        )
+
+        return [node.get_attribute('textContent') for node in emphasized_nodes]
 
     @property
     def visible_results(self):
         """Supplies the list of visible autocomplete result nodes.
 
         :returns: The list of visible results.
         """
+        match_count = self.element.get_attribute('_matchCount')
+
         return self.marionette.execute_script("""
           let rv = [];
           let node = arguments[0];
-          for (let i = 0; i < node.itemCount; ++i) {
-            let item = node.getItemAtIndex(i);
-            if (!item.hasAttribute("collapsed")) {
-              rv.push(item);
-            }
+          let count = arguments[1];
+
+          for (let i = 0; i < count; ++i) {
+            rv.push(node.getItemAtIndex(i));
           }
+
           return rv;
-        """, script_args=[self.results])
+        """, script_args=[self.results, match_count])
 
     @property
     def is_open(self):
         """Returns whether this popup is currently open.
 
         :returns: True when the popup is open, otherwise false.
         """
         return self.element.get_attribute('state') == 'open'