Debian Codegraph @

closure-library/closure/goog/ui/ac/cachingmatcher_test.js

raw
1 // Copyright 2013 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.CachingMatcherTest');
16 goog.setTestOnly('goog.ui.ac.CachingMatcherTest');
17
18 goog.require('goog.testing.MockControl');
19 goog.require('goog.testing.jsunit');
20 goog.require('goog.testing.mockmatchers');
21 goog.require('goog.ui.ac.CachingMatcher');
22 ignoreArgument = goog.testing.mockmatchers.ignoreArgument;
23
24
25
26 /**
27 * Fake version of Throttle which only fires when we call permitOne().
28 * @constructor
29 * @suppress {missingProvide}
30 */
31 goog.async.Throttle = function(fn, time, self) {
32 this.fn = fn;
33 this.time = time;
34 this.self = self;
35 this.numFires = 0;
36 };
37
38
39 /** @suppress {missingProvide} */
40 goog.async.Throttle.prototype.fire = function() {
41 this.numFires++;
42 };
43
44
45 /** @suppress {missingProvide} */
46 goog.async.Throttle.prototype.permitOne = function() {
47 if (this.numFires == 0) {
48 return;
49 }
50 this.fn.call(this.self);
51 this.numFires = 0;
52 };
53
54 // Actual tests.
55 var mockControl;
56 var mockMatcher;
57 var mockHandler;
58 var matcher;
59
60 function setUp() {
61 mockControl = new goog.testing.MockControl();
62 mockMatcher = {
63 requestMatchingRows: mockControl.createFunctionMock('requestMatchingRows')
64 };
65 mockHandler = mockControl.createFunctionMock('matchHandler');
66 matcher = new goog.ui.ac.CachingMatcher(mockMatcher);
67 }
68
69 function tearDown() {
70 mockControl.$tearDown();
71 }
72
73 function testLocalThenRemoteMatch() {
74 // We immediately get the local match.
75 mockHandler('foo', []);
76 mockControl.$replayAll();
77 matcher.requestMatchingRows('foo', 12, mockHandler);
78 mockControl.$verifyAll();
79 mockControl.$resetAll();
80
81 // Now we run the remote match.
82 mockHandler('foo', ['foo1', 'foo2'], ignoreArgument);
83 mockMatcher.requestMatchingRows('foo', 100, ignoreArgument)
84 .$does(function(token, maxResults, matchHandler) {
85 matchHandler('foo', ['foo1', 'foo2', 'bar3']);
86 });
87 mockControl.$replayAll();
88 matcher.throttledTriggerBaseMatch_.permitOne();
89 mockControl.$verifyAll();
90 mockControl.$resetAll();
91 }
92
93 function testCacheSize() {
94 matcher.setMaxCacheSize(4);
95
96 // First we populate, but not overflow the cache.
97 mockHandler('foo', []);
98 mockHandler('foo', ['foo111', 'foo222'], ignoreArgument);
99 mockMatcher.requestMatchingRows('foo', 100, ignoreArgument)
100 .$does(function(token, maxResults, matchHandler) {
101 matchHandler('foo', ['foo111', 'foo222', 'bar333']);
102 });
103 mockControl.$replayAll();
104 matcher.requestMatchingRows('foo', 12, mockHandler);
105 matcher.throttledTriggerBaseMatch_.permitOne();
106 mockControl.$verifyAll();
107 mockControl.$resetAll();
108
109 // Now we verify the cache is populated.
110 mockHandler('foo1', ['foo111']);
111 mockControl.$replayAll();
112 matcher.requestMatchingRows('foo1', 12, mockHandler);
113 mockControl.$verifyAll();
114 mockControl.$resetAll();
115
116 // Now we overflow the cache. Check that the remote results show the first
117 // time we get them back, even though they overflow the cache.
118 mockHandler('foo11', ['foo111']);
119 mockHandler(
120 'foo11', ['foo111', 'foo112', 'foo113', 'foo114'], ignoreArgument);
121 mockMatcher.requestMatchingRows('foo11', 100, ignoreArgument)
122 .$does(function(token, maxResults, matchHandler) {
123 matchHandler('foo11', ['foo111', 'foo112', 'foo113', 'foo114']);
124 });
125 mockControl.$replayAll();
126 matcher.requestMatchingRows('foo11', 12, mockHandler);
127 matcher.throttledTriggerBaseMatch_.permitOne();
128 mockControl.$verifyAll();
129 mockControl.$resetAll();
130
131 // Now check that the cache is empty.
132 mockHandler('foo11', []);
133 mockControl.$replayAll();
134 matcher.requestMatchingRows('foo11', 12, mockHandler);
135 mockControl.$verifyAll();
136 mockControl.$resetAll();
137 }
138
139 function testClearCache() {
140 // First we populate the cache.
141 mockHandler('foo', []);
142 mockHandler('foo', ['foo111', 'foo222'], ignoreArgument);
143 mockMatcher.requestMatchingRows('foo', 100, ignoreArgument)
144 .$does(function(token, maxResults, matchHandler) {
145 matchHandler('foo', ['foo111', 'foo222', 'bar333']);
146 });
147 mockControl.$replayAll();
148 matcher.requestMatchingRows('foo', 12, mockHandler);
149 matcher.throttledTriggerBaseMatch_.permitOne();
150 mockControl.$verifyAll();
151 mockControl.$resetAll();
152
153 // Now we verify the cache is populated.
154 mockHandler('foo1', ['foo111']);
155 mockControl.$replayAll();
156 matcher.requestMatchingRows('foo1', 12, mockHandler);
157 mockControl.$verifyAll();
158 mockControl.$resetAll();
159
160 // Now we clear the cache.
161 matcher.clearCache();
162
163 // Now check that the cache is empty.
164 mockHandler('foo11', []);
165 mockControl.$replayAll();
166 matcher.requestMatchingRows('foo11', 12, mockHandler);
167 mockControl.$verifyAll();
168 mockControl.$resetAll();
169 }
170
171 function testSimilarMatchingDoesntReorderResults() {
172 // Populate the cache. We get two prefix matches.
173 mockHandler('ba', []);
174 mockHandler('ba', ['bar', 'baz', 'bam'], ignoreArgument);
175 mockMatcher.requestMatchingRows('ba', 100, ignoreArgument)
176 .$does(function(token, maxResults, matchHandler) {
177 matchHandler('ba', ['bar', 'baz', 'bam']);
178 });
179 mockControl.$replayAll();
180 matcher.requestMatchingRows('ba', 12, mockHandler);
181 matcher.throttledTriggerBaseMatch_.permitOne();
182 mockControl.$verifyAll();
183 mockControl.$resetAll();
184
185 // The user types another character. The local match gives us two similar
186 // matches, but no prefix matches. The remote match returns a prefix match,
187 // which would normally be ranked above the similar matches, but gets ranked
188 // below the similar matches because the user hasn't typed any more
189 // characters.
190 mockHandler('bad', ['bar', 'baz', 'bam']);
191 mockHandler(
192 'bad', ['bar', 'baz', 'bam', 'bad', 'badder', 'baddest'], ignoreArgument);
193 mockMatcher.requestMatchingRows('bad', 100, ignoreArgument)
194 .$does(function(token, maxResults, matchHandler) {
195 matchHandler('bad', ['bad', 'badder', 'baddest']);
196 });
197 mockControl.$replayAll();
198 matcher.requestMatchingRows('bad', 12, mockHandler);
199 matcher.throttledTriggerBaseMatch_.permitOne();
200 mockControl.$verifyAll();
201 mockControl.$resetAll();
202
203 // The user types yet another character, which allows the prefix matches to
204 // jump to the top of the list of suggestions.
205 mockHandler('badd', ['badder', 'baddest']);
206 mockControl.$replayAll();
207 matcher.requestMatchingRows('badd', 12, mockHandler);
208 mockControl.$verifyAll();
209 mockControl.$resetAll();
210 }
211
212 function testSetThrottleTime() {
213 assertEquals(150, matcher.throttledTriggerBaseMatch_.time);
214 matcher.setThrottleTime(234);
215 assertEquals(234, matcher.throttledTriggerBaseMatch_.time);
216 }
217
218 function testSetBaseMatcherMaxMatches() {
219 mockHandler('foo', []); // Local match
220 mockMatcher.requestMatchingRows('foo', 789, ignoreArgument);
221 mockControl.$replayAll();
222 matcher.setBaseMatcherMaxMatches();
223 matcher.requestMatchingRows('foo', 12, mockHandler);
224 }
225
226 function testSetLocalMatcher() {
227 // Use a local matcher which just sorts all the rows alphabetically.
228 function sillyMatcher(token, maxMatches, rows) {
229 rows = rows.concat([]);
230 rows.sort();
231 return rows;
232 }
233
234 mockHandler('foo', []);
235 mockHandler('foo', ['a', 'b', 'c'], ignoreArgument);
236 mockMatcher.requestMatchingRows('foo', 100, ignoreArgument)
237 .$does(function(token, maxResults, matchHandler) {
238 matchHandler('foo', ['b', 'a', 'c']);
239 });
240 mockControl.$replayAll();
241 matcher.setLocalMatcher(sillyMatcher);
242 matcher.requestMatchingRows('foo', 12, mockHandler);
243 matcher.throttledTriggerBaseMatch_.permitOne();
244 mockControl.$verifyAll();
245 mockControl.$resetAll();
246 }