Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: chrome/renderer/resources/extension_apitest.js

Issue 174633: Add browser tests for extensions tab API. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
1 // extension_apitest.js 1 // extension_apitest.js
2 // mini-framework for ExtensionApiTest browser tests 2 // mini-framework for ExtensionApiTest browser tests
3 3
4 var chrome = chrome || {}; 4 var chrome = chrome || {};
5 (function() { 5 (function() {
6 chrome.test = chrome.test || {}; 6 chrome.test = chrome.test || {};
7 7
8 chrome.test.tests = chrome.test.tests || []; 8 chrome.test.tests = chrome.test.tests || [];
9 9
10 var completed = false; 10 var completed = false;
11 var currentTest; 11 var currentTest;
12 12
13 function complete() { 13 function complete() {
14 completed = true; 14 completed = true;
15 15
16 // Try to get the script to stop running immediately. 16 // Try to get the script to stop running immediately.
17 // This isn't an error, just an attempt at saying "done". 17 // This isn't an error, just an attempt at saying "done".
18 throw "completed"; 18 throw "completed";
19 } 19 }
20 20
21 chrome.test.fail = function(message) { 21 chrome.test.fail = function(message) {
22 if (completed) throw "completed"; 22 if (completed) throw "completed";
23 chrome.test.log("( FAILED ) " + currentTest.name);
23 24
24 var stack; 25 var stack;
25 try { 26 try {
26 crash.me += 0; // An intentional exception to get the stack trace. 27 crash.me += 0; // An intentional exception to get the stack trace.
27 } catch (e) { 28 } catch (e) {
28 stack = e.stack.split("\n"); 29 stack = e.stack.split("\n");
29 stack = stack.slice(2); // Remove title and fail() lines. 30 stack = stack.slice(2); // Remove title and fail() lines.
30 stack = stack.join("\n"); 31 stack = stack.join("\n");
31 } 32 }
32 33
33 if (!message) { 34 if (!message) {
34 message = "FAIL (no message)"; 35 message = "FAIL (no message)";
35 } 36 }
36 message += "\n" + stack; 37 message += "\n" + stack;
37 console.log("[FAIL] " + currentTest.name + ": " + message); 38 console.log("[FAIL] " + currentTest.name + ": " + message);
38 chrome.test.notifyFail(message); 39 chrome.test.notifyFail(message);
39 complete(); 40 complete();
40 } 41 };
41 42
42 function allTestsSucceeded() { 43 function allTestsSucceeded() {
43 console.log("All tests succeeded"); 44 console.log("All tests succeeded");
44 if (completed) throw "completed"; 45 if (completed) throw "completed";
45 46
46 chrome.test.notifyPass(); 47 chrome.test.notifyPass();
47 complete(); 48 complete();
48 } 49 }
49 50
50 chrome.test.runNextTest = function() { 51 chrome.test.runNextTest = function() {
51 currentTest = chrome.test.tests.shift(); 52 currentTest = chrome.test.tests.shift();
52 if (!currentTest) { 53 if (!currentTest) {
53 allTestsSucceeded(); 54 allTestsSucceeded();
54 return; 55 return;
55 } 56 }
56 try { 57 try {
58 chrome.test.log("( RUN ) " + currentTest.name);
57 currentTest.call(); 59 currentTest.call();
58 } catch (e) { 60 } catch (e) {
59 message = e.stack; 61 var message = e.stack;
60 console.log("[FAIL] " + currentTest.name + ": " + message); 62 console.log("[FAIL] " + currentTest.name + ": " + message);
61 chrome.test.notifyFail(message); 63 chrome.test.notifyFail(message);
62 complete(); 64 complete();
63 } 65 }
64 } 66 };
65 67
66 chrome.test.succeed = function() { 68 chrome.test.succeed = function() {
67 console.log("[SUCCESS] " + currentTest.name); 69 console.log("[SUCCESS] " + currentTest.name);
68 chrome.test.runNextTest(); 70 chrome.test.log("( SUCCESS )");
69 } 71 // Use setTimeout here to allow previous test contexts to be
72 // eligible for garbage collection.
73 setTimeout(chrome.test.runNextTest, 0);
74 };
70 75
71 chrome.test.assertTrue = function(test, message) { 76 chrome.test.assertTrue = function(test, message) {
72 if (test !== true) { 77 if (test !== true) {
73 if (typeof(test) == "string") { 78 if (typeof(test) == "string") {
74 if (message) { 79 if (message) {
75 message = test + "\n" + message; 80 message = test + "\n" + message;
76 } else { 81 } else {
77 message = test; 82 message = test;
78 } 83 }
79 } 84 }
80 chrome.test.fail(message); 85 chrome.test.fail(message);
81 } 86 }
82 } 87 };
88
89 chrome.test.assertEq = function(expected, actual) {
90 if (expected != actual) {
91 chrome.test.fail("API Test Error in " + currentTest.name +
92 "\nActual: " + actual + "\nExpected: " + expected);
93 }
94 if (typeof(expected) != typeof(actual)) {
95 chrome.test.fail("API Test Error in " + currentTest.name +
96 " (type mismatch)\nActual Type: " + typeof(actual) +
97 "\nExpected Type:" + typeof(expected));
98 }
99 };
83 100
84 chrome.test.assertNoLastError = function() { 101 chrome.test.assertNoLastError = function() {
85 if (chrome.extension.lastError != undefined) { 102 if (chrome.extension.lastError != undefined) {
86 chrome.test.fail("lastError.message == " + 103 chrome.test.fail("lastError.message == " +
87 chrome.extension.lastError.message); 104 chrome.extension.lastError.message);
88 } 105 }
89 } 106 };
90 107
91 // Wrapper for generating test functions, that takes care of calling 108 // Wrapper for generating test functions, that takes care of calling
92 // assertNoLastError() and succeed() for you. 109 // assertNoLastError() and (optionally) succeed() for you.
93 chrome.test.testFunction = function(func) { 110 chrome.test.testCallback = function(succeedWhenDone, func) {
94 return function() { 111 return function() {
95 chrome.test.assertNoLastError(); 112 chrome.test.assertNoLastError();
96 try { 113 try {
97 func.apply(null, arguments); 114 if (func) {
115 func.apply(null, arguments);
116 }
98 } catch (e) { 117 } catch (e) {
99 var stack = null; 118 var stack = null;
100 if (typeof(e.stack) != "undefined") { 119 if (typeof(e.stack) != "undefined") {
101 stack = e.stack.toString() 120 stack = e.stack.toString();
102 } 121 }
103 var msg = "Exception during execution of testFunction in " + 122 var msg = "Exception during execution of testCallback in " +
104 currentTest.name; 123 currentTest.name;
105 if (stack) { 124 if (stack) {
106 msg += "\n" + stack; 125 msg += "\n" + stack;
107 } else { 126 } else {
108 msg += "\n(no stack available)"; 127 msg += "\n(no stack available)";
109 } 128 }
110 chrome.test.fail(msg); 129 chrome.test.fail(msg);
111 } 130 }
112 chrome.test.succeed(); 131 if (succeedWhenDone) {
132 chrome.test.succeed();
133 }
113 }; 134 };
114 } 135 };
115 136
116 chrome.test.runTests = function(tests) { 137 chrome.test.runTests = function(tests) {
117 chrome.test.tests = tests; 138 chrome.test.tests = tests;
118 chrome.test.runNextTest(); 139 chrome.test.runNextTest();
119 } 140 };
141
120 })(); 142 })();
OLDNEW
« no previous file with comments | « chrome/renderer/renderer_resources.grd ('k') | chrome/test/data/extensions/api_test/bookmarks/test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698