Author: kien_nguyen
Date: 2012-02-03 02:58:09 -0500 (Fri, 03 Feb 2012)
New Revision: 8379
Added:
portal/trunk/gadgets/server/src/test/
portal/trunk/gadgets/server/src/test/java/
portal/trunk/gadgets/server/src/test/java/org/
portal/trunk/gadgets/server/src/test/java/org/exoplatform/
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/EndToEndServer.java
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/EndToEndTest.java
portal/trunk/gadgets/server/src/test/resources/
portal/trunk/gadgets/server/src/test/resources/conf/
portal/trunk/gadgets/server/src/test/resources/conf/gadgets/
portal/trunk/gadgets/server/src/test/resources/conf/gadgets/key.txt
portal/trunk/gadgets/server/src/test/resources/endtoend/
portal/trunk/gadgets/server/src/test/resources/endtoend/coreFeatures.xml
portal/trunk/gadgets/server/src/test/resources/endtoend/hello.xml
portal/trunk/gadgets/server/src/test/resources/endtoend/messages.xml
portal/trunk/gadgets/server/src/test/resources/endtoend/messages_ar.xml
portal/trunk/gadgets/server/src/test/resources/endtoend/test.json
portal/trunk/gadgets/server/src/test/resources/endtoend/testframework.js
Modified:
portal/trunk/gadgets/server/pom.xml
Log:
GTNPORTAL-2346 Integrate testcases for Gadget Server
Modified: portal/trunk/gadgets/server/pom.xml
===================================================================
--- portal/trunk/gadgets/server/pom.xml 2012-02-03 01:32:36 UTC (rev 8378)
+++ portal/trunk/gadgets/server/pom.xml 2012-02-03 07:58:09 UTC (rev 8379)
@@ -42,7 +42,15 @@
<dependentWarExcludes>WEB-INF/web**</dependentWarExcludes>
</configuration>
</plugin>
- </plugins>
+ </plugins>
+ <testResources>
+ <testResource>
+ <directory>src/test/resources</directory>
+ </testResource>
+ <testResource>
+ <directory>src/main/webapp</directory>
+ </testResource>
+ </testResources>
</build>
<dependencies>
@@ -118,5 +126,47 @@
<artifactId>commons-digester</artifactId>
<scope>provided</scope>
</dependency>
+
+ <!-- Test scope -->
+ <dependency>
+ <groupId>org.gatein.shindig</groupId>
+ <artifactId>shindig-common</artifactId>
+ <version>${org.shindig.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.exoplatform.portal</groupId>
+ <artifactId>exo.portal.gadgets-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.exoplatform.portal</groupId>
+ <artifactId>exo.portal.component.test.core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.exoplatform.portal</groupId>
+ <artifactId>exo.portal.component.portal</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty</artifactId>
+ <version>6.1.25</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>net.sourceforge.htmlunit</groupId>
+ <artifactId>htmlunit</artifactId>
+ <version>2.8</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Added:
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/EndToEndServer.java
===================================================================
---
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/EndToEndServer.java
(rev 0)
+++
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/EndToEndServer.java 2012-02-03
07:58:09 UTC (rev 8379)
@@ -0,0 +1,171 @@
+/**
+ * Copyright (C) 2012 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.portal.gadget.server;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.shindig.common.PropertiesModule;
+import org.apache.shindig.common.cache.ehcache.EhCacheModule;
+import org.apache.shindig.gadgets.DefaultGuiceModule;
+import org.apache.shindig.gadgets.servlet.AuthenticationModule;
+import org.apache.shindig.gadgets.servlet.ConcatProxyServlet;
+import org.apache.shindig.gadgets.servlet.GadgetRenderingServlet;
+import org.apache.shindig.gadgets.servlet.JsServlet;
+import org.apache.shindig.gadgets.servlet.MakeRequestServlet;
+import org.exoplatform.portal.gadget.core.ExoModule;
+import org.exoplatform.portal.gadget.core.ExoOAuthModule;
+import org.exoplatform.portal.gadget.core.GateInGuiceServletContextListener;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.handler.ResourceHandler;
+import org.mortbay.jetty.servlet.Context;
+import org.mortbay.jetty.servlet.ServletHolder;
+import org.mortbay.resource.Resource;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Map;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.Maps;
+
+/**
+ * Suite for running the end-to-end tests. The suite is responsible for starting up and
shutting
+ * down the server.
+ *
+ * @author <a href="kienna(a)exoplatform.com">Kien Nguyen</a>
+ * @version $Revision$
+ */
+public class EndToEndServer
+{
+ private static final int JETTY_PORT = 9003;
+
+ private static final String GADGET_BASE = "/eXoGadgetServer/gadgets/ifr";
+
+ private static final String CONCAT_BASE =
"/eXoGadgetServer/gadgets/concat";
+
+ private static final String JS_BASE = "/eXoGadgetServer/gadgets/js/*";
+
+ private static final String MAKE_REQUEST_BASE =
"/eXoGadgetServer/gadgets/makeRequest";
+
+ public static final String SERVER_URL = "http://localhost:" + JETTY_PORT;
+
+ public static final String GADGET_BASEURL = SERVER_URL + GADGET_BASE;
+
+ private final Server server;
+
+ /** Fake error code for data service servlet request */
+ protected int errorCode;
+
+ /** Fake error message for data service servlet request */
+ protected String errorMessage;
+
+ public EndToEndServer() throws Exception
+ {
+ server = createServer(JETTY_PORT);
+ }
+
+ public void start() throws Exception
+ {
+ server.start();
+ }
+
+ public void stop() throws Exception
+ {
+ server.stop();
+ }
+
+ public void clearDataServiceError()
+ {
+ errorCode = 0;
+ }
+
+ public void setDataServiceError(int errorCode, String errorMessage)
+ {
+ this.errorCode = errorCode;
+ this.errorMessage = errorMessage;
+ }
+
+ /**
+ * Create the server for end-to-end tests.
+ */
+ private Server createServer(int port) throws Exception
+ {
+ System.setProperty("shindig.port", String.valueOf(port));
+ System.setProperty("jetty.port", String.valueOf(port));
+ System.setProperty("gatein.gadgets.securitytokenkeyfile",
"src/test/resources/conf/gadgets/key.txt");
+
+ Server newServer = new Server(port);
+
+ // Attach the test resources in /endtoend as static content for the test
+ ResourceHandler resources = new ResourceHandler();
+ URL resource = EndToEndTest.class.getResource("/endtoend");
+ resources.setBaseResource(Resource.newResource(resource));
+ newServer.addHandler(resources);
+
+ Context context = new Context(newServer, "/", Context.SESSIONS);
+ context.addEventListener(new GateInGuiceServletContextListener());
+
+ Map<String, String> initParams = Maps.newHashMap();
+ String modules =
+ Joiner.on(":").join(ExoModule.class.getName(),
ExoOAuthModule.class.getName(),
+ DefaultGuiceModule.class.getName(), AuthenticationModule.class.getName(),
PropertiesModule.class.getName(),
+ EhCacheModule.class.getName());
+
+ initParams.put(GateInGuiceServletContextListener.MODULES_ATTRIBUTE, modules);
+ context.setInitParams(initParams);
+
+ // Attach the gadget rendering servlet
+ ServletHolder gadgetServletHolder = new ServletHolder(new
GadgetRenderingServlet());
+ context.addServlet(gadgetServletHolder, GADGET_BASE);
+
+ // Attach the ConcatProxyServlet - needed for rewritten JS
+ ServletHolder concatHolder = new ServletHolder(new ConcatProxyServlet());
+ context.addServlet(concatHolder, CONCAT_BASE);
+
+ // Attach the JsServlet - needed for rewritten JS
+ ServletHolder jsHolder = new ServletHolder(new JsServlet());
+ context.addServlet(jsHolder, JS_BASE);
+
+ // Attach MakeRequestServlet
+ ServletHolder makeRequestHolder = new ServletHolder(new MakeRequestServlet());
+ context.addServlet(makeRequestHolder, MAKE_REQUEST_BASE);
+
+ // Attach an EchoServlet, used to test proxied rendering
+ ServletHolder echoHolder = new ServletHolder(new EchoServlet());
+ context.addServlet(echoHolder, "/echo");
+
+ return newServer;
+ }
+
+ static private class EchoServlet extends HttpServlet
+ {
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
IOException
+ {
+ req.setCharacterEncoding("UTF-8");
+ resp.setContentType(req.getContentType());
+
+ IOUtils.copy(req.getReader(), resp.getWriter());
+ }
+ }
+}
Added:
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/EndToEndTest.java
===================================================================
---
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/EndToEndTest.java
(rev 0)
+++
portal/trunk/gadgets/server/src/test/java/org/exoplatform/portal/gadget/server/EndToEndTest.java 2012-02-03
07:58:09 UTC (rev 8379)
@@ -0,0 +1,165 @@
+/**
+ * Copyright (C) 2012 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.portal.gadget.server;
+
+import org.apache.shindig.auth.BasicSecurityToken;
+import org.apache.shindig.auth.BasicSecurityTokenCodec;
+import org.apache.shindig.auth.SecurityToken;
+import org.apache.shindig.common.crypto.BlobCrypterException;
+import org.exoplatform.portal.AbstractPortalTest;
+
+import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
+import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
+import com.gargoylesoftware.htmlunit.Page;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import com.gargoylesoftware.htmlunit.html.HTMLParserListener;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+
+/**
+ * Base class for end-to-end tests.
+ *
+ * @author <a href="kienna(a)exoplatform.com">Kien Nguyen</a>
+ * @version $Revision$
+ */
+public class EndToEndTest extends AbstractPortalTest
+{
+ private static final String[] EXPECTED_RESOURCES = {"hello.xml",
"coreFeatures.xml", "testframework.js"};
+
+ static private EndToEndServer server = null;
+
+ private WebClient webClient;
+
+ private CollectingAlertHandler alertHandler;
+
+ private SecurityToken token;
+
+ private String language;
+
+ public void testCheckResources() throws Exception
+ {
+ for (String resource : EXPECTED_RESOURCES)
+ {
+ String url = EndToEndServer.SERVER_URL + '/' + resource;
+ Page p = webClient.getPage(url);
+ assertEquals("Failed to load test resource " + url, 200,
p.getWebResponse().getStatusCode());
+ }
+ }
+
+ public void testHelloWorld() throws Exception
+ {
+ HtmlPage page = executeAllPageTests("hello");
+ final String pageAsText = page.asText();
+ assertTrue(pageAsText.contains("Hello, world!"));
+ }
+
+ public void testCoreFeature() throws Exception
+ {
+ HtmlPage page = executeAllPageTests("coreFeatures");
+ final String pageAsText = page.asText();
+ assertTrue(pageAsText.contains("Core Features Test Cases!"));
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ server.stop();
+ }
+
+ @Override
+ public void setUp() throws Exception
+ {
+ server = new EndToEndServer();
+ server.start();
+
+ webClient = new WebClient();
+ // NicelyResynchronizingAjaxController changes XHR calls from asynchronous
+ // to synchronous, saving the test from needing to wait or sleep for XHR
+ // completion.
+ webClient.setAjaxController(new NicelyResynchronizingAjaxController());
+ webClient.waitForBackgroundJavaScript(2000);
+ webClient.setHTMLParserListener(HTMLParserListener.LOG_REPORTER);
+ webClient.setTimeout(3000);
+
+ alertHandler = new CollectingAlertHandler();
+ webClient.setAlertHandler(alertHandler);
+ token = createToken("canonical", "john.doe");
+ language = null;
+ server.clearDataServiceError();
+ }
+
+ /**
+ * Executes a page test by loading the HTML page.
+ * @param testName name of the test, which must match a gadget XML file
+ * name in test/resources/endtoend (minus .xml).
+ * @param testMethod name of the javascript method to execute
+ * @return the parsed HTML page
+ */
+ private HtmlPage executePageTest(String testName, String testMethod, boolean caja)
throws IOException
+ {
+ if (!testName.endsWith(".xml"))
+ {
+ testName = testName + ".xml";
+ }
+
+ String gadgetUrl = EndToEndServer.SERVER_URL + '/' + testName;
+ String url = EndToEndServer.GADGET_BASEURL + "?url=" +
URLEncoder.encode(gadgetUrl, "UTF-8");
+ BasicSecurityTokenCodec codec = new BasicSecurityTokenCodec();
+ url += "&st=" + URLEncoder.encode(codec.encodeToken(token),
"UTF-8");
+ if (testMethod != null)
+ {
+ url += "&testMethod=" + URLEncoder.encode(testMethod,
"UTF-8");
+ }
+ if (caja)
+ {
+ url += "&caja=1&libs=caja";
+ }
+
+ url += "&nocache=1";
+ if (language != null)
+ {
+ url += "&lang=" + language;
+ }
+ Page page = webClient.getPage(url);
+ if (!(page instanceof HtmlPage))
+ {
+ fail("Got wrong page type. Was: " +
page.getWebResponse().getContentType());
+ }
+ webClient.waitForBackgroundJavaScript(3000);
+ return (HtmlPage)page;
+ }
+
+ /**
+ * Executes all page test in a single XML file.
+ * @param testName name of the test, which must match a gadget XML file
+ * name in test/resources/endtoend (minus .xml).
+ * @throws IOException
+ */
+ private HtmlPage executeAllPageTests(String testName) throws IOException
+ {
+ return executePageTest(testName, "all", false);
+ }
+
+ private BasicSecurityToken createToken(String owner, String viewer) throws
BlobCrypterException
+ {
+ return new BasicSecurityToken(owner, viewer, "test", "domain",
"appUrl", "1", "default", null, null);
+ }
+}
\ No newline at end of file
Added: portal/trunk/gadgets/server/src/test/resources/conf/gadgets/key.txt
===================================================================
--- portal/trunk/gadgets/server/src/test/resources/conf/gadgets/key.txt
(rev 0)
+++ portal/trunk/gadgets/server/src/test/resources/conf/gadgets/key.txt 2012-02-03
07:58:09 UTC (rev 8379)
@@ -0,0 +1 @@
+XeQtHqy4StAWBXggc4pQx/5fxBsDYUUNhk8U08lgCoA=
Added: portal/trunk/gadgets/server/src/test/resources/endtoend/coreFeatures.xml
===================================================================
--- portal/trunk/gadgets/server/src/test/resources/endtoend/coreFeatures.xml
(rev 0)
+++ portal/trunk/gadgets/server/src/test/resources/endtoend/coreFeatures.xml 2012-02-03
07:58:09 UTC (rev 8379)
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Module>
+ <ModulePrefs title="EndToEndTest">
+ <Locale messages="messages.xml"/>
+ <Locale lang="ar" messages="messages_ar.xml"
language_direction="rtl"/>
+ </ModulePrefs>
+ <Content type="html">
+ <![CDATA[
+ /** For test message bundle */
+ <span id="substituteHere">__MSG_TEST__</span>
+ <span id="bidi">__BIDI_DIR__</span>
+
+ /** Script test framework */
+ <script type="text/javascript"
src="/testframework.js"></script>
+
+ /** Test cases */
+ <script type="text/javascript">
+
+ var tests = {
+ /** Test json feature */
+ jsonTest: function() {
+ var val = {foo: 1, bar: [0, 1, 2], baz: {key: 'value'}};
+ var str = gadgets.json.stringify(val);
+ assertTrue("Serialization missing scalar value",
/"foo":1/.test(str));
+ assertTrue("Serialization missing array value",
/"bar":\[0,1,2\]/.test(str));
+ assertTrue("Serialization missing literal value",
/"baz":\{"key":"value"\}/.test(str));
+ finished();
+ },
+
+ /** Test fetching a makeRequest proxied call */
+ makeRequestTest: function() {
+ function receivedData(response) {
+ assertEquals('Text property not set', '{"key":
"value"}', response.text);
+ assertEquals('Data property not set', '{"key":
"value"}', response.data);
+ assertEquals('Response code not set', 200, response.rc);
+ assertEquals('Errors not an empty array', 0,
response.errors.length);
+ finished();
+ }
+
+ gadgets.io.makeRequest('http://localhost:9003/test.json',
receivedData);
+ },
+
+ /** Test fetching a makeRequest proxied for JSON */
+ makeRequestJsonTest: function() {
+ function receivedData(response) {
+ assertEquals('Text property not set', '{"key":
"value"}', response.text);
+ assertEquals('Data property not set', 'value',
response.data.key);
+ assertEquals('Response code not set', 200, response.rc);
+ assertEquals('Errors not an empty array', 0,
response.errors.length);
+ finished();
+ }
+
+ gadgets.io.makeRequest('http://localhost:9003/test.json',
receivedData,
+ {CONTENT_TYPE: 'JSON'});
+ },
+
+ /** Test fetching a makeRequest proxied call that fails */
+ makeRequestJsonDoesntExistTest: function() {
+ function receivedData(response) {
+ assertEquals('Response code not set', 404, response.rc);
+ assertEquals('Errors not an empty array', 1,
response.errors.length);
+ finished();
+ }
+
+ gadgets.io.makeRequest('http://localhost:9003/doesntexist.txt',
receivedData);
+ },
+
+ /** Message bundle */
+ messageBundleTest: function() {
+ var msg = document.getElementById('substituteHere');
+ var bidi = document.getElementById('bidi');
+ var prefs = new gadgets.Prefs();
+ var expectedText = 'test FTW';
+ var expectedLtr = 'ltr';
+
+ if('ar' == prefs.getLang()) {
+ expectedText = 'WTF tset';
+ expectedLtr = 'rtl';
+ }
+
+ assertEquals('Text not substituted', expectedText,
msg.firstChild.data);
+ assertEquals('getMsg not successful', expectedText,
prefs.getMsg('TEST'));
+ assertEquals('BIDI not substituted', expectedLtr,
bidi.firstChild.data);
+ finished();
+ }
+ }
+ </script>
+
+ <div>Core Features Test Cases!</div>
+ ]]>
+ </Content>
+</Module>
Added: portal/trunk/gadgets/server/src/test/resources/endtoend/hello.xml
===================================================================
--- portal/trunk/gadgets/server/src/test/resources/endtoend/hello.xml
(rev 0)
+++ portal/trunk/gadgets/server/src/test/resources/endtoend/hello.xml 2012-02-03 07:58:09
UTC (rev 8379)
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Module>
+ <ModulePrefs title="hello world example" description="The hello
world gadget." />
+ <Content type="html"> <![CDATA[
+ Hello, world!
+ ]]>
+ </Content>
+</Module>
\ No newline at end of file
Added: portal/trunk/gadgets/server/src/test/resources/endtoend/messages.xml
===================================================================
--- portal/trunk/gadgets/server/src/test/resources/endtoend/messages.xml
(rev 0)
+++ portal/trunk/gadgets/server/src/test/resources/endtoend/messages.xml 2012-02-03
07:58:09 UTC (rev 8379)
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<messagebundle>
+ <msg name="TEST">test FTW</msg>
+</messagebundle>
\ No newline at end of file
Added: portal/trunk/gadgets/server/src/test/resources/endtoend/messages_ar.xml
===================================================================
--- portal/trunk/gadgets/server/src/test/resources/endtoend/messages_ar.xml
(rev 0)
+++ portal/trunk/gadgets/server/src/test/resources/endtoend/messages_ar.xml 2012-02-03
07:58:09 UTC (rev 8379)
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<messagebundle>
+ <!-- No, not really Arabic -->
+ <msg name="TEST">WTF tset</msg>
+</messagebundle>
\ No newline at end of file
Added: portal/trunk/gadgets/server/src/test/resources/endtoend/test.json
===================================================================
--- portal/trunk/gadgets/server/src/test/resources/endtoend/test.json
(rev 0)
+++ portal/trunk/gadgets/server/src/test/resources/endtoend/test.json 2012-02-03 07:58:09
UTC (rev 8379)
@@ -0,0 +1 @@
+{"key": "value"}
\ No newline at end of file
Added: portal/trunk/gadgets/server/src/test/resources/endtoend/testframework.js
===================================================================
--- portal/trunk/gadgets/server/src/test/resources/endtoend/testframework.js
(rev 0)
+++ portal/trunk/gadgets/server/src/test/resources/endtoend/testframework.js 2012-02-03
07:58:09 UTC (rev 8379)
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var tests;
+
+function assertTrue(msg, value) {
+ if (!value) {
+ throw "assertTrue() failed: " + msg;
+ }
+}
+
+function assertFalse(msg, value) {
+ if (value) {
+ throw "assertFalse() failed: " + msg;
+ }
+}
+
+function assertEquals(msg, a, b) {
+ if (a != b) {
+ throw "assertEquals() failed: " + msg +
+ "\nExpected \"" + a + "\", was \"" + b +
"\"";
+ }
+}
+
+/**
+ * Signals the server code that a test successfully finished. This
+ * method must be called to verify that a test completed successfully,
+ * instead of simply failing to load.
+ */
+
+function finished() {
+ alert("FINISHED");
+ // After finishing run the next test..
+ runOneTest();
+}
+
+/** Executes the test identifed by the testMethod URL parameter */
+function executeTest() {
+ var params = gadgets.util.getUrlParameters();
+ var testMethod = params["testMethod"];
+ if (!testMethod) {
+ throw "No testMethod parameter found.";
+ }
+
+ // "all": run all the tests
+ if ("all" == testMethod) {
+ allTests();
+ } else {
+ // Show an alert for the test method name, identifying what test started.
+ alert(testMethod);
+
+ var testMethodFunction = tests[testMethod];
+ if (!testMethodFunction) {
+ throw "Test method " + testMethod + " not found.";
+ }
+
+ // Execute the test method
+ testMethodFunction();
+ }
+}
+
+var testFunctions = [];
+
+function runOneTest() {
+ var t = testFunctions.pop();
+ if (t) {
+ alert(t);
+ tests[t]();
+ }
+}
+function allTests() {
+ // Collect the test names and iterate through them serially
+ for (var testMethod in tests) {
+ testFunctions.push(testMethod);
+ }
+ runOneTest();
+}
+
+gadgets.util.registerOnLoadHandler(executeTest);