summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_homeDrop.js
blob: e4afa03dead3b54769e1a6b57e19c79424449668 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_task(async function setupHomeButton() {
  // Put the home button in the pre-proton placement to test focus states.
  CustomizableUI.addWidgetToArea(
    "home-button",
    "nav-bar",
    CustomizableUI.getPlacementOfWidget("stop-reload-button").position + 1
  );
  CustomizableUI.addWidgetToArea("sidebar-button", "nav-bar");
  registerCleanupFunction(async function resetToolbar() {
    await CustomizableUI.reset();
  });
});

add_task(async function() {
  let HOMEPAGE_PREF = "browser.startup.homepage";

  await pushPrefs([HOMEPAGE_PREF, "about:mozilla"]);

  let EventUtils = {};
  Services.scriptloader.loadSubScript(
    "chrome://mochikit/content/tests/SimpleTest/EventUtils.js",
    EventUtils
  );

  // Since synthesizeDrop triggers the srcElement, need to use another button
  // that should be visible.
  let dragSrcElement = document.getElementById("sidebar-button");
  ok(dragSrcElement, "Sidebar button exists");
  let homeButton = document.getElementById("home-button");
  ok(homeButton, "home button present");

  async function drop(dragData, homepage) {
    let setHomepageDialogPromise = BrowserTestUtils.promiseAlertDialogOpen(
      "accept"
    );
    let setHomepagePromise = TestUtils.waitForPrefChange(
      HOMEPAGE_PREF,
      newVal => newVal == homepage
    );

    EventUtils.synthesizeDrop(
      dragSrcElement,
      homeButton,
      dragData,
      "copy",
      window
    );

    // Ensure dnd suppression is cleared.
    EventUtils.synthesizeMouseAtCenter(homeButton, { type: "mouseup" }, window);

    await setHomepageDialogPromise;
    ok(true, "dialog appeared in response to home button drop");

    await setHomepagePromise;

    let modified = Services.prefs.getStringPref(HOMEPAGE_PREF);
    is(modified, homepage, "homepage is set correctly");
    Services.prefs.setStringPref(HOMEPAGE_PREF, "about:mozilla;");
  }

  function dropInvalidURI() {
    return new Promise(resolve => {
      let consoleListener = {
        observe(m) {
          if (m.message.includes("NS_ERROR_DOM_BAD_URI")) {
            ok(true, "drop was blocked");
            resolve();
          }
        },
      };
      Services.console.registerListener(consoleListener);
      registerCleanupFunction(function() {
        Services.console.unregisterListener(consoleListener);
      });

      executeSoon(function() {
        info("Attempting second drop, of a javascript: URI");
        // The drop handler throws an exception when dragging URIs that inherit
        // principal, e.g. javascript:
        expectUncaughtException();
        EventUtils.synthesizeDrop(
          dragSrcElement,
          homeButton,
          [[{ type: "text/plain", data: "javascript:8888" }]],
          "copy",
          window
        );
        // Ensure dnd suppression is cleared.
        EventUtils.synthesizeMouseAtCenter(
          homeButton,
          { type: "mouseup" },
          window
        );
      });
    });
  }

  await drop(
    [[{ type: "text/plain", data: "http://mochi.test:8888/" }]],
    "http://mochi.test:8888/"
  );
  await drop(
    [
      [
        {
          type: "text/plain",
          data:
            "http://mochi.test:8888/\nhttp://mochi.test:8888/b\nhttp://mochi.test:8888/c",
        },
      ],
    ],
    "http://mochi.test:8888/|http://mochi.test:8888/b|http://mochi.test:8888/c"
  );
  await dropInvalidURI();
});