summaryrefslogtreecommitdiffstats
path: root/services/cloudsync/tests/mochitest/browser_tabEvents.js
blob: 9d80090a0a5b8f8f316f64bf6aeec049270f90dd (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
/* 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/. */

function test() {

  let local = {};

  Components.utils.import("resource://gre/modules/CloudSync.jsm", local);
  Components.utils.import("resource:///modules/sessionstore/TabStateFlusher.jsm", local);

  let cloudSync = local.CloudSync();
  let opentabs = [];

  waitForExplicitFinish();

  let testURL = "chrome://mochitests/content/browser/services/cloudsync/tests/mochitest/other_window.html";
  let expected = [
    testURL,
    testURL+"?x=1",
    testURL+"?x=%20a",
    // testURL+"?x=å",
  ];

  let nevents = 0;
  let nflushed = 0;
  function handleTabChangeEvent () {
    cloudSync.tabs.removeEventListener("change", handleTabChangeEvent);
    ++ nevents;
    info("tab change event " + nevents);
    next();
  }

  function getLocalTabs() {
    cloudSync.tabs.getLocalTabs().then(
      function (tabs) {
        for (let tab of tabs) {
          ok(expected.indexOf(tab.url) >= 0, "found an expected tab");
        }

        is(tabs.length, expected.length, "found the right number of tabs");

        opentabs.forEach(function (tab) {
          gBrowser.removeTab(tab);
        });

        is(nevents, 1, "expected number of change events");

        finish();
      }
    )
  }

  cloudSync.tabs.addEventListener("change", handleTabChangeEvent);

  expected.forEach(function(url) {
    let tab = gBrowser.addTab(url);

    function flush() {
      tab.linkedBrowser.removeEventListener("load", flush, true);
      local.TabStateFlusher.flush(tab.linkedBrowser).then(() => {
        ++ nflushed;
        info("flushed " + nflushed);
        next();
      });
    }

    tab.linkedBrowser.addEventListener("load", flush, true);

    opentabs.push(tab);
  });

  function next() {
    if (nevents == 1 && nflushed == expected.length) {
      getLocalTabs();
    }
  }

}