File: browser_tabview_snapping.js

package info (click to toggle)
icedove 31.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 1,007,612 kB
  • sloc: cpp: 3,959,977; ansic: 1,866,528; java: 219,720; python: 200,664; xml: 142,383; asm: 133,557; sh: 75,814; makefile: 27,028; perl: 26,849; objc: 4,014; yacc: 1,995; pascal: 1,024; lex: 950; exp: 449; lisp: 228; awk: 211; php: 113; sed: 43; csh: 31; ada: 16; ruby: 3
file content (157 lines) | stat: -rw-r--r-- 6,085 bytes parent folder | download | duplicates (14)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function test() {
  waitForExplicitFinish();

  newWindowWithTabView(onTabViewWindowLoaded, null, 1000, 800);
}

function onTabViewWindowLoaded(win) {
  let contentWindow = win.document.getElementById("tab-view").contentWindow;
  let [originalTab] = win.gBrowser.visibleTabs;

  ok(win.TabView.isVisible(), "Tab View is visible");
  is(contentWindow.GroupItems.groupItems.length, 1, "There is only one group");
  let currentActiveGroup = contentWindow.GroupItems.getActiveGroupItem();

  // Create a group
  // Note: 150 x 150 should be larger than the minimum size for a group item
  let firstBox = new contentWindow.Rect(80, 80, 160, 160);
  let firstGroup = new contentWindow.GroupItem([], { bounds: firstBox,
                                                     immediately: true });
  ok(firstGroup.getBounds().equals(firstBox), "This group got its bounds");
  
  // Create a second group
  let secondBox = new contentWindow.Rect(80, 280, 160, 160);
  let secondGroup = new contentWindow.GroupItem([], { bounds: secondBox,
                                                      immediately: true });
  ok(secondGroup.getBounds().equals(secondBox), "This second group got its bounds");
  
  // A third group is created later, but multiple functions need access to it.
  let thirdGroup = null;

  is(secondGroup.getBounds().top - firstGroup.getBounds().bottom, 40,
    "There's currently 40 px between the first group and second group");

  let endGame = function() {
    firstGroup.container.parentNode.removeChild(firstGroup.container);
    firstGroup.close();
    thirdGroup.container.parentNode.removeChild(thirdGroup.container);
    thirdGroup.close();

    win.close();
    ok(win.closed, "new window is closed");
    finish();
  }
  
  let continueWithPart2 = function() {
    
    ok(firstGroup.getBounds().equals(firstBox), "The first group should still have its bounds");
    
    // Create a third group
    let thirdBox = new contentWindow.Rect(80, 280, 200, 160);
    thirdGroup = new contentWindow.GroupItem([], { bounds: thirdBox,
                                                   immediately: true });
    ok(thirdGroup.getBounds().equals(thirdBox), "This third group got its bounds");
  
    is(thirdGroup.getBounds().top - firstGroup.getBounds().bottom, 40,
      "There's currently 40 px between the first group and third group");
  
    // Just move it to the left and drop it.
    checkSnap(thirdGroup, 0, 0, contentWindow, function(snapped){
      ok(!snapped,"Offset: Just move it to the left and drop it");
      
      // Move the second group up 10 px. It shouldn't snap yet.
      checkSnap(thirdGroup, 0, -10, contentWindow, function(snapped){
        ok(!snapped,"Offset: Moving up 10 should not snap");
  
        // Move the second group up 10 px. It now should snap.
        checkSnap(thirdGroup, 0, -10, contentWindow, function(snapped){
          ok(snapped,"Offset: Moving up 10 again should snap!");
          endGame();
        });
      });
    });
  };

  let part1 = function() {
    // Just pick it up and drop it.
    checkSnap(secondGroup, 0, 0, contentWindow, function(snapped){
      ok(!snapped,"Right under: Just pick it up and drop it");
      
      // Move the second group up 10 px. It shouldn't snap yet.
      checkSnap(secondGroup, 0, -10, contentWindow, function(snapped){
        ok(!snapped,"Right under: Moving up 10 should not snap");
  
        // Move the second group up 10 px. It now should snap.
        checkSnap(secondGroup, 0, -10, contentWindow, function(snapped){
          ok(snapped,"Right under: Moving up 10 again should snap!");
          // cheat by removing the second group, so that we disappear immediately
          secondGroup.container.parentNode.removeChild(secondGroup.container);
          secondGroup.close();
          continueWithPart2();
        });
      });
    });
  }
  
  part1();
}

function simulateDragDrop(tabItem, offsetX, offsetY, contentWindow) {
  // enter drag mode
  let dataTransfer;

  EventUtils.synthesizeMouse(
    tabItem.container, 1, 1, { type: "mousedown" }, contentWindow);
  let event = contentWindow.document.createEvent("DragEvents");
  event.initDragEvent(
    "dragenter", true, true, contentWindow, 0, 0, 0, 0, 0,
    false, false, false, false, 1, null, dataTransfer);
  tabItem.container.dispatchEvent(event);

  // drag over
  if (offsetX || offsetY) {
    let Ci = Components.interfaces;
    let utils = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor).
                              getInterface(Ci.nsIDOMWindowUtils);
    let rect = tabItem.getBounds();
    for (let i = 1; i <= 5; i++) {
      let left = rect.left + 1 + Math.round(i * offsetX / 5);
      let top = rect.top + 1 + Math.round(i * offsetY / 5);
      utils.sendMouseEvent("mousemove", left, top, 0, 1, 0);
    }
    event = contentWindow.document.createEvent("DragEvents");
    event.initDragEvent(
      "dragover", true, true, contentWindow, 0, 0, 0, 0, 0,
      false, false, false, false, 0, null, dataTransfer);
    tabItem.container.dispatchEvent(event);
  }
  
  // drop
  EventUtils.synthesizeMouse(
    tabItem.container, 0, 0, { type: "mouseup" }, contentWindow);
  event = contentWindow.document.createEvent("DragEvents");
  event.initDragEvent(
    "drop", true, true, contentWindow, 0, 0, 0, 0, 0,
    false, false, false, false, 0, null, dataTransfer);
  tabItem.container.dispatchEvent(event);
}

function checkSnap(item, offsetX, offsetY, contentWindow, callback) {
  let firstTop = item.getBounds().top;
  let firstLeft = item.getBounds().left;
  let onDrop = function() {
    let snapped = false;
    item.container.removeEventListener('drop', onDrop, false);
    if (item.getBounds().top != firstTop + offsetY)
      snapped = true;
    if (item.getBounds().left != firstLeft + offsetX)
      snapped = true;
    callback(snapped);
  };
  item.container.addEventListener('drop', onDrop, false);
  simulateDragDrop(item, offsetX, offsetY, contentWindow);
}