OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Library providing basic test framework functionality. | 5 // Library providing basic test framework functionality. |
6 | 6 |
7 (function() { | 7 (function() { |
8 // Indicates a pass to the C++ backend. | 8 // Indicates a pass to the C++ backend. |
9 function pass() { | 9 function pass() { |
10 chrome.send('Pass', []); | 10 chrome.send('Pass', []); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 throw new Error('Test Error' + | 46 throw new Error('Test Error' + |
47 ' (type mismatch)\nActual Type: ' + typeof actual + | 47 ' (type mismatch)\nActual Type: ' + typeof actual + |
48 '\nExpected Type:' + typeof expected + '\n' + message); | 48 '\nExpected Type:' + typeof expected + '\n' + message); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 function assertNotReached(message) { | 52 function assertNotReached(message) { |
53 throw new Error(message); | 53 throw new Error(message); |
54 } | 54 } |
55 | 55 |
56 function runTest(currentTest, testArguments) { | 56 function runTest(testFunction, testArguments) { |
57 try { | 57 try { |
58 currentTest = eval(currentTest); | 58 // Avoid eval() if at all possible, since it will not work on pages |
| 59 // that have enabled content-security-policy. |
| 60 currentTest = this[testFunction]; // global object -- not a method. |
| 61 if (typeof currentTest === "undefined") { |
| 62 currentTest = eval(testFunction); |
| 63 } |
59 console.log('Running test ' + currentTest.name); | 64 console.log('Running test ' + currentTest.name); |
60 currentTest.apply(null, testArguments); | 65 currentTest.apply(null, testArguments); |
61 } catch (e) { | 66 } catch (e) { |
62 console.log( | 67 console.log( |
63 'Failed: ' + currentTest.name + '\nwith exception: ' + e.message); | 68 'Failed: ' + currentTest.name + '\nwith exception: ' + e.message); |
64 | 69 |
65 fail(e.message); | 70 fail(e.message); |
66 return; | 71 return; |
67 } | 72 } |
68 | 73 |
69 | 74 |
70 // All tests passed. | 75 // All tests passed. |
71 pass(''); | 76 pass(''); |
72 } | 77 } |
73 | 78 |
74 // Exports. | 79 // Exports. |
75 window.assertTrue = assertTrue; | 80 window.assertTrue = assertTrue; |
76 window.assertFalse = assertFalse; | 81 window.assertFalse = assertFalse; |
77 window.assertEquals = assertEquals; | 82 window.assertEquals = assertEquals; |
78 window.assertNotReached = assertNotReached; | 83 window.assertNotReached = assertNotReached; |
79 window.runTest = runTest; | 84 window.runTest = runTest; |
80 })(); | 85 })(); |
OLD | NEW |