summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/test/browser_inspector_portrait_mode.js
blob: c4734645c32d3e227640567f2e100d26d20111fd (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
/* 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/. */

"use strict";

// Test that the inspector splitter is properly initialized in horizontal mode if the
// inspector starts in portrait mode.

add_task(async function () {
  let { inspector, toolbox } = await openInspectorForURL(
    "data:text/html;charset=utf-8,<h1>foo</h1><span>bar</span>",
    "window"
  );

  const hostWindow = toolbox.win.parent;
  const originalWidth = hostWindow.outerWidth;
  const originalHeight = hostWindow.outerHeight;

  let splitter = inspector.panelDoc.querySelector(
    ".inspector-sidebar-splitter"
  );

  // If the inspector is not already in landscape mode.
  if (!splitter.classList.contains("vert")) {
    info("Resize toolbox window to force inspector to landscape mode");
    const onClassnameMutation = waitForClassMutation(splitter);
    hostWindow.resizeTo(800, 500);
    await onClassnameMutation;

    ok(splitter.classList.contains("vert"), "Splitter is in vertical mode");
  }

  info("Resize toolbox window to force inspector to portrait mode");
  const onClassnameMutation = waitForClassMutation(splitter);
  hostWindow.resizeTo(500, 500);
  await onClassnameMutation;

  ok(splitter.classList.contains("horz"), "Splitter is in horizontal mode");

  info("Close the inspector");
  await toolbox.destroy();

  info("Reopen inspector");
  ({ inspector, toolbox } = await openInspector("window"));

  // Devtools window should still be 500px * 500px, inspector should still be in portrait.
  splitter = inspector.panelDoc.querySelector(".inspector-sidebar-splitter");
  ok(splitter.classList.contains("horz"), "Splitter is in horizontal mode");

  info("Restore original window size");
  toolbox.win.parent.resizeTo(originalWidth, originalHeight);
});

/**
 * Helper waiting for a class attribute mutation on the provided target. Returns a
 * promise.
 *
 * @param {Node} target
 *         Node to observe
 * @return {Promise} promise that will resolve upon receiving a mutation for the class
 *         attribute on the target.
 */
function waitForClassMutation(target) {
  return new Promise(resolve => {
    const observer = new MutationObserver(mutations => {
      for (const mutation of mutations) {
        if (mutation.attributeName === "class") {
          observer.disconnect();
          resolve();
          return;
        }
      }
    });
    observer.observe(target, { attributes: true });
  });
}

registerCleanupFunction(function () {
  // Restore the host type for other tests.
  Services.prefs.clearUserPref("devtools.toolbox.host");
});