browser_taskbar_preview.js
Go to the documentation of this file.
1 function test() {
2  var isWin7OrHigher = false;
3  try {
4  let version = Cc["@mozilla.org/system-info;1"]
5  .getService(Ci.nsIPropertyBag2)
6  .getProperty("version");
7  isWin7OrHigher = (parseFloat(version) >= 6.1);
8  } catch (ex) { }
9 
10  is(!!Win7Features, isWin7OrHigher, "Win7Features available when it should be");
11  if (!isWin7OrHigher)
12  return;
13 
14  const ENABLE_PREF_NAME = "browser.taskbar.previews.enable";
15 
16  let temp = {};
17  Cu.import("resource://gre/modules/WindowsPreviewPerTab.jsm", temp);
18  let AeroPeek = temp.AeroPeek;
19 
21 
22  gPrefService.setBoolPref(ENABLE_PREF_NAME, true);
23 
24  is(1, AeroPeek.windows.length, "Got the expected number of windows");
25 
26  checkPreviews(1, "Browser starts with one preview");
27 
28  gBrowser.addTab();
29  gBrowser.addTab();
30  gBrowser.addTab();
31 
32  checkPreviews(4, "Correct number of previews after adding");
33 
34  for each (let preview in AeroPeek.previews)
35  ok(preview.visible, "Preview is shown as expected");
36 
37  gPrefService.setBoolPref(ENABLE_PREF_NAME, false);
38  checkPreviews(4, "Previews are unchanged when disabling");
39 
40  for each (let preview in AeroPeek.previews)
41  ok(!preview.visible, "Preview is not shown as expected after disabling");
42 
43  gPrefService.setBoolPref(ENABLE_PREF_NAME, true);
44  checkPreviews(4, "Previews are unchanged when re-enabling");
45  for each (let preview in AeroPeek.previews)
46  ok(preview.visible, "Preview is shown as expected after re-enabling");
47 
48  [1,2,3,4].forEach(function (idx) {
49  gBrowser.selectedTab = gBrowser.mTabs[idx];
50  ok(checkSelectedTab(), "Current tab is correctly selected");
51  });
52 
53  let currentSelectedTab = gBrowser.selectedTab;
54  for each (let idx in [1,2,3,4]) {
55  let preview = getPreviewForTab(gBrowser.mTabs[0]);
56  let canvas = createThumbnailSurface(preview);
57  let ctx = canvas.getContext("2d");
58  preview.controller.drawThumbnail(ctx, canvas.width, canvas.height);
59  ok(currentSelectedTab == gBrowser.selectedTab, "Drawing thumbnail does not change selection");
60  canvas = getCanvas(preview.controller.width, preview.controller.height);
61  ctx = canvas.getContext("2d");
62  preview.controller.drawPreview(ctx);
63  ok(currentSelectedTab == gBrowser.selectedTab, "Drawing preview does not change selection");
64  }
65 
66  // Close #4
67  getPreviewForTab(gBrowser.selectedTab).controller.onClose();
68  checkPreviews(3, "Expected number of previews after closing selected tab via controller");
69  ok(gBrowser.mTabs.length == 3, "Successfully closed a tab");
70 
71  // Select #1
72  ok(getPreviewForTab(gBrowser.mTabs[0]).controller.onActivate(), "Activation was accepted");
73  ok(gBrowser.mTabs[0] == gBrowser.selectedTab, "Correct tab was selected");
74  checkSelectedTab();
75 
76  // Remove #3 (non active)
77  gBrowser.removeTab(gBrowser.tabContainer.lastChild);
78  checkPreviews(2, "Expected number of previews after closing unselected via browser");
79 
80  // Remove #1 (active)
81  gBrowser.removeTab(gBrowser.tabContainer.firstChild);
82  checkPreviews(1, "Expected number of previews after closing selected tab via browser");
83 
84  // Add a new tab
85  gBrowser.addTab();
86  checkPreviews(2);
87  // Check default selection
88  checkSelectedTab();
89 
90  // Change selection
91  gBrowser.selectedTab = gBrowser.mTabs[0];
92  checkSelectedTab();
93  // Close nonselected tab via controller
94  getPreviewForTab(gBrowser.mTabs[1]).controller.onClose();
95  checkPreviews(1);
96 
97  if (gPrefService.prefHasUserValue(ENABLE_PREF_NAME))
98  gPrefService.clearUserPref(ENABLE_PREF_NAME);
99 
100  finish();
101 
102  function checkPreviews(aPreviews, msg) {
103  let nPreviews = AeroPeek.previews.length;
104  is(aPreviews, gBrowser.mTabs.length, "Browser has expected number of tabs");
105  is(nPreviews, gBrowser.mTabs.length, "Browser has one preview per tab");
106  is(nPreviews, aPreviews, msg || "Got expected number of previews");
107  }
108 
109  function getPreviewForTab(tab)
110  window.gTaskbarTabGroup.previewFromTab(tab);
111 
112  function checkSelectedTab()
113  getPreviewForTab(gBrowser.selectedTab).active;
114 
115  function isTabSelected(idx)
116  gBrowser.selectedTab == gBrowser.mTabs[idx];
117 
118  function createThumbnailSurface(p) {
119  let thumbnailWidth = 200,
120  thumbnailHeight = 120;
121  let ratio = p.controller.thumbnailAspectRatio;
122 
123  if (thumbnailWidth/thumbnailHeight > ratio)
124  thumbnailWidth = thumbnailHeight * ratio;
125  else
126  thumbnailHeight = thumbnailWidth / ratio;
127 
128  return getCanvas(thumbnailWidth, thumbnailHeight);
129  }
130 
131  function getCanvas(width, height) {
132  let win = window.QueryInterface(Ci.nsIDOMWindowInternal);
133  let doc = win.document;
134  let canvas = doc.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
135  canvas.width = width;
136  canvas.height = height;
137  return canvas;
138  }
139 }
const Cu
const Cc
function test()
function doc() browser.contentDocument
version(170)
var tab
function width(ele) rect(ele).width
let window
waitForExplicitFinish()
_updateDatepicker height
var gPrefService
Definition: overlay.js:34
const Ci
function msg