browser_Browser.js
Go to the documentation of this file.
1 const Ci = Components.interfaces;
2 const Cc = Components.classes;
3 
4 function url(spec) {
5  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
6  return ios.newURI(spec, null, null);
7 }
8 var gPageA = null;
9 var gPageB = null;
10 
11 // cached data from events
14 var gTabOpenCount = 0;
16 var gTabMoveCount = 0;
18 
19 function test() {
20  var windows = Application.windows;
21  ok(windows, "Check access to browser windows");
22  ok(windows.length, "There should be at least one browser window open");
23 
24  var activeWin = Application.activeWindow;
25  activeWin.events.addListener("TabOpen", onTabOpen);
26  activeWin.events.addListener("TabClose", onTabClose);
27  activeWin.events.addListener("TabMove", onTabMove);
28 
29  gPageA = activeWin.open(url("chrome://mochikit/content/browser/browser/fuel/test/ContentA.html"));
30  gPageA.events.addListener("load", onPageAFirstLoad);
31 
32  is(activeWin.tabs.length, 2, "Checking length of 'Browser.tabs' after opening 1 additional tab");
33 
35 
36  function onPageAFirstLoad(event) {
37  gPageA.events.removeListener("load", onPageAFirstLoad);
38  is(gPageA.uri.spec, event.data.uri.spec, "Checking event browser tab is equal to page A");
39 
40  gPageB = activeWin.open(url("chrome://mochikit/content/browser/browser/fuel/test/ContentB.html"));
41  gPageB.events.addListener("load", delayAfterOpen);
42  gPageB.focus();
43 
44  is(activeWin.tabs.length, 3, "Checking length of 'Browser.tabs' after opening a second additional tab");
45  is(activeWin.activeTab.index, gPageB.index, "Checking 'Browser.activeTab' after setting focus");
46  }
47 
48  function delayAfterOpen() {
49  executeSoon(afterOpen);
50  }
51 
52  // need to wait for the url's to be refreshed during the load
53  function afterOpen(event) {
54  gPageB.events.removeListener("load", delayAfterOpen);
55  // check actuals
56  is(gPageA.uri.spec, "chrome://mochikit/content/browser/browser/fuel/test/ContentA.html", "Checking 'BrowserTab.uri' after opening");
57  is(gPageB.uri.spec, "chrome://mochikit/content/browser/browser/fuel/test/ContentB.html", "Checking 'BrowserTab.uri' after opening");
58 
59  // check cached values from TabOpen event
60  is(gPageA.uri.spec, gTabOpenPageA.uri.spec, "Checking first browser tab open is equal to page A");
61  is(gPageB.uri.spec, gTabOpenPageB.uri.spec, "Checking second browser tab open is equal to page B");
62  // check event
63  is(gTabOpenCount, 2, "Checking event handler for tab open");
64 
65  // test document access
66  var test1 = gPageA.document.getElementById("test1");
67  ok(test1, "Checking existence of element in content DOM");
68  is(test1.innerHTML, "A", "Checking content of element in content DOM");
69 
70  // test moving tab
71  is(gTabMoveCount, 0, "Checking initial tab move count");
72 
73  // move the tab
74  gPageA.moveToEnd();
75  is(gPageA.index, 2, "Checking index after moving tab");
76 
77  // check event
78  is(gTabMoveCount, 1, "Checking event handler for tab move");
79 
80  let browser = gBrowser.getBrowserAtIndex(gPageB.index);
81  browser.addProgressListener({
82  onStateChange: function(webProgress, request, stateFlags, status) {
83  const complete = Ci.nsIWebProgressListener.STATE_IS_WINDOW +
84  Ci.nsIWebProgressListener.STATE_IS_NETWORK +
85  Ci.nsIWebProgressListener.STATE_STOP;
86  if ((stateFlags & complete) == complete) {
87  browser.removeProgressListener(this);
88  onPageBLoadComplete();
89  }
90  },
91 
92  onLocationChange: function() { return 0; },
93  onProgressChange: function() { return 0; },
94  onStatusChange: function() { return 0; },
95  onSecurityChange: function() { return 0; },
96  QueryInterface: function(iid) {
97  if (iid.equals(Ci.nsISupportsWeakReference) ||
98  iid.equals(Ci.nsIWebProgressListener) ||
99  iid.equals(Ci.nsISupports))
100  return this;
101 
102  throw Components.results.NS_ERROR_NO_INTERFACE;
103  }
104  });
105 
106  // test loading new content with a frame into a tab
107  // the event will be checked in onPageBLoadComplete
108  gPageB.events.addListener("load", onPageBLoadWithFrames);
109  gPageB.load(url("chrome://mochikit/content/browser/browser/fuel/test/ContentWithFrames.html"));
110  }
111 
112  function onPageBLoadWithFrames(event) {
113  gPageLoadCount++;
114  }
115 
116  function onPageBLoadComplete() {
117  gPageB.events.removeListener("load", onPageBLoadWithFrames);
118  // check page load with frame event
119  is(gPageLoadCount, 1, "Checking load count after loading new content with a frame");
120 
121  // test loading new content into a tab
122  // the event will be checked in onPageASecondLoad
123  gPageA.events.addListener("load", onPageASecondLoad);
124  gPageA.load(url("chrome://mochikit/content/browser/browser/fuel/test/ContentB.html"));
125  }
126 
127  function onPageASecondLoad(event) {
128  gPageA.events.removeListener("load", onPageASecondLoad);
129  is(gPageA.uri.spec, "chrome://mochikit/content/browser/browser/fuel/test/ContentB.html", "Checking 'BrowserTab.uri' after loading new content");
130 
131  // start testing closing tabs
132  // the event will be checked in afterClose
133  // use executeSoon so the onPageASecondLoad
134  // has a chance to finish first
135  gPageA.close();
136  gPageB.close();
137 
138  is(gTabCloseCount, 2, "Checking that tabs closed");
139  is(activeWin.tabs.length, 1, "Checking length of 'Browser.tabs' after closing 2 tabs");
140  finish();
141  }
142 }
143 function onTabOpen(event) {
144  gTabOpenCount++;
145 
146  // cache these values so we can check them later (after loading completes)
147  if (gTabOpenCount == 1)
148  gTabOpenPageA = event.data;
149 
150  if (gTabOpenCount == 2)
151  gTabOpenPageB = event.data;
152 }
153 function onTabClose(event) {
154  gTabCloseCount++;
155 }
156 
157 function onTabMove(event) {
158  gTabMoveCount++;
159 }
function url(spec)
var gTabOpenPageA
var gTabCloseCount
var gPageB
var Application
Definition: sbAboutDRM.js:37
var windows
var gTabOpenPageB
var event
function onTabMove(event)
sbOSDControlService prototype QueryInterface
function test1()
var gTabOpenCount
const Cc
function onTabOpen(event)
waitForExplicitFinish()
var gTabMoveCount
return null
Definition: FeedWriter.js:1143
var gPageA
function test()
ContinuingWebProgressListener prototype onStateChange
function onTabClose(event)
var ios
Definition: head_feeds.js:5
var browser
Definition: openLocation.js:42
var gPageLoadCount
const Ci