tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_noUpdateResultsFromOtherProviders.js (4002B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // This test makes sure that when the view updates itself it doesn't try to
      5 // update a row with a new result from a different provider. We avoid that
      6 // because it's a cause of results flickering.
      7 
      8 "use strict";
      9 
     10 add_task(async function test() {
     11  // This slow provider is used to delay the end of the query.
     12  let slowProvider = new UrlbarTestUtils.TestProvider({
     13    results: [],
     14    priority: 10,
     15    addTimeout: 1000,
     16  });
     17 
     18  // We'll run a first query with this provider to generate results, that should
     19  // be overriden by results from the second provider.
     20  let firstProvider = new UrlbarTestUtils.TestProvider({
     21    results: [
     22      new UrlbarResult({
     23        type: UrlbarUtils.RESULT_TYPE.URL,
     24        source: UrlbarUtils.RESULT_SOURCE.HISTORY,
     25        payload: { url: "https://mozilla.org/c" },
     26      }),
     27      new UrlbarResult({
     28        type: UrlbarUtils.RESULT_TYPE.URL,
     29        source: UrlbarUtils.RESULT_SOURCE.HISTORY,
     30        payload: { url: "https://mozilla.org/d" },
     31      }),
     32    ],
     33    priority: 10,
     34  });
     35 
     36  // Then we'll run a second query with this provider, the results should not
     37  // immediately replace the ones from the first provider, but rather be
     38  // appended, until the query is done or the stale timer elapses.
     39  let secondProvider = new UrlbarTestUtils.TestProvider({
     40    results: [
     41      new UrlbarResult({
     42        type: UrlbarUtils.RESULT_TYPE.URL,
     43        source: UrlbarUtils.RESULT_SOURCE.HISTORY,
     44        payload: { url: "https://mozilla.org/c" },
     45      }),
     46      new UrlbarResult({
     47        type: UrlbarUtils.RESULT_TYPE.URL,
     48        source: UrlbarUtils.RESULT_SOURCE.HISTORY,
     49        payload: { url: "https://mozilla.org/d" },
     50      }),
     51    ],
     52    priority: 10,
     53  });
     54 
     55  UrlbarProvidersManager.registerProvider(slowProvider);
     56  UrlbarProvidersManager.registerProvider(firstProvider);
     57  function cleanup() {
     58    UrlbarProvidersManager.unregisterProvider(slowProvider);
     59    UrlbarProvidersManager.unregisterProvider(firstProvider);
     60    UrlbarProvidersManager.unregisterProvider(secondProvider);
     61  }
     62  registerCleanupFunction(cleanup);
     63 
     64  // Execute the first query.
     65  await UrlbarTestUtils.promiseAutocompleteResultPopup({
     66    window,
     67    value: "moz",
     68  });
     69 
     70  // Now run the second query but don't wait for it to finish, we want to
     71  // observe the view contents along the way.
     72  UrlbarProvidersManager.unregisterProvider(firstProvider);
     73  UrlbarProvidersManager.registerProvider(secondProvider);
     74  let hasAtLeast4Children = BrowserTestUtils.waitForMutationCondition(
     75    UrlbarTestUtils.getResultsContainer(window),
     76    { childList: true },
     77    () => UrlbarTestUtils.getResultCount(window) == 4
     78  );
     79  let queryPromise = UrlbarTestUtils.promiseAutocompleteResultPopup({
     80    window,
     81    value: "moz",
     82  });
     83  await hasAtLeast4Children;
     84  // At this point we have the old results marked as "stale", and the new ones.
     85  Assert.equal(
     86    UrlbarTestUtils.getResultCount(window),
     87    4,
     88    "There should be 4 results"
     89  );
     90  Assert.ok(
     91    UrlbarTestUtils.getRowAt(window, 0).hasAttribute("stale"),
     92    "Should be stale"
     93  );
     94  Assert.ok(
     95    UrlbarTestUtils.getRowAt(window, 1).hasAttribute("stale"),
     96    "Should be stale"
     97  );
     98  Assert.ok(
     99    !UrlbarTestUtils.getRowAt(window, 2).hasAttribute("stale"),
    100    "Should not be stale"
    101  );
    102  Assert.ok(
    103    !UrlbarTestUtils.getRowAt(window, 3).hasAttribute("stale"),
    104    "Should not be stale"
    105  );
    106 
    107  // Now wait for the query end, this should remove stale results.
    108  await queryPromise;
    109 
    110  Assert.equal(
    111    UrlbarTestUtils.getResultCount(window),
    112    2,
    113    "There should be 2 results"
    114  );
    115  Assert.ok(
    116    !UrlbarTestUtils.getRowAt(window, 0).hasAttribute("stale"),
    117    "Should not be stale"
    118  );
    119  Assert.ok(
    120    !UrlbarTestUtils.getRowAt(window, 1).hasAttribute("stale"),
    121    "Should not be stale"
    122  );
    123 
    124  await UrlbarTestUtils.promisePopupClose(window);
    125  gURLBar.handleRevert();
    126 
    127  cleanup();
    128 });