summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_pinnedTabs.js
blob: e0ddb5072f8d3763974641777c360c053ea95e5c (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
var tabs;

function index(tab) {
  return Array.indexOf(gBrowser.tabs, tab);
}

function indexTest(tab, expectedIndex, msg) {
  var diag = "tab " + tab + " should be at index " + expectedIndex;
  if (msg)
    msg = msg + " (" + diag + ")";
  else
    msg = diag;
  is(index(tabs[tab]), expectedIndex, msg);
}

function PinUnpinHandler(tab, eventName) {
  this.eventCount = 0;
  var self = this;
  tab.addEventListener(eventName, function() {
    tab.removeEventListener(eventName, arguments.callee, true);

    self.eventCount++;
  }, true);
  gBrowser.tabContainer.addEventListener(eventName, function(e) {
    gBrowser.tabContainer.removeEventListener(eventName, arguments.callee, true);

    if (e.originalTarget == tab) {
      self.eventCount++;
    }
  }, true);
}

function test() {
  tabs = [gBrowser.selectedTab, gBrowser.addTab(), gBrowser.addTab(), gBrowser.addTab()];
  indexTest(0, 0);
  indexTest(1, 1);
  indexTest(2, 2);
  indexTest(3, 3);

  var eh = new PinUnpinHandler(tabs[3], "TabPinned");
  gBrowser.pinTab(tabs[3]);
  is(eh.eventCount, 2, "TabPinned event should be fired");
  indexTest(0, 1);
  indexTest(1, 2);
  indexTest(2, 3);
  indexTest(3, 0);

  eh = new PinUnpinHandler(tabs[1], "TabPinned");
  gBrowser.pinTab(tabs[1]);
  is(eh.eventCount, 2, "TabPinned event should be fired");
  indexTest(0, 2);
  indexTest(1, 1);
  indexTest(2, 3);
  indexTest(3, 0);

  gBrowser.moveTabTo(tabs[3], 3);
  indexTest(3, 1, "shouldn't be able to mix a pinned tab into normal tabs");

  gBrowser.moveTabTo(tabs[2], 0);
  indexTest(2, 2, "shouldn't be able to mix a normal tab into pinned tabs");

  eh = new PinUnpinHandler(tabs[1], "TabUnpinned");
  gBrowser.unpinTab(tabs[1]);
  is(eh.eventCount, 2, "TabUnpinned event should be fired");
  indexTest(1, 1, "unpinning a tab should move a tab to the start of normal tabs");

  eh = new PinUnpinHandler(tabs[3], "TabUnpinned");
  gBrowser.unpinTab(tabs[3]);
  is(eh.eventCount, 2, "TabUnpinned event should be fired");
  indexTest(3, 0, "unpinning a tab should move a tab to the start of normal tabs");

  gBrowser.removeTab(tabs[1]);
  gBrowser.removeTab(tabs[2]);
  gBrowser.removeTab(tabs[3]);
}