summaryrefslogtreecommitdiffstats
path: root/browser/components/touchbar/tests/browser/browser_touchbar_tests.js
blob: f9cacd55b2df4c76804fcedecdd5d3d98224bc19 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

XPCOMUtils.defineLazyServiceGetter(
  this,
  "TouchBarHelper",
  "@mozilla.org/widget/touchbarhelper;1",
  "nsITouchBarHelper"
);

XPCOMUtils.defineLazyServiceGetter(
  this,
  "TouchBarInput",
  "@mozilla.org/widget/touchbarinput;1",
  "nsITouchBarInput"
);

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://example.com"
);

function is_element_visible(aElement, aMsg) {
  isnot(aElement, null, "Element should not be null when checking visibility");
  ok(!BrowserTestUtils.is_hidden(aElement), aMsg);
}

function is_element_hidden(aElement, aMsg) {
  isnot(aElement, null, "Element should not be null when checking visibility");
  ok(BrowserTestUtils.is_hidden(aElement), aMsg);
}

/**
 * Tests if our bookmark button updates with our event.
 */
add_task(async function updateBookmarkButton() {
  // We first check the default state. This also serves the purpose of forcing
  // nsITouchBarHelper to load on Macs without Touch Bars so that it will be
  // listening for "bookmark-icon-updated".
  Assert.equal(
    TouchBarHelper.getTouchBarInput("AddBookmark").image.spec,
    "chrome://browser/skin/bookmark-hollow.svg",
    "AddBookmark image should be unfilled bookmark after event."
  );

  Services.obs.notifyObservers(null, "bookmark-icon-updated", "starred");
  Assert.equal(
    TouchBarHelper.getTouchBarInput("AddBookmark").image.spec,
    "chrome://browser/skin/bookmark.svg",
    "AddBookmark image should be filled bookmark after event."
  );

  Services.obs.notifyObservers(null, "bookmark-icon-updated", "unstarred");
  Assert.equal(
    TouchBarHelper.getTouchBarInput("AddBookmark").image.spec,
    "chrome://browser/skin/bookmark-hollow.svg",
    "AddBookmark image should be unfilled bookmark after event."
  );
});

/**
 * Tests if our Reader View button updates when a page can be reader viewed.
 */
add_task(async function updateReaderView() {
  const PREF_READERMODE = "reader.parse-on-load.enabled";
  await SpecialPowers.pushPrefEnv({ set: [[PREF_READERMODE, true]] });

  // The page actions reader mode button
  var readerButton = document.getElementById("reader-mode-button");
  is_element_hidden(readerButton, "Reader Mode button should be hidden.");

  Assert.equal(
    TouchBarHelper.getTouchBarInput("ReaderView").disabled,
    true,
    "ReaderView Touch Bar button should be disabled by default."
  );

  let url = TEST_PATH + "readerModeArticle.html";
  await BrowserTestUtils.withNewTab(url, async function () {
    await BrowserTestUtils.waitForCondition(() => !readerButton.hidden);

    Assert.equal(
      TouchBarHelper.getTouchBarInput("ReaderView").disabled,
      false,
      "ReaderView Touch Bar button should be enabled on reader-able pages."
    );
  });
});

add_task(async function updateMainButtonInFullscreen() {
  Assert.equal(
    TouchBarHelper.getTouchBarInput("OpenLocation").image.spec,
    "chrome://global/skin/icons/search-glass.svg",
    "OpenLocation should be displaying the search glass icon."
  );
  BrowserTestUtils.loadURIString(
    gBrowser.selectedBrowser,
    TEST_PATH + "video_test.html"
  );
  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  let entered = waitForFullScreenState(gBrowser.selectedBrowser, true);
  // Fullscreen video must be triggered from a user input handler so the video
  // page contains a script to enter fullscreen on Enter instead of us calling
  // requestFullscreen directly here.
  EventUtils.synthesizeKey("KEY_Enter");
  await entered;
  Assert.equal(
    TouchBarHelper.getTouchBarInput("OpenLocation").image.spec,
    "chrome://browser/skin/fullscreen-exit.svg",
    "OpenLocation should be displaying the exit fullscreen icon."
  );
  let exited = waitForFullScreenState(gBrowser.selectedBrowser, false);
  EventUtils.synthesizeKey("KEY_Enter");
  await exited;
  Assert.equal(
    TouchBarHelper.getTouchBarInput("OpenLocation").image.spec,
    "chrome://global/skin/icons/search-glass.svg",
    "OpenLocation should be displaying the search glass icon."
  );
});

add_task(async function toggleUrlbarFocusOnOpenLocation() {
  Assert.equal(TouchBarHelper.isUrlbarFocused, false, "Urlbar is unfocused.");
  TouchBarHelper.toggleFocusUrlbar();
  Assert.equal(TouchBarHelper.isUrlbarFocused, true, "Urlbar is unfocused.");
  TouchBarHelper.toggleFocusUrlbar();
});

add_task(async function unfocusUrlbar() {
  window.gURLBar.focus();
  Assert.equal(TouchBarHelper.isUrlbarFocused, true, "Urlbar is unfocused.");
  TouchBarHelper.unfocusUrlbar();
  Assert.equal(TouchBarHelper.isUrlbarFocused, false, "Urlbar is unfocused.");
});

function waitForFullScreenState(browser, state) {
  info("inside waitforfullscreenstate");
  return new Promise(resolve => {
    let eventReceived = false;

    let observe = (subject, topic, data) => {
      if (!eventReceived) {
        return;
      }
      Services.obs.removeObserver(observe, "fullscreen-painted");
      resolve();
    };
    Services.obs.addObserver(observe, "fullscreen-painted");

    window.addEventListener(
      `MozDOMFullscreen:${state ? "Entered" : "Exited"}`,
      () => {
        eventReceived = true;
      },
      { once: true }
    );
  });
}