Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(866)

Unified Diff: chrome/browser/resources/google_now/background_unittest.gtestjs

Issue 202293003: Server Request Tests and Mock Promise Enhancements (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CR Feedback Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/common_test_util.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/google_now/background_unittest.gtestjs
diff --git a/chrome/browser/resources/google_now/background_unittest.gtestjs b/chrome/browser/resources/google_now/background_unittest.gtestjs
index d9cbb07d13e028d8a8117377ba655d062ae20f70..14d8e58429e11417fd0ff857ca6979d62f869761 100644
--- a/chrome/browser/resources/google_now/background_unittest.gtestjs
+++ b/chrome/browser/resources/google_now/background_unittest.gtestjs
@@ -52,6 +52,112 @@ TEST_F('GoogleNowBackgroundUnitTest', 'AreTasksConflicting', function() {
});
/**
+ * Server Request Tests
+ */
+TEST_F('GoogleNowBackgroundUnitTest', 'AuthServerRequestSuccess', function() {
+ expectServerRequests(this, 200, '{}');
+ var callbackCalled = false;
+ requestFromServer('GET', 'test/target').then(function(request) {
+ callbackCalled = true;
+ assertTrue(request.status === 200);
+ assertTrue(request.responseText === '{}');
+ });
+ assertTrue(callbackCalled);
+});
+
+TEST_F('GoogleNowBackgroundUnitTest', 'AuthServerRequestForbidden', function() {
+ this.makeAndRegisterMockApis(['authenticationManager.removeToken']);
+ this.mockApis.expects(once()).authenticationManager_removeToken(ANYTHING);
+
+ expectServerRequests(this, 403, '');
+
+ var callbackCalled = false;
+ requestFromServer('GET', 'test/target').then(function(request) {
+ // The promise is resolved even on HTTP failures.
+ callbackCalled = true;
+ assertTrue(request.status === 403);
+ });
+ assertTrue(callbackCalled);
+});
+
+TEST_F('GoogleNowBackgroundUnitTest', 'AuthServerRequestNoAuth', function() {
+ this.makeAndRegisterMockApis(['authenticationManager.removeToken']);
+ this.mockApis.expects(once()).authenticationManager_removeToken(ANYTHING);
+
+ expectServerRequests(this, 401, '');
+
+ var callbackCalled = false;
+ requestFromServer('GET', 'test/target').then(function(request) {
+ // The promise is resolved even on HTTP failures.
+ callbackCalled = true;
+ assertTrue(request.status === 401);
+ });
+ assertTrue(callbackCalled);
+});
+
+function expectServerRequests(fixture, httpStatus, responseText) {
+ fixture.makeAndRegisterMockApis([
+ 'authenticationManager.getAuthToken',
+ 'buildServerRequest'
+ ]);
+
+ function XMLHttpRequest() {}
+
+ XMLHttpRequest.prototype = {
+ addEventListener: function(type, listener, wantsUntrusted) {},
+ setRequestHeader: function(header, value) {},
+ send: function() {}
+ }
+
+ fixture.mockApis.expects(once()).authenticationManager_getAuthToken()
+ .will(returnValue(Promise.resolve('token')));
+
+ var mockXMLHttpRequest = mock(XMLHttpRequest);
+ var mockXMLHttpRequestProxy = mockXMLHttpRequest.proxy();
+ fixture.mockApis.expects(once())
+ .buildServerRequest(ANYTHING, ANYTHING, ANYTHING)
+ .will(returnValue(mockXMLHttpRequestProxy));
+
+ mockXMLHttpRequest.expects(once())
+ .setRequestHeader('Authorization', 'Bearer token');
+
+ var loadEndSavedArgs = new SaveMockArguments();
+ mockXMLHttpRequest.expects(once())
+ .addEventListener(
+ loadEndSavedArgs.match(eq('loadend')),
+ loadEndSavedArgs.match(ANYTHING),
+ loadEndSavedArgs.match(eq(false)));
+
+ mockXMLHttpRequestProxy.status = httpStatus;
+ mockXMLHttpRequestProxy.response = responseText;
+ mockXMLHttpRequestProxy.responseText = responseText;
+
+ mockXMLHttpRequest.expects(once()).send()
+ .will(invokeCallback(loadEndSavedArgs, 1, mockXMLHttpRequestProxy));
+}
+
+TEST_F('GoogleNowBackgroundUnitTest', 'AuthServerRequestNoToken', function() {
+ this.makeAndRegisterMockApis([
+ 'authenticationManager.getAuthToken',
+ 'buildServerRequest'
+ ]);
+
+ this.mockApis.expects(once()).authenticationManager_getAuthToken()
+ .will(returnValue(Promise.reject()));
+ this.mockApis.expects(never()).buildServerRequest()
+
+ var thenCalled = false;
+ var catchCalled = false;
+ requestFromServer('GET', 'test/target').then(function(request) {
+ thenCalled = true;
+ }).catch(function() {
+ catchCalled = true;
+ });
+ assertFalse(thenCalled);
+ assertTrue(catchCalled);
+})
+
+/**
* Mocks global functions and APIs that initialize() depends upon.
* @param {Test} fixture Test fixture.
*/
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/common_test_util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698