webfontloader 1.0.15 → 1.0.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,555 +3,244 @@ var FontWatcherTest = TestCase('FontWatcherTest');
3
3
  FontWatcherTest.prototype.setUp = function() {
4
4
  var self = this;
5
5
 
6
- this.classNames_ = {};
7
- this.classNamesCount_ = 0;
8
- this.fakeDomHelper_ = {
9
- removeElement: function() {},
10
- createElement: function(name, attrs, innerHtml) {
11
- var element = document.createElement(name);
12
-
13
- for (var attr in attrs) {
14
- element.setAttribute(attr, attrs[attr]);
15
- }
16
- element.innerHTML = innerHtml;
17
- return element;
6
+ this.fontLoadingEventCalled_ = 0;
7
+ this.fontLoading_ = {};
8
+ this.fontActiveEventCalled_ = 0;
9
+ this.fontActive_ = {};
10
+ this.fontInactiveEventCalled_ = 0;
11
+ this.fontInactive_ = {};
12
+ this.activeEventCalled_ = 0;
13
+ this.inactiveEventCalled_ = 0;
14
+ this.fakeEventDispatcher_ = {
15
+ dispatchLoading: function() {
16
+ fail('dispatchLoading should not be called by FontWatcher.');
17
+ },
18
+ dispatchFontLoading: function(fontFamily, fontDescription) {
19
+ self.fontLoadingEventCalled_++;
20
+ self.fontLoading_[fontFamily + ' ' + fontDescription] = true;
21
+ },
22
+ dispatchFontActive: function(fontFamily, fontDescription) {
23
+ self.fontActiveEventCalled_++;
24
+ self.fontActive_[fontFamily + ' ' + fontDescription] = true;
18
25
  },
19
- insertInto: function() {},
20
- appendClassName: function(e, name) {
21
- self.classNames_[name] = true;
22
- self.classNamesCount_++;
26
+ dispatchFontInactive: function(fontFamily, fontDescription) {
27
+ self.fontInactiveEventCalled_++;
28
+ self.fontInactive_[fontFamily + ' ' + fontDescription] = true;
23
29
  },
24
- removeClassName: function(e, name) {
25
- var exists = self.classNames_[name];
26
- if (exists) {
27
- self.classNames_[name] = false;
28
- self.classNamesCount_--;
29
- }
30
+ dispatchActive: function() {
31
+ self.activeEventCalled_++;
32
+ },
33
+ dispatchInactive: function() {
34
+ self.inactiveEventCalled_++;
30
35
  }
31
36
  };
32
- this.fakeHtmlElement_ = { className: '' };
33
- this.loadingEventCalled_ = false;
34
- this.fontLoadingEventCalled_ = 0;
35
- this.fontLoading_ = [];
36
- this.fontActiveEventCalled_ = 0;
37
- this.fontActive_ = [];
38
- this.fontInactvieEventCalled_ = 0;
39
- this.fontInactive_ = [];
40
- this.activeEventCalled_ = 0;
41
37
 
42
- var namespace = 'ns';
43
-
44
- this.callbacks_ = {
45
- loading: function() {
46
- self.loadingEventCalled_ = true;
47
- },
48
- active: function() {
49
- self.activeEventCalled_++;
50
- },
51
- fontloading: function(fontFamily, fontDescription) {
52
- self.fontLoadingEventCalled_++;
53
- self.fontLoading_.push(fontFamily + ' ' + fontDescription);
54
- },
55
- fontactive: function(fontFamily, fontDescription) {
56
- self.fontActiveEventCalled_++;
57
- self.fontActive_.push(fontFamily + ' ' + fontDescription);
58
- },
59
- fontinactive: function(fontFamily, fontDescription) {
60
- self.fontInactvieEventCalled_++;
61
- self.fontInactive_.push(fontFamily + ' ' + fontDescription);
62
- }
38
+ this.fakeFontSizer_ = {
39
+ getWidth: function() {
40
+ fail('Fake getWidth should not be called.');
41
+ }
63
42
  };
64
- this.eventDispatcher_ = new webfont.EventDispatcher(this.fakeDomHelper_,
65
- this.fakeHtmlElement_, this.callbacks_, namespace);
66
- };
67
43
 
68
- FontWatcherTest.prototype.testWatchOneFontAlreadyLoaded = function() {
69
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
70
- this.eventDispatcher_, {
71
- getWidth: function(element) {
72
- var fontFamily = element.style.fontFamily;
73
- var fonts = fontFamily.split(',');
74
- var size = fonts.length;
75
-
76
- if (size == 6) {
77
- return 1;
78
- } else {
79
- return 2;
80
- }
81
- }
82
- }, function() {}, function() { return 0; });
83
- var fontFamilies = [ 'fontFamily1' ];
44
+ this.fakeAsyncCall_ = function() {
45
+ fail('Fake asyncCall should not be called.');
46
+ };
84
47
 
85
- fontWatcher.watch(fontFamilies, {}, {}, false);
86
- assertEquals(1, this.fontLoadingEventCalled_);
87
- assertEquals(1, this.fontLoading_.length);
88
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
89
- assertEquals(1, this.fontActiveEventCalled_);
90
- assertEquals(1, this.fontActive_.length);
91
- assertEquals('fontFamily1 n4', this.fontActive_[0]);
92
- };
48
+ this.fakeGetTime_ = function() {
49
+ fail('Fake getTime should not be called.');
50
+ };
93
51
 
94
- FontWatcherTest.prototype.testWatchMultipleFontsAlreadyLoaded = function() {
95
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.eventDispatcher_,{
96
- getWidth: function(element) {
97
- var fontFamily = element.style.fontFamily;
98
- var fonts = fontFamily.split(',');
99
- var size = fonts.length;
100
-
101
- if (size == 6) {
102
- return 1;
103
- } else {
104
- return 2;
105
- }
106
- }
107
- }, function() {}, function() { return 0; });
108
- var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
52
+ // Mock out FontWatchRunner to return active/inactive for families we give it
53
+ this.originalFontWatchRunner_ = webfont.FontWatchRunner;
54
+ this.fontWatchRunnerActiveFamilies_ = [];
55
+ this.testStringCount_ = 0;
56
+ this.testStrings_ = {};
57
+ webfont.FontWatchRunner = function(activeCallback, inactiveCallback, domHelper,
58
+ fontSizer, asyncCall, getTime, fontFamily, fontDescription, opt_fontTestString) {
59
+ if (opt_fontTestString) {
60
+ self.testStringCount_++;
61
+ self.testStrings_[fontFamily] = opt_fontTestString;
62
+ }
63
+
64
+ if (self.fontWatchRunnerActiveFamilies_.indexOf(fontFamily) > -1) {
65
+ activeCallback(fontFamily, fontDescription);
66
+ } else {
67
+ inactiveCallback(fontFamily, fontDescription);
68
+ }
69
+
70
+ };
109
71
 
110
- fontWatcher.watch(fontFamilies, {}, {}, false);
111
- assertEquals(3, this.fontLoadingEventCalled_);
112
- assertEquals(3, this.fontLoading_.length);
113
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
114
- assertEquals('fontFamily2 n4', this.fontLoading_[1]);
115
- assertEquals('fontFamily3 n4', this.fontLoading_[2]);
116
- assertEquals(3, this.fontActiveEventCalled_);
117
- assertEquals(3, this.fontActive_.length);
118
- assertEquals('fontFamily1 n4', this.fontActive_[0]);
119
- assertEquals('fontFamily2 n4', this.fontActive_[1]);
120
- assertEquals('fontFamily3 n4', this.fontActive_[2]);
121
72
  };
122
73
 
123
- FontWatcherTest.prototype.testWatchOneFontWaitForLoad = function() {
124
- var async = false;
125
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
126
- this.eventDispatcher_, {
127
-
128
- count_: 0,
129
-
130
- getWidth: function(element) {
131
- var fontFamily = element.style.fontFamily;
132
- var fonts = fontFamily.split(',');
133
- var size = fonts.length;
134
-
135
- if (size == 6) {
136
- return 1;
137
- } else if (this.count_ == 0) {
138
- this.count_++;
139
- return 1;
140
- } else if (this.count_ == 1) {
141
- return 2;
142
- }
143
- }
144
- }, function(func, timeout) {
145
- async = true;
146
- func();
147
- }, function() { return 0; });
74
+ FontWatcherTest.prototype.tearDown = function() {
75
+ // Replace the original FontWatchRunner implementation
76
+ webfont.FontWatchRunner = this.originalFontWatchRunner_;
77
+ };
78
+
79
+ FontWatcherTest.prototype.testWatchOneFontNotLast = function() {
148
80
  var fontFamilies = [ 'fontFamily1' ];
81
+ this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1' ];
82
+
83
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.fakeEventDispatcher_,
84
+ this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
149
85
 
150
86
  fontWatcher.watch(fontFamilies, {}, {}, false);
151
- assertTrue(async);
152
- assertEquals(1, this.fontLoadingEventCalled_);
153
- assertEquals(1, this.fontLoading_.length);
154
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
155
- assertEquals(1, this.fontActiveEventCalled_);
156
- assertEquals(1, this.fontActive_.length);
157
- assertEquals('fontFamily1 n4', this.fontActive_[0]);
87
+
88
+ assertEquals(0, this.fontInactiveEventCalled_);
89
+ assertEquals(0, this.activeEventCalled_);
90
+ assertEquals(0, this.inactiveEventCalled_);
158
91
  };
159
92
 
160
- FontWatcherTest.prototype.testWatchMultipleFontsWaitForLoad = function() {
161
- var async = 0;
162
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
163
- this.eventDispatcher_, {
164
-
165
- font1Count_: 0,
166
- font2Count_: 0,
167
- font3Count_: 0,
168
-
169
- getWidth: function(element) {
170
- var fontFamily = element.style.fontFamily;
171
- var fonts = fontFamily.split(',');
172
- var size = fonts.length;
173
-
174
- if (size == 6) {
175
- return 1;
176
- } else if (fonts[0].indexOf("fontFamily1") != -1 &&
177
- this.font1Count_ != 2) {
178
- this.font1Count_++;
179
- return 1;
180
- } else if (fonts[0].indexOf("fontFamily2") != -1 &&
181
- this.font2Count_ != 1) {
182
- this.font2Count_++;
183
- return 1;
184
- } else if (fonts[0].indexOf("fontFamily3") != -1 &&
185
- this.font3Count_ != 5) {
186
- this.font3Count_++;
187
- return 1;
188
- } else {
189
- return 2;
190
- }
191
- }
192
- }, function(func, timeout) {
193
- async++;
194
- func();
195
- }, function() { return 0; });
196
- var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
93
+ FontWatcherTest.prototype.testWatchOneFontActive = function() {
94
+ var fontFamilies = [ 'fontFamily1' ];
95
+ this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1' ];
197
96
 
198
- fontWatcher.watch(fontFamilies, {}, {}, false);
199
- assertEquals(8, async);
200
- assertEquals(3, this.fontLoadingEventCalled_);
201
- assertEquals(3, this.fontLoading_.length);
202
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
203
- assertEquals('fontFamily2 n4', this.fontLoading_[1]);
204
- assertEquals('fontFamily3 n4', this.fontLoading_[2]);
205
- assertEquals(3, this.fontActiveEventCalled_);
206
- assertEquals(3, this.fontActive_.length);
207
- assertEquals('fontFamily1 n4', this.fontActive_[0]);
208
- assertEquals('fontFamily2 n4', this.fontActive_[1]);
209
- assertEquals('fontFamily3 n4', this.fontActive_[2]);
210
- };
97
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.fakeEventDispatcher_,
98
+ this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
211
99
 
212
- FontWatcherTest.prototype.testWatchMultipleFontsWaitForLoadAndLoaded =
213
- function() {
214
- var async = 0;
215
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
216
- this.eventDispatcher_, {
217
-
218
- font1Count_: 0,
219
- font3Count_: 0,
220
-
221
- getWidth: function(element) {
222
- var fontFamily = element.style.fontFamily;
223
- var fonts = fontFamily.split(',');
224
- var size = fonts.length;
225
-
226
- if (size == 6) {
227
- return 1;
228
- } else if (fonts[0].indexOf("fontFamily1") != -1 &&
229
- this.font1Count_ != 2) {
230
- this.font1Count_++;
231
- return 1;
232
- } else if (fonts[0].indexOf("fontFamily3") != -1 &&
233
- this.font3Count_ != 5) {
234
- this.font3Count_++;
235
- return 1;
236
- } else {
237
- return 2;
238
- }
239
- }
240
- }, function(func, timeout) {
241
- async++;
242
- func();
243
- }, function() { return 0; });
244
- var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
100
+ fontWatcher.watch(fontFamilies, {}, {}, true);
245
101
 
246
- fontWatcher.watch(fontFamilies, {}, {}, false);
247
- assertEquals(7, async);
248
- assertEquals(3, this.fontLoadingEventCalled_);
249
- assertEquals(3, this.fontLoading_.length);
250
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
251
- assertEquals('fontFamily2 n4', this.fontLoading_[1]);
252
- assertEquals('fontFamily3 n4', this.fontLoading_[2]);
253
- assertEquals(3, this.fontActiveEventCalled_);
254
- assertEquals(3, this.fontActive_.length);
255
- assertEquals('fontFamily1 n4', this.fontActive_[0]);
256
- assertEquals('fontFamily2 n4', this.fontActive_[1]);
257
- assertEquals('fontFamily3 n4', this.fontActive_[2]);
102
+ assertEquals(1, this.fontLoadingEventCalled_);
103
+ assertEquals(true, this.fontLoading_['fontFamily1 n4']);
104
+ assertEquals(1, this.fontActiveEventCalled_);
105
+ assertEquals(true, this.fontActive_['fontFamily1 n4']);
106
+ assertEquals(0, this.fontInactiveEventCalled_);
107
+ assertEquals(1, this.activeEventCalled_);
108
+ assertEquals(0, this.inactiveEventCalled_);
258
109
  };
259
110
 
260
- FontWatcherTest.prototype.testWatchOneFontWaitForLoadInactive = function() {
261
- var time = 0;
262
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
263
- this.eventDispatcher_, {
264
-
265
- count_: 0,
266
-
267
- getWidth: function(element) {
268
- return 1;
269
- }
270
- }, function(func, timeout) {
271
- func();
272
- }, function() {
273
- time += 2500;
274
- return time;
275
- });
111
+ FontWatcherTest.prototype.testWatchOneFontInactive = function() {
276
112
  var fontFamilies = [ 'fontFamily1' ];
113
+ this.fontWatchRunnerActiveFamilies_ = [];
114
+
115
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.fakeEventDispatcher_,
116
+ this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
117
+
118
+ fontWatcher.watch(fontFamilies, {}, {}, true);
277
119
 
278
- fontWatcher.watch(fontFamilies, {}, {}, false);
279
120
  assertEquals(1, this.fontLoadingEventCalled_);
280
- assertEquals(1, this.fontLoading_.length);
281
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
282
- assertEquals(1, this.fontInactvieEventCalled_);
283
- assertEquals(1, this.fontInactive_.length);
284
- assertEquals('fontFamily1 n4', this.fontInactive_[0]);
121
+ assertEquals(true, this.fontLoading_['fontFamily1 n4']);
122
+ assertEquals(0, this.fontActiveEventCalled_);
123
+ assertEquals(1, this.fontInactiveEventCalled_);
124
+ assertEquals(true, this.fontInactive_['fontFamily1 n4']);
125
+ assertEquals(0, this.activeEventCalled_);
126
+ assertEquals(1, this.inactiveEventCalled_);
285
127
  };
286
128
 
287
- FontWatcherTest.prototype.testWatchMultipleFontsWaitForLoadAndInactive =
288
- function() {
289
- var count = 0;
290
- var async = 0;
291
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
292
- this.eventDispatcher_, {
293
-
294
- font1Count_: 0,
295
- font3Count_: 0,
296
-
297
- getWidth: function(element) {
298
- var fontFamily = element.style.fontFamily;
299
- var fonts = fontFamily.split(',');
300
- var size = fonts.length;
301
-
302
- if (size == 6) {
303
- return 1;
304
- } else if (fonts[0].indexOf("fontFamily1") != -1 &&
305
- this.font1Count_ != 2) {
306
- this.font1Count_++;
307
- return 1;
308
- } else if (fonts[0].indexOf("fontFamily2") != -1) {
309
- return 1;
310
- } else if (fonts[0].indexOf("fontFamily3") != -1 &&
311
- this.font3Count_ != 5) {
312
- this.font3Count_++;
313
- return 1;
314
- } else {
315
- return 2;
316
- }
317
- }
318
- }, function(func, timeout) {
319
- async++;
320
- func();
321
- }, function() {
322
- if (++count == 5) {
323
- return 5001;
324
- }
325
- return 0;
326
- });
129
+ FontWatcherTest.prototype.testWatchMultipleFontsActive = function() {
327
130
  var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
131
+ this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
132
+
133
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.fakeEventDispatcher_,
134
+ this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
135
+
136
+ fontWatcher.watch(fontFamilies, {}, {}, true);
328
137
 
329
- fontWatcher.watch(fontFamilies, {}, {}, false);
330
- assertEquals(9, async);
331
138
  assertEquals(3, this.fontLoadingEventCalled_);
332
- assertEquals(3, this.fontLoading_.length);
333
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
334
- assertEquals('fontFamily2 n4', this.fontLoading_[1]);
335
- assertEquals('fontFamily3 n4', this.fontLoading_[2]);
336
- assertEquals(2, this.fontActiveEventCalled_);
337
- assertEquals(2, this.fontActive_.length);
338
- assertEquals('fontFamily1 n4', this.fontActive_[0]);
339
- assertEquals('fontFamily3 n4', this.fontActive_[1]);
340
- assertEquals(1, this.fontInactvieEventCalled_);
341
- assertEquals('fontFamily2 n4', this.fontInactive_[0]);
139
+ assertEquals(true, this.fontLoading_['fontFamily1 n4']);
140
+ assertEquals(true, this.fontLoading_['fontFamily2 n4']);
141
+ assertEquals(true, this.fontLoading_['fontFamily3 n4']);
142
+ assertEquals(3, this.fontActiveEventCalled_);
143
+ assertEquals(true, this.fontActive_['fontFamily1 n4']);
144
+ assertEquals(true, this.fontActive_['fontFamily2 n4']);
145
+ assertEquals(true, this.fontActive_['fontFamily3 n4']);
146
+ assertEquals(0, this.fontInactiveEventCalled_);
147
+ assertEquals(1, this.activeEventCalled_);
148
+ assertEquals(0, this.inactiveEventCalled_);
342
149
  };
343
150
 
344
- FontWatcherTest.prototype.testWatchMultipleFontsAlreadyLoadedAndLastBatchOnDone
345
- = function() {
346
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.eventDispatcher_,{
347
- getWidth: function(element) {
348
- var fontFamily = element.style.fontFamily;
349
- var fonts = fontFamily.split(',');
350
- var size = fonts.length;
351
-
352
- if (size == 6) {
353
- return 1;
354
- } else {
355
- return 2;
356
- }
357
- }
358
- }, function() {}, function() { return 0; });
151
+ FontWatcherTest.prototype.testWatchMultipleFontsInactive = function() {
359
152
  var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
153
+ this.fontWatchRunnerActiveFamilies_ = [];
154
+
155
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.fakeEventDispatcher_,
156
+ this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
360
157
 
361
158
  fontWatcher.watch(fontFamilies, {}, {}, true);
159
+
362
160
  assertEquals(3, this.fontLoadingEventCalled_);
363
- assertEquals(3, this.fontLoading_.length);
364
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
365
- assertEquals('fontFamily2 n4', this.fontLoading_[1]);
366
- assertEquals('fontFamily3 n4', this.fontLoading_[2]);
367
- assertEquals(3, this.fontActiveEventCalled_);
368
- assertEquals(3, this.fontActive_.length);
369
- assertEquals('fontFamily1 n4', this.fontActive_[0]);
370
- assertEquals('fontFamily2 n4', this.fontActive_[1]);
371
- assertEquals('fontFamily3 n4', this.fontActive_[2]);
372
- assertEquals(1, this.activeEventCalled_);
373
- assertEquals(4, this.classNamesCount_);
374
- assertEquals(true, this.classNames_['ns-fontfamily1-n4-active']);
375
- assertEquals(true, this.classNames_['ns-fontfamily2-n4-active']);
376
- assertEquals(true, this.classNames_['ns-fontfamily3-n4-active']);
377
- assertEquals(true, this.classNames_['ns-active']);
161
+ assertEquals(true, this.fontLoading_['fontFamily1 n4']);
162
+ assertEquals(true, this.fontLoading_['fontFamily2 n4']);
163
+ assertEquals(true, this.fontLoading_['fontFamily3 n4']);
164
+ assertEquals(0, this.fontActiveEventCalled_);
165
+ assertEquals(3, this.fontInactiveEventCalled_);
166
+ assertEquals(true, this.fontInactive_['fontFamily1 n4']);
167
+ assertEquals(true, this.fontInactive_['fontFamily2 n4']);
168
+ assertEquals(true, this.fontInactive_['fontFamily3 n4']);
169
+ assertEquals(0, this.activeEventCalled_);
170
+ assertEquals(1, this.inactiveEventCalled_);
378
171
  };
379
172
 
380
- FontWatcherTest.prototype.testWatchMultipleFontsWaitForLoadAndLastBatchOnDone =
381
- function() {
382
- var async = 0;
383
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
384
- this.eventDispatcher_, {
385
-
386
- font1Count_: 0,
387
- font2Count_: 0,
388
- font3Count_: 0,
389
-
390
- getWidth: function(element) {
391
- var fontFamily = element.style.fontFamily;
392
- var fonts = fontFamily.split(',');
393
- var size = fonts.length;
394
-
395
- if (size == 6) {
396
- return 1;
397
- } else if (fonts[0].indexOf("fontFamily1") != -1 &&
398
- this.font1Count_ != 2) {
399
- this.font1Count_++;
400
- return 1;
401
- } else if (fonts[0].indexOf("fontFamily2") != -1 &&
402
- this.font2Count_ != 1) {
403
- this.font2Count_++;
404
- return 1;
405
- } else if (fonts[0].indexOf("fontFamily3") != -1 &&
406
- this.font3Count_ != 5) {
407
- this.font3Count_++;
408
- return 1;
409
- } else {
410
- return 2;
411
- }
412
- }
413
- }, function(func, timeout) {
414
- async++;
415
- func();
416
- }, function() { return 0; });
173
+ FontWatcherTest.prototype.testWatchMultipleFontsMixed = function() {
417
174
  var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
175
+ this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1', 'fontFamily3' ];
176
+
177
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.fakeEventDispatcher_,
178
+ this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
418
179
 
419
180
  fontWatcher.watch(fontFamilies, {}, {}, true);
420
- assertEquals(8, async);
181
+
421
182
  assertEquals(3, this.fontLoadingEventCalled_);
422
- assertEquals(3, this.fontLoading_.length);
423
- assertEquals('fontFamily1 n4', this.fontLoading_[0]);
424
- assertEquals('fontFamily2 n4', this.fontLoading_[1]);
425
- assertEquals('fontFamily3 n4', this.fontLoading_[2]);
426
- assertEquals(3, this.fontActiveEventCalled_);
427
- assertEquals(3, this.fontActive_.length);
428
- assertEquals('fontFamily1 n4', this.fontActive_[0]);
429
- assertEquals('fontFamily2 n4', this.fontActive_[1]);
430
- assertEquals('fontFamily3 n4', this.fontActive_[2]);
183
+ assertEquals(true, this.fontLoading_['fontFamily1 n4']);
184
+ assertEquals(true, this.fontLoading_['fontFamily2 n4']);
185
+ assertEquals(true, this.fontLoading_['fontFamily3 n4']);
186
+ assertEquals(2, this.fontActiveEventCalled_);
187
+ assertEquals(true, this.fontActive_['fontFamily1 n4']);
188
+ assertEquals(true, this.fontActive_['fontFamily3 n4']);
189
+ assertEquals(1, this.fontInactiveEventCalled_);
190
+ assertEquals(true, this.fontInactive_['fontFamily2 n4']);
431
191
  assertEquals(1, this.activeEventCalled_);
432
- assertEquals(true, this.classNames_['ns-fontfamily1-n4-active']);
433
- assertEquals(true, this.classNames_['ns-fontfamily2-n4-active']);
434
- assertEquals(true, this.classNames_['ns-fontfamily3-n4-active']);
435
- assertEquals(true, this.classNames_['ns-active']);
436
- assertEquals(4, this.classNamesCount_);
192
+ assertEquals(0, this.inactiveEventCalled_);
437
193
  };
438
194
 
439
- FontWatcherTest.prototype
440
- .testWatchMultipleFontsWaitForLoadAndLastBatchOnDoneWithVariations =
441
- function() {
442
- var async = 0;
443
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
444
- this.eventDispatcher_, {
445
-
446
- font1Count_: 0,
447
- font2Count_: 0,
448
- font3Count_: 0,
449
-
450
- getWidth: function(element) {
451
- var fontFamily = element.style.fontFamily;
452
- var fonts = fontFamily.split(',');
453
- var size = fonts.length;
454
-
455
- if (size == 6) {
456
- return 1;
457
- } else if (fonts[0].indexOf("fontFamily1") != -1 &&
458
- this.font1Count_ != 2) {
459
- this.font1Count_++;
460
- return 1;
461
- } else if (fonts[0].indexOf("fontFamily2") != -1 &&
462
- this.font2Count_ != 1) {
463
- this.font2Count_++;
464
- return 1;
465
- } else if (fonts[0].indexOf("fontFamily3") != -1 &&
466
- this.font3Count_ != 5) {
467
- this.font3Count_++;
468
- return 1;
469
- } else {
470
- return 2;
471
- }
472
- }
473
- }, function(func, timeout) {
474
- async++;
475
- func();
476
- }, function() { return 0; });
195
+ FontWatcherTest.prototype.testWatchMultipleFontsWithDescriptions = function() {
477
196
  var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
197
+ this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1', 'fontFamily2' ];
478
198
 
479
- fontWatcher.watch(fontFamilies, {
480
- 'fontFamily1': ['i7'],
481
- 'fontFamily2': null,
482
- 'fontFamily3': ['n4', 'i4', 'n7'] }, {}, true);
199
+ var fontDescriptions = {
200
+ 'fontFamily1': ['i7'],
201
+ 'fontFamily2': null,
202
+ 'fontFamily3': ['n4', 'i4', 'n7']
203
+ };
204
+
205
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.fakeEventDispatcher_,
206
+ this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
207
+
208
+ fontWatcher.watch(fontFamilies, fontDescriptions, {}, true);
483
209
 
484
- assertEquals(8, async);
485
210
  assertEquals(5, this.fontLoadingEventCalled_);
486
- assertEquals(5, this.fontLoading_.length);
487
- assertEquals('fontFamily1 i7',
488
- this.fontLoading_[0]);
489
- assertEquals('fontFamily2 n4', this.fontLoading_[1]);
490
- assertEquals('fontFamily3 n4',
491
- this.fontLoading_[2]);
492
- assertEquals('fontFamily3 i4', this.fontLoading_[3]);
493
- assertEquals('fontFamily3 n7', this.fontLoading_[4]);
494
- assertEquals(5, this.fontActiveEventCalled_);
495
- assertEquals(5, this.fontActive_.length);
496
- assertEquals('fontFamily1 i7',
497
- this.fontActive_[0]);
498
- assertEquals('fontFamily2 n4', this.fontActive_[1]);
499
- assertEquals('fontFamily3 n4',
500
- this.fontActive_[2]);
501
- assertEquals('fontFamily3 i4', this.fontActive_[3]);
502
- assertEquals('fontFamily3 n7', this.fontActive_[4]);
211
+ assertEquals(true, this.fontLoading_['fontFamily1 i7']);
212
+ assertEquals(true, this.fontLoading_['fontFamily2 n4']);
213
+ assertEquals(true, this.fontLoading_['fontFamily3 n4']);
214
+ assertEquals(true, this.fontLoading_['fontFamily3 i4']);
215
+ assertEquals(true, this.fontLoading_['fontFamily3 n7']);
216
+ assertEquals(2, this.fontActiveEventCalled_);
217
+ assertEquals(true, this.fontActive_['fontFamily1 i7']);
218
+ assertEquals(true, this.fontActive_['fontFamily2 n4']);
219
+ assertEquals(3, this.fontInactiveEventCalled_);
220
+ assertEquals(true, this.fontInactive_['fontFamily3 n4']);
221
+ assertEquals(true, this.fontInactive_['fontFamily3 i4']);
222
+ assertEquals(true, this.fontInactive_['fontFamily3 n7']);
503
223
  assertEquals(1, this.activeEventCalled_);
504
- assertEquals(6, this.classNamesCount_);
505
- assertEquals(true, this.classNames_['ns-fontfamily1-i7-active']);
506
- assertEquals(true, this.classNames_['ns-fontfamily2-n4-active']);
507
- assertEquals(true, this.classNames_['ns-fontfamily3-n4-active']);
508
- assertEquals(true, this.classNames_['ns-fontfamily3-i4-active']);
509
- assertEquals(true, this.classNames_['ns-fontfamily3-n7-active']);
510
- assertEquals(true, this.classNames_['ns-active']);
224
+ assertEquals(0, this.inactiveEventCalled_);
511
225
  };
512
226
 
513
- FontWatcherTest.prototype.testTestStringIsNotDefault = function() {
514
- var testString = '';
515
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
516
- this.eventDispatcher_, {
517
- getWidth: function(element) {
518
- var fontFamily = element.style.fontFamily;
519
- var fonts = fontFamily.split(',');
520
- var size = fonts.length;
521
-
522
- testString = element.innerHTML;
523
- if (size == 6) {
524
- return 1;
525
- } else {
526
- return 2;
527
- }
528
- }
529
- }, function() {}, function() { return 0; });
530
- var fontFamilies = [ 'fontFamily1' ];
227
+ FontWatcherTest.prototype.testWatchMultipleFontsWithTestStrings = function() {
228
+ var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3', 'fontFamily4' ];
229
+ this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1', 'fontFamily2' ];
531
230
 
532
- fontWatcher.watch(fontFamilies, {}, { 'fontFamily1': 'testString!' }, false);
533
- assertEquals('testString!', testString);
534
- };
231
+ var fontTestStrings = {
232
+ 'fontFamily1': 'testString1',
233
+ 'fontFamily2': null,
234
+ 'fontFamily3': 'testString3',
235
+ 'fontFamily4': null
236
+ };
535
237
 
536
- FontWatcherTest.prototype.testTestStringIsDefault = function() {
537
- var testString = '';
538
- var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
539
- this.eventDispatcher_, {
540
- getWidth: function(element) {
541
- var fontFamily = element.style.fontFamily;
542
- var fonts = fontFamily.split(',');
543
- var size = fonts.length;
544
-
545
- testString = element.innerHTML;
546
- if (size == 6) {
547
- return 1;
548
- } else {
549
- return 2;
550
- }
551
- }
552
- }, function() {}, function() { return 0; });
553
- var fontFamilies = [ 'fontFamily1' ];
238
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.fakeEventDispatcher_,
239
+ this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
554
240
 
555
- fontWatcher.watch(fontFamilies, {}, {}, false);
556
- assertEquals('Mm', testString);
241
+ fontWatcher.watch(fontFamilies, {}, fontTestStrings, true);
242
+
243
+ assertEquals(2, this.testStringCount_);
244
+ assertEquals('testString1', this.testStrings_['fontFamily1']);
245
+ assertEquals('testString3', this.testStrings_['fontFamily3']);
557
246
  };