richremotearraymatcher_test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. goog.provide('goog.ui.ac.RichRemoteArrayMatcherTest');
  15. goog.setTestOnly('goog.ui.ac.RichRemoteArrayMatcherTest');
  16. goog.require('goog.net.XhrIo');
  17. goog.require('goog.testing.MockControl');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.testing.net.XhrIo');
  20. goog.require('goog.ui.ac.RichRemoteArrayMatcher');
  21. var url = 'http://www.google.com';
  22. var token = 'goog';
  23. var maxMatches = 5;
  24. var responseJsonText = '[["type1", "eric", "larry", "sergey"]]';
  25. var responseJsonType1 = ["eric", "larry", "sergey"];
  26. var mockControl;
  27. var mockMatchHandler;
  28. function setUp() {
  29. goog.net.XhrIo = goog.testing.net.XhrIo;
  30. mockControl = new goog.testing.MockControl();
  31. mockMatchHandler = mockControl.createFunctionMock();
  32. }
  33. /**
  34. * Callback for type1 responses.
  35. * @param {string} response
  36. * @return {string}
  37. */
  38. function type1(response) {
  39. return response;
  40. }
  41. function testRequestMatchingRows() {
  42. var matcher = new goog.ui.ac.RichRemoteArrayMatcher(url);
  43. mockMatchHandler(token, responseJsonType1);
  44. mockControl.$replayAll();
  45. matcher.requestMatchingRows(token, maxMatches, mockMatchHandler);
  46. matcher.xhr_.simulateResponse(200, responseJsonText);
  47. mockControl.$verifyAll();
  48. mockControl.$resetAll();
  49. }
  50. function testSetRowBuilder() {
  51. var matcher = new goog.ui.ac.RichRemoteArrayMatcher(url);
  52. matcher.setRowBuilder(function(type, response) {
  53. assertEquals("type1", type);
  54. return response;
  55. });
  56. mockMatchHandler(token, responseJsonType1);
  57. mockControl.$replayAll();
  58. matcher.requestMatchingRows(token, maxMatches, mockMatchHandler);
  59. matcher.xhr_.simulateResponse(200, responseJsonText);
  60. mockControl.$verifyAll();
  61. mockControl.$resetAll();
  62. }