aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/i18n/uchar/remotenamefetcher_test.html
blob: 3e2dd87a60eed2ff966915138ac6ce5a8cd6d0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<html>
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Closure Unit Tests - goog.i18n.uChar.RemoteNameFetcher</title>
    <meta charset="utf-8">
    <script src='../../base.js'>
    </script>
    <script>
      goog.require('goog.i18n.uChar.RemoteNameFetcher');
      goog.require('goog.net.XhrIo');
      goog.require('goog.testing.jsunit');
      goog.require('goog.testing.recordFunction');
      goog.require('goog.testing.net.XhrIo');
    </script>
  </head>
  <body>
    <script>
var nameFetcher = null;

function setUp() {
  goog.net.XhrIo = goog.testing.net.XhrIo;
  nameFetcher = new goog.i18n.uChar.RemoteNameFetcher('http://www.example.com');
}

function tearDown() {
  nameFetcher.dispose();
}

function testGetName_remote() {
  var callback = goog.testing.recordFunction(function(name) {
    assertEquals('Latin Capital Letter P', name);
    assertTrue(nameFetcher.charNames_.containsKey('50'));
  });
  nameFetcher.getName('P', callback);
  var responseJsonText = '{"50":{"name":"Latin Capital Letter P"}}';
  nameFetcher.getNameXhrIo_.simulateResponse(200, responseJsonText);
  assertEquals(1, callback.getCallCount());
}

function testGetName_existing() {
  nameFetcher.charNames_.set('1049d', 'OSYMANYA LETTER OO');
  var callback = goog.testing.recordFunction(function(name) {
    assertEquals('OSYMANYA LETTER OO', name);
  });
  nameFetcher.getName('\uD801\uDC9D', callback);
  assertEquals(1, callback.getCallCount());
}

function testGetName_fail() {
  var callback = goog.testing.recordFunction(function(name) {
    assertNull(name);
  });
  nameFetcher.getName('\uD801\uDC9D', callback);
  assertEquals('http://www.example.com?c=1049d&p=name',
      nameFetcher.getNameXhrIo_.getLastUri().toString());
  nameFetcher.getNameXhrIo_.simulateResponse(400);
  assertEquals(1, callback.getCallCount());
}

function testGetName_abort() {
  var callback1 = goog.testing.recordFunction(function(name) {
    assertNull(name);
  });
  nameFetcher.getName('I', callback1);
  var callback2 = goog.testing.recordFunction(function(name) {
    assertEquals(name, 'LATIN SMALL LETTER Y');
  });
  nameFetcher.getName('ÿ', callback2);
  assertEquals('http://www.example.com?c=ff&p=name',
      nameFetcher.getNameXhrIo_.getLastUri().toString());
  var responseJsonText = '{"ff":{"name":"LATIN SMALL LETTER Y"}}';
  nameFetcher.getNameXhrIo_.simulateResponse(200, responseJsonText);
  assertEquals(1, callback1.getCallCount());
  assertEquals(1, callback2.getCallCount());
}

function testPrefetch() {
  nameFetcher.prefetch('ÿI\uD801\uDC9D');
  assertEquals('http://www.example.com?b88=%C3%BFI%F0%90%92%9D&p=name',
      nameFetcher.prefetchXhrIo_.getLastUri().toString());

  var responseJsonText = '{"ff":{"name":"LATIN SMALL LETTER Y"},"49":{'
      + '"name":"LATIN CAPITAL LETTER I"}, "1049d":{"name":"OSMYANA OO"}}';
  nameFetcher.prefetchXhrIo_.simulateResponse(200, responseJsonText);

  assertEquals(3, nameFetcher.charNames_.getCount());
  assertEquals('LATIN SMALL LETTER Y', nameFetcher.charNames_.get('ff'));
  assertEquals('LATIN CAPITAL LETTER I', nameFetcher.charNames_.get('49'));
  assertEquals('OSMYANA OO', nameFetcher.charNames_.get('1049d'));
}

function testPrefetch_abort() {
  nameFetcher.prefetch('I\uD801\uDC9D');
  nameFetcher.prefetch('ÿ');
  assertEquals('http://www.example.com?b88=%C3%BF&p=name',
      nameFetcher.prefetchXhrIo_.getLastUri().toString());
}

function testIsNameAvailable() {
  assertTrue(nameFetcher.isNameAvailable('a'));
}
    </script>
  </body>
</html>