summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/zoom/browser_zoom_commands.js
blob: 88b6f4205969b41259fccef1cb0bba7a8d5a502b (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_PAGE_URL =
  "data:text/html;charset=utf-8,<body>test_zoom_levels</body>";

/**
 * Waits for the zoom commands in the window to have the expected enabled
 * state.
 *
 * @param {Object} expectedState
 *   An object where each key represents one of the zoom commands,
 *   and the value is a boolean that is true if the command should
 *   be enabled, and false if it should be disabled.
 *
 *   The keys are "enlarge", "reduce" and "reset" for readability,
 *   and internally this function maps those keys to the appropriate
 *   commands.
 * @returns Promise
 * @resolves undefined
 */
async function waitForCommandEnabledState(expectedState) {
  const COMMAND_MAP = {
    enlarge: "cmd_fullZoomEnlarge",
    reduce: "cmd_fullZoomReduce",
    reset: "cmd_fullZoomReset",
  };

  await TestUtils.waitForCondition(() => {
    for (let commandKey in expectedState) {
      let commandID = COMMAND_MAP[commandKey];
      let command = document.getElementById(commandID);
      let expectedEnabled = expectedState[commandKey];

      if (command.hasAttribute("disabled") == expectedEnabled) {
        return false;
      }
    }
    Assert.ok("Commands finally reached the expected state.");
    return true;
  }, "Waiting for commands to reach the right state.");
}

/**
 * Tests that the "Zoom Text Only" command is in the right checked
 * state.
 *
 * @param {boolean} isChecked
 *   True if the command should have its "checked" attribute set to
 *   "true". Otherwise, ensures that the attribute is set to "false".
 */
function assertTextZoomCommandCheckedState(isChecked) {
  let command = document.getElementById("cmd_fullZoomToggle");
  Assert.equal(
    command.getAttribute("checked"),
    "" + isChecked,
    "Text zoom command has expected checked attribute"
  );
}

/**
 * Tests that zoom commands are properly updated when changing
 * zoom levels and/or preferences on an individual browser.
 */
add_task(async function test_update_browser_zoom() {
  await BrowserTestUtils.withNewTab(TEST_PAGE_URL, async browser => {
    let currentZoom = await FullZoomHelper.getGlobalValue();
    Assert.equal(
      currentZoom,
      1,
      "We expect to start at the default zoom level."
    );

    await waitForCommandEnabledState({
      enlarge: true,
      reduce: true,
      reset: false,
    });
    assertTextZoomCommandCheckedState(false);

    // We'll run two variations of this test - one with text zoom enabled,
    // and the other without.
    for (let textZoom of [true, false]) {
      info(`Running variation with textZoom set to ${textZoom}`);

      await SpecialPowers.pushPrefEnv({
        set: [["browser.zoom.full", !textZoom]],
      });

      // 120% global zoom
      info("Changing default zoom by a single level");
      ZoomManager.zoom = 1.2;

      await waitForCommandEnabledState({
        enlarge: true,
        reduce: true,
        reset: true,
      });
      await assertTextZoomCommandCheckedState(textZoom);

      // Now max out the zoom level.
      ZoomManager.zoom = ZoomManager.MAX;

      await waitForCommandEnabledState({
        enlarge: false,
        reduce: true,
        reset: true,
      });
      await assertTextZoomCommandCheckedState(textZoom);

      // Now min out the zoom level.
      ZoomManager.zoom = ZoomManager.MIN;
      await waitForCommandEnabledState({
        enlarge: true,
        reduce: false,
        reset: true,
      });
      await assertTextZoomCommandCheckedState(textZoom);

      // Now reset back to the default zoom level
      ZoomManager.zoom = 1;
      await waitForCommandEnabledState({
        enlarge: true,
        reduce: true,
        reset: false,
      });
      await assertTextZoomCommandCheckedState(textZoom);
    }
  });
});

/**
 * Tests that zoom commands are properly updated when changing
 * zoom levels when the default zoom is not at 1.0.
 */
add_task(async function test_update_browser_zoom() {
  await BrowserTestUtils.withNewTab(TEST_PAGE_URL, async browser => {
    let currentZoom = await FullZoomHelper.getGlobalValue();
    Assert.equal(
      currentZoom,
      1,
      "We expect to start at the default zoom level."
    );

    // Now change the default zoom to 200%, which is what we'll switch
    // back to when choosing to reset the zoom level.
    //
    // It's a bit maddening that changeDefaultZoom takes values in integer
    // units from 30 to 500, whereas ZoomManager.zoom takes things in float
    // units from 0.3 to 5.0, but c'est la vie for now.
    await FullZoomHelper.changeDefaultZoom(200);
    registerCleanupFunction(async () => {
      await FullZoomHelper.changeDefaultZoom(100);
    });

    await waitForCommandEnabledState({
      enlarge: true,
      reduce: true,
      reset: false,
    });

    // 120% global zoom
    info("Changing default zoom by a single level");
    ZoomManager.zoom = 2.2;

    await waitForCommandEnabledState({
      enlarge: true,
      reduce: true,
      reset: true,
    });
    await assertTextZoomCommandCheckedState(false);

    // Now max out the zoom level.
    ZoomManager.zoom = ZoomManager.MAX;

    await waitForCommandEnabledState({
      enlarge: false,
      reduce: true,
      reset: true,
    });
    await assertTextZoomCommandCheckedState(false);

    // Now min out the zoom level.
    ZoomManager.zoom = ZoomManager.MIN;
    await waitForCommandEnabledState({
      enlarge: true,
      reduce: false,
      reset: true,
    });
    await assertTextZoomCommandCheckedState(false);

    // Now reset back to the default zoom level
    ZoomManager.zoom = 2;
    await waitForCommandEnabledState({
      enlarge: true,
      reduce: true,
      reset: false,
    });
    await assertTextZoomCommandCheckedState(false);
  });
});