Bug 371127, add a test for domstorage in chrome, r=sayrer
authorenndeakin@sympatico.ca
Thu, 26 Apr 2007 08:35:57 -0700
changeset 839 2c50767cdf7f5f1142c6f78421bb65c44d400644
parent 838 72c7ca26cf0b2796177f9959262b59b48cc92ff2
child 840 ed668038d7df248d06081600b8fb1e92a2b372b7
push id1
push userbsmedberg@mozilla.com
push dateThu, 20 Mar 2008 16:49:24 +0000
treeherdermozilla-central@61007906a1f8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssayrer
bugs371127
milestone1.9a4pre
Bug 371127, add a test for domstorage in chrome, r=sayrer
dom/tests/mochitest/Makefile.in
dom/tests/mochitest/chrome/Makefile.in
dom/tests/mochitest/chrome/domstorage_global.js
dom/tests/mochitest/chrome/domstorage_global.xul
dom/tests/mochitest/chrome/test_domstorage.xul
--- a/dom/tests/mochitest/Makefile.in
+++ b/dom/tests/mochitest/Makefile.in
@@ -42,12 +42,13 @@ VPATH		= @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 
 DIRS		+= dom-level1-core \
 		dom-level2-core \
 		ajax \
 		bugs \
+		chrome \
 		$(NULL)
 
 include $(topsrcdir)/config/rules.mk
 
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/chrome/Makefile.in
@@ -0,0 +1,53 @@
+#
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is
+# Mozilla Foundation.
+# Portions created by the Initial Developer are Copyright (C) 2007
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either of the GNU General Public License Version 2 or later (the "GPL"),
+# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH		= ../../../..
+topsrcdir	= @top_srcdir@
+srcdir		= @srcdir@
+VPATH		= @srcdir@
+relativesrcdir  = dom/tests/mochitest/chrome
+
+include $(DEPTH)/config/autoconf.mk
+include $(topsrcdir)/config/rules.mk
+
+_TEST_FILES = test_domstorage.xul \
+		domstorage_global.xul \
+		domstorage_global.js \
+		$(NULL)
+
+libs:: $(_TEST_FILES)
+	$(INSTALL) $^ $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/chrome/domstorage_global.js
@@ -0,0 +1,155 @@
+function test_DOMStorage_global()
+{
+  var currentDomain = "mozilla.com";
+
+  var globalStorage = window.globalStorage;
+  is(globalStorage instanceof StorageList, true, "globalStorage property");
+
+  var storage = globalStorage.namedItem(currentDomain);
+  is(storage instanceof Storage, true, "StorageList namedItem");
+
+  var storage2 = globalStorage[currentDomain];
+  is(storage2 instanceof Storage, true, "StorageList property syntax");
+
+  is(storage, storage2, "StorageList namedItem and array return same value");
+
+  // the initial value should be null
+  test_DOMStorage_global_Item(storage, "sample", null, 0, "initial");
+
+  // if no key is supplied, the return value will be null
+  is(storage.getItem("", "Test"), null, "getItem no key");
+
+  exh = false;
+  try {
+    storage.key(0);
+  }
+  catch (ex) { exh = true; }
+  is(exh, true, "key index 0 too high");
+
+  // change the value
+  storage.setItem("sample", "This is the first message");
+  var item = test_DOMStorage_global_Item(storage, "sample", "This is the first message", 1, "setItem");
+
+  // change value using property syntax
+  storage.test = "Second message";
+  test_DOMStorage_global_Item(storage, "test", "Second message", 2, "setItem property");
+
+  // change the first value using property syntax
+  storage.sample = "Third message is this";
+  var item2 = test_DOMStorage_global_Item(storage, "sample", "Third message is this", 2, "setItem property again");
+
+  is(item.value, "Third message is this", "other item reference holds updated value");
+
+  // change the second value using setItem
+  storage.setItem("test", "Look at this, the fourth message");
+  test_DOMStorage_global_Item(storage, "test", "Look at this, the fourth message", 2, "setItem again");
+
+  // don't use this test currently. The spec says that the same object should
+  // always be returned, but this doesn't happen in chrome due to the wrappers
+  // being different
+  // is(item, item2, "same item always returned");
+
+  // using an empty key should not change the value
+  storage.setItem("", "Message Number Five");
+  test_DOMStorage_global_Item(storage, "test", "Look at this, the fourth message", 2, "setItem no key");
+  is(storage.getItem("", "Test"), null, "getItem no key");
+
+  // now test iteration through the keys
+  var key1 = storage.key(0);
+  var key2 = storage.key(1);
+  is(key1 != key2 && (key1 == "sample" || key1 == "test") &&
+                                (key2 == "sample" || key2 == "test"), true, "key");
+  exh = false;
+  try {
+    storage.key(2);
+  }
+  catch (ex) { exh = true; }
+  is(exh, true, "key index 2 too high");
+
+  storage.removeItem("sample");
+  test_DOMStorage_global_Item(storage, "sample", null, 1, "removeItem");
+
+  is(item.value, "", "other item reference holds deleted value");
+
+  delete storage.test;
+  test_DOMStorage_global_Item(storage, "test", null, 0, "delete item");
+
+  storage.removeItem("test");
+
+  // now test size constraints
+
+  // create a string which is 1MB - 4 bytes long. The key will use up this
+  // extra 4 bytes though.
+  var stringbit = "---abcdefghijklmnopqrstuvwxyz---";
+  var longstring = "This is quite a long string:";
+  for (var l = 0; l < 32767; l++)
+    longstring += stringbit;
+
+  var s;
+  for (s = 0; s < 5; s++)
+    storage.setItem("key" + s, longstring);
+
+  // at this point, there will be 5MB of data stored, so no more is allowed
+  exh = false;
+  try {
+    storage.setItem("keyfail", "One");
+  }
+  catch (ex) { exh = true; }
+  is(exh, true, "per-domain size constraint");
+
+  // change the first item, to free up space
+  storage.setItem("key1", "This");
+
+  // there is just under 1MB of space available, so this should fail 
+  exh = false;
+  try {
+    storage.setItem("keyfail", longstring);
+  }
+  catch (ex) { exh = true; }
+  is(exh, true, "per-domain size constraint second check");
+
+  // now clear up another value, and set it back again
+  storage.removeItem("key2");
+  storage.setItem("key2", longstring);
+
+  // this should still fail
+  exh = false;
+  try {
+    storage.setItem(keyfail, longstring);
+  }
+  catch (ex) { exh = true; }
+  is(exh, true, "per-domain size constraint third check");
+
+  // this should be stored OK
+  storage.setItem("key2", "Simple string");
+
+  for (s = 0; s < 5; s++)
+    storage.removeItem("key" + s);
+}
+
+function test_DOMStorage_global_Item(storage, key, expectedvalue, expectedlength, testid)
+{
+  var item = storage.getItem(key);
+  if (expectedvalue != null)
+    is(item instanceof StorageItem, true, testid + " is a Storage");
+
+  is(expectedvalue == null ? item : "" + item, expectedvalue, testid + " getItem");
+
+  var item2 = storage[key];
+  // skip this check for this test, as the property isn't actually removed
+  if (testid != "removeItem") {
+    if (item === null)
+      is(item2, undefined, testid + " get property syntax");
+    else
+      is(item2, item, testid + " get property syntax");
+  }
+  if (expectedvalue != null)
+    is(item2 instanceof StorageItem, true, testid + " property syntax is a Storage");
+
+  if (expectedvalue != null)
+    is(item.value, expectedvalue, testid + " value");
+
+  is(storage.length, expectedlength, testid + " length");
+
+  return item;
+}
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/chrome/domstorage_global.xul
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
+<!--
+  Test for Persistent Storage in chrome
+  -->
+<window id="sample-window" width="400" height="400"
+        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript" 
+          src="chrome://mochikit/content/MochiKit/packed.js"></script>
+  <script type="application/javascript" 
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>      
+
+<script src="domstorage_global.js"/>
+
+<script>
+test_DOMStorage_global();
+window.opener.wrappedJSObject.done();
+</script>
+
+<body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>
+
+</window>
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/chrome/test_domstorage.xul
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
+<!--
+  Test for Persistent Storage in chrome
+  -->
+<window id="sample-window" width="400" height="400"
+        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript" 
+          src="chrome://mochikit/content/MochiKit/packed.js"></script>
+  <script type="application/javascript" 
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>      
+
+<script>
+SimpleTest.waitForExplicitFinish();
+
+var newwindow = window.open("domstorage_global.xul", "testwindow", "chrome");
+
+function done()
+{
+  SimpleTest.finish();
+  newwindow.close();
+}
+</script>
+
+<body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>
+
+</window>