summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-tabs-common.js
blob: 7ab8b3c7698c1f1aec6b3b0df0bd5354297de090 (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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

const { Loader, LoaderWithHookedConsole } = require("sdk/test/loader");
const { browserWindows } = require('sdk/windows');
const tabs = require('sdk/tabs');
const { isPrivate } = require('sdk/private-browsing');
const { openDialog } = require('sdk/window/utils');
const { isWindowPrivate } = require('sdk/window/utils');
const { setTimeout } = require('sdk/timers');
const { openWebpage } = require('./private-browsing/helper');
const { isTabPBSupported, isWindowPBSupported } = require('sdk/private-browsing/utils');
const { getTabContentWindow } = require('sdk/tabs/utils');
const { attach, detach } = require('sdk/content/mod');
const { Style } = require('sdk/stylesheet/style');
const fixtures = require('./fixtures');
const { viewFor } = require('sdk/view/core');
const app = require("sdk/system/xul-app");
const { cleanUI } = require('sdk/test/utils');

const URL = 'data:text/html;charset=utf-8,<html><head><title>#title#</title></head></html>';

// TEST: tab count
exports.testTabCounts = function(assert, done) {
  tabs.open({
    url: 'about:blank',
    onReady: function(tab) {
      let count1 = 0,
          count2 = 0;
      for (let window of browserWindows) {
        count1 += window.tabs.length;
        for (let tab of window.tabs) {
          count2 += 1;
        }
      }

      assert.ok(tabs.length > 1, 'tab count is > 1');
      assert.equal(count1, tabs.length, 'tab count by length is correct');
      assert.equal(count2, tabs.length, 'tab count by iteration is correct');

      // end test
      tab.close(done);
    }
  });
};

exports.testTabRelativePath = function(assert, done) {
  const { merge } = require("sdk/util/object");
  const self = require("sdk/self");

  const options = merge({}, require('@loader/options'),
                        { prefixURI: require('./fixtures').url() });

  let loader = Loader(module, null, options);

  let tabs = loader.require("sdk/tabs");

  tabs.open({
    url: "./test.html",
    onReady: (tab) => {
      assert.equal(tab.title, "foo",
        "tab opened a document with relative path");

      tab.attach({
        contentScriptFile: "./test-contentScriptFile.js",
        onMessage: (message) => {
          assert.equal(message, "msg from contentScriptFile",
            "Tab attach a contentScriptFile with relative path worked");

          tab.close(done);
          loader.unload();
        }
      });
    }
  });
};

// TEST: tabs.activeTab getter
exports.testActiveTab_getter = function(assert, done) {
  let evtCount = 0;
  let activeTab = null;

  function endTest(type, tab) {
    if (type == 'activate') {
      assert.strictEqual(tabs.activeTab, tab, 'the active tab is the opened tab');
      activeTab = tabs.activeTab;
    }
    else {
      assert.equal(tab.url, url, 'the opened tab has the correct url');
    }

    if (++evtCount != 2)
      return;

    assert.strictEqual(activeTab, tab, 'the active tab is the ready tab');
    assert.strictEqual(tabs.activeTab, tab, 'the active tab is the ready tab');

    tab.close(done);
  }

  let url = URL.replace("#title#", "testActiveTab_getter");
  tabs.open({
    url: url,
    onReady: endTest.bind(null, 'ready'),
    onActivate: endTest.bind(null, 'activate')
  });
};

// TEST: tab.activate()
exports.testActiveTab_setter = function(assert, done) {
  let url = URL.replace("#title#", "testActiveTab_setter");
  let tab1URL = URL.replace("#title#", "tab1");

  tabs.open({
    url: tab1URL,
    onReady: function(activeTab) {
      let activeTabURL = tabs.activeTab.url;

      tabs.open({
        url: url,
        inBackground: true,
        onReady: function onReady(tab) {
          assert.equal(tabs.activeTab.url, activeTabURL, "activeTab url has not changed");
          assert.equal(tab.url, url, "url of new background tab matches");

          tab.once('activate', function onActivate(eventTab) {
            assert.equal(tabs.activeTab.url, url, "url after activeTab setter matches");
            assert.equal(eventTab, tab, "event argument is the activated tab");
            assert.equal(eventTab, tabs.activeTab, "the tab is the active one");

            activeTab.close(function() {
              tab.close(done);
            });
          });

          tab.activate();
        }
      });
    }
  });
};

// TEST: tab.close()
exports.testTabClose_alt = function(assert, done) {
  let url = URL.replace('#title#', 'TabClose_alt');
  let tab1URL = URL.replace('#title#', 'tab1');

  tabs.open({
    url: tab1URL,
    onReady: function(tab1) {
      // make sure that our tab is not active first
      assert.notEqual(tabs.activeTab.url, url, "tab is not the active tab");

      tabs.open({
        url: url,
        onReady: function(tab) {
          assert.equal(tab.url, url, "tab is now the active tab");
          assert.equal(tabs.activeTab.url, url, "tab is now the active tab");

          // another tab should be activated on close
          tabs.once('activate', function() {
            assert.notEqual(tabs.activeTab.url, url, "tab is no longer the active tab");

            // end test
            tab1.close(done);
          });

          tab.close();
        }
      });
    }
  });
};

exports.testAttachOnMultipleDocuments_alt = function (assert, done) {
  // Example of attach that process multiple tab documents
  let firstLocation = "data:text/html;charset=utf-8,foobar";
  let secondLocation = "data:text/html;charset=utf-8,bar";
  let thirdLocation = "data:text/html;charset=utf-8,fox";
  let onReadyCount = 0;
  let worker1 = null;
  let worker2 = null;
  let detachEventCount = 0;

  tabs.open({
    url: firstLocation,
    onReady: function (tab) {
      onReadyCount++;
      if (onReadyCount == 1) {
        worker1 = tab.attach({
          contentScript: 'self.on("message", ' +
                         '  () => self.postMessage(document.location.href)' +
                         ');',
          onMessage: function (msg) {
            assert.equal(msg, firstLocation,
                             "Worker url is equal to the 1st document");
            tab.url = secondLocation;
          },
          onDetach: function () {
            detachEventCount++;
            assert.pass("Got worker1 detach event");
            assert.throws(function () {
                worker1.postMessage("ex-1");
              },
              /Couldn't find the worker/,
              "postMessage throw because worker1 is destroyed");
            checkEnd();
          }
        });
        worker1.postMessage("new-doc-1");
      }
      else if (onReadyCount == 2) {
        worker2 = tab.attach({
          contentScript: 'self.on("message", ' +
                         '  () => self.postMessage(document.location.href)' +
                         ');',
          onMessage: function (msg) {
            assert.equal(msg, secondLocation,
                             "Worker url is equal to the 2nd document");
            tab.url = thirdLocation;
          },
          onDetach: function () {
            detachEventCount++;
            assert.pass("Got worker2 detach event");
            assert.throws(function () {
                worker2.postMessage("ex-2");
              },
              /Couldn't find the worker/,
              "postMessage throw because worker2 is destroyed");
            checkEnd(tab);
          }
        });
        worker2.postMessage("new-doc-2");
      }
      else if (onReadyCount == 3) {
        tab.close();
      }
    }
  });

  function checkEnd(tab) {
    if (detachEventCount != 2)
      return;

    assert.pass("Got all detach events");

    done();
  }
};

exports.testAttachWrappers_alt = function (assert, done) {
  // Check that content script has access to wrapped values by default

  let document = "data:text/html;charset=utf-8,<script>var globalJSVar = true; " +
                 "                       document.getElementById = 3;</script>";
  let count = 0;

  tabs.open({
    url: document,
    onReady: function (tab) {
      let worker = tab.attach({
        contentScript: 'try {' +
                       '  self.postMessage(!("globalJSVar" in window));' +
                       '  self.postMessage(typeof window.globalJSVar == "undefined");' +
                       '} catch(e) {' +
                       '  self.postMessage(e.message);' +
                       '}',
        onMessage: function (msg) {
          assert.equal(msg, true, "Worker has wrapped objects ("+count+")");
          if (count++ == 1)
            tab.close(() => done());
        }
      });
    }
  });
};

// TEST: activeWindow getter and activeTab getter on tab 'activate' event
exports.testActiveWindowActiveTabOnActivate_alt = function(assert, done) {

  let activateCount = 0;
  let newTabs = [];
  let tabs = browserWindows.activeWindow.tabs;

  tabs.on('activate', function onActivate(tab) {
    assert.equal(tabs.activeTab, tab,
                    "the active window's active tab is the tab provided");

    if (++activateCount == 2) {
      assert.equal(newTabs.length, activateCount, "Should have seen the right number of tabs open");
      tabs.removeListener('activate', onActivate);

      newTabs.forEach(function(tab) {
        tab.close(function() {
          if (--activateCount == 0) {
            done();
          }
        });
      });
    }
    else if (activateCount > 2) {
      assert.fail("activateCount is greater than 2 for some reason..");
    }
  });

  tabs.open({
    url: URL.replace("#title#", "tabs.open1"),
    onOpen: tab => newTabs.push(tab)
  });
  tabs.open({
    url: URL.replace("#title#", "tabs.open2"),
    onOpen: tab => newTabs.push(tab)
  });
};

// TEST: tab properties
exports.testTabContentTypeAndReload = function(assert, done) {

  let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>";
  let urlXML = "data:text/xml;charset=utf-8,<foo>bar</foo>";
  tabs.open({
    url: url,
    onReady: function(tab) {
      if (tab.url === url) {
        assert.equal(tab.contentType, "text/html");
        tab.url = urlXML;
      }
      else {
        assert.equal(tab.contentType, "text/xml");
        tab.close(done);
      }
    }
  });
};

// test that it isn't possible to open a private tab without the private permission
exports.testTabOpenPrivate = function(assert, done) {

  let url = 'about:blank';
  tabs.open({
    url: url,
    isPrivate: true,
    onReady: function(tab) {
      assert.equal(tab.url, url, 'opened correct tab');
      assert.equal(isPrivate(tab), false, 'private tabs are not supported by default');

      tab.close(done);
    }
  });
}

// We need permission flag in order to see private window's tabs
exports.testPrivateAreNotListed = function (assert, done) {
  let originalTabCount = tabs.length;

  let page = openWebpage("about:blank", true);
  if (!page) {
    assert.pass("Private browsing isn't supported in this release");
    return;
  }

  page.ready.then(function (win) {
    if (isTabPBSupported || isWindowPBSupported) {
      assert.ok(isWindowPrivate(win), "the window is private");
      assert.equal(tabs.length, originalTabCount,
                       'but the tab is *not* visible in tabs list');
    }
    else {
      assert.ok(!isWindowPrivate(win), "the window isn't private");
      assert.equal(tabs.length, originalTabCount + 1,
                       'so that the tab is visible is tabs list');
    }
    page.close().then(done);
  });
}

// If we close the tab while being in `onOpen` listener,
// we end up synchronously consuming TabOpen, closing the tab and still
// synchronously consuming the related TabClose event before the second
// loader have a change to process the first TabOpen event!
exports.testImmediateClosing = function (assert, done) {
  let tabURL = 'data:text/html,foo';

  let { loader, messages } = LoaderWithHookedConsole(module, onMessage);
  let concurrentTabs = loader.require("sdk/tabs");
  concurrentTabs.on("open", function (tab) {
    // On Firefox, It shouldn't receive such event as the other loader will just
    // open and destroy the tab without giving a chance to other loader to even
    // know about the existance of this tab.
    if (app.is("Firefox")) {
      assert.fail("Concurrent loader received a tabs `open` event");
    }
    else {
      // On mobile, we can still receive an open event,
      // but not the related ready event
      tab.on("ready", function () {
        assert.fail("Concurrent loader received a tabs `ready` event");
      });
    }
  });
  function onMessage(type, msg) {
    assert.fail("Unexpected mesage on concurrent loader: " + msg);
  }

  tabs.open({
    url: tabURL,
    onOpen: function(tab) {
      tab.close(function () {
        assert.pass("Tab succesfully removed");
        // Let a chance to the concurrent loader to receive a TabOpen event
        // on the next event loop turn
        setTimeout(function () {
          loader.unload();
          done();
        }, 0);
      });
    }
  });
}

// TEST: tab.reload()
exports.testTabReload = function(assert, done) {

  let url = "data:text/html;charset=utf-8,<!doctype%20html><title></title>";

  tabs.open({
    url: url,
    onReady: function onReady(tab) {
      tab.removeListener('ready', onReady);

      tab.once(
        'ready',
        function onReload() {
          assert.pass("the tab was loaded again");
          assert.equal(tab.url, url, "the tab has the same URL");

          tab.close(() => done());
        }
      );

      tab.reload();
    }
  });
};

exports.testOnPageShowEvent = function (assert, done) {
  let events = [];
  let firstUrl = 'data:text/html;charset=utf-8,First';
  let secondUrl = 'data:text/html;charset=utf-8,Second';

  let counter = 0;
  function onPageShow (tab, persisted) {
    events.push('pageshow');
    counter++;
    if (counter === 1) {
      assert.equal(persisted, false, 'page should not be cached on initial load');
      tab.url = secondUrl;
    }
    else if (counter === 2) {
      assert.equal(persisted, false, 'second test page should not be cached either');
      tab.attach({
        contentScript: 'setTimeout(function () { window.history.back(); }, 0)'
      });
    }
    else {
      assert.equal(persisted, true, 'when we get back to the fist page, it has to' +
                             'come from cache');
      tabs.removeListener('pageshow', onPageShow);
      tabs.removeListener('open', onOpen);
      tabs.removeListener('ready', onReady);
      tab.close(() => {
        ['open', 'ready', 'pageshow', 'ready',
            'pageshow', 'pageshow'].map((type, i) => {
          assert.equal(type, events[i], 'correct ordering of events');
        });
        done()
      });
    }
  }

  function onOpen () {
    return events.push('open');
  }
  function onReady () {
    return events.push('ready');
  }

  tabs.on('pageshow', onPageShow);
  tabs.on('open', onOpen);
  tabs.on('ready', onReady);
  tabs.open({
    url: firstUrl
  });
};

exports.testOnPageShowEventDeclarative = function (assert, done) {
  let events = [];
  let firstUrl = 'data:text/html;charset=utf-8,First';
  let secondUrl = 'data:text/html;charset=utf-8,Second';

  let counter = 0;
  function onPageShow (tab, persisted) {
    events.push('pageshow');
    counter++;
    if (counter === 1) {
      assert.equal(persisted, false, 'page should not be cached on initial load');
      tab.url = secondUrl;
    }
    else if (counter === 2) {
      assert.equal(persisted, false, 'second test page should not be cached either');
      tab.attach({
        contentScript: 'setTimeout(function () { window.history.back(); }, 0)'
      });
    }
    else {
      assert.equal(persisted, true, 'when we get back to the fist page, it has to' +
                             'come from cache');
      tabs.removeListener('pageshow', onPageShow);
      tabs.removeListener('open', onOpen);
      tabs.removeListener('ready', onReady);
      tab.close(() => {
        ['open', 'ready', 'pageshow', 'ready',
            'pageshow', 'pageshow'].map((type, i) => {
          assert.equal(type, events[i], 'correct ordering of events');
        });
        done()
      });
    }
  }

  function onOpen () {
    return events.push('open');
  }
  function onReady () {
    return events.push('ready');
  }

  tabs.open({
    url: firstUrl,
    onPageShow: onPageShow,
    onOpen: onOpen,
    onReady: onReady
  });
};

exports.testAttachStyleToTab = function(assert, done) {
   let style = Style({
    source: "div { height: 100px; }",
    uri: fixtures.url("include-file.css")
  });

  tabs.open({
    url: "data:text/html;charset=utf-8,<div style='background: silver'>css test</div>",
    onReady: (tab) => {
      let xulTab = viewFor(tab);

      attach(style, tab)

      let { document } = getTabContentWindow(xulTab);
      let div = document.querySelector("div");

      assert.equal(div.clientHeight, 100,
        "Style.source properly attached to tab");

      assert.equal(div.offsetHeight, 120,
        "Style.uri properly attached to tab");

      detach(style, tab);

      assert.notEqual(div.clientHeight, 100,
        "Style.source properly detached from tab");

      assert.notEqual(div.offsetHeight, 120,
        "Style.uri properly detached from tab");

      attach(style, xulTab);

      assert.equal(div.clientHeight, 100,
        "Style.source properly attached to xul tab");

      assert.equal(div.offsetHeight, 120,
        "Style.uri properly attached to xul tab");

      detach(style, tab);

      assert.notEqual(div.clientHeight, 100,
        "Style.source properly detached from xul tab");

      assert.notEqual(div.offsetHeight, 120,
        "Style.uri properly detached from xul tab");

      tab.close(done);
    }
  });
};

// Tests that the this property is correct in event listeners called from
// the tabs API.
exports.testTabEventBindings = function(assert, done) {
  let loader = Loader(module);
  let tabs = loader.require("sdk/tabs");
  let firstTab = tabs.activeTab;

  let EVENTS = ["open", "close", "activate", "deactivate",
                "load", "ready", "pageshow"];

  let tabBoundEventHandler = (event) => function(tab) {
    assert.equal(this, tab, "tab listener for " + event + " event should be bound to the tab object.");
  };

  let tabsBoundEventHandler = (event) => function(tab) {
    assert.equal(this, tabs, "tabs listener for " + event + " event should be bound to the tabs object.");
  }
  for (let event of EVENTS)
    tabs.on(event, tabsBoundEventHandler(event));

  let tabsOpenEventHandler = (event) => function(tab) {
    assert.equal(this, tab, "tabs open listener for " + event + " event should be bound to the tab object.");
  }

  let openArgs = {
    url: "data:text/html;charset=utf-8,binding-test",
    onOpen: function(tab) {
      tabsOpenEventHandler("open").call(this, tab);

      for (let event of EVENTS)
        tab.on(event, tabBoundEventHandler(event));

      tab.once("pageshow", () => {
        tab.once("deactivate", () => {
          tab.once("close", () => {
            loader.unload();
            done();
          });

          tab.close();
        });

        firstTab.activate();
      });
    }
  };
  // Listen to everything except onOpen
  for (let event of EVENTS.slice(1)) {
    let eventProperty = "on" + event.slice(0, 1).toUpperCase() + event.slice(1);
    openArgs[eventProperty] = tabsOpenEventHandler(event);
  }

  tabs.open(openArgs);
}

require('sdk/test').run(exports);