| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.ui.ac.RichRemoteArrayMatcherTest'); |
| 16 |
goog.setTestOnly('goog.ui.ac.RichRemoteArrayMatcherTest'); |
| 17 |
|
| 18 |
goog.require('goog.net.XhrIo'); |
| 19 |
goog.require('goog.testing.MockControl'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.testing.net.XhrIo'); |
| 22 |
goog.require('goog.ui.ac.RichRemoteArrayMatcher'); |
| 23 |
|
| 24 |
var url = 'http://www.google.com'; |
| 25 |
var token = 'goog'; |
| 26 |
var maxMatches = 5; |
| 27 |
|
| 28 |
var responseJsonText = '[["type1", "eric", "larry", "sergey"]]'; |
| 29 |
var responseJsonType1 = ["eric", "larry", "sergey"]; |
| 30 |
|
| 31 |
var mockControl; |
| 32 |
var mockMatchHandler; |
| 33 |
|
| 34 |
|
| 35 |
function setUp() { |
| 36 |
goog.net.XhrIo = goog.testing.net.XhrIo; |
| 37 |
mockControl = new goog.testing.MockControl(); |
| 38 |
mockMatchHandler = mockControl.createFunctionMock(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Callback for type1 responses. |
| 43 |
* @param {string} response |
| 44 |
* @return {string} |
| 45 |
*/ |
| 46 |
function type1(response) { |
| 47 |
return response; |
| 48 |
} |
| 49 |
|
| 50 |
function testRequestMatchingRows() { |
| 51 |
var matcher = new goog.ui.ac.RichRemoteArrayMatcher(url); |
| 52 |
mockMatchHandler(token, responseJsonType1); |
| 53 |
mockControl.$replayAll(); |
| 54 |
matcher.requestMatchingRows(token, maxMatches, mockMatchHandler); |
| 55 |
matcher.xhr_.simulateResponse(200, responseJsonText); |
| 56 |
mockControl.$verifyAll(); |
| 57 |
mockControl.$resetAll(); |
| 58 |
} |
| 59 |
|
| 60 |
function testSetRowBuilder() { |
| 61 |
var matcher = new goog.ui.ac.RichRemoteArrayMatcher(url); |
| 62 |
matcher.setRowBuilder(function(type, response) { |
| 63 |
assertEquals("type1", type); |
| 64 |
return response; |
| 65 |
}); |
| 66 |
mockMatchHandler(token, responseJsonType1); |
| 67 |
mockControl.$replayAll(); |
| 68 |
matcher.requestMatchingRows(token, maxMatches, mockMatchHandler); |
| 69 |
matcher.xhr_.simulateResponse(200, responseJsonText); |
| 70 |
mockControl.$verifyAll(); |
| 71 |
mockControl.$resetAll(); |
| 72 |
} |