OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // TODO(robliao,vadimt): Determine the granularity of testing to perform. | 5 // TODO(robliao,vadimt): Determine the granularity of testing to perform. |
6 | 6 |
7 /** | 7 /** |
8 * Test fixture for background.js. | 8 * Test fixture for background.js. |
9 * @constructor | 9 * @constructor |
10 * @extends {testing.Test} | 10 * @extends {testing.Test} |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true); | 45 testTaskPair(RETRY_DISMISS_TASK_NAME, RETRY_DISMISS_TASK_NAME, true); |
46 testTaskPair(RETRY_DISMISS_TASK_NAME, STATE_CHANGED_TASK_NAME, false); | 46 testTaskPair(RETRY_DISMISS_TASK_NAME, STATE_CHANGED_TASK_NAME, false); |
47 | 47 |
48 testTaskPair(STATE_CHANGED_TASK_NAME, UPDATE_CARDS_TASK_NAME, false); | 48 testTaskPair(STATE_CHANGED_TASK_NAME, UPDATE_CARDS_TASK_NAME, false); |
49 testTaskPair(STATE_CHANGED_TASK_NAME, DISMISS_CARD_TASK_NAME, false); | 49 testTaskPair(STATE_CHANGED_TASK_NAME, DISMISS_CARD_TASK_NAME, false); |
50 testTaskPair(STATE_CHANGED_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); | 50 testTaskPair(STATE_CHANGED_TASK_NAME, RETRY_DISMISS_TASK_NAME, false); |
51 testTaskPair(STATE_CHANGED_TASK_NAME, STATE_CHANGED_TASK_NAME, false); | 51 testTaskPair(STATE_CHANGED_TASK_NAME, STATE_CHANGED_TASK_NAME, false); |
52 }); | 52 }); |
53 | 53 |
54 /** | 54 /** |
| 55 * Server Request Tests |
| 56 */ |
| 57 TEST_F('GoogleNowBackgroundUnitTest', 'AuthServerRequestSuccess', function() { |
| 58 expectServerRequests(this, 200, '{}'); |
| 59 var callbackCalled = false; |
| 60 requestFromServer('GET', 'test/target').then(function(request) { |
| 61 callbackCalled = true; |
| 62 assertTrue(request.status === 200); |
| 63 assertTrue(request.responseText === '{}'); |
| 64 }); |
| 65 assertTrue(callbackCalled); |
| 66 }); |
| 67 |
| 68 TEST_F('GoogleNowBackgroundUnitTest', 'AuthServerRequestForbidden', function() { |
| 69 this.makeAndRegisterMockApis(['authenticationManager.removeToken']); |
| 70 this.mockApis.expects(once()).authenticationManager_removeToken(ANYTHING); |
| 71 |
| 72 expectServerRequests(this, 403, ''); |
| 73 |
| 74 var callbackCalled = false; |
| 75 requestFromServer('GET', 'test/target').then(function(request) { |
| 76 // The promise is resolved even on HTTP failures. |
| 77 callbackCalled = true; |
| 78 assertTrue(request.status === 403); |
| 79 }); |
| 80 assertTrue(callbackCalled); |
| 81 }); |
| 82 |
| 83 TEST_F('GoogleNowBackgroundUnitTest', 'AuthServerRequestNoAuth', function() { |
| 84 this.makeAndRegisterMockApis(['authenticationManager.removeToken']); |
| 85 this.mockApis.expects(once()).authenticationManager_removeToken(ANYTHING); |
| 86 |
| 87 expectServerRequests(this, 401, ''); |
| 88 |
| 89 var callbackCalled = false; |
| 90 requestFromServer('GET', 'test/target').then(function(request) { |
| 91 // The promise is resolved even on HTTP failures. |
| 92 callbackCalled = true; |
| 93 assertTrue(request.status === 401); |
| 94 }); |
| 95 assertTrue(callbackCalled); |
| 96 }); |
| 97 |
| 98 function expectServerRequests(fixture, httpStatus, responseText) { |
| 99 fixture.makeAndRegisterMockApis([ |
| 100 'authenticationManager.getAuthToken', |
| 101 'buildServerRequest' |
| 102 ]); |
| 103 |
| 104 function XMLHttpRequest() {} |
| 105 |
| 106 XMLHttpRequest.prototype = { |
| 107 addEventListener: function(type, listener, wantsUntrusted) {}, |
| 108 setRequestHeader: function(header, value) {}, |
| 109 send: function() {} |
| 110 } |
| 111 |
| 112 fixture.mockApis.expects(once()).authenticationManager_getAuthToken() |
| 113 .will(returnValue(Promise.resolve('token'))); |
| 114 |
| 115 var mockXMLHttpRequest = mock(XMLHttpRequest); |
| 116 var mockXMLHttpRequestProxy = mockXMLHttpRequest.proxy(); |
| 117 fixture.mockApis.expects(once()) |
| 118 .buildServerRequest(ANYTHING, ANYTHING, ANYTHING) |
| 119 .will(returnValue(mockXMLHttpRequestProxy)); |
| 120 |
| 121 mockXMLHttpRequest.expects(once()) |
| 122 .setRequestHeader('Authorization', 'Bearer token'); |
| 123 |
| 124 var loadEndSavedArgs = new SaveMockArguments(); |
| 125 mockXMLHttpRequest.expects(once()) |
| 126 .addEventListener( |
| 127 loadEndSavedArgs.match(eq('loadend')), |
| 128 loadEndSavedArgs.match(ANYTHING), |
| 129 loadEndSavedArgs.match(eq(false))); |
| 130 |
| 131 mockXMLHttpRequestProxy.status = httpStatus; |
| 132 mockXMLHttpRequestProxy.response = responseText; |
| 133 mockXMLHttpRequestProxy.responseText = responseText; |
| 134 |
| 135 mockXMLHttpRequest.expects(once()).send() |
| 136 .will(invokeCallback(loadEndSavedArgs, 1, mockXMLHttpRequestProxy)); |
| 137 } |
| 138 |
| 139 TEST_F('GoogleNowBackgroundUnitTest', 'AuthServerRequestNoToken', function() { |
| 140 this.makeAndRegisterMockApis([ |
| 141 'authenticationManager.getAuthToken', |
| 142 'buildServerRequest' |
| 143 ]); |
| 144 |
| 145 this.mockApis.expects(once()).authenticationManager_getAuthToken() |
| 146 .will(returnValue(Promise.reject())); |
| 147 this.mockApis.expects(never()).buildServerRequest() |
| 148 |
| 149 var thenCalled = false; |
| 150 var catchCalled = false; |
| 151 requestFromServer('GET', 'test/target').then(function(request) { |
| 152 thenCalled = true; |
| 153 }).catch(function() { |
| 154 catchCalled = true; |
| 155 }); |
| 156 assertFalse(thenCalled); |
| 157 assertTrue(catchCalled); |
| 158 }) |
| 159 |
| 160 /** |
55 * Mocks global functions and APIs that initialize() depends upon. | 161 * Mocks global functions and APIs that initialize() depends upon. |
56 * @param {Test} fixture Test fixture. | 162 * @param {Test} fixture Test fixture. |
57 */ | 163 */ |
58 function mockInitializeDependencies(fixture) { | 164 function mockInitializeDependencies(fixture) { |
59 fixture.makeAndRegisterMockGlobals([ | 165 fixture.makeAndRegisterMockGlobals([ |
60 'recordEvent', | 166 'recordEvent', |
61 'setBackgroundEnable', | 167 'setBackgroundEnable', |
62 'startPollingCards' | 168 'startPollingCards' |
63 ]); | 169 ]); |
64 fixture.makeAndRegisterMockApis([ | 170 fixture.makeAndRegisterMockApis([ |
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 eqJSON({ | 874 eqJSON({ |
769 notificationGroups: expectedUpdatedGroups, | 875 notificationGroups: expectedUpdatedGroups, |
770 recentDismissals: {}})); | 876 recentDismissals: {}})); |
771 | 877 |
772 this.mockGlobals.expects(once()). | 878 this.mockGlobals.expects(once()). |
773 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); | 879 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); |
774 | 880 |
775 // Invoking the tested function. | 881 // Invoking the tested function. |
776 processServerResponse(serverResponse, fakeOnCardShownFunction); | 882 processServerResponse(serverResponse, fakeOnCardShownFunction); |
777 }); | 883 }); |
OLD | NEW |