summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_page_style_menu_update.js
blob: a0c741e484f6c5e7ba377518381972fba0c52680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"use strict";

const PAGE = "http://example.com/browser/browser/base/content/test/general/page_style_sample.html";

/**
 * Stylesheets are updated for a browser after the pageshow event.
 * This helper function returns a Promise that waits for that pageshow
 * event, and then resolves on the next tick to ensure that gPageStyleMenu
 * has had a chance to update the stylesheets.
 *
 * @param browser
 *        The <xul:browser> to wait for.
 * @return Promise
 */
function promiseStylesheetsUpdated(browser) {
  return ContentTask.spawn(browser, { PAGE }, function*(args) {
    return new Promise((resolve) => {
      addEventListener("pageshow", function onPageShow(e) {
        if (e.target.location == args.PAGE) {
          removeEventListener("pageshow", onPageShow);
          content.setTimeout(resolve, 0);
        }
      });
    })
  });
}

/**
 * Tests that the Page Style menu shows the currently
 * selected Page Style after a new one has been selected.
 */
add_task(function*() {
  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank", false);
  let browser = tab.linkedBrowser;

  yield BrowserTestUtils.loadURI(browser, PAGE);
  yield promiseStylesheetsUpdated(browser);

  let menupopup = document.getElementById("pageStyleMenu").menupopup;
  gPageStyleMenu.fillPopup(menupopup);

  // page_style_sample.html should default us to selecting the stylesheet
  // with the title "6" first.
  let selected = menupopup.querySelector("menuitem[checked='true']");
  is(selected.getAttribute("label"), "6", "Should have '6' stylesheet selected by default");

  // Now select stylesheet "1"
  let target = menupopup.querySelector("menuitem[label='1']");
  target.click();

  // Now we need to wait for the content process to send its stylesheet
  // update for the selected tab to the parent. Because messages are
  // guaranteed to be sent in order, we'll make sure we do the check
  // after the parent has been updated by yielding until the child
  // has finished running a ContentTask for us.
  yield ContentTask.spawn(browser, {}, function*() {
    dump('\nJust wasting some time.\n');
  });

  gPageStyleMenu.fillPopup(menupopup);
  // gPageStyleMenu empties out the menu between opens, so we need
  // to get a new reference to the selected menuitem
  selected = menupopup.querySelector("menuitem[checked='true']");
  is(selected.getAttribute("label"), "1", "Should now have stylesheet 1 selected");

  yield BrowserTestUtils.removeTab(tab);
});