webfontloader 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/CHANGELOG +9 -0
  2. data/Gemfile +2 -8
  3. data/README.md +31 -32
  4. data/Rakefile +2 -33
  5. data/docs/EVENTS.md +10 -1
  6. data/docs/MODULES.md +4 -3
  7. data/lib/webfontloader.rb +1 -1
  8. data/spec/ascender/ascenderscript_spec.js +43 -0
  9. data/spec/core/cssclassname_spec.js +42 -0
  10. data/spec/core/cssfontfamilyname_spec.js +38 -0
  11. data/spec/core/domhelper_spec.js +158 -0
  12. data/spec/core/eventdispatcher_spec.js +209 -0
  13. data/spec/core/font_spec.js +218 -0
  14. data/spec/core/fontmoduleloader_spec.js +55 -0
  15. data/spec/core/fontruler_spec.js +33 -0
  16. data/spec/core/fontvariationdescription_spec.js +67 -0
  17. data/spec/core/fontwatcher_spec.js +204 -0
  18. data/spec/core/fontwatchrunner_spec.js +398 -0
  19. data/spec/core/size_spec.js +17 -0
  20. data/spec/core/useragentparser_spec.js +921 -0
  21. data/spec/custom/customcss_spec.js +36 -0
  22. data/spec/fontdeck/fontdeckscript_spec.js +111 -0
  23. data/spec/fonts/LICENSE.txt +93 -0
  24. data/spec/fonts/nullfont.css +1 -0
  25. data/spec/fonts/nullfont1.css +1 -0
  26. data/spec/fonts/nullfont2.css +1 -0
  27. data/spec/fonts/nullfont3.css +1 -0
  28. data/spec/fonts/sourcesans.eot +0 -0
  29. data/spec/fonts/sourcesans.otf +0 -0
  30. data/spec/fonts/sourcesans.svg +2523 -0
  31. data/spec/fonts/sourcesans.ttf +0 -0
  32. data/spec/fonts/sourcesans.woff +0 -0
  33. data/spec/fonts/sourcesansa.css +1 -0
  34. data/spec/fonts/sourcesansb.css +1 -0
  35. data/spec/google/fontapiparser_spec.js +348 -0
  36. data/spec/google/fontapiurlbuilder_spec.js +40 -0
  37. data/spec/google/googlefontapi_spec.js +123 -0
  38. data/spec/google/lastresortwebkitfontwatchrunner_spec.js +145 -0
  39. data/spec/index.html +95 -0
  40. data/spec/monotype/monotypescript_spec.js +49 -0
  41. data/spec/typekit/typekitscript_spec.js +93 -0
  42. data/src/core/domhelper.js +6 -3
  43. data/src/core/fontruler.js +1 -1
  44. data/src/core/fontwatcher.js +5 -0
  45. data/src/core/fontwatchrunner.js +7 -4
  46. data/src/monotype/monotype_script.js +4 -3
  47. data/tools/jasmine-phantomjs/jasmine-phantomjs.js +26 -0
  48. data/tools/jasmine-phantomjs/terminal-reporter.js +177 -0
  49. data/tools/jasmine/MIT.LICENSE +20 -0
  50. data/tools/jasmine/jasmine-html.js +681 -0
  51. data/tools/jasmine/jasmine.css +82 -0
  52. data/tools/jasmine/jasmine.js +2600 -0
  53. data/webfontloader.gemspec +46 -25
  54. metadata +77 -42
  55. data/src-test/ascender/ascender_script_test.js +0 -51
  56. data/src-test/core/cssclassnametest.js +0 -42
  57. data/src-test/core/cssfontfamilynametest.js +0 -54
  58. data/src-test/core/domhelpertest.js +0 -151
  59. data/src-test/core/eventdispatchertest.js +0 -275
  60. data/src-test/core/fontmoduleloadertest.js +0 -30
  61. data/src-test/core/fonttest.js +0 -121
  62. data/src-test/core/fontvariationdescriptiontest.js +0 -76
  63. data/src-test/core/fontwatchertest.js +0 -287
  64. data/src-test/core/fontwatchrunnertest.js +0 -454
  65. data/src-test/core/useragenttest.js +0 -755
  66. data/src-test/custom/customcsstest.js +0 -35
  67. data/src-test/fontdeck/fontdeck_script_test.js +0 -116
  68. data/src-test/google/fontapiparsertest.js +0 -252
  69. data/src-test/google/fontapiurlbuildertest.js +0 -71
  70. data/src-test/google/googlefontapitest.js +0 -185
  71. data/src-test/google/lastresortwebkitfontwatchrunnertest.js +0 -204
  72. data/src-test/monotype/monotype_script_test.js +0 -304
  73. data/src-test/typekit/typekit_script_test.js +0 -195
  74. data/tools/jstestdriver/JsTestDriver-1.2.1.jar +0 -0
