summaryrefslogtreecommitdiffstats
path: root/toolkit/components/tooltiptext/tests/browser_input_file_tooltips.js
blob: a1323095d4b99061dfd3248b01f61a9e60dcb72d (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

let tempFile;
add_task(function* setup() {
  yield new Promise(resolve => {
    SpecialPowers.pushPrefEnv({"set": [["ui.tooltipDelay", 0]]}, resolve);
  });
  tempFile = createTempFile();
  registerCleanupFunction(function() {
    tempFile.remove(true);
  });
});

add_task(function* test_singlefile_selected() {
  yield do_test({value: true, result: "testfile_bug1251809"});
});

add_task(function* test_title_set() {
  yield do_test({title: "foo", result: "foo"});
});

add_task(function* test_nofile_selected() {
  yield do_test({result: "No file selected."});
});

add_task(function* test_multipleset_nofile_selected() {
  yield do_test({multiple: true, result: "No files selected."});
});

add_task(function* test_requiredset() {
  yield do_test({required: true, result: "Please select a file."});
});

function* do_test(test) {
  info(`starting test ${JSON.stringify(test)}`);

  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser);

  info("Moving mouse out of the way.");
  yield new Promise(resolve => {
    EventUtils.synthesizeNativeMouseMove(tab.linkedBrowser, 300, 300, resolve);
  });

  info("creating input field");
  yield ContentTask.spawn(tab.linkedBrowser, test, function*(test) {
    let doc = content.document;
    let input = doc.createElement("input");
    doc.body.appendChild(input);
    input.id = "test_input";
    input.setAttribute("style", "position: absolute; top: 0; left: 0;");
    input.type = "file";
    if (test.title) {
      input.setAttribute("title", test.title);
    }
    if (test.multiple) {
      input.multiple = true;
    }
    if (test.required) {
      input.required = true;
    }
  });

  if (test.value) {
    info("Creating mock filepicker to select files");
    let MockFilePicker = SpecialPowers.MockFilePicker;
    MockFilePicker.init(window);
    MockFilePicker.returnValue = MockFilePicker.returnOK;
    MockFilePicker.displayDirectory = FileUtils.getDir("TmpD", [], false);
    MockFilePicker.returnFiles = [tempFile];

    try {
      // Open the File Picker dialog (MockFilePicker) to select
      // the files for the test.
      yield BrowserTestUtils.synthesizeMouseAtCenter("#test_input", {}, tab.linkedBrowser);
      info("Waiting for the input to have the requisite files");
      yield ContentTask.spawn(tab.linkedBrowser, {}, function*() {
        let input = content.document.querySelector("#test_input");
        yield ContentTaskUtils.waitForCondition(() => input.files.length,
          "The input should have at least one file selected");
        info(`The input has ${input.files.length} file(s) selected.`);
      });
    } finally {
      MockFilePicker.cleanup();
    }
  } else {
    info("No real file selection required.");
  }

  let awaitTooltipOpen = new Promise(resolve => {
    let tooltipId = Services.appinfo.browserTabsRemoteAutostart ?
                      "remoteBrowserTooltip" :
                      "aHTMLTooltip";
    let tooltip = document.getElementById(tooltipId);
    tooltip.addEventListener("popupshown", function onpopupshown(event) {
      tooltip.removeEventListener("popupshown", onpopupshown);
      resolve(event.target);
    });
  });
  info("Initial mouse move");
  yield new Promise(resolve => {
    EventUtils.synthesizeNativeMouseMove(tab.linkedBrowser, 50, 5, resolve);
  });
  info("Waiting");
  yield new Promise(resolve => setTimeout(resolve, 400));
  info("Second mouse move");
  yield new Promise(resolve => {
    EventUtils.synthesizeNativeMouseMove(tab.linkedBrowser, 70, 5, resolve);
  });
  info("Waiting for tooltip to open");
  let tooltip = yield awaitTooltipOpen;

  is(tooltip.getAttribute("label"), test.result, "tooltip label should match expectation");

  info("Closing tab");
  yield BrowserTestUtils.removeTab(tab);
}

function createTempFile() {
  let file = FileUtils.getDir("TmpD", [], false);
  file.append("testfile_bug1251809");
  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
  return file;
}