Chromium Code Reviews| 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, | |
| 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'; | |
|
Dan Beam
2016/01/25 18:48:37
nit: CHROME_SEND_NAME
dpapad
2016/01/25 19:10:36
Done.
| |
| 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 cr.webUIResponse.apply(null, globalCallbackArgs); | |
| 62 }); | |
|
Dan Beam
2016/01/25 18:48:37
whenChromeSendCalled(CHROME_SEND_NAME).then(functi
dpapad
2016/01/25 19:10:36
Done.
| |
| 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 |