summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_window_reload_target.js
blob: 9f3339728b91949e43abb2e71b8bbaefd86d64b3 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

requestLongerTimeout(10);

const TEST_URL = "data:text/html;charset=utf-8," +
                 "<html><head><title>Test reload</title></head>" +
                 "<body><h1>Testing reload from devtools</h1></body></html>";

var {Toolbox} = require("devtools/client/framework/toolbox");

const {LocalizationHelper} = require("devtools/shared/l10n");
const L10N = new LocalizationHelper("devtools/client/locales/toolbox.properties");

var target, toolbox, description, reloadsSent, toolIDs;

function test() {
  addTab(TEST_URL).then(() => {
    target = TargetFactory.forTab(gBrowser.selectedTab);

    target.makeRemote().then(() => {
      toolIDs = gDevTools.getToolDefinitionArray()
                  .filter(def => def.isTargetSupported(target))
                  .map(def => def.id);
      gDevTools.showToolbox(target, toolIDs[0], Toolbox.HostType.BOTTOM)
               .then(startReloadTest);
    });
  });
}

function startReloadTest(aToolbox) {
  getFrameScript(); // causes frame-script-utils to be loaded into the child.
  toolbox = aToolbox;

  reloadsSent = 0;
  let reloads = 0;
  let reloadCounter = (msg) => {
    reloads++;
    info("Detected reload #" + reloads);
    is(reloads, reloadsSent, "Reloaded from devtools window once and only for " + description + "");
  };
  gBrowser.selectedBrowser.messageManager.addMessageListener("devtools:test:load", reloadCounter);

  testAllTheTools("docked", () => {
    let origHostType = toolbox.hostType;
    toolbox.switchHost(Toolbox.HostType.WINDOW).then(() => {
      toolbox.win.focus();
      testAllTheTools("undocked", () => {
        toolbox.switchHost(origHostType).then(() => {
          gBrowser.selectedBrowser.messageManager.removeMessageListener("devtools:test:load", reloadCounter);
          // If we finish too early, the inspector breaks promises:
          toolbox.getPanel("inspector").once("new-root", finishUp);
        });
      });
    });
  }, toolIDs.length - 1 /* only test 1 tool in docked mode, to cut down test time */);
}

function testAllTheTools(docked, callback, toolNum = 0) {
  if (toolNum >= toolIDs.length) {
    return callback();
  }
  toolbox.selectTool(toolIDs[toolNum]).then(() => {
    testReload("toolbox.reload.key", docked, toolIDs[toolNum], () => {
      testReload("toolbox.reload2.key", docked, toolIDs[toolNum], () => {
        testReload("toolbox.forceReload.key", docked, toolIDs[toolNum], () => {
          testReload("toolbox.forceReload2.key", docked, toolIDs[toolNum], () => {
            testAllTheTools(docked, callback, toolNum + 1);
          });
        });
      });
    });
  });
}

function testReload(shortcut, docked, toolID, callback) {
  let complete = () => {
    gBrowser.selectedBrowser.messageManager.removeMessageListener("devtools:test:load", complete);
    return callback();
  };
  gBrowser.selectedBrowser.messageManager.addMessageListener("devtools:test:load", complete);

  description = docked + " devtools with tool " + toolID + ", shortcut #" + shortcut;
  info("Testing reload in " + description);
  toolbox.win.focus();
  synthesizeKeyShortcut(L10N.getStr(shortcut), toolbox.win);
  reloadsSent++;
}

function finishUp() {
  toolbox.destroy().then(() => {
    gBrowser.removeCurrentTab();

    target = toolbox = description = reloadsSent = toolIDs = null;

    finish();
  });
}