summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorasargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-25 15:51:55 +0000
committerasargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-25 15:51:55 +0000
commitb93865b931f15ef93230c7ab6946224a30a6a5de (patch)
tree558abcd4e2755480816707e5f61cc55986367f0d
parent371ed7a0e83c44764fef64ddbf00996b5385271e (diff)
downloadchromium_src-b93865b931f15ef93230c7ab6946224a30a6a5de.zip
chromium_src-b93865b931f15ef93230c7ab6946224a30a6a5de.tar.gz
chromium_src-b93865b931f15ef93230c7ab6946224a30a6a5de.tar.bz2
Introduce "testFunction" to reduce boilerplate in extensions API tests.
BUG=none TEST=none Review URL: http://codereview.chromium.org/173284 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@24232 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--[-rwxr-xr-x]chrome/test/data/extensions/api_test/bookmarks/api_test.js189
-rw-r--r--[-rwxr-xr-x]chrome/test/data/extensions/api_test/bookmarks/test.js204
2 files changed, 204 insertions, 189 deletions
diff --git a/chrome/test/data/extensions/api_test/bookmarks/api_test.js b/chrome/test/data/extensions/api_test/bookmarks/api_test.js
index b72ef94..84d0bb1 100755..100644
--- a/chrome/test/data/extensions/api_test/bookmarks/api_test.js
+++ b/chrome/test/data/extensions/api_test/bookmarks/api_test.js
@@ -1,82 +1,107 @@
-// api_test.js
-// mini-framework for ExtensionApiTest browser tests
-// TODO(erikkay) - figure out a way to share this code across extensions
-
-var completed = false;
-var tests;
-var currentTest;
-
-function complete() {
- completed = true;
-
- // a bit of a hack just to try to get the script to stop running at this point
- throw "completed";
-}
-
-function fail(message) {
- if (completed) throw "completed";
-
- var stack;
- try {
- crash.me += 0; // intentional exception to get the stack trace
- } catch (e) {
- stack = e.stack.split("\n");
- stack = stack.slice(2); // remove title and fail()
- stack = stack.join("\n");
- }
-
- if (!message) {
- message = "FAIL (no message)";
- }
- message += "\n" + stack;
- console.log("[FAIL] " + currentTest.name + ": " + message);
- chrome.test.fail(message);
- complete();
-}
-
-function allTestsSucceeded() {
- console.log("All tests succeeded");
- if (completed) throw "completed";
-
- chrome.test.pass();
- complete();
-}
-
-function runNextTest() {
- currentTest = tests.shift();
- if (!currentTest) {
- allTestsSucceeded();
- return;
- }
- currentTest.call();
-}
-
-function succeed() {
- console.log("[SUCCESS] " + currentTest.name);
- runNextTest();
-}
-
-window.onerror = function(message, url, code) {
- if (completed) return;
-
- fail(message);
-};
-
-function assertTrue(test, message) {
- if (test !== true) {
- if (typeof(test) == "string") {
- if (message) {
- message = test + "\n" + message;
- } else {
- message = test;
- }
- }
- fail(message);
- }
-}
-
-function assertNoLastError() {
- if (chrome.extension.lastError != undefined) {
- fail("lastError.message == " + chrome.extension.lastError.message);
- }
-}
+// api_test.js
+// mini-framework for ExtensionApiTest browser tests
+// TODO(erikkay) - figure out a way to share this code across extensions
+
+var completed = false;
+var tests;
+var currentTest;
+
+function complete() {
+ completed = true;
+
+ // a bit of a hack just to try to get the script to stop running at this point
+ throw "completed";
+}
+
+function fail(message) {
+ if (completed) throw "completed";
+
+ var stack;
+ try {
+ crash.me += 0; // intentional exception to get the stack trace
+ } catch (e) {
+ stack = e.stack.split("\n");
+ stack = stack.slice(2); // remove title and fail()
+ stack = stack.join("\n");
+ }
+
+ if (!message) {
+ message = "FAIL (no message)";
+ }
+ message += "\n" + stack;
+ console.log("[FAIL] " + currentTest.name + ": " + message);
+ chrome.test.fail(message);
+ complete();
+}
+
+function allTestsSucceeded() {
+ console.log("All tests succeeded");
+ if (completed) throw "completed";
+
+ chrome.test.pass();
+ complete();
+}
+
+function runNextTest() {
+ currentTest = tests.shift();
+ if (!currentTest) {
+ allTestsSucceeded();
+ return;
+ }
+ currentTest.call();
+}
+
+function succeed() {
+ console.log("[SUCCESS] " + currentTest.name);
+ runNextTest();
+}
+
+window.onerror = function(message, url, code) {
+ if (completed) return;
+
+ fail(message);
+};
+
+function assertTrue(test, message) {
+ if (test !== true) {
+ if (typeof(test) == "string") {
+ if (message) {
+ message = test + "\n" + message;
+ } else {
+ message = test;
+ }
+ }
+ fail(message);
+ }
+}
+
+function assertNoLastError() {
+ if (chrome.extension.lastError != undefined) {
+ fail("lastError.message == " + chrome.extension.lastError.message);
+ }
+}
+
+// Wrapper for generating test functions, that takes care of calling
+// assertNoLastError() and succeed() for you.
+function testFunction(func) {
+ return function() {
+ assertNoLastError();
+ try {
+ func.apply(null, arguments);
+ } catch (e) {
+ var stack = null;
+ if (typeof(e.stack) != "undefined") {
+ stack = e.stack.toString()
+ }
+ var msg = "Exception during execution of testFunction in " +
+ currentTest.name;
+ if (stack) {
+ msg += "\n" + stack;
+ } else {
+ msg += "\n(no stack available)";
+ }
+ fail(msg);
+ }
+ succeed();
+ };
+}
diff --git a/chrome/test/data/extensions/api_test/bookmarks/test.js b/chrome/test/data/extensions/api_test/bookmarks/test.js
index 69dc4dd..bab0de2 100755..100644
--- a/chrome/test/data/extensions/api_test/bookmarks/test.js
+++ b/chrome/test/data/extensions/api_test/bookmarks/test.js
@@ -1,107 +1,97 @@
-// bookmarks api test
-// browser_tests.exe --gtest_filter=ExtensionApiTest.Bookmarks
-
-var expected = [
- {"children": [
- {"children": [], "id": "1", "parentId": "0", "index": 0, "title":"Bookmarks bar"},
- {"children": [], "id": "2", "parentId": "0", "index": 1, "title":"Other bookmarks"}
- ],
- "id": "0", "title": ""
- }
-];
-
-function compareNode(left, right) {
- //console.log(JSON.stringify(left));
- //console.log(JSON.stringify(right));
- // TODO(erikkay): do some comparison of dateAdded
- if (left.id != right.id)
- return "id mismatch: " + left.id + " != " + right.id;
- if (left.title != right.title)
- return "title mismatch: " + left.title + " != " + right.title;
- if (left.url != right.url)
- return "url mismatch: " + left.url + " != " + right.url;
- if (left.index != right.index)
- return "index mismatch: " + left.index + " != " + right.index;
- return true;
-}
-
-function compareTrees(left, right) {
- //console.log(JSON.stringify(left));
- //console.log(JSON.stringify(right));
- if (left == null && right == null) {
- console.log("both left and right are NULL");
- return true;
- }
- if (left == null || right == null)
- return left + " !+ " + right;
- if (left.length != right.length)
- return "count mismatch: " + left.length + " != " + right.length;
- for (var i = 0; i < left.length; i++) {
- var result = compareNode(left[i], right[i]);
- if (result !== true) {
- console.log(JSON.stringify(left));
- console.log(JSON.stringify(right));
- return result;
- }
- result = compareTrees(left[i].children, right[i].children);
- if (result !== true)
- return result;
- }
- return true;
-}
-
-var tests = [
- function getTree() {
- chrome.bookmarks.getTree(function(results) {
- assertNoLastError();
- assertTrue(compareTrees(results, expected),
- "getTree() result != expected");
- expected = results;
- succeed();
- });
- },
-
- function get() {
- chrome.bookmarks.get("1", function(results) {
- assertNoLastError();
- assertTrue(compareNode(results[0], expected[0].children[0]));
- succeed();
- });
- },
-
- function getArray() {
- chrome.bookmarks.get(["1", "2"], function(results) {
- assertNoLastError();
- assertTrue(compareNode(results[0], expected[0].children[0]),
- "get() result != expected");
- assertTrue(compareNode(results[1], expected[0].children[1]),
- "get() result != expected");
- succeed();
- });
- },
-
- function getChildren() {
- chrome.bookmarks.getChildren("0", function(results) {
- assertNoLastError();
- assertTrue(compareNode(results[0], expected[0].children[0]),
- "getChildren() result != expected");
- assertTrue(compareNode(results[1], expected[0].children[1]),
- "getChildren() result != expected");
- succeed();
- });
- },
-
- function create() {
- var node = {parentId: "1", title:"google", url:"http://www.google.com/"};
- chrome.bookmarks.create(node, function(results) {
- assertNoLastError();
- node.id = results.id; // since we couldn't know this going in
- node.index = 0;
- assertTrue(compareNode(node, results),
- "created node != source");
- succeed();
- });
- },
-];
-
-runNextTest();
+// bookmarks api test
+// browser_tests.exe --gtest_filter=ExtensionApiTest.Bookmarks
+
+var expected = [
+ {"children": [
+ {"children": [], "id": "1", "parentId": "0", "index": 0, "title":"Bookmarks bar"},
+ {"children": [], "id": "2", "parentId": "0", "index": 1, "title":"Other bookmarks"}
+ ],
+ "id": "0", "title": ""
+ }
+];
+
+function compareNode(left, right) {
+ //console.log(JSON.stringify(left));
+ //console.log(JSON.stringify(right));
+ // TODO(erikkay): do some comparison of dateAdded
+ if (left.id != right.id)
+ return "id mismatch: " + left.id + " != " + right.id;
+ if (left.title != right.title)
+ return "title mismatch: " + left.title + " != " + right.title;
+ if (left.url != right.url)
+ return "url mismatch: " + left.url + " != " + right.url;
+ if (left.index != right.index)
+ return "index mismatch: " + left.index + " != " + right.index;
+ return true;
+}
+
+function compareTrees(left, right) {
+ //console.log(JSON.stringify(left));
+ //console.log(JSON.stringify(right));
+ if (left == null && right == null) {
+ console.log("both left and right are NULL");
+ return true;
+ }
+ if (left == null || right == null)
+ return left + " !+ " + right;
+ if (left.length != right.length)
+ return "count mismatch: " + left.length + " != " + right.length;
+ for (var i = 0; i < left.length; i++) {
+ var result = compareNode(left[i], right[i]);
+ if (result !== true) {
+ console.log(JSON.stringify(left));
+ console.log(JSON.stringify(right));
+ return result;
+ }
+ result = compareTrees(left[i].children, right[i].children);
+ if (result !== true)
+ return result;
+ }
+ return true;
+}
+
+var tests = [
+ function getTree() {
+ chrome.bookmarks.getTree(testFunction(function(results) {
+ assertTrue(compareTrees(results, expected),
+ "getTree() result != expected");
+ expected = results;
+ }));
+ },
+
+ function get() {
+ chrome.bookmarks.get("1", testFunction(function(results) {
+ assertTrue(compareNode(results[0], expected[0].children[0]));
+ }));
+ },
+
+ function getArray() {
+ chrome.bookmarks.get(["1", "2"], testFunction(function(results) {
+ assertTrue(compareNode(results[0], expected[0].children[0]),
+ "get() result != expected");
+ assertTrue(compareNode(results[1], expected[0].children[1]),
+ "get() result != expected");
+ }));
+ },
+
+ function getChildren() {
+ chrome.bookmarks.getChildren("0", testFunction(function(results) {
+ assertTrue(compareNode(results[0], expected[0].children[0]),
+ "getChildren() result != expected");
+ assertTrue(compareNode(results[1], expected[0].children[1]),
+ "getChildren() result != expected");
+ }));
+ },
+
+ function create() {
+ var node = {parentId: "1", title:"google", url:"http://www.google.com/"};
+ chrome.bookmarks.create(node, testFunction(function(results) {
+ node.id = results.id; // since we couldn't know this going in
+ node.index = 0;
+ assertTrue(compareNode(node, results),
+ "created node != source");
+ }));
+ },
+];
+
+runNextTest();