@@ -1,287 +0,0 @@
1
- var FontWatcherTest = TestCase('FontWatcherTest');
2
-
3
- FontWatcherTest.prototype.setUp = function() {
4
- var self = this;
5
-
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;
25
- },
26
- dispatchFontInactive: function(fontFamily, fontDescription) {
27
- self.fontInactiveEventCalled_++;
28
- self.fontInactive_[fontFamily + ' ' + fontDescription] = true;
29
- },
30
- dispatchActive: function() {
31
- self.activeEventCalled_++;
32
- },
33
- dispatchInactive: function() {
34
- self.inactiveEventCalled_++;
35
- }
36
- };
37
-
38
- this.fakeDomHelper_ = {
39
- createElement: function(name, attrs, innerHtml) {
40
- var element = document.createElement(name);
41
- return element;
42
- },
43
- insertInto: function() {},
44
- removeElement: function() {},
45
- setStyle: function() {}
46
- };
47
-
48
- this.userAgent_ = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', 'Macintosh', '10.6', undefined, new webfont.BrowserInfo(false, false));
49
-
50
- this.fakeFontSizer_ = {
51
- getSize: function() {
52
- fail('Fake getSize should not be called.');
53
- }
54
- };
55
-
56
- this.fakeAsyncCall_ = function() {
57
- fail('Fake asyncCall should not be called.');
58
- };
59
-
60
- this.fakeGetTime_ = function() {
61
- fail('Fake getTime should not be called.');
62
- };
63
-
64
- // Mock out FontWatchRunner to return active/inactive for families we give it
65
- this.originalFontWatchRunner_ = webfont.FontWatchRunner;
66
- this.fontWatchRunnerActiveFamilies_ = [];
67
- this.testStringCount_ = 0;
68
- this.testStrings_ = {};
69
- webfont.FontWatchRunner = function(activeCallback, inactiveCallback, domHelper,
70
- fontSizer, asyncCall, getTime, fontFamily, fontDescription, hasWebkitFallbackBug, opt_metricCompatibleFonts, opt_fontTestString) {
71
- if (opt_fontTestString) {
72
- self.testStringCount_++;
73
- self.testStrings_[fontFamily] = opt_fontTestString;
74
- }
75
- this.activeCallback_ = activeCallback;
76
- this.inactiveCallback_ = inactiveCallback;
77
- this.fontFamily_ = fontFamily;
78
- this.fontDescription_ = fontDescription;
79
- };
80
-
81
- webfont.FontWatchRunner.prototype.start = function() {
82
- if (self.fontWatchRunnerActiveFamilies_.indexOf(this.fontFamily_) > -1) {
83
- this.activeCallback_(this.fontFamily_, this.fontDescription_);
84
- } else {
85
- this.inactiveCallback_(this.fontFamily_, this.fontDescription_);
86
- }
87
- };
88
- };
89
-
90
- FontWatcherTest.prototype.tearDown = function() {
91
- // Replace the original FontWatchRunner implementation
92
- webfont.FontWatchRunner = this.originalFontWatchRunner_;
93
- };
94
-
95
- FontWatcherTest.prototype.testWatchOneFontNotLast = function() {
96
- var fontFamilies = [ 'fontFamily1' ];
97
- this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1' ];
98
-
99
- var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.fakeDomHelper_, this.fakeEventDispatcher_,
100
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
101
-
102
- fontWatcher.watch(fontFamilies, {}, {}, webfont.FontWatchRunner, false);
103
-
104
- assertEquals(0, this.fontInactiveEventCalled_);
105
- assertEquals(0, this.activeEventCalled_);
106
- assertEquals(0, this.inactiveEventCalled_);
107
- };
108
-
109
- FontWatcherTest.prototype.testWatchOneFontActive = function() {
110
- var fontFamilies = [ 'fontFamily1' ];
111
- this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1' ];
112
-
113
- var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.fakeDomHelper_, this.fakeEventDispatcher_,
114
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
115
-
116
- fontWatcher.watch(fontFamilies, {}, {}, webfont.FontWatchRunner, true);
117
-
118
- assertEquals(1, this.fontLoadingEventCalled_);
119
- assertEquals(true, this.fontLoading_['fontFamily1 n4']);
120
- assertEquals(1, this.fontActiveEventCalled_);
121
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
122
- assertEquals(0, this.fontInactiveEventCalled_);
123
- assertEquals(1, this.activeEventCalled_);
124
- assertEquals(0, this.inactiveEventCalled_);
125
- };
126
-
127
- FontWatcherTest.prototype.testWatchOneFontInactive = function() {
128
- var fontFamilies = [ 'fontFamily1' ];
129
- this.fontWatchRunnerActiveFamilies_ = [];
130
-
131
- var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.fakeDomHelper_, this.fakeEventDispatcher_,
132
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
133
-
134
- fontWatcher.watch(fontFamilies, {}, {}, webfont.FontWatchRunner, true);
135
-
136
- assertEquals(1, this.fontLoadingEventCalled_);
137
- assertEquals(true, this.fontLoading_['fontFamily1 n4']);
138
- assertEquals(0, this.fontActiveEventCalled_);
139
- assertEquals(1, this.fontInactiveEventCalled_);
140
- assertEquals(true, this.fontInactive_['fontFamily1 n4']);
141
- assertEquals(0, this.activeEventCalled_);
142
- assertEquals(1, this.inactiveEventCalled_);
143
- };
144
-
145
- FontWatcherTest.prototype.testWatchMultipleFontsActive = function() {
146
- var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
147
- this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
148
-
149
- var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.fakeDomHelper_, this.fakeEventDispatcher_,
150
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
151
-
152
- fontWatcher.watch(fontFamilies, {}, {}, webfont.FontWatchRunner, true);
153
-
154
- assertEquals(3, this.fontLoadingEventCalled_);
155
- assertEquals(true, this.fontLoading_['fontFamily1 n4']);
156
- assertEquals(true, this.fontLoading_['fontFamily2 n4']);
157
- assertEquals(true, this.fontLoading_['fontFamily3 n4']);
158
- assertEquals(3, this.fontActiveEventCalled_);
159
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
160
- assertEquals(true, this.fontActive_['fontFamily2 n4']);
161
- assertEquals(true, this.fontActive_['fontFamily3 n4']);
162
- assertEquals(0, this.fontInactiveEventCalled_);
163
- assertEquals(1, this.activeEventCalled_);
164
- assertEquals(0, this.inactiveEventCalled_);
165
- };
166
-
167
- FontWatcherTest.prototype.testWatchMultipleFontsInactive = function() {
168
- var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
169
- this.fontWatchRunnerActiveFamilies_ = [];
170
-
171
- var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.fakeDomHelper_, this.fakeEventDispatcher_,
172
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
173
-
174
- fontWatcher.watch(fontFamilies, {}, {}, webfont.FontWatchRunner, true);
175
-
176
- assertEquals(3, this.fontLoadingEventCalled_);
177
- assertEquals(true, this.fontLoading_['fontFamily1 n4']);
178
- assertEquals(true, this.fontLoading_['fontFamily2 n4']);
179
- assertEquals(true, this.fontLoading_['fontFamily3 n4']);
180
- assertEquals(0, this.fontActiveEventCalled_);
181
- assertEquals(3, this.fontInactiveEventCalled_);
182
- assertEquals(true, this.fontInactive_['fontFamily1 n4']);
183
- assertEquals(true, this.fontInactive_['fontFamily2 n4']);
184
- assertEquals(true, this.fontInactive_['fontFamily3 n4']);
185
- assertEquals(0, this.activeEventCalled_);
186
- assertEquals(1, this.inactiveEventCalled_);
187
- };
188
-
189
- FontWatcherTest.prototype.testWatchMultipleFontsMixed = function() {
190
- var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
191
- this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1', 'fontFamily3' ];
192
-
193
- var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.fakeDomHelper_, this.fakeEventDispatcher_,
194
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
195
-
196
- fontWatcher.watch(fontFamilies, {}, {}, webfont.FontWatchRunner, true);
197
-
198
- assertEquals(3, this.fontLoadingEventCalled_);
199
- assertEquals(true, this.fontLoading_['fontFamily1 n4']);
200
- assertEquals(true, this.fontLoading_['fontFamily2 n4']);
201
- assertEquals(true, this.fontLoading_['fontFamily3 n4']);
202
- assertEquals(2, this.fontActiveEventCalled_);
203
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
204
- assertEquals(true, this.fontActive_['fontFamily3 n4']);
205
- assertEquals(1, this.fontInactiveEventCalled_);
206
- assertEquals(true, this.fontInactive_['fontFamily2 n4']);
207
- assertEquals(1, this.activeEventCalled_);
208
- assertEquals(0, this.inactiveEventCalled_);
209
- };
210
-
211
- FontWatcherTest.prototype.testWatchMultipleFontsWithDescriptions = function() {
212
- var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
213
- this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1', 'fontFamily2' ];
214
-
215
- var fontDescriptions = {
216
- 'fontFamily1': ['i7'],
217
- 'fontFamily2': null,
218
- 'fontFamily3': ['n4', 'i4', 'n7']
219
- };
220
-
221
- var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.fakeDomHelper_, this.fakeEventDispatcher_,
222
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
223
-
224
- fontWatcher.watch(fontFamilies, fontDescriptions, {}, webfont.FontWatchRunner, true);
225
-
226
- assertEquals(5, this.fontLoadingEventCalled_);
227
- assertEquals(true, this.fontLoading_['fontFamily1 i7']);
228
- assertEquals(true, this.fontLoading_['fontFamily2 n4']);
229
- assertEquals(true, this.fontLoading_['fontFamily3 n4']);
230
- assertEquals(true, this.fontLoading_['fontFamily3 i4']);
231
- assertEquals(true, this.fontLoading_['fontFamily3 n7']);
232
- assertEquals(2, this.fontActiveEventCalled_);
233
- assertEquals(true, this.fontActive_['fontFamily1 i7']);
234
- assertEquals(true, this.fontActive_['fontFamily2 n4']);
235
- assertEquals(3, this.fontInactiveEventCalled_);
236
- assertEquals(true, this.fontInactive_['fontFamily3 n4']);
237
- assertEquals(true, this.fontInactive_['fontFamily3 i4']);
238
- assertEquals(true, this.fontInactive_['fontFamily3 n7']);
239
- assertEquals(1, this.activeEventCalled_);
240
- assertEquals(0, this.inactiveEventCalled_);
241
- };
242
-
243
- FontWatcherTest.prototype.testWatchMultipleFontsWithTestStrings = function() {
244
- var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3', 'fontFamily4' ];
245
- this.fontWatchRunnerActiveFamilies_ = [ 'fontFamily1', 'fontFamily2' ];
246
-
247
- var fontTestStrings = {
248
- 'fontFamily1': 'testString1',
249
- 'fontFamily2': null,
250
- 'fontFamily3': 'testString3',
251
- 'fontFamily4': null
252
- };
253
-
254
- var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.fakeDomHelper_, this.fakeEventDispatcher_,
255
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
256
-
257
- fontWatcher.watch(fontFamilies, {}, fontTestStrings, webfont.FontWatchRunner,
258
- true);
259
-
260
- assertEquals(2, this.testStringCount_);
261
- assertEquals('testString1', this.testStrings_['fontFamily1']);
262
- assertEquals('testString3', this.testStrings_['fontFamily3']);
263
- };
264
-
265
- FontWatcherTest.prototype.testNoWebkitBugDetectionOnNonWebkit = function() {
266
- var ua = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', 'Macintosh', '10.6', undefined, new webfont.BrowserInfo(true, false));
267
- var fontWatcher = new webfont.FontWatcher(ua, this.fakeDomHelper_, this.fakeEventDispatcher_,
268
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
269
-
270
- assertEquals(false, fontWatcher.hasWebKitFallbackBug_);
271
- };
272
-
273
- FontWatcherTest.prototype.testNoWebkitBugDetectionOnNewWebkit = function() {
274
- var ua = new webfont.UserAgent('Safari', '6.0.2', 'AppleWebKit', '537.6.17', 'Macintosh', '10_7_5', undefined, new webfont.BrowserInfo(true, false));
275
- var fontWatcher = new webfont.FontWatcher(ua, this.fakeDomHelper_, this.fakeEventDispatcher_,
276
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
277
-
278
- assertEquals(false, fontWatcher.hasWebKitFallbackBug_);
279
- };
280
-
281
- FontWatcherTest.prototype.testYesWebkitBugDetectionOnOlderWebkit = function() {
282
- var ua = new webfont.UserAgent('Chrome', '16.0.912.75', 'AppleWebKit', '535.7', 'Android', '4.0.3', undefined, new webfont.BrowserInfo(true, true));
283
- var fontWatcher = new webfont.FontWatcher(ua, this.fakeDomHelper_, this.fakeEventDispatcher_,
284
- this.fakeFontSizer_, this.fakeAsyncCall_, this.fakeGetTime_);
285
-
286
- assertEquals(true, fontWatcher.hasWebKitFallbackBug_);
287
- };
@@ -1,454 +0,0 @@
1
- var FontWatchRunnerTest = TestCase('FontWatchRunnerTest');
2
-
3
- FontWatchRunnerTest.prototype.setUp = function() {
4
- var self = this;
5
-
6
- this.fontFamily_ = 'fontFamily1';
7
- this.fontDescription_ = 'n4';
8
-
9
- this.fontActiveCalled_ = 0;
10
- this.fontActive_ = {};
11
- this.fontInactiveCalled_ = 0;
12
- this.fontInactive_ = {};
13
- this.activeCallback_ = function(fontFamily, fontDescription) {
14
- self.fontActiveCalled_++;
15
- self.fontActive_[fontFamily + ' ' + fontDescription] = true;
16
- };
17
- this.inactiveCallback_ = function(fontFamily, fontDescription) {
18
- self.fontInactiveCalled_++;
19
- self.fontInactive_[fontFamily + ' ' + fontDescription] = true;
20
- };
21
-
22
- this.createElementCalled_ = 0;
23
- this.createdElements_ = [];
24
- this.insertIntoCalled_ = 0;
25
- this.removeElementCalled_ = 0;
26
- this.fakeDomHelper_ = {
27
- createElement: function(name, attrs, innerHtml) {
28
- self.createElementCalled_++;
29
- var element = document.createElement(name);
30
- self.createdElements_.push({
31
- 'name': name,
32
- 'attrs': attrs,
33
- 'innerHtml': innerHtml,
34
- 'element': element
35
- });
36
-
37
- for (var attr in attrs) {
38
- element.setAttribute(attr, attrs[attr]);
39
- }
40
- element.innerHTML = innerHtml;
41
- return element;
42
- },
43
- insertInto: function(name, el) {
44
- self.insertIntoCalled_++;
45
- },
46
- removeElement: function(el) {
47
- self.removeElementCalled_++;
48
- },
49
- setStyle: function(el, style) {
50
- el.setAttribute('style', style);
51
- for (var i = 0; i < self.createdElements_.length; i += 1) {
52
- if (self.createdElements_[i].element === el) {
53
- if (!self.createdElements_[i].attrs) {
54
- self.createdElements_[i].attrs = {};
55
- }
56
- self.createdElements_[i].attrs.style = style;
57
- break;
58
- }
59
- }
60
- }
61
- };
62
-
63
- this.timesToCheckSizesBeforeChange_ = 0;
64
- this.fakeFontSizer_ = {
65
- getSize: function(el) {
66
- if (el.style.fontFamily.indexOf(self.fontFamily_) != -1) {
67
- // This is a font stack with fontFamily included (not just fallbacks)
68
- if (self.timesToCheckSizesBeforeChange_ <= 0) {
69
- // Decrement by 0.5 because we're checking two separate font stacks each iteration
70
- self.timesToCheckSizesBeforeChange_ -= 0.5;
71
- return new webfont.Size(2, 2);
72
- } else {
73
- // Decrement by 0.5 because we're checking two separate font stacks each iteration
74
- self.timesToCheckSizesBeforeChange_ -= 0.5;
75
- return new webfont.Size(1, 1);
76
- }
77
- } else {
78
- return new webfont.Size(1, 1);
79
- }
80
- }
81
- };
82
-
83
- this.fakeFontSizerWithDifferentHeight_ = {
84
- getSize: function(el) {
85
- if (el.style.fontFamily.indexOf(self.fontFamily_) != -1) {
86
- if (self.timesToCheckSizesBeforeChange_ <= 0) {
87
- self.timesToCheckSizesBeforeChange_ -= 0.5;
88
- return new webfont.Size(1, 2);
89
- } else {
90
- self.timesToCheckSizesBeforeChange_ -= 0.5;
91
- return new webfont.Size(1, 1);
92
- }
93
- } else {
94
- return new webfont.Size(1, 1);
95
- }
96
- }
97
- };
98
-
99
- /**
100
- * This accurately models the way webkit used to handle
101
- * fallback fonts while loading web fonts. Even though
102
- * this Webkit bug is now patched, we still have a large
103
- * portion of our users using old webkit builds.
104
- *
105
- * See: https://bugs.webkit.org/show_bug.cgi?id=76684
106
- */
107
- this.timesToDelayChangedSizeWebkit_ = 1;
108
- this.firstCallToRetrieveSizeWebkit_ = true;
109
- this.fakeWebkitFontSizer_ = {
110
- getSize: function(el) {
111
- if (el.style.fontFamily.indexOf(self.fontFamily_) !== -1) {
112
- if (self.timesToDelayChangedSizeWebkit_ > 0) {
113
- self.timesToDelayChangedSizeWebkit_ -= 0.5;
114
- // Return the incorrect width for a certain number of cycles.
115
- // The actual number depends on how fast or how slow the font
116
- // is parsed and applied.
117
- return new webfont.Size(2, 2);
118
- } else {
119
- // Return the correct width
120
- return new webfont.Size(3, 3);
121
- }
122
- } else {
123
- if (self.firstCallToRetrieveSizeWebkit_) {
124
- self.firstCallToRetrieveSizeWebkit_ = false;
125
- return new webfont.Size(2, 2);
126
- } else {
127
- // Return the default width
128
- return new webfont.Size(1, 1);
129
- }
130
- }
131
- }
132
- };
133
-
134
- this.fakeWebkitFontSizerFailedLoad_ = {
135
- getSize: function(el) {
136
- if (el.style.fontFamily.indexOf(self.fontFamily_) !== -1) {
137
- if (self.timesToDelayChangedSizeWebkit_ > 0) {
138
- self.timesToDelayChangedSizeWebkit_ -= 0.5;
139
- return new webfont.Size(2, 2);
140
- } else {
141
- // Return the original width, indicating the font
142
- // failed to load. This should incorrectly trigger `inactive`.
143
- return new webfont.Size(1, 1);
144
- }
145
- } else {
146
- if (self.firstCallToRetrieveSizeWebkit_) {
147
- self.firstCallToRetrieveSizeWebkit_ = false;
148
- return new webfont.Size(2, 2);
149
- } else {
150
- return new webfont.Size(1, 1);
151
- }
152
- }
153
- }
154
- };
155
-
156
- this.fakeWebkitFontSizerWithEqualMetrics_ = {
157
- getSize: function(el) {
158
- if (el.style.fontFamily.indexOf(self.fontFamily_) !== -1) {
159
- if (self.timesToDelayChangedSizeWebkit_ > 0) {
160
- self.timesToDelayChangedSizeWebkit_ -= 0.5;
161
- return new webfont.Size(2, 2);
162
- } else {
163
- // This time the fallback font picked by Webkit has the
164
- // same metrics as the font being loaded. This is a rare
165
- // case but we should be able to handle it.
166
- return new webfont.Size(2, 2);
167
- }
168
- } else {
169
- if (self.firstCallToRetrieveSizeWebkit_) {
170
- self.firstCallToRetrieveSizeWebkit_ = false;
171
- return new webfont.Size(2, 2);
172
- } else {
173
- return new webfont.Size(1, 1);
174
- }
175
- }
176
- }
177
- };
178
-
179
- this.fakeWebkitFontSizeWithDifferentMetrics_ = {
180
- getSize: function(el) {
181
- if (el.style.fontFamily.indexOf(self.fontFamily_) !== -1) {
182
- if (self.timesToDelayChangedSizeWebkit_ > 0) {
183
- self.timesToDelayChangedSizeWebkit_ -= 0.5;
184
- return new webfont.Size(2, 2);
185
- } else {
186
- // Even though the width is the same, the height
187
- // is different, so this should trigger the active event.
188
- return new webfont.Size(2, 3);
189
- }
190
- } else {
191
- if (self.firstCallToRetrieveSizeWebkit_) {
192
- self.firstCallToRetrieveSizeWebkit_ = false;
193
- return new webfont.Size(2, 2);
194
- } else {
195
- return new webfont.Size(1, 1);
196
- }
197
- }
198
- }
199
- };
200
-
201
- this.timesToGetTimeBeforeTimeout_ = 10;
202
- this.fakeGetTime_ = function() {
203
- if (self.timesToGetTimeBeforeTimeout_ <= 0) {
204
- return 6000;
205
- } else {
206
- self.timesToGetTimeBeforeTimeout_--;
207
- return 1;
208
- }
209
- };
210
-
211
- this.asyncCount_ = 0;
212
- this.fakeAsyncCall_ = function(func, timeout) {
213
- self.asyncCount_++;
214
- func();
215
- };
216
-
217
- };
218
-
219
- FontWatchRunnerTest.prototype.testWatchFontAlreadyLoaded = function() {
220
- this.timesToCheckSizesBeforeChange_ = 0;
221
- this.timesToGetTimeBeforeTimeout_ = 10;
222
-
223
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
224
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeFontSizer_,
225
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
226
- this.fontDescription_, false);
227
-
228
- fontWatchRunner.start();
229
-
230
- assertEquals(0, this.asyncCount_);
231
-
232
- assertEquals(1, this.fontActiveCalled_);
233
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
234
- assertEquals(0, this.fontInactiveCalled_);
235
- };
236
-
237
- FontWatchRunnerTest.prototype.testWatchFontWaitForLoadActive = function() {
238
- this.timesToCheckSizesBeforeChange_ = 3;
239
- this.timesToGetTimeBeforeTimeout_ = 10;
240
-
241
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
242
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeFontSizer_,
243
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
244
- this.fontDescription_, false);
245
-
246
- fontWatchRunner.start();
247
-
248
- assertEquals(3, this.asyncCount_);
249
-
250
- assertEquals(1, this.fontActiveCalled_);
251
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
252
- assertEquals(0, this.fontInactiveCalled_);
253
- };
254
-
255
- FontWatchRunnerTest.prototype.testWatchFontWaitForLoadActiveWithDifferentHeight = function() {
256
- this.timesToCheckSizesBeforeChange_ = 3;
257
- this.timesToGetTimeBeforeTimeout_ = 10;
258
-
259
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
260
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeFontSizerWithDifferentHeight_,
261
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
262
- this.fontDescription_, false);
263
-
264
- fontWatchRunner.start();
265
-
266
- assertEquals(3, this.asyncCount_);
267
-
268
- assertEquals(1, this.fontActiveCalled_);
269
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
270
- assertEquals(0, this.fontInactiveCalled_);
271
- };
272
-
273
- FontWatchRunnerTest.prototype.testWatchFontWaitForLoadInactive = function() {
274
- this.timesToCheckSizesBeforeChange_ = 10;
275
- this.timesToGetTimeBeforeTimeout_ = 5;
276
-
277
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
278
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeFontSizer_,
279
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
280
- this.fontDescription_, false);
281
-
282
- fontWatchRunner.start();
283
-
284
- assertEquals(4, this.asyncCount_);
285
-
286
- assertEquals(0, this.fontActiveCalled_);
287
- assertEquals(1, this.fontInactiveCalled_);
288
- assertEquals(true, this.fontInactive_['fontFamily1 n4']);
289
- };
290
-
291
- FontWatchRunnerTest.prototype.testWatchFontWebkitWithFastFont = function() {
292
- this.timesToGetTimeBeforeTimeout_ = 10;
293
- this.timesToDelayChangedSizeWebkit_ = 1;
294
- this.firstCallToRetrieveSizeWebkit_ = true;
295
-
296
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
297
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeWebkitFontSizer_,
298
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
299
- this.fontDescription_, true);
300
-
301
- fontWatchRunner.start();
302
- assertEquals(1, this.asyncCount_);
303
- assertEquals(1, this.fontActiveCalled_);
304
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
305
- };
306
-
307
- FontWatchRunnerTest.prototype.testWatchFontWebkitWithSlowFont = function() {
308
- this.timesToGetTimeBeforeTimeout_ = 10;
309
- this.timesToDelayChangedSizeWebkit_ = 2;
310
- this.firstCallToRetrieveSizeWebkit_ = true;
311
-
312
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
313
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeWebkitFontSizer_,
314
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
315
- this.fontDescription_, true);
316
-
317
- fontWatchRunner.start();
318
- assertEquals(2, this.asyncCount_);
319
- assertEquals(1, this.fontActiveCalled_);
320
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
321
- };
322
-
323
- FontWatchRunnerTest.prototype.testWatchFontWebkitWithEqualMetrics = function() {
324
- this.timesToGetTimeBeforeTimeout_ = 10;
325
- this.timesToDelayChangedSizeWebkit_ = 2;
326
- this.firstCallToRetrieveSizeWebkit_ = true;
327
-
328
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
329
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeWebkitFontSizerWithEqualMetrics_,
330
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
331
- this.fontDescription_, true);
332
-
333
- fontWatchRunner.start();
334
- assertEquals(9, this.asyncCount_);
335
- assertEquals(1, this.fontActiveCalled_);
336
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
337
- };
338
-
339
- FontWatchRunnerTest.prototype.testWatchFontWebkitWithDifferentMetrics = function() {
340
- this.timesToGetTimeBeforeTimeout_ = 10;
341
- this.timesToDelayChangedSizeWebkit_ = 2;
342
- this.firstCallToRetrieveSizeWebkit_ = true;
343
-
344
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
345
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeWebkitFontSizeWithDifferentMetrics_,
346
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
347
- this.fontDescription_, true);
348
-
349
- fontWatchRunner.start();
350
- assertEquals(2, this.asyncCount_);
351
- assertEquals(1, this.fontActiveCalled_);
352
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
353
- };
354
-
355
- FontWatchRunnerTest.prototype.testWatchFontWebkitFailedLoad = function() {
356
- this.timesToGetTimeBeforeTimeout_ = 10;
357
- this.timesToDelayChangedSizeWebkit_ = 5;
358
- this.firstCallToRetrieveSizeWebkit_ = true;
359
-
360
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
361
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeWebkitFontSizerFailedLoad_,
362
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
363
- this.fontDescription_, true);
364
-
365
- fontWatchRunner.start();
366
- assertEquals(9, this.asyncCount_);
367
- assertEquals(1, this.fontActiveCalled_);
368
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
369
- };
370
-
371
- FontWatchRunnerTest.prototype.testWatchFontWebkitWithEqualMetricsIncompatibleFont = function() {
372
- this.timesToGetTimeBeforeTimeout_ = 10;
373
- this.timesToDelayChangedSizeWebkit_ = 2;
374
- this.firstCallToRetrieveSizeWebkit_ = true;
375
-
376
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
377
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeWebkitFontSizerWithEqualMetrics_,
378
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
379
- this.fontDescription_, true, { 'fontFamily2': true });
380
-
381
- fontWatchRunner.start();
382
- assertEquals(9, this.asyncCount_);
383
- assertEquals(1, this.fontInactiveCalled_);
384
- assertEquals(true, this.fontInactive_['fontFamily1 n4']);
385
- };
386
-
387
- FontWatchRunnerTest.prototype.testWatchFontWebkitWithEqualMetricsCompatibleFont = function() {
388
- this.timesToGetTimeBeforeTimeout_ = 10;
389
- this.timesToDelayChangedSizeWebkit_ = 2;
390
- this.firstCallToRetrieveSizeWebkit_ = true;
391
-
392
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
393
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeWebkitFontSizerWithEqualMetrics_,
394
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
395
- this.fontDescription_, true, { 'fontFamily1': true });
396
-
397
- fontWatchRunner.start();
398
- assertEquals(9, this.asyncCount_);
399
- assertEquals(1, this.fontActiveCalled_);
400
- assertEquals(true, this.fontActive_['fontFamily1 n4']);
401
- };
402
-
403
-
404
- FontWatchRunnerTest.prototype.testDomWithDefaultTestString = function() {
405
- this.timesToCheckSizesBeforeChange_ = 3;
406
- this.timesToGetTimeBeforeTimeout_ = 10;
407
-
408
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
409
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeFontSizer_,
410
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
411
- this.fontDescription_, false);
412
-
413
- fontWatchRunner.start();
414
- assertEquals(3, this.createElementCalled_);
415
- assertEquals('span', this.createdElements_[0]['name']);
416
- assertNotEquals(-1, this.createdElements_[0]['attrs']['style'].indexOf('fontFamily1'));
417
- assertNotEquals(-1, this.createdElements_[0]['attrs']['style'].indexOf(webfont.FontWatchRunner.LastResortFonts.SERIF));
418
- assertEquals('BESbswy', this.createdElements_[0]['innerHtml']);
419
-
420
- assertEquals('span', this.createdElements_[1]['name']);
421
- assertNotEquals(-1, this.createdElements_[1]['attrs']['style'].indexOf('fontFamily1'));
422
- assertNotEquals(-1, this.createdElements_[1]['attrs']['style'].indexOf(webfont.FontWatchRunner.LastResortFonts.SANS_SERIF));
423
- assertEquals('BESbswy', this.createdElements_[1]['innerHtml']);
424
-
425
- assertEquals(3, this.insertIntoCalled_);
426
- assertEquals(3, this.removeElementCalled_);
427
- };
428
-
429
- FontWatchRunnerTest.prototype.testDomWithNotDefaultTestString = function() {
430
- this.timesToCheckSizesBeforeChange_ = 3;
431
- this.timesToGetTimeBeforeTimeout_ = 10;
432
-
433
- var fontWatchRunner = new webfont.FontWatchRunner(this.activeCallback_,
434
- this.inactiveCallback_, this.fakeDomHelper_, this.fakeFontSizer_,
435
- this.fakeAsyncCall_, this.fakeGetTime_, this.fontFamily_,
436
- this.fontDescription_, false, null, 'testString');
437
-
438
- fontWatchRunner.start();
439
-
440
- assertEquals(3, this.createElementCalled_);
441
- assertEquals('span', this.createdElements_[0]['name']);
442
- assertNotEquals(-1, this.createdElements_[0]['attrs']['style'].indexOf('fontFamily1'));
443
- assertNotEquals(-1, this.createdElements_[0]['attrs']['style'].indexOf(webfont.FontWatchRunner.LastResortFonts.SERIF));
444
- assertEquals('testString', this.createdElements_[0]['innerHtml']);
445
-
446
- assertEquals('span', this.createdElements_[1]['name']);
447
- assertNotEquals(-1, this.createdElements_[1]['attrs']['style'].indexOf('fontFamily1'));
448
- assertNotEquals(-1, this.createdElements_[1]['attrs']['style'].indexOf(webfont.FontWatchRunner.LastResortFonts.SANS_SERIF));
449
- assertEquals('testString', this.createdElements_[1]['innerHtml']);
450
-
451
- assertEquals(3, this.insertIntoCalled_);
452
- assertEquals(3, this.removeElementCalled_);
453
-
454
- };