summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_session_zombification.html
blob: eba255ff62ec35ff4dda36d9c6282a9ec15eb923 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1044556
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1044556</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="head.js"></script>
  <script type="application/javascript">

  /** Test for Bug 1044556 **/

  "use strict";

  const { classes: Cc, interfaces: Ci, utils: Cu } = Components;

  Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  Cu.import("resource://gre/modules/Services.jsm");
  Cu.import("resource://gre/modules/Messaging.jsm");
  Cu.import("resource://gre/modules/Task.jsm");

  // Import the MemoryObserver
  Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
  XPCOMUtils.defineLazyGetter(this, "MemoryObserver", function() {
    let sandbox = {};
    Services.scriptloader.loadSubScript("chrome://browser/content/MemoryObserver.js", sandbox);
    return sandbox["MemoryObserver"];
  });

  // The chrome window
  let chromeWin;

  // Track the tabs where the tests are happening
  let tabBlank;
  let tabTest;

  const url1 = "data:text/html;charset=utf-8,It%20was%20a%20dark%20and%20stormy%20night.";
  const url2 = "data:text/html;charset=utf-8,Suddenly%2C%20a%20tab%20was%20zombified.";

  add_task(function* test_sessionStoreZombify() {
    chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
    let BrowserApp = chromeWin.BrowserApp;

    SimpleTest.registerCleanupFunction(function() {
      BrowserApp.closeTab(tabBlank);
      BrowserApp.closeTab(tabTest);
    });

    // Add a new tab with some content
    tabTest = BrowserApp.addTab(url1 , { selected: true, parentId: BrowserApp.selectedTab.id });
    yield promiseBrowserEvent(tabTest.browser, "DOMContentLoaded");

    // Add a new tab with a blank page
    tabBlank = BrowserApp.addTab("about:blank", { selected: true, parentId: BrowserApp.selectedTab.id });
    is(BrowserApp.selectedTab, tabBlank, "Test tab is in background.");

    // Zombify the backgrounded test tab
    MemoryObserver.zombify(tabTest);

    // Check that the test tab is actually zombified
    ok(tabTest.browser.__SS_restore, "Test tab is set for delay loading.");

    // Switch back to the test tab and wait for it to reload
    BrowserApp.selectTab(tabTest);
    yield promiseBrowserEvent(tabTest.browser, "DOMContentLoaded");

    // Check that the test tab has loaded the correct url
    is(tabTest.browser.currentURI.spec, url1, "Test tab is showing the first URL.");

    // Navigate to some other content
    BrowserApp.loadURI(url2, tabTest.browser);
    yield promiseBrowserEvent(tabTest.browser, "DOMContentLoaded");
    is(tabTest.browser.currentURI.spec, url2, "Test tab is showing the second URL.");

    // Switch to the other tab
    BrowserApp.selectTab(tabBlank);
    yield promiseTabEvent(BrowserApp.deck, "TabSelect");
    is(BrowserApp.selectedTab, tabBlank, "Test tab is in background.");

    // Zombify the backgrounded test tab again
    MemoryObserver.zombify(tabTest);

    // Check that the test tab is actually zombified
    ok(tabTest.browser.__SS_restore, "Test tab is set for delay loading.");

    // Switch back to the test tab and wait for it to reload
    BrowserApp.selectTab(tabTest);
    yield promiseBrowserEvent(tabTest.browser, "DOMContentLoaded");

    // Check that the test tab has loaded the correct url
    is(tabTest.browser.currentURI.spec, url2, "Test tab is showing the second URL.");
  });

  add_task(function* test_sessionStoreKeepAsZombie() {
    chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
    let BrowserApp = chromeWin.BrowserApp;
    let observerService = Services.obs;

    SimpleTest.registerCleanupFunction(function() {
      BrowserApp.closeTab(tabBlank);
      BrowserApp.closeTab(tabTest);
    });

    // Add a new tab with some content
    tabTest = BrowserApp.addTab(url1 , { selected: true, parentId: BrowserApp.selectedTab.id });
    yield promiseBrowserEvent(tabTest.browser, "DOMContentLoaded");

    // Add a new tab with a blank page
    tabBlank = BrowserApp.addTab("about:blank", { selected: true, parentId: BrowserApp.selectedTab.id });
    yield promiseTabEvent(BrowserApp.deck, "TabSelect");
    is(BrowserApp.selectedTab, tabBlank, "Test tab is in background.");

    // Zombify the backgrounded test tab
    MemoryObserver.zombify(tabTest);

    // Check that the test tab is actually zombified
    ok(tabTest.browser.__SS_restore, "Test tab is set for delay loading.");
    is(tabTest.browser.currentURI.spec, "about:blank", "Test tab is zombified.");

    // Tell the session store that it shouldn't restore that tab on selecting
    observerService.notifyObservers(null, "Tab:KeepZombified", tabTest.id);

    // Switch back to the test tab and check that it remains zombified
    BrowserApp.selectTab(tabTest);
    yield promiseTabEvent(BrowserApp.deck, "TabSelect");
    is(BrowserApp.selectedTab, tabTest, "Test tab is selected.");
    ok(tabTest.browser.__SS_restore, "Test tab is still set for delay loading.");

    // Switch to the other tab and back again
    BrowserApp.selectTab(tabBlank);
    yield promiseTabEvent(BrowserApp.deck, "TabSelect");
    is(BrowserApp.selectedTab, tabBlank, "Test tab is in background.");
    BrowserApp.selectTab(tabTest);

    // "Tab:KeepZombified should be good for one TabSelect only
    yield promiseBrowserEvent(tabTest.browser, "DOMContentLoaded");
    is(BrowserApp.selectedTab, tabTest, "Test tab is selected.");

    // Check that the test tab is no longer a zombie and has loaded the correct url
    ok(!tabTest.browser.__SS_restore, "Test tab is no longer set for delay loading.");
    is(tabTest.browser.currentURI.spec, url1, "Test tab is showing the test URL.");

    // Zombify the test tab again
    BrowserApp.selectTab(tabBlank);
    yield promiseTabEvent(BrowserApp.deck, "TabSelect");
    is(BrowserApp.selectedTab, tabBlank, "Test tab is in background.");
    MemoryObserver.zombify(tabTest);
    ok(tabTest.browser.__SS_restore, "Test tab is set for delay loading.");
    is(tabTest.browser.currentURI.spec, "about:blank", "Test tab is zombified.");

    // Tell the session store that it shouldn't restore that tab on selecting
    observerService.notifyObservers(null, "Tab:KeepZombified", tabTest.id);

    // Switch back to the test tab and check that it remains zombified
    BrowserApp.selectTab(tabTest);
    yield promiseTabEvent(BrowserApp.deck, "TabSelect");
    is(BrowserApp.selectedTab, tabTest, "Test tab is selected.");
    ok(tabTest.browser.__SS_restore, "Test tab is still set for delay loading.");

    // Fake an "application-foreground" notification
    observerService.notifyObservers(null, "application-foreground", null);

    // The test tab should now start reloading
    yield promiseBrowserEvent(tabTest.browser, "DOMContentLoaded");
    ok(!tabTest.browser.__SS_restore, "Test tab is no longer set for delay loading.");
    is(tabTest.browser.currentURI.spec, url1, "Test tab is showing the test URL.");
  });

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1044556">Mozilla Bug 1044556</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>