Commit a14ce6ec authored by dpapad's avatar dpapad Committed by Commit Bot

WebUI: Eliminate usages and remove chrome/third_party/mock4js.

Reviving original CL by @tapted at
https://chromium-review.googlesource.com/c/chromium/src/+/2173856

Bug: 844820
Change-Id: I2865e36952ae1de032b4edf4fee0903db95122f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2531902Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarJochen Eisinger <jochen@chromium.org>
Commit-Queue: dpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827220}
parent 5a25be46
...@@ -27,13 +27,6 @@ SyncInternalsWebUITest.prototype = { ...@@ -27,13 +27,6 @@ SyncInternalsWebUITest.prototype = {
*/ */
runAccessibilityChecks: false, runAccessibilityChecks: false,
/** @override */
preLoad: function() {
this.makeAndRegisterMockHandler([
'getAllNodes',
]);
},
/** /**
* Checks aboutInfo's details section for the specified field. * Checks aboutInfo's details section for the specified field.
* @param {boolean} isValid Whether the field is valid. * @param {boolean} isValid Whether the field is valid.
...@@ -334,12 +327,9 @@ TEST_F('SyncInternalsWebUITest', 'SearchTabDoesntChangeOnItemSelect', ...@@ -334,12 +327,9 @@ TEST_F('SyncInternalsWebUITest', 'SearchTabDoesntChangeOnItemSelect',
}); });
TEST_F('SyncInternalsWebUITest', 'NodeBrowserTest', function() { TEST_F('SyncInternalsWebUITest', 'NodeBrowserTest', function() {
const getAllNodesSavedArgs = new SaveMockArguments(); chrome.sync.getAllNodes = (callback) => {
this.mockHandler.expects(once()). callback(HARD_CODED_ALL_NODES);
getAllNodes(getAllNodesSavedArgs.match(ANYTHING)). };
will(callFunctionWithSavedArgs(getAllNodesSavedArgs,
chrome.sync.getAllNodesCallback,
HARD_CODED_ALL_NODES));
// Hit the refresh button. // Hit the refresh button.
$('node-browser-refresh-button').click(); $('node-browser-refresh-button').click();
...@@ -347,7 +337,7 @@ TEST_F('SyncInternalsWebUITest', 'NodeBrowserTest', function() { ...@@ -347,7 +337,7 @@ TEST_F('SyncInternalsWebUITest', 'NodeBrowserTest', function() {
// Check that the refresh time was updated. // Check that the refresh time was updated.
expectNotEquals($('node-browser-refresh-time').textContent, 'Never'); expectNotEquals($('node-browser-refresh-time').textContent, 'Never');
// Verify some hard-coded assumptions. These depend on the vaue of the // Verify some hard-coded assumptions. These depend on the value of the
// hard-coded nodes, specified elsewhere in this file. // hard-coded nodes, specified elsewhere in this file.
// Start with the tree itself. // Start with the tree itself.
...@@ -370,12 +360,9 @@ TEST_F('SyncInternalsWebUITest', 'NodeBrowserTest', function() { ...@@ -370,12 +360,9 @@ TEST_F('SyncInternalsWebUITest', 'NodeBrowserTest', function() {
}); });
TEST_F('SyncInternalsWebUITest', 'NodeBrowserRefreshOnTabSelect', function() { TEST_F('SyncInternalsWebUITest', 'NodeBrowserRefreshOnTabSelect', function() {
const getAllNodesSavedArgs = new SaveMockArguments(); chrome.sync.getAllNodes = (callback) => {
this.mockHandler.expects(once()). callback(HARD_CODED_ALL_NODES);
getAllNodes(getAllNodesSavedArgs.match(ANYTHING)). };
will(callFunctionWithSavedArgs(getAllNodesSavedArgs,
chrome.sync.getAllNodesCallback,
HARD_CODED_ALL_NODES));
// Should start with non-refreshed node browser. // Should start with non-refreshed node browser.
expectEquals($('node-browser-refresh-time').textContent, 'Never'); expectEquals($('node-browser-refresh-time').textContent, 'Never');
......
...@@ -10,7 +10,6 @@ import("//v8/gni/v8.gni") ...@@ -10,7 +10,6 @@ import("//v8/gni/v8.gni")
# Changing the size or the order of files in this array also requires changing # Changing the size or the order of files in this array also requires changing
# src/tools/gypv8sh.py. # src/tools/gypv8sh.py.
js2gtest_js_input_libraries = [ js2gtest_js_input_libraries = [
"//chrome/third_party/mock4js/mock4js.js",
"//chrome/test/data/webui/test_api.js", "//chrome/test/data/webui/test_api.js",
"//chrome/test/base/js2gtest.js", "//chrome/test/base/js2gtest.js",
] ]
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
JsTestApiConfig::JsTestApiConfig() { JsTestApiConfig::JsTestApiConfig() {
constexpr const base::FilePath::CharType* kLibraries[] = { constexpr const base::FilePath::CharType* kLibraries[] = {
FILE_PATH_LITERAL("chrome/third_party/mock4js/mock4js.js"),
FILE_PATH_LITERAL("third_party/chaijs/chai.js"), FILE_PATH_LITERAL("third_party/chaijs/chai.js"),
}; };
constexpr base::FilePath::CharType kWebUITestFolder[] = constexpr base::FilePath::CharType kWebUITestFolder[] =
......
...@@ -59,7 +59,6 @@ js_type_check("closure_compile_local") { ...@@ -59,7 +59,6 @@ js_type_check("closure_compile_local") {
js_library("test_api_js") { js_library("test_api_js") {
sources = [ "webui/test_api.js" ] sources = [ "webui/test_api.js" ]
externs_list = [ externs_list = [
"webui/mock4js_externs.js",
"//third_party/chaijs/externs/chai-3.5.js", "//third_party/chaijs/externs/chai-3.5.js",
"//ui/webui/resources/js/dom_automation_controller.js", "//ui/webui/resources/js/dom_automation_controller.js",
] ]
......
...@@ -85,7 +85,6 @@ if (include_js_tests) { ...@@ -85,7 +85,6 @@ if (include_js_tests) {
"js2gtest_browsertest.js", "js2gtest_browsertest.js",
"load_time_data_browsertest.js", "load_time_data_browsertest.js",
"management/a11y/management_a11y_test.js", "management/a11y/management_a11y_test.js",
"mock4js_browsertest.js",
"net_internals/dns_view.js", "net_internals/dns_view.js",
"net_internals/domain_security_policy_view.js", "net_internals/domain_security_policy_view.js",
"net_internals/main.js", "net_internals/main.js",
......
...@@ -108,64 +108,59 @@ TEST_F('WebUIBrowserAsyncGenTest', 'TestContinue', function() { ...@@ -108,64 +108,59 @@ TEST_F('WebUIBrowserAsyncGenTest', 'TestContinue', function() {
// Test that runAllActionsAsync can be called with multiple functions, and with // Test that runAllActionsAsync can be called with multiple functions, and with
// bound, saved, or mixed arguments. // bound, saved, or mixed arguments.
TEST_F('WebUIBrowserAsyncGenTest', 'TestRunAllActionsAsyncMock', function() { TEST_F('WebUIBrowserAsyncGenTest', 'TestRunAllActionsAsync', function() {
this.makeAndRegisterMockHandler([
'testBoundArgs',
'testSavedArgs',
'testMixedArgs',
]);
// Bind some arguments. // Bind some arguments.
var var1, var2; var var1, var2;
this.mockHandler.expects(once()).testBoundArgs().will( function testBoundArgs() {
runAllActionsAsync(WhenTestDone.DEFAULT, callFunction(function(args) { const action1 = callFunction(function(args) {
var1 = args[0]; var1 = args[0];
}, ['val1']), callFunction(function(args) { }, ['val1']);
var2 = args[0]; const action2 = callFunction(function(args) {
}, ['val2']))); var2 = args[0];
}, ['val2']);
runAllActionsAsync(WhenTestDone.DEFAULT, action1, action2).invoke();
}
// Receive some saved arguments. // Receive some saved arguments.
var var3, var4; var var3, var4;
var savedArgs = new SaveMockArguments(); var savedArgs = new SaveMockArguments();
var savedArgs2 = new SaveMockArguments(); var savedArgs2 = new SaveMockArguments();
this.mockHandler.expects(once()) function testSavedArgs(sendArg) {
.testSavedArgs(savedArgs.match(savedArgs2.match(eq(['passedVal1'])))) const action1 = callFunctionWithSavedArgs(savedArgs, function(args) {
.will(runAllActionsAsync( var3 = args[0];
WhenTestDone.DEFAULT, }, sendArg);
callFunctionWithSavedArgs(savedArgs, function(args) { const action2 = callFunctionWithSavedArgs(savedArgs2, function(args) {
var3 = args[0]; var4 = args[0];
}), callFunctionWithSavedArgs(savedArgs2, function(args) { }, sendArg);
var4 = args[0]; runAllActionsAsync(WhenTestDone.DEFAULT, action1, action2).invoke();
}))); }
// Receive some saved arguments and some bound arguments. // Receive some saved arguments and some bound arguments.
var var5, var6, var7, var8; var var5, var6, var7, var8;
this.mockHandler.expects(once()) function testMixedArgs(sendArg) {
.testMixedArgs(savedArgs.match(savedArgs2.match(eq('passedVal2')))) const action1 =
.will(runAllActionsAsync( callFunctionWithSavedArgs(savedArgs, function(passedArgs, boundArgs) {
WhenTestDone.DEFAULT, var5 = passedArgs[0];
callFunctionWithSavedArgs( var6 = boundArgs[0];
savedArgs, }, sendArg, ['val6']);
function(passedArgs, boundArgs) { const action2 =
var5 = passedArgs[0]; callFunctionWithSavedArgs(savedArgs2, function(passedArgs, boundArgs) {
var6 = boundArgs[0]; var7 = passedArgs[0];
}, var8 = boundArgs[0];
['val6']), }, sendArg, ['val8']);
callFunctionWithSavedArgs( runAllActionsAsync(WhenTestDone.DEFAULT, action1, action2).invoke();
savedArgs2, function(passedArgs, boundArgs) { }
var7 = passedArgs[0];
var8 = boundArgs[0];
}, ['val8'])));
// Send the cases to the mocked handler & tell the C++ handler to continue2. // Send the cases to the mocked handler & tell the C++ handler to continue2.
continueTest = this.continueTest(WhenTestDone.ASSERT, function() { window.continueTest = this.continueTest(WhenTestDone.ASSERT, function() {
chrome.send('testBoundArgs'); testBoundArgs();
chrome.send('testSavedArgs', ['passedVal1']); testSavedArgs(['passedVal1']);
chrome.send('testMixedArgs', ['passedVal2']); testMixedArgs(['passedVal2']);
chrome.send('callJS', ['continueTest2']); setTimeout(window.continueTest2, 0);
}); });
// Check expectations after mocks have been called. // Check expectations after mocks have been called.
continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() { window.continueTest2 = this.continueTest(WhenTestDone.ALWAYS, function() {
expectEquals('val1', var1); expectEquals('val1', var1);
expectEquals('val2', var2); expectEquals('val2', var2);
expectEquals('passedVal1', var3); expectEquals('passedVal1', var3);
...@@ -177,7 +172,7 @@ TEST_F('WebUIBrowserAsyncGenTest', 'TestRunAllActionsAsyncMock', function() { ...@@ -177,7 +172,7 @@ TEST_F('WebUIBrowserAsyncGenTest', 'TestRunAllActionsAsyncMock', function() {
}); });
// Kick off the tests asynchronously. // Kick off the tests asynchronously.
chrome.send('callJS', ['continueTest']); setTimeout(window.continueTest, 0);
}); });
/** /**
......
...@@ -30,34 +30,14 @@ ChromeSendWebUITest.prototype = { ...@@ -30,34 +30,14 @@ ChromeSendWebUITest.prototype = {
/** @inheritDoc */ /** @inheritDoc */
browsePreload: DUMMY_URL, browsePreload: DUMMY_URL,
/** @inheritDoc */
setUp: function() {
this.makeAndRegisterMockHandler(['checkSend']);
}
}; };
// Test that chrome.send can be mocked outside the preLoad method. // Test that chrome.send can be mocked outside the preLoad method.
TEST_F('ChromeSendWebUITest', 'NotInPreload', function() { TEST_F('ChromeSendWebUITest', 'NotInPreload', function() {
this.mockHandler.expects(once()).checkSend(); let invoked = false;
chrome.send('checkSend'); registerMessageCallback('checkSend', undefined, () => {
}); invoked = true;
});
/**
* Test fixture for chrome send WebUI testing with passthrough.
* @constructor
* @extends {ChromeSendWebUITest}
*/
function ChromeSendPassthroughWebUITest() {}
ChromeSendPassthroughWebUITest.prototype = {
__proto__: ChromeSendWebUITest.prototype,
};
// Test that the mocked chrome.send can call the original.
TEST_F('ChromeSendPassthroughWebUITest', 'CanCallOriginal', function() {
this.mockHandler.expects(once()).checkSend().will(callFunction(function() {
chrome.originalSend('checkSend');
}));
chrome.send('checkSend'); chrome.send('checkSend');
assertTrue(invoked);
}); });
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Tests for Mock4JS to ensure that expectations pass or fail as
* expected using the test framework.
* @author scr@chromium.org (Sheridan Rawlins)
* @see test_api.js
*/
GEN('#include "content/public/test/browser_test.h"');
/**
* Test fixture for Mock4JS testing.
* @constructor
* @extends {testing.Test}
*/
function Mock4JSWebUITest() {}
Mock4JSWebUITest.prototype = {
__proto__: testing.Test.prototype,
/** @inheritDoc */
browsePreload: DUMMY_URL,
/** @inheritDoc */
setUp: function() {
function MockHandler() {}
MockHandler.prototype = {
callMe: function() {},
};
this.mockHandler = mock(MockHandler);
},
};
TEST_F('Mock4JSWebUITest', 'CalledExpectPasses', function() {
this.mockHandler.expects(once()).callMe();
this.mockHandler.proxy().callMe();
});
TEST_F('Mock4JSWebUITest', 'CalledTwiceExpectTwice', function() {
this.mockHandler.expects(exactly(2)).callMe();
var proxy = this.mockHandler.proxy();
proxy.callMe();
proxy.callMe();
});
/**
* Test fixture for Mock4JS testing which is expected to fail.
* @constructor
* @extends {Mock4JSWebUITest}
*/
function Mock4JSWebUITestFails() {}
Mock4JSWebUITestFails.prototype = {
__proto__: Mock4JSWebUITest.prototype,
/** @inheritDoc */
testShouldFail: true,
};
TEST_F('Mock4JSWebUITestFails', 'NotCalledExpectFails', function() {
this.mockHandler.expects(once()).callMe();
});
TEST_F('Mock4JSWebUITestFails', 'CalledTwiceExpectOnceFails', function() {
this.mockHandler.expects(once()).callMe();
var proxy = this.mockHandler.proxy();
proxy.callMe();
proxy.callMe();
});
TEST_F('Mock4JSWebUITestFails', 'CalledOnceExpectTwiceFails', function() {
this.mockHandler.expects(exactly(2)).callMe();
var proxy = this.mockHandler.proxy();
proxy.callMe();
});
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview
* Limited externs used by webui testing from http://mock4js.sourceforge.net/.
* Mock4JS is not closure-annotated and unmaintained.
* TODO(crbug/844820): Eliminate/replace usage of mock4js and delete this file.
*/
function mock(klass) {}
const Mock4JS = {
verifyAllMocks: function() {},
addMockSupport: function(exports) {},
};
class Mock {
proxy() {}
expects(expectedCallCount) {}
stubs() {}
verify() {}
}
...@@ -159,13 +159,6 @@ Test.prototype = { ...@@ -159,13 +159,6 @@ Test.prototype = {
*/ */
featuresWithParameters: null, featuresWithParameters: null,
/**
* This should be initialized by the test fixture and can be referenced
* during the test run. It holds any mocked handler methods.
* @type {?Mock}
*/
mockHandler: null,
/** /**
* Value is passed through call to C++ RunJavascriptF to invoke this test. * Value is passed through call to C++ RunJavascriptF to invoke this test.
* @type {boolean} * @type {boolean}
...@@ -203,30 +196,6 @@ Test.prototype = { ...@@ -203,30 +196,6 @@ Test.prototype = {
*/ */
closureModuleDeps: [], closureModuleDeps: [],
/**
* Create a new class to handle |messageNames|, assign it to
* |this.mockHandler|, register its messages and return it.
* @return {Mock} Mock handler class assigned to |this.mockHandler|.
*/
makeAndRegisterMockHandler: function(messageNames) {
var MockClass = makeMockClass(messageNames);
this.mockHandler = mock(MockClass);
registerMockMessageCallbacks(this.mockHandler, MockClass);
return this.mockHandler;
},
/**
* Create a container of mocked standalone functions to handle
* |functionNames|, assign it to |this.mockLocalFunctions| and return it.
* @param {!Array<string>} functionNames
* @return {Mock} Mock handler class.
* @see makeMockFunctions
*/
makeMockLocalFunctions: function(functionNames) {
this.mockLocalFunctions = makeMockFunctions(functionNames);
return this.mockLocalFunctions;
},
/** /**
* Override this method to perform initialization during preload (such as * Override this method to perform initialization during preload (such as
* creating mocks and registering handlers). * creating mocks and registering handlers).
...@@ -241,9 +210,7 @@ Test.prototype = { ...@@ -241,9 +210,7 @@ Test.prototype = {
setUp: function() {}, setUp: function() {},
/** /**
* Override this method to perform tasks after running your test. If you * Override this method to perform tasks after running your test.
* create a mock class, you must call Mock4JS.verifyAllMocks() in this
* phase.
* @type {Function} * @type {Function}
*/ */
tearDown: function() { tearDown: function() {
...@@ -253,8 +220,6 @@ Test.prototype = { ...@@ -253,8 +220,6 @@ Test.prototype = {
noAnimationStyle.parentNode.removeChild(noAnimationStyle); noAnimationStyle.parentNode.removeChild(noAnimationStyle);
} }
} }
Mock4JS.verifyAllMocks();
}, },
/** /**
...@@ -454,27 +419,6 @@ function registerMessageCallback(name, messageHandler, callback) { ...@@ -454,27 +419,6 @@ function registerMessageCallback(name, messageHandler, callback) {
sendCallbacks[name] = [messageHandler, callback]; sendCallbacks[name] = [messageHandler, callback];
} }
/**
* Register all methods of {@code mockClass.prototype} with messages of the
* same name as the method, using the proxy of the |mockObject| as the
* |messageHandler| when registering.
* @param {Mock} mockObject The mock to register callbacks against.
* @param {Function} mockClass Constructor for the mocked class.
* @see registerMessageCallback
* @see overrideChrome
*/
function registerMockMessageCallbacks(mockObject, mockClass) {
if (!deferGlobalOverrides && !originalChrome) {
overrideChrome();
}
var mockProxy = mockObject.proxy();
for (var func in mockClass.prototype) {
if (typeof mockClass.prototype[func] === 'function') {
registerMessageCallback(func, mockProxy, mockProxy[func]);
}
}
}
/** /**
* When preloading JavaScript libraries, this is true until the * When preloading JavaScript libraries, this is true until the
* DOMContentLoaded event has been received as globals cannot be overridden * DOMContentLoaded event has been received as globals cannot be overridden
...@@ -483,52 +427,6 @@ function registerMockMessageCallbacks(mockObject, mockClass) { ...@@ -483,52 +427,6 @@ function registerMockMessageCallbacks(mockObject, mockClass) {
*/ */
var deferGlobalOverrides = false; var deferGlobalOverrides = false;
/**
* Empty function for use in making mocks.
* @const
*/
function emptyFunction() {}
/**
* Make a mock from the supplied |methodNames| array.
* @param {Array<string>} methodNames Array of names of methods to mock.
* @return {Function} Constructor with prototype filled in with methods
* matching |methodNames|.
*/
function makeMockClass(methodNames) {
function MockConstructor() {}
for (var i = 0; i < methodNames.length; i++) {
MockConstructor.prototype[methodNames[i]] = emptyFunction;
}
return MockConstructor;
}
/**
* Create a new class to handle |functionNames|, add method 'functions()'
* that returns a container of standalone functions based on the mock class
* members, and return it.
* @return {Mock} Mock handler class.
*/
function makeMockFunctions(functionNames) {
var MockClass = makeMockClass(functionNames);
var mockFunctions = mock(MockClass);
var mockProxy = mockFunctions.proxy();
mockFunctions.functions_ = {};
for (var func in MockClass.prototype) {
if (typeof MockClass.prototype[func] === 'function') {
mockFunctions.functions_[func] = mockProxy[func].bind(mockProxy);
}
}
mockFunctions.functions = function() {
return this.functions_;
};
return mockFunctions;
}
/** /**
* Overrides {@code chrome.send} for routing messages to javascript * Overrides {@code chrome.send} for routing messages to javascript
* functions. Also falls back to sending with the original chrome object. * functions. Also falls back to sending with the original chrome object.
...@@ -944,7 +842,6 @@ function overrideChrome() { ...@@ -944,7 +842,6 @@ function overrideChrome() {
chrome = { chrome = {
__proto__: originalChrome, __proto__: originalChrome,
send: send, send: send,
originalSend: originalChrome.send.bind(originalChrome),
}; };
} }
...@@ -1427,9 +1324,6 @@ function exportMock4JsHelpers() { ...@@ -1427,9 +1324,6 @@ function exportMock4JsHelpers() {
exports.callFunction = callFunction; exports.callFunction = callFunction;
exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs; exports.callFunctionWithSavedArgs = callFunctionWithSavedArgs;
exports.SaveMockArguments = SaveMockArguments; exports.SaveMockArguments = SaveMockArguments;
// Import the Mock4JS helpers.
Mock4JS.addMockSupport(exports);
} }
// Exports. // Exports.
......
Copyright (C) 2009 by Tung Mac.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Name: mock4js
Short Name: mock4js
URL: http://mock4js.sourceforge.net/
Version: 0.2
License: MIT
License File: NOT_SHIPPED
Mock4JS is a dynamic-mock library for Javascript. It enables developers to write
tests that exercise and verify the interactions between the objects in the
application. Its syntax is very expressive and is closely based on jMock 1.
Local Modifications:
* Replace a fail() call with throwing Mock4JSException.
* Changed `new Function()` call in order to avoid Trusted Types violation.
/**
* PriceCache
*/
function PriceCache() {
}
PriceCache.prototype = {
getCachedPrice: function(instrumentId) {
},
setCachedPrice: function(instrumentId, price) {
}
}
/**
* PriceFetcher
*/
function PriceFetcher() {
}
PriceFetcher.prototype = {
getPriceFromServer: function(instrumentId) {
}
}
/**
* PriceService
*/
function PriceService(priceFetcher, priceCache) {
this._priceFetcher = priceFetcher;
this._priceCache = priceCache;
}
PriceService.prototype = {
getPrice: function(instrumentId) {
var price = this._priceCache.getCachedPrice(instrumentId);
if(price==null) {
price = this._priceFetcher.getPriceFromServer(instrumentId);
this._priceCache.setCachedPrice(instrumentId, price);
}
return price;
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tests</title>
<link rel="stylesheet" type="text/css" href="jsunit/css/jsUnitStyle.css">
<script language="JavaScript" type="text/javascript" src="../../jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript" type="text/javascript" src="../mock4js.js"></script>
<script language="JavaScript" type="text/javascript" src="PriceService.js"></script>
<script language="JavaScript" type="text/javascript">
Mock4JS.addMockSupport(this);
var mockPriceFetcher;
var mockPriceCache;
var priceService;
function setUp() {
Mock4JS.clearMocksToVerify();
mockPriceFetcher = mock(PriceFetcher);
mockPriceCache = mock(PriceCache);
priceService = new PriceService(mockPriceFetcher.proxy(), mockPriceCache.proxy());
}
function tearDown() {
Mock4JS.verifyAllMocks();
}
function testGetsPriceFromFetcherWhenPriceNotInCache() {
mockPriceCache.expects(once()).getCachedPrice("USDGBP").will(returnValue(null));
mockPriceFetcher.expects(once()).getPriceFromServer("USDGBP").will(returnValue(123.4));
mockPriceCache.expects(once()).setCachedPrice("USDGBP", 123.4);
var result = priceService.getPrice("USDGBP");
assertEquals("Should have returned price from server", 123.4, result);
}
function testDoesntGetsPriceFromFetcherWhenPriceAlreadyInCache() {
mockPriceCache.expects(once()).getCachedPrice("USDGBP").will(returnValue(123.4));
mockPriceCache.expects(never()).setCachedPrice();
mockPriceFetcher.expects(never()).getPriceFromServer("USDGBP");
var result = priceService.getPrice("USDGBP");
assertEquals("Should have returned price from cache", 123.4, result);
}
</script>
</head>
<body>
<h1>JsUnit Tests</h1>
<p>This page contains some JsUnit tests. To see them, take a look at the source.</p>
</body>
</html>
/**
* Subscriber
*/
function Subscriber() {
}
Subscriber.prototype = {
receive: function(message) {
}
}
/**
* Publisher
*/
function Publisher() {
this._subscribers = [];
}
Publisher.prototype = {
publish: function(message) {
for(var i=0; i<this._subscribers.length; i++) {
var subscriber = this._subscribers[i];
subscriber.receive(message);
}
},
add: function(subscriber) {
this._subscribers.push(subscriber);
}
}
\ No newline at end of file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tests</title>
<link rel="stylesheet" type="text/css" href="jsunit/css/jsUnitStyle.css">
<script language="JavaScript" type="text/javascript" src="../../jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript" type="text/javascript" src="..//mock4js.js"></script>
<script language="JavaScript" type="text/javascript" src="Publisher.js"></script>
<script language="JavaScript" type="text/javascript">
Mock4JS.addMockSupport(this);
function setUp() {
Mock4JS.clearMocksToVerify();
}
function tearDown() {
Mock4JS.verifyAllMocks();
}
function testOneSubscriberReceivesAMessage() {
// setup
var mockSubscriber = mock(Subscriber);
var publisher = new Publisher();
publisher.add(mockSubscriber.proxy());
var message = "message";
// expectations
mockSubscriber.expects(once()).receive(message);
// execute
publisher.publish(message);
}
</script>
</head>
<body>
<h1>JsUnit Tests</h1>
<p>This page contains some JsUnit tests. To see them, take a look at the source.</p>
</body>
</html>
This diff is collapsed.
...@@ -29,9 +29,8 @@ def HasSameContent(filename, content): ...@@ -29,9 +29,8 @@ def HasSameContent(filename, content):
def main (): def main ():
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.set_usage( parser.set_usage("%prog v8_shell test_api.js js2webui.js "
"%prog v8_shell mock.js test_api.js js2webui.js " "testtype inputfile srcrootdir cxxoutfile jsoutfile")
"testtype inputfile srcrootdir cxxoutfile jsoutfile")
parser.add_option('-v', '--verbose', action='store_true') parser.add_option('-v', '--verbose', action='store_true')
parser.add_option('-n', '--impotent', action='store_true', parser.add_option('-n', '--impotent', action='store_true',
help="don't execute; just print (as if verbose)") help="don't execute; just print (as if verbose)")
...@@ -42,15 +41,14 @@ def main (): ...@@ -42,15 +41,14 @@ def main ():
help="Load V8's initial snapshot from external files (y/n)") help="Load V8's initial snapshot from external files (y/n)")
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
if len(args) != 9: if len(args) != 8:
parser.error('all arguments are required.') parser.error('all arguments are required.')
(v8_shell, mock_js, test_api, js2webui, test_type, (v8_shell, test_api, js2webui, test_type, inputfile, srcrootdir, cxxoutfile,
inputfile, srcrootdir, cxxoutfile, jsoutfile) = args jsoutfile) = args
cmd = [v8_shell] cmd = [v8_shell]
arguments = [js2webui, inputfile, srcrootdir, opts.deps_js, arguments = [js2webui, inputfile, srcrootdir, opts.deps_js,
cxxoutfile, test_type] cxxoutfile, test_type]
cmd.extend(['-e', "arguments=" + json.dumps(arguments), mock_js, cmd.extend(['-e', "arguments=" + json.dumps(arguments), test_api, js2webui])
test_api, js2webui])
if opts.verbose or opts.impotent: if opts.verbose or opts.impotent:
print(cmd) print(cmd)
if not opts.impotent: if not opts.impotent:
......
...@@ -320,7 +320,6 @@ KNOWN_NON_IOS_LIBRARIES = set([ ...@@ -320,7 +320,6 @@ KNOWN_NON_IOS_LIBRARIES = set([
os.path.join('chrome', 'installer', 'mac', 'third_party', 'xz'), os.path.join('chrome', 'installer', 'mac', 'third_party', 'xz'),
os.path.join('chrome', 'test', 'data', 'third_party', 'kraken'), os.path.join('chrome', 'test', 'data', 'third_party', 'kraken'),
os.path.join('chrome', 'test', 'data', 'third_party', 'spaceport'), os.path.join('chrome', 'test', 'data', 'third_party', 'spaceport'),
os.path.join('chrome', 'third_party', 'mock4js'),
os.path.join('chrome', 'third_party', 'mozilla_security_manager'), os.path.join('chrome', 'third_party', 'mozilla_security_manager'),
os.path.join('third_party', 'angle'), os.path.join('third_party', 'angle'),
os.path.join('third_party', 'apple_apsl'), os.path.join('third_party', 'apple_apsl'),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment