Bug 350525 - New session restore API needs accompanying unit tests, patch by Dietrich Ayala <dietrich@mozilla.com> and Adam Guthrie <ispiked@gmail.com>, r=sayrer
--- a/browser/components/sessionstore/Makefile.in
+++ b/browser/components/sessionstore/Makefile.in
@@ -45,9 +45,13 @@ XPIDL_MODULE = sessionstore
XPIDLSRCS = \
nsISessionStartup.idl \
nsISessionStore.idl \
$(NULL)
DIRS = src
+ifdef MOZ_MOCHITEST
+DIRS += test
+endif
+
include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/browser/components/sessionstore/test/Makefile.in
@@ -0,0 +1,49 @@
+# ***** 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@
+
+include $(DEPTH)/config/autoconf.mk
+
+
+DIRS += chrome \
+ $(NULL)
+
+include $(topsrcdir)/config/rules.mk
+
new file mode 100644
--- /dev/null
+++ b/browser/components/sessionstore/test/chrome/Makefile.in
@@ -0,0 +1,50 @@
+# ***** 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 = browser/components/sessionstore/test/chrome
+
+include $(DEPTH)/config/autoconf.mk
+include $(topsrcdir)/config/rules.mk
+
+_TEST_FILES = test_bug350525.xul \
+ $(NULL)
+
+libs:: $(_TEST_FILES)
+ $(INSTALL) $^ $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/browser/components/sessionstore/test/chrome/test_bug350525.xul
@@ -0,0 +1,132 @@
+<?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"?>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=350525
+-->
+<window title="Mozilla Bug 350525"
+ xmlns:html="http://www.w3.org/1999/xhtml"
+ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
+ xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+
+ <title>Test for Bug 350525</title>
+ <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>
+<body xmlns="http://www.w3.org/1999/xhtml">
+ <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=350525">Mozilla Bug 350525</a>
+
+ <p id="display"></p>
+
+ <pre id="test">
+ <script class="testbody" type="application/javascript">
+
+ /** Test for Bug 350525 **/
+
+ const Cc = Components.classes;
+ const Ci = Components.interfaces;
+ const Cr = Components.results;
+
+ // component
+ try {
+ Cc["@mozilla.org/browser/sessionstore;1"];
+ ok(1==1, "Able to reference the sessionstore component?");
+ } catch(ex) {
+ alert(ex);
+ ok(1==2, "Able to reference the sessionstore component?");
+ }
+
+ // service
+ try {
+ var ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
+ ok(true, "Able to reference the sessionstore service?");
+ } catch(ex) {
+ ok(false, "Able to reference the sessionstore service?");
+ }
+
+ // get current window, tabbrowser
+ var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
+ var windowEnumerator = wm.getEnumerator("navigator:browser");
+ var currentWindow = windowEnumerator.getNext();
+ var tabbrowser = currentWindow.getBrowser();
+
+ /*****************
+ undoCloseTab, getClosedTabCount
+ *****************/
+
+ // get closed tab count
+ var count = ss.getClosedTabCount(currentWindow);
+ ok(count > -1, "getClosedTabCount returns zero or more?");
+
+ // create a new tab
+ var newTab = tabbrowser.addTab("http://www.mozilla.org");
+
+ // remove tab
+ tabbrowser.removeTab(newTab);
+
+ // getClosedTabCount
+ var newcount = ss.getClosedTabCount(currentWindow);
+ todo(newcount > count, "After closing a tab, getClosedTabCount has been incremented? " + newcount + " > " + count);
+
+ // undoCloseTab
+ var undid = ss.undoCloseTab(currentWindow, null);
+ ok(undid != -1, "undoCloseTab throws?");
+
+ // clean up
+ tabbrowser.removeAllTabsBut(tabbrowser.selectedTab);
+
+ /*****************
+ setWindowValue
+ *****************/
+ var key = "key1";
+ var value = "value1";
+
+ // create a new tab
+ var newTab = tabbrowser.addTab("http://www.mozilla.org");
+
+ // test adding
+ ok(ss.setWindowValue(currentWindow, key, value) != -1, "Able to set a window value?");
+
+ // test retrieving
+ var storedValue = ss.getWindowValue(currentWindow, key);
+ is(value, storedValue, "Stored window value matches original?");
+
+ // test deleting
+ ok(ss.deleteWindowValue(currentWindow, key) != -1, "Delete window value?");
+
+ // value should not exist post-delete
+ is(ss.getWindowValue(currentWindow, key), "", "Fetching deleted window value fails?");
+
+ // clean up
+ tabbrowser.removeTab(newTab);
+
+ /*********************
+ tabValues
+ *********************/
+ key = "key1";
+ value = "value1";
+
+ // create a new tab
+ newTab = tabbrowser.addTab("http://www.mozilla.org");
+
+ // test adding
+ ok(ss.setTabValue(newTab, key, value) != -1, "Able to store a tab value?");
+
+ // test retrieving
+ var storedValue = ss.getTabValue(newTab, key);
+ ok(value==storedValue, "Stored tab value match original?");
+
+ // test deleting
+ ok(ss.deleteTabValue(newTab, key) != -1, "Able to delete a tab value?");
+ // value should not exist post-delete
+ ok(ss.getTabValue(newTab, key) == "", "Unable to retrieve deleted tab value?");
+
+ // clean up
+ tabbrowser.removeTab(newTab);
+
+ </script>
+ </pre>
+</body>
+
+</window>
deleted file mode 100644
--- a/browser/components/sessionstore/test/nsSessionStoreTest.xul
+++ /dev/null
@@ -1,140 +0,0 @@
-<?xml version="1.0"?>
-<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
-
-<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
- title="nsSessionstore Tests">
-
- <script type="text/javascript">
-
- const Cc = Components.classes;
- const Ci = Components.interfaces;
- const Cr = Components.results;
-
- var ssTests = {
- log: function sst_log(aMsg) {
- Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
- },
-
- run: function sst_run() {
- var tests = ["componentExists", "getService", "windowValues", "tabValues"];
- var results = {};
- tests.forEach(function(aTest) {
- try {
- results[aTest] = ssTests[aTest]();
- } catch(ex) {
- results[aTest] = false;
- }
- });
- return results;
- },
-
- componentExists: function sst_componentExists() {
- return Cc["@mozilla.org/browser/sessionstore;1"] ? true : false;
- },
-
- getService: function sst_getService() {
- return Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore) ? true : false;
- },
-
- // setWindowValue
- windowValues: function sst_windowValues() {
- var ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
- if (!ss)
- return false;
-
- var key = "key1";
- var value = "value1";
-
- // get current window, tabbrowser
- var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
- var windowEnumerator = wm.getEnumerator("navigator:browser");
- var currentWindow = windowEnumerator.getNext();
- var tabbrowser = currentWindow.getBrowser();
-
- // create a new tab
- var newTab = tabbrowser.addTab("http://www.mozilla.org");
-
- // test adding
- if (ss.setWindowValue(currentWindow, key, value) == -1) {
- this.log("setWindowValue failed");
- return false;
- }
-
- // test retrieving
- var storedValue = ss.getWindowValue(currentWindow, key);
- if (value != storedValue)
- return false;
-
- // test deleting
- if (ss.deleteWindowValue(currentWindow, key) == -1) {
- this.log("deleteWindowValue failed");
- return false;
- }
- if (ss.getWindowValue(currentWindow, key)) {
- this.log("deleteWindowValue failed: data was not deleted");
- return false;
- }
-
- // clean up
- tabbrowser.removeTab(newTab);
- return true;
- },
-
- // tabValues
- tabValues: function sst_tabValues() {
- var ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
- if (!ss)
- return false;
-
- var key = "key1";
- var value = "value1";
-
- // get current window, tabbrowser
- var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
- var windowEnumerator = wm.getEnumerator("navigator:browser");
- var currentWindow = windowEnumerator.getNext();
- var tabbrowser = currentWindow.getBrowser();
-
- // create a new tab
- var newTab = tabbrowser.addTab("http://www.mozilla.org");
-
- // test adding
- if (ss.setTabValue(newTab, key, value) == -1) {
- this.log("setTabValue failed");
- return false;
- }
-
- // test retrieving
- var storedValue = ss.getTabValue(newTab, key);
- if (value != storedValue)
- return false;
-
- // test deleting
- if (ss.deleteTabValue(newTab, key) == -1) {
- this.log("deleteTabValue failed");
- return false;
- }
- if (ss.getTabValue(newTab, key)) {
- this.log("deleteTabValue failed: data was not deleted");
- return false;
- }
-
- // clean up
- tabbrowser.removeTab(newTab);
- return true;
- }
- };
-
- function debug(aMsg) {
- Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
- }
-
- var results = ssTests.run();
-
- for (key in results) {
- debug(key + ": " + results[key]);
- }
-
- </script>
-
-</window>