OLD | NEW |
(Empty) | |
| 1 // api_test.js |
| 2 // mini-framework for ExtensionApiTest browser tests |
| 3 // TODO(erikkay) - figure out a way to share this code across extensions |
| 4 |
| 5 var completed = false; |
| 6 var tests; |
| 7 var currentTest; |
| 8 |
| 9 function complete() { |
| 10 completed = true; |
| 11 |
| 12 // a bit of a hack just to try to get the script to stop running at this point |
| 13 throw "completed"; |
| 14 } |
| 15 |
| 16 function fail(message) { |
| 17 if (completed) throw "completed"; |
| 18 |
| 19 var stack; |
| 20 try { |
| 21 crash.me += 0; // intentional exception to get the stack trace |
| 22 } catch (e) { |
| 23 stack = e.stack.split("\n"); |
| 24 stack = stack.slice(2); // remove title and fail() |
| 25 stack = stack.join("\n"); |
| 26 } |
| 27 |
| 28 if (!message) { |
| 29 message = "FAIL (no message)"; |
| 30 } |
| 31 message += "\n" + stack; |
| 32 console.log("[FAIL] " + currentTest.name + ": " + message); |
| 33 chrome.test.fail(message); |
| 34 complete(); |
| 35 } |
| 36 |
| 37 function allTestsSucceeded() { |
| 38 console.log("All tests succeeded"); |
| 39 if (completed) throw "completed"; |
| 40 |
| 41 chrome.test.pass(); |
| 42 complete(); |
| 43 } |
| 44 |
| 45 function runNextTest() { |
| 46 currentTest = tests.shift(); |
| 47 if (!currentTest) { |
| 48 allTestsSucceeded(); |
| 49 return; |
| 50 } |
| 51 currentTest.call(); |
| 52 } |
| 53 |
| 54 function succeed() { |
| 55 console.log("[SUCCESS] " + currentTest.name); |
| 56 runNextTest(); |
| 57 } |
| 58 |
| 59 window.onerror = function(message, url, code) { |
| 60 if (completed) return; |
| 61 |
| 62 fail(message); |
| 63 }; |
| 64 |
| 65 function assertTrue(test, message) { |
| 66 if (test !== true) { |
| 67 if (typeof(test) == "string") { |
| 68 if (message) { |
| 69 message = test + "\n" + message; |
| 70 } else { |
| 71 message = test; |
| 72 } |
| 73 } |
| 74 fail(message); |
| 75 } |
| 76 } |
| 77 |
| 78 function assertNoLastError() { |
| 79 if (chrome.extension.lastError != undefined) { |
| 80 fail("lastError.message == " + chrome.extension.lastError.message); |
| 81 } |
| 82 } |
| 83 |
| 84 // Wrapper for generating test functions, that takes care of calling |
| 85 // assertNoLastError() and succeed() for you. |
| 86 function testFunction(func) { |
| 87 return function() { |
| 88 assertNoLastError(); |
| 89 try { |
| 90 func.apply(null, arguments); |
| 91 } catch (e) { |
| 92 var stack = null; |
| 93 if (typeof(e.stack) != "undefined") { |
| 94 stack = e.stack.toString() |
| 95 } |
| 96 var msg = "Exception during execution of testFunction in " + |
| 97 currentTest.name; |
| 98 if (stack) { |
| 99 msg += "\n" + stack; |
| 100 } else { |
| 101 msg += "\n(no stack available)"; |
| 102 } |
| 103 fail(msg); |
| 104 } |
| 105 succeed(); |
| 106 }; |
| 107 } |
OLD | NEW |