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 /** @const {string} Name of the chrome.send() message to be used in tests. */ | |
| 13 var CHROME_SEND_NAME = 'echoMessage'; | |
| 14 | |
| 15 /** | |
| 16 * Test fixture for testing async methods of cr.js. | |
| 17 * @constructor | |
| 18 * @extends testing.Test | |
| 19 */ | |
| 20 function WebUIResourceAsyncTest() {} | |
| 21 | |
| 22 WebUIResourceAsyncTest.prototype = { | |
| 23 __proto__: testing.Test.prototype, | |
| 24 | |
| 25 /** @override */ | |
| 26 browsePreload: DUMMY_URL, | |
| 27 | |
| 28 /** @override */ | |
| 29 isAsync: true, | |
| 30 | |
| 31 /** @override */ | |
| 32 runAccessibilityChecks: false, | |
| 33 | |
| 34 /** @override */ | |
| 35 extraLibraries: [ | |
| 36 ROOT_PATH + 'third_party/mocha/mocha.js', | |
| 37 ROOT_PATH + 'chrome/test/data/webui/mocha_adapter.js', | |
| 38 ROOT_PATH + 'ui/webui/resources/js/cr.js', | |
| 39 ], | |
| 40 }; | |
| 41 | |
| 42 TEST_F('WebUIResourceAsyncTest', 'SendWithPromise', function() { | |
| 43 /** | |
| 44 * TODO(dpapad): Move this helper method in test_api.js. | |
| 45 * @param {string} name chrome.send message name. | |
| 46 * @return {!Promise} Fires when chrome.send is called with the given message | |
| 47 * name. | |
| 48 */ | |
| 49 function whenChromeSendCalled(name) { | |
| 50 return new Promise(function(resolve, reject) { | |
| 51 registerMessageCallback(name, null, resolve); | |
| 52 }); | |
| 53 } | |
| 54 | |
| 55 suite('cr.js', function() { | |
| 56 setup(function() { | |
| 57 // Simulate a WebUI handler that echoes back all parameters passed to it. | |
| 58 whenChromeSendCalled(CHROME_SEND_NAME).then( | |
| 59 function(args) { | |
|
Dan Beam
2016/01/25 23:13:38
please use the minimum, reasonable amount of lines
dpapad
2016/01/25 23:54:44
See response to similar comment in appearance_page
| |
| 60 assertEquals('cr.webUIResponse', args[0]); | |
| 61 var globalCallbackArgs = args.slice(1); | |
| 62 cr.webUIResponse.apply(null, globalCallbackArgs); | |
| 63 }); | |
| 64 }); | |
| 65 | |
| 66 test('sendWithPromise_MultipleArgs', function() { | |
| 67 return cr.sendWithPromise(CHROME_SEND_NAME, '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(CHROME_SEND_NAME).then( | |
| 75 function(response) { | |
| 76 assertEquals(undefined, response); | |
| 77 }); | |
| 78 }); | |
| 79 }); | |
| 80 | |
| 81 // Run all registered tests. | |
| 82 mocha.run(); | |
| 83 }); | |
| OLD | NEW |