Chromium Code Reviews| Index: chrome/test/data/webui/print_preview_destination_search_test.js |
| diff --git a/chrome/test/data/webui/print_preview_destination_search_test.js b/chrome/test/data/webui/print_preview_destination_search_test.js |
| index b2f38db8730782a4749227cea1ce0c2898588900..88ecfc3e4c17127a15b4562aefbf2130cd1c17b0 100644 |
| --- a/chrome/test/data/webui/print_preview_destination_search_test.js |
| +++ b/chrome/test/data/webui/print_preview_destination_search_test.js |
| @@ -24,7 +24,10 @@ PrintPreviewDestinationSearchTest.prototype = { |
| runAccessibilityChecks: false, |
| /** @override */ |
| - extraLibraries: PolymerTest.getLibraries(ROOT_PATH), |
| + extraLibraries: PolymerTest.getLibraries(ROOT_PATH).concat([ |
| + ROOT_PATH + 'chrome/test/data/webui/settings/test_browser_proxy.js', |
| + ]), |
| + |
| }; |
| TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { |
| @@ -39,6 +42,49 @@ TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { |
| var destinationStore_; |
| var userInfo_; |
| + /** |
| + * Test version of the native layer. |
| + * @constructor |
| + * @extends {settings.TestBrowserProxy} |
|
dpapad
2017/05/18 19:11:26
Can you add a TODO to extract NativeLayerStub to a
rbpotter
2017/05/18 23:40:57
Done.
|
| + */ |
| + function NativeLayerStub() { |
| + settings.TestBrowserProxy.call(this, [ 'setupPrinter' ]); |
| + this.destinationToWatch_ = ''; |
| + this.eventTarget_ = mock(cr.EventTarget); |
| + this.getLocalDestinationCapabilitiesCallCount_ = 0; |
| + this.setupPrinterResponse_ = null; |
| + this.shouldReject_ = false; |
| + } |
| + |
| + NativeLayerStub.prototype = { |
| + __proto__: settings.TestBrowserProxy.prototype, |
| + didGetCapabilitiesOnce: function(destinationId) { |
| + return (destinationId == this.destinationToWatch_ && |
| + this.getLocalDestinationCapabilitiesCallCount_ == 1); |
| + }, |
| + getEventTarget: function() { return this.eventTarget_; }, |
| + setDestinationToWatch: function(destinationId) { |
| + this.destinationToWatch_ = destinationId; |
| + this.getLocalDestinationCapabilitiesCallCount_ = 0; |
| + }, |
| + setSetupPrinterResponse: function(reject, response) { |
| + this.shouldReject_ = reject; |
| + this.printerSetupResponse_ = response; |
| + }, |
| + setupPrinter: function() { |
| + this.methodCalled('setupPrinter'); |
| + if (this.shouldReject_) |
| + return Promise.resolve(this.setupPrinterResponse_); |
| + else |
| + return Promise.reject(this.setupPrinterResponse_); |
| + }, |
| + startGetLocalDestinationCapabilities: function(destinationId) { |
| + if (destinationId == this.destinationToWatch_) |
| + this.getLocalDestinationCapabilitiesCallCount_++; |
| + }, |
| + }; |
| + NativeLayerStub.EventType = print_preview.NativeLayer.EventType; |
| + |
| function getCaps() { |
| return { |
| 'printer': { |
| @@ -99,17 +145,12 @@ TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { |
| }; |
| function mockSetupCall(destId, nativeLayerMock) { |
| - var resolver = new PromiseResolver(); |
| - |
| if (cr.isChromeOS) { |
| - nativeLayerMock.expects(once()).setupPrinter(destId). |
| - will(returnValue(resolver.promise)); |
| - |
| - return resolver; |
| + return nativeLayerMock.whenCalled('setupPrinter'); |
| } |
| - nativeLayerMock.expects(once()).startGetLocalDestinationCapabilities( |
| - destId); |
| + var resolver = new PromiseResolver(); |
|
dpapad
2017/05/18 19:11:26
Where is resolver.promise resolved? I don't see a
rbpotter
2017/05/18 23:40:57
Done. Rewrote these to be very different for CrOs
|
| + nativeLayerMock.setDestinationToWatch(destId); |
| resolver.promise.then( |
| function(result) { |
| // Simulate the native layer dispatching capabilities. |
| @@ -117,15 +158,17 @@ TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { |
| new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET); |
| capsSetEvent.settingsInfo = result; |
| destinationStore_.onLocalDestinationCapabilitiesSet_(capsSetEvent); |
| + expectTrue(nativeLayerMock.didGetCapabilitiesOnce(destId)); |
| }.bind(this), |
| function() { |
| var failEvent = new Event( |
| print_preview.NativeLayer.EventType.GET_CAPABILITIES_FAIL); |
| failEvent.destinationId = destId; |
| destinationStore_.onGetCapabilitiesFail_(failEvent); |
| + expectTrue(nativeLayerMock.didGetCapabilitiesOnce(destId)); |
| }.bind(this)); |
| - return resolver; |
| + return resolver.promise; |
| }; |
| function requestSetup(destId, destinationSearch) { |
| @@ -149,26 +192,18 @@ TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { |
| } |
| }; |
| - function resolveSetup(resolver, printerId, success, capabilities) { |
| - var response = { |
| - printerId: printerId, |
| - capabilities: capabilities, |
| - success: success |
| - }; |
| - |
| - resolver.resolve(response); |
| - } |
| - |
| setup(function() { |
| Mock4JS.clearMocksToVerify(); |
| - |
| - nativeLayer_ = mock(print_preview.NativeLayer); |
| - nativeLayer_.expects(atLeastOnce()) |
| + nativeLayer_ = new NativeLayerStub(); |
| + var nativeLayerEventTarget = nativeLayer_.getEventTarget(); |
| + nativeLayerEventTarget.expects(atLeastOnce()) |
| .addEventListener(ANYTHING, ANYTHING, ANYTHING); |
| invitationStore_ = new print_preview.InvitationStore(); |
| + var nativeLayerProxy = nativeLayer_; |
| + nativeLayerProxy.eventTarget_ = nativeLayerEventTarget.proxy(); |
| destinationStore_ = new print_preview.DestinationStore( |
| - nativeLayer_.proxy(), new print_preview.UserInfo(), |
| + nativeLayerProxy, new print_preview.UserInfo(), |
| new print_preview.AppState()); |
| userInfo_ = new print_preview.UserInfo(); |
| @@ -183,45 +218,53 @@ TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { |
| test('ResolutionFails', function() { |
| var destId = "001122DEADBEEF"; |
| - |
| - var resolver = mockSetupCall(destId, nativeLayer_); |
| - requestSetup(destId, destinationSearch_); |
| - resolver.reject(destId); |
| + nativeLayer_.setSetupPrinterResponse(true, { printerId: destId }); |
| + nativeLayer_.whenCalled('setupPrinter').then( function() { |
| + requestSetup(destId, destinationSearch_); |
| + }.bind(destId, destinationSearch_)); |
| }); |
| test('ReceiveSuccessfulSetup', function() { |
| var destId = "00112233DEADBEEF"; |
| - |
| + var response = { |
| + printerId: destId, |
| + capabilities: getCaps(), |
| + success: true |
| + }; |
| + nativeLayer_.setSetupPrinterResponse(false, response); |
| var waiter = waitForEvent( |
| destinationStore_, |
| print_preview.DestinationStore.EventType.DESTINATION_SELECT); |
| - var resolver = mockSetupCall(destId, nativeLayer_); |
| + mockSetupCall(destId, nativeLayer_).then( function() { |
| + requestSetup(destId, destinationSearch_); |
| - requestSetup(destId, destinationSearch_); |
| - resolveSetup(resolver, destId, true, getCaps()); |
| + // wait for event propogation to complete. |
| + return waiter.then(function() { |
| + // after setup succeeds, the destination should be selected. |
| + assertNotEquals(null, destinationStore_.selectedDestination); |
| + assertEquals(destId, destinationStore_.selectedDestination.id); |
| + }); |
| + }.bind(waiter)); |
| - // wait for event propogation to complete. |
| - return waiter.then(function() { |
| - // after setup succeeds, the destination should be selected. |
| - assertNotEquals(null, destinationStore_.selectedDestination); |
| - assertEquals(destId, destinationStore_.selectedDestination.id); |
| - }); |
| }); |
| if (cr.isChromeOS) { |
| // The 'ResolutionFails' test covers this case for non-CrOS. |
| test('ReceiveFailedSetup', function() { |
| var destId = '00112233DEADBEEF'; |
| + var response = { |
| + printerId: destId, |
| + capabilities: getCaps(), |
| + success: false |
| + }; |
| + nativeLayer_.setSetupPrinterResponse(false, response); |
| + nativeLayer_.whenCalled('setupPrinter').then(function () { |
| + requestSetup(destId, destinationSearch_); |
| - var resolver = mockSetupCall(destId, nativeLayer_); |
| - requestSetup(destId, destinationSearch_); |
| - |
| - // Force resolution to fail. |
| - resolveSetup(resolver, destId, false, null); |
| - |
| - // Selection should not change on ChromeOS. |
| - assertEquals(null, destinationStore_.selectedDestination); |
| + // Selection should not change on ChromeOS. |
| + assertEquals(null, destinationStore_.selectedDestination); |
| + }.bind(destId, destinationSearch_, destinationStore_)); |
| }); |
| } |