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