summaryrefslogtreecommitdiff
path: root/browser/base/content/test/browser_tab_dragdrop.js
blob: 04e4acdd7fa043541ffeddeeecb6f9654f88cf5b (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
function test()
{
  var embed = '<embed type="application/x-test" allowscriptaccess="always" allowfullscreen="true" wmode="window" width="640" height="480"></embed>'

  waitForExplicitFinish();

  // create a few tabs
  var tabs = [
    gBrowser.tabs[0],
    gBrowser.addTab("about:blank", {skipAnimation: true}),
    gBrowser.addTab("about:blank", {skipAnimation: true}),
    gBrowser.addTab("about:blank", {skipAnimation: true}),
    gBrowser.addTab("about:blank", {skipAnimation: true})
  ];

  function setLocation(i, url) {
    gBrowser.getBrowserForTab(tabs[i]).contentWindow.location = url;
  }
  function moveTabTo(a, b) {
    gBrowser.swapBrowsersAndCloseOther(gBrowser.tabs[b], gBrowser.tabs[a]);
  }
  function clickTest(doc, win) {
    var clicks = doc.defaultView.clicks;
    EventUtils.synthesizeMouseAtCenter(doc.body, {}, win);
    is(doc.defaultView.clicks, clicks+1, "adding 1 more click on BODY");
  }
  function test1() {
    moveTabTo(2, 3); // now: 0 1 2 4
    is(gBrowser.tabs[1], tabs[1], "tab1");
    is(gBrowser.tabs[2], tabs[3], "tab3");

    var plugin = gBrowser.getBrowserForTab(tabs[4]).docShell.contentViewer.DOMDocument.wrappedJSObject.body.firstChild;
    var tab4_plugin_object = plugin.getObjectValue();

    gBrowser.selectedTab = gBrowser.tabs[2];
    moveTabTo(3, 2); // now: 0 1 4
    gBrowser.selectedTab = tabs[4];
    var doc = gBrowser.getBrowserForTab(gBrowser.tabs[2]).docShell.contentViewer.DOMDocument.wrappedJSObject;
    plugin = doc.body.firstChild;
    ok(plugin && plugin.checkObjectValue(tab4_plugin_object), "same plugin instance");
    is(gBrowser.tabs[1], tabs[1], "tab1");
    is(gBrowser.tabs[2], tabs[3], "tab4");
    is(doc.defaultView.clicks, 0, "no click on BODY so far");
    clickTest(doc, window);

    moveTabTo(2, 1); // now: 0 4
    is(gBrowser.tabs[1], tabs[1], "tab1");
    doc = gBrowser.getBrowserForTab(gBrowser.tabs[1]).docShell.contentViewer.DOMDocument.wrappedJSObject;
    plugin = doc.body.firstChild;
    ok(plugin && plugin.checkObjectValue(tab4_plugin_object), "same plugin instance");
    clickTest(doc, window);

    // Load a new document (about:blank) in tab4, then detach that tab into a new window.
    // In the new window, navigate back to the original document and click on its <body>,
    // verify that its onclick was called.
    var t = tabs[1];
    var b = gBrowser.getBrowserForTab(t);
    gBrowser.selectedTab = t;
    b.addEventListener("load", function() {
      b.removeEventListener("load", arguments.callee, true);

      executeSoon(function () {
        var win = gBrowser.replaceTabWithWindow(t);
        whenDelayedStartupFinished(win, function () {
          // Verify that the original window now only has the initial tab left in it.
          is(gBrowser.tabs[0], tabs[0], "tab0");
          is(gBrowser.getBrowserForTab(gBrowser.tabs[0]).contentWindow.location, "about:blank", "tab0 uri");

          executeSoon(function () {
            win.gBrowser.addEventListener("pageshow", function () {
              win.gBrowser.removeEventListener("pageshow", arguments.callee, false);
              executeSoon(function () {
                t = win.gBrowser.tabs[0];
                b = win.gBrowser.getBrowserForTab(t);
                var doc = b.docShell.contentViewer.DOMDocument.wrappedJSObject;
                clickTest(doc, win);
                win.close();
                finish();
              });
            }, false);
            win.gBrowser.goBack();
          });
        });
      });
    }, true);
    b.loadURI("about:blank");

  }

  var loads = 0;
  function waitForLoad(event, tab, listenerContainer) {
    var b = gBrowser.getBrowserForTab(gBrowser.tabs[tab]);
    if (b.contentDocument != event.target) {
      return;
    }
    gBrowser.getBrowserForTab(gBrowser.tabs[tab]).removeEventListener("load", listenerContainer.listener, true);
    ++loads;
    if (loads == tabs.length - 1) {
      executeSoon(test1);
    }
  }

  function fn(f, arg) {
    var listenerContainer = { listener: null }
    listenerContainer.listener = function (event) { return f(event, arg, listenerContainer); };
    return listenerContainer.listener;
  }
  for (var i = 1; i < tabs.length; ++i) {
    gBrowser.getBrowserForTab(tabs[i]).addEventListener("load", fn(waitForLoad,i), true);
  }

  setLocation(1, "data:text/html;charset=utf-8,<title>tab1</title><body>tab1<iframe>");
  setLocation(2, "data:text/plain;charset=utf-8,tab2");
  setLocation(3, "data:text/html;charset=utf-8,<title>tab3</title><body>tab3<iframe>");
  setLocation(4, "data:text/html;charset=utf-8,<body onload='clicks=0' onclick='++clicks'>"+embed);
  gBrowser.selectedTab = tabs[3];

}