summaryrefslogtreecommitdiff
path: root/dom/tests/mochitest/localstorage/test_clear_browser_data.html
blob: bed3d0aa22b1bfc503aefa186fb984f6e8413a07 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=795134
-->
<head>
  <meta charset="utf-8">
  <title>Tests that clearing mozbrowser private data removes the localStorage data</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=795134">Mozilla Bug 795134</a>
<p id="display"></p>
<div id="content">
  <iframe src="http://example.com/tests/error404"></iframe>
</div>
<pre id="test">
<script type="application/javascript;version=1.7">

/** Test for Bug 795134 **/

/*
 * This test will check that localStorage data are correctly deleted when it is
 * requested that a mozbrowser private data are deleted.
 *
 * Here is the big picture of what the test does:
 * 1. Setup permissions and preferences.
 * 2. Install a dummy application and embed it in an iframe.
 * 3. Load a mozbrowser iframe from this application.
 * 4. Fill storages for the app and the mozbrowser iframe.
 * 5. Uninstall the application.
 *
 * Expected result: all localStorage data from the mozbrowser have been deleted
 * but sessionStorage stays untouched such as all non-browser data.
 *
 * This test is asynchronous and methods are called in a reading order.
 * Each method has a deeper explanation of what it does
 */

const Ci = Components.interfaces;
const Cc = Components.classes;
const Cu = Components.utils;

SimpleTest.waitForExplicitFinish();

var permManager = Cc["@mozilla.org/permissionmanager;1"]
                    .getService(Ci.nsIPermissionManager);
var appsService = Cc['@mozilla.org/AppsService;1']
                    .getService(Ci.nsIAppsService);

/**
 * Initialize the |storage| that has been given with "foo" => "bar".
 * Checks that the storage wasn't initialized and checks that the initialization
 * was successful.
 */
function setupStorage(storage) {
  is(storage.getItem("foo"), null, "no data");

  storage.setItem("foo", "bar");
  is(storage.getItem("foo"), "bar", "data written");
}

permManager.addFromPrincipal(window.document.nodePrincipal, "webapps-manage",
                             Ci.nsIPermissionManager.ALLOW_ACTION);
permManager.addFromPrincipal(window.document.nodePrincipal, "browser",
                             Ci.nsIPermissionManager.ALLOW_ACTION);

SimpleTest.registerCleanupFunction(() => {
  gWitnessStorage.localStorage.clear();
  gWitnessStorage.sessionStorage.clear();

  permManager.removeFromPrincipal(window.document.nodePrincipal, "webapps-manage",
                                  Ci.nsIPermissionManager.ALLOW_ACTION);
  permManager.removeFromPrincipal(window.document.nodePrincipal, "browser",
                                  Ci.nsIPermissionManager.ALLOW_ACTION);
});

// We want to simulate that all apps are launchable, for testing purpose.
SpecialPowers.setAllAppsLaunchable(true);

// URL of the manifest of the app we want to install.
const gManifestURL = "http://www.example.com/chrome/dom/tests/mochitest/webapps/apps/basic.webapp";
// ID of the installed app.
var gTestAppId = 0;
// Cookies currently in the system.
var gCurrentCookiesCount = 0;
// Storages from a non-app to make sure we do not remove cookies from everywhere.
var gWitnessStorage = {};
// Storages for the app.
var gAppStorage = {};
// Storage for a mozbrowser inside the app.
var gBrowserStorage = {};

function runTest() {
  /*
   * We are setuping the witness storage (non-app) and will install the
   * application.
   * When the application is installed, we will insert it in an iframe and wait
   * for the load event. to be fired.
   */

  gWitnessStorage.localStorage = window.frames[0].localStorage;
  gWitnessStorage.sessionStorage = window.frames[0].sessionStorage;

  setupStorage(gWitnessStorage.localStorage);
  setupStorage(gWitnessStorage.sessionStorage);

  navigator.mozApps.install(gManifestURL, null).onsuccess = function() {
    gTestAppId = appsService.getAppLocalIdByManifestURL(gManifestURL);

    var frame = document.createElement('iframe');
    frame.setAttribute('mozbrowser', '');
    frame.setAttribute('mozapp', gManifestURL);
    frame.src = 'http://www.example.com/chrome/dom/tests/mochitest/localstorage/frame_clear_browser_data.html';
    frame.name = 'app';

    frame.addEventListener('load', appFrameLoadEvent);

    document.body.appendChild(frame);
  };
}

function appFrameLoadEvent() {
  /*
   * The app frame has been loaded. We can now add permissions for the app to
   * create browsers and we will load a page in this browser and wait for the
   * load event.
   */
  permManager.addFromPrincipal(window.frames[1].document.nodePrincipal, "browser",
                               Ci.nsIPermissionManager.ALLOW_ACTION);

  var frame = document.createElement('iframe');
  frame.setAttribute('mozbrowser', '');
  frame.src = 'http://example.com/tests/error404_2';

  frame.addEventListener('load', browserLoadEvent);

  document.getElementsByName('app')[0].contentDocument.body.appendChild(frame);
}

function browserLoadEvent() {
  /*
   * The browser inside the app has loaded.
   * We can now setup the app and browser storages and request the browser data
   * to be cleared.
   */

  gAppStorage.localStorage = window.frames[1].localStorage;
  gAppStorage.sessionStorage = window.frames[1].sessionStorage;

  gBrowserStorage.localStorage = window.frames[1].frames[0].localStorage;
  gBrowserStorage.sessionStorage = window.frames[1].frames[0].sessionStorage;

  setupStorage(gAppStorage.localStorage);
  setupStorage(gAppStorage.sessionStorage);
  setupStorage(gBrowserStorage.localStorage);
  setupStorage(gBrowserStorage.sessionStorage);

  frames[1].postMessage("clear", "*");

  waitForClearBrowserData();
};

function waitForClearBrowserData() {
  SimpleTest.executeSoon(function() {
    if (frames[1].document.getElementById('status').textContent != 'done') {
      waitForClearBrowserData();
    } else {
      checks();
    }
  });
}

function checks() {
  navigator.mozApps.mgmt.getAll().onsuccess = function() {
    for (i in this.result) {
      var app = this.result[i];
      if (app.manifestURL == gManifestURL) {
        is(gBrowserStorage.localStorage.getItem("foo"), null, "localstorage data have been deleted");
        is(gBrowserStorage.sessionStorage.getItem("foo"), "bar", "sessionstorage data have not been deleted");

        is(gAppStorage.localStorage.getItem("foo"), "bar", "data are still there");
        is(gAppStorage.sessionStorage.getItem("foo"), "bar", "data are still there");

        is(gWitnessStorage.localStorage.getItem("foo"), "bar", "data are still there");
        is(gWitnessStorage.sessionStorage.getItem("foo"), "bar", "data are still there");

        // Now we uninstall the app and make sure everything is clean.
        navigator.mozApps.mgmt.uninstall(app).onsuccess = function() {
          SimpleTest.finish();
        };
      }
    }
  };
}

addLoadEvent(() =>
  SpecialPowers.pushPrefEnv({set: [['dom.mozBrowserFramesEnabled', true]]}, () =>
    SpecialPowers.autoConfirmAppInstall(() =>
      SpecialPowers.autoConfirmAppUninstall(runTest)
    )
  )
);

</script>
</pre>
</body>
</html>