OLD | NEW |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2011 The Native Client 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 function setupTests(tester, plugin) { | 5 function setupTests(tester, plugin) { |
6 // TODO(tester): replace with your tests. | 6 // TODO(tester): replace with your tests. |
7 | 7 |
8 // Below are the before & after versions of sample test cases demonstrating | 8 // Below are sample test cases demonstrating postMessage as the test |
9 // how to transition from synchronous scripting to postMessage as the test | |
10 // driving mechanism. | 9 // driving mechanism. |
11 | 10 |
12 // Before. | 11 tester.addAsyncTest('Example::Simple', function(status) { |
13 tester.addTest('Example::SimpleSync', function() { | |
14 assert(plugin.TestSimpleSync()); | |
15 }); | |
16 | |
17 // After. | |
18 tester.addAsyncTest('Example::SimpleAsync', function(status) { | |
19 var messageListener = status.wrap(function(message) { | 12 var messageListener = status.wrap(function(message) { |
20 status.log('Received message: ' + message.data); | 13 status.log('Received message: ' + message.data); |
21 plugin.removeEventListener('message', messageListener, false); | 14 plugin.removeEventListener('message', messageListener, false); |
22 status.assertEqual(message.data, 'TestSimpleAsync:PASSED'); | 15 status.assertEqual(message.data, 'TestSimple:PASSED'); |
23 status.pass(); | 16 status.pass(); |
24 }); | 17 }); |
25 | 18 |
26 plugin.addEventListener("message", messageListener, false); | 19 plugin.addEventListener("message", messageListener, false); |
27 plugin.postMessage("TestSimpleAsync"); | 20 plugin.postMessage("TestSimple"); |
28 }); | 21 }); |
29 | 22 |
30 // Before. | 23 tester.addAsyncTest('Example::Callback', function(status) { |
31 tester.addTest('Example::CallbackSync', function(status) { | |
32 status.assert(plugin.TestCallbackSync()); | |
33 status.waitForCallback('CallbackSync', 1); | |
34 }); | |
35 | |
36 // After. | |
37 tester.addAsyncTest('Example::CallbackAsync', function(status) { | |
38 var gotPassed = false; | 24 var gotPassed = false; |
39 var messageListener = status.wrap(function(message) { | 25 var messageListener = status.wrap(function(message) { |
40 // <IGNORE> | |
41 // If you are consulting this example while porting or writing | |
42 // addAsyncTest's, ignore this part. This is a short-term side-effect of | |
43 // C++ test harness migration and affects only this example | |
44 // that uses both old-style and new-style tests. | |
45 // To simplify C++ test harness migration, the callback handler | |
46 // notifies JS both via browser scripting and postMessage, | |
47 // so we should ignore the message from TestCallbackSync above. | |
48 if (message.data == 'CallbackSync') | |
49 return; | |
50 // </IGNORE> | |
51 status.log('Received message: ' + message.data); | 26 status.log('Received message: ' + message.data); |
52 plugin.removeEventListener('message', messageListener, false); | 27 plugin.removeEventListener('message', messageListener, false); |
53 if (!gotPassed) { | 28 if (!gotPassed) { |
54 status.assertEqual(message.data, 'TestCallbackAsync:PASSED'); | 29 status.assertEqual(message.data, 'TestCallback:PASSED'); |
55 gotPassed = true; | 30 gotPassed = true; |
56 plugin.addEventListener("message", messageListener, false); | 31 plugin.addEventListener("message", messageListener, false); |
57 } else { | 32 } else { |
58 status.assertEqual(message.data, 'CallbackAsync'); | 33 status.assertEqual(message.data, 'Callback'); |
59 status.pass(); | 34 status.pass(); |
60 } | 35 } |
61 }); | 36 }); |
62 | 37 |
63 plugin.addEventListener("message", messageListener, false); | 38 plugin.addEventListener("message", messageListener, false); |
64 plugin.postMessage("TestCallbackAsync"); | 39 plugin.postMessage("TestCallback"); |
65 }); | 40 }); |
66 } | 41 } |
OLD | NEW |