OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview Framework for running async JS tests for cr.js utility methods. | |
7 */ | |
8 | |
9 /** @const {string} Path to source root. */ | |
10 var ROOT_PATH = '../../../../'; | |
11 | |
12 /** | |
13 * Test fixture for testing async methods of cr.js. | |
14 * @constructor | |
15 * @extends testing.Test | |
16 */ | |
17 function WebUIResourceAsyncTest() {} | |
18 | |
19 WebUIResourceAsyncTest.prototype = { | |
20 __proto__: testing.Test.prototype, | |
21 | |
22 /** @override */ | |
23 browsePreload: DUMMY_URL, | |
Dan Beam
2016/01/23 02:48:51
where does this magic come from?
dpapad
2016/01/25 18:34:22
https://code.google.com/p/chromium/codesearch#chro
| |
24 | |
25 /** @override */ | |
26 isAsync: true, | |
27 | |
28 /** @override */ | |
29 runAccessibilityChecks: false, | |
30 | |
31 /** @override */ | |
32 extraLibraries: [ | |
33 ROOT_PATH + 'third_party/mocha/mocha.js', | |
34 ROOT_PATH + 'chrome/test/data/webui/mocha_adapter.js', | |
35 ROOT_PATH + 'ui/webui/resources/js/cr.js', | |
36 ], | |
37 }; | |
38 | |
39 TEST_F('WebUIResourceAsyncTest', 'SendWithPromise', function() { | |
40 var expectedChromeSendMessageName = 'echoMessage'; | |
41 | |
42 /** | |
43 * TODO(dpapad): Move this helper method in test_api.js. | |
44 * @param {string} name chrome.send message name. | |
45 * @return {!Promise} Fires when chrome.send is called with the given message | |
46 * name. | |
47 */ | |
48 function whenChromeSendCalled(name) { | |
49 return new Promise(function(resolve, reject) { | |
50 registerMessageCallback(name, null, resolve); | |
51 }); | |
52 } | |
53 | |
54 suite('cr.js', function() { | |
55 setup(function() { | |
56 // Simulate a WebUI handler that echoes back all parameters passed to it. | |
57 whenChromeSendCalled(expectedChromeSendMessageName).then( | |
58 function(args) { | |
59 assertEquals('cr.webUIResponse', args[0]); | |
60 var globalCallbackArgs = args.slice(1); | |
61 window['cr']['webUIResponse'].apply(null, globalCallbackArgs); | |
Dan Beam
2016/01/23 02:48:51
why can't this just be cr.webUIResponse.apply(cr,
dpapad
2016/01/25 18:34:22
Done.
| |
62 }); | |
63 }); | |
64 | |
65 test('sendWithPromise_MultipleArgs', function() { | |
66 return cr.sendWithPromise( | |
67 expectedChromeSendMessageName, 'foo', 'bar').then( | |
68 function(response) { | |
69 assertEquals(['foo', 'bar'].join(), response.join()); | |
70 }); | |
71 }); | |
72 | |
73 test('sendWithPromise_NoArgs', function() { | |
74 return cr.sendWithPromise(expectedChromeSendMessageName).then( | |
75 function(response) { | |
76 assertEquals(undefined, response); | |
77 }); | |
78 }); | |
79 }); | |
80 | |
81 // Run all registered tests. | |
82 mocha.run(); | |
83 }); | |
OLD | NEW |