Commit bad1c19a authored by Bob Silverberg's avatar Bob Silverberg
Browse files

Bug 1397383 - Add loadReplace option to tabs.update, r=mixedpuppy

This adds a loadReplace option into the updateOptions object for tabs.update()
which, when set to true, will cause the loading of the new URL to replace the
current URL in the tab's history.

MozReview-Commit-ID: KZTuEl7cgb0

--HG--
extra : rebase_source : 24f006fe197f56b3102cb00e226d53036b9ba93b
parent 9c1a7b47
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -438,7 +438,10 @@ this.tabs = class extends ExtensionAPI {
              return Promise.reject({message: `Illegal URL: ${url}`});
            }

            nativeTab.linkedBrowser.loadURI(url);
            let flags = updateProperties.loadReplace
              ? Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY
              : Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
            nativeTab.linkedBrowser.loadURIWithFlags(url, {flags});
          }

          if (updateProperties.active !== null) {
+5 −0
Original line number Diff line number Diff line
@@ -748,6 +748,11 @@
                "minimum": 0,
                "optional": true,
                "description": "The ID of the tab that opened this tab. If specified, the opener tab must be in the same window as this tab."
              },
              "loadReplace": {
                "type": "boolean",
                "optional": true,
                "description": "Whether the load should replace the current history entry for the tab."
              }
            }
          },
+75 −0
Original line number Diff line number Diff line
@@ -2,6 +2,11 @@
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

XPCOMUtils.defineLazyModuleGetter(this, "SessionStore",
                                  "resource:///modules/sessionstore/SessionStore.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "TabStateFlusher",
                                  "resource:///modules/sessionstore/TabStateFlusher.jsm");

async function testTabsUpdateURL(existentTabURL, tabsUpdateURL, isErrorExpected) {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
@@ -108,3 +113,73 @@ add_task(async function() {

  info("done");
});

add_task(async function test_update_reload() {
  const URL = "https://example.com/";

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      "permissions": ["tabs", "history"],
    },

    background() {
      browser.test.onMessage.addListener(async (cmd, ...args) => {
        const result = await browser.tabs[cmd](...args);
        browser.test.sendMessage("result", result);
      });

      browser.history.onVisited.addListener(data => {
        browser.test.sendMessage("historyAdded");
      });
    },
  });

  let win = await BrowserTestUtils.openNewBrowserWindow();
  let tabBrowser = win.gBrowser.selectedBrowser;
  await BrowserTestUtils.loadURI(tabBrowser, URL);
  await BrowserTestUtils.browserLoaded(tabBrowser, false, URL);
  let tab = win.gBrowser.selectedTab;

  async function getTabHistory() {
    await TabStateFlusher.flush(tabBrowser);
    return JSON.parse(SessionStore.getTabState(tab));
  }

  await extension.startup();
  extension.sendMessage("query", {url: URL});
  let tabs = await extension.awaitMessage("result");
  let tabId = tabs[0].id;

  let history = await getTabHistory();
  is(history.entries.length, 1,
     "Tab history contains the expected number of entries.");
  is(history.entries[0].url, URL,
    `Tab history contains the expected entry: URL.`);

  extension.sendMessage("update", tabId, {url: `${URL}1/`});
  await Promise.all([
    extension.awaitMessage("result"),
    extension.awaitMessage("historyAdded"),
  ]);

  history = await getTabHistory();
  is(history.entries.length, 2,
     "Tab history contains the expected number of entries.");
  is(history.entries[1].url, `${URL}1/`,
    `Tab history contains the expected entry: ${URL}1/.`);

  extension.sendMessage("update", tabId, {url: `${URL}2/`, loadReplace: true});
  await Promise.all([
    extension.awaitMessage("result"),
    extension.awaitMessage("historyAdded"),
  ]);

  history = await getTabHistory();
  is(history.entries.length, 2,
     "Tab history contains the expected number of entries.");
  is(history.entries[1].url, `${URL}2/`,
    `Tab history contains the expected entry: ${URL}2/.`);

  await extension.unload();
  await BrowserTestUtils.closeWindow(win);
});