remotenamefetcher_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2012 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.i18n.uChar.RemoteNameFetcherTest');
  15. goog.setTestOnly('goog.i18n.uChar.RemoteNameFetcherTest');
  16. goog.require('goog.i18n.uChar.RemoteNameFetcher');
  17. goog.require('goog.net.XhrIo');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.testing.net.XhrIo');
  20. goog.require('goog.testing.recordFunction');
  21. var nameFetcher = null;
  22. function setUp() {
  23. goog.net.XhrIo = goog.testing.net.XhrIo;
  24. nameFetcher = new goog.i18n.uChar.RemoteNameFetcher('http://www.example.com');
  25. }
  26. function tearDown() {
  27. nameFetcher.dispose();
  28. }
  29. function testGetName_remote() {
  30. var callback = goog.testing.recordFunction(function(name) {
  31. assertEquals('Latin Capital Letter P', name);
  32. assertTrue(nameFetcher.charNames_.containsKey('50'));
  33. });
  34. nameFetcher.getName('P', callback);
  35. var responseJsonText = '{"50":{"name":"Latin Capital Letter P"}}';
  36. nameFetcher.getNameXhrIo_.simulateResponse(200, responseJsonText);
  37. assertEquals(1, callback.getCallCount());
  38. }
  39. function testGetName_existing() {
  40. nameFetcher.charNames_.set('1049d', 'OSYMANYA LETTER OO');
  41. var callback = goog.testing.recordFunction(function(name) {
  42. assertEquals('OSYMANYA LETTER OO', name);
  43. });
  44. nameFetcher.getName('\uD801\uDC9D', callback);
  45. assertEquals(1, callback.getCallCount());
  46. }
  47. function testGetName_fail() {
  48. var callback =
  49. goog.testing.recordFunction(function(name) { assertNull(name); });
  50. nameFetcher.getName('\uD801\uDC9D', callback);
  51. assertEquals(
  52. 'http://www.example.com?c=1049d&p=name',
  53. nameFetcher.getNameXhrIo_.getLastUri().toString());
  54. nameFetcher.getNameXhrIo_.simulateResponse(400);
  55. assertEquals(1, callback.getCallCount());
  56. }
  57. function testGetName_abort() {
  58. var callback1 =
  59. goog.testing.recordFunction(function(name) { assertNull(name); });
  60. nameFetcher.getName('I', callback1);
  61. var callback2 = goog.testing.recordFunction(function(name) {
  62. assertEquals(name, 'LATIN SMALL LETTER Y');
  63. });
  64. nameFetcher.getName('ÿ', callback2);
  65. assertEquals(
  66. 'http://www.example.com?c=ff&p=name',
  67. nameFetcher.getNameXhrIo_.getLastUri().toString());
  68. var responseJsonText = '{"ff":{"name":"LATIN SMALL LETTER Y"}}';
  69. nameFetcher.getNameXhrIo_.simulateResponse(200, responseJsonText);
  70. assertEquals(1, callback1.getCallCount());
  71. assertEquals(1, callback2.getCallCount());
  72. }
  73. function testPrefetch() {
  74. nameFetcher.prefetch('ÿI\uD801\uDC9D');
  75. assertEquals(
  76. 'http://www.example.com?b88=%C3%BFI%F0%90%92%9D&p=name',
  77. nameFetcher.prefetchXhrIo_.getLastUri().toString());
  78. var responseJsonText = '{"ff":{"name":"LATIN SMALL LETTER Y"},"49":{' +
  79. '"name":"LATIN CAPITAL LETTER I"}, "1049d":{"name":"OSMYANA OO"}}';
  80. nameFetcher.prefetchXhrIo_.simulateResponse(200, responseJsonText);
  81. assertEquals(3, nameFetcher.charNames_.getCount());
  82. assertEquals('LATIN SMALL LETTER Y', nameFetcher.charNames_.get('ff'));
  83. assertEquals('LATIN CAPITAL LETTER I', nameFetcher.charNames_.get('49'));
  84. assertEquals('OSMYANA OO', nameFetcher.charNames_.get('1049d'));
  85. }
  86. function testPrefetch_abort() {
  87. nameFetcher.prefetch('I\uD801\uDC9D');
  88. nameFetcher.prefetch('ÿ');
  89. assertEquals(
  90. 'http://www.example.com?b88=%C3%BF&p=name',
  91. nameFetcher.prefetchXhrIo_.getLastUri().toString());
  92. }
  93. function testIsNameAvailable() {
  94. assertTrue(nameFetcher.isNameAvailable('a'));
  95. }