webfontloader 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/Gemfile +9 -0
  2. data/LICENSE +201 -0
  3. data/README.md +148 -0
  4. data/Rakefile +243 -0
  5. data/bin/webfontloader-demos +28 -0
  6. data/docs/EVENTS.md +115 -0
  7. data/docs/MODULES.md +49 -0
  8. data/docs/TRANSITIONS.md +107 -0
  9. data/lib/webfontloader.rb +10 -0
  10. data/lib/webfontloader/demo/public/ascender.html +99 -0
  11. data/lib/webfontloader/demo/public/basic.css +9 -0
  12. data/lib/webfontloader/demo/public/custom.html +88 -0
  13. data/lib/webfontloader/demo/public/event-css-active-multiple.html +44 -0
  14. data/lib/webfontloader/demo/public/event-css-active.html +38 -0
  15. data/lib/webfontloader/demo/public/event-css-inactive.html +38 -0
  16. data/lib/webfontloader/demo/public/event-css-loading.html +55 -0
  17. data/lib/webfontloader/demo/public/event-js-active.html +39 -0
  18. data/lib/webfontloader/demo/public/event-js-font-active.html +40 -0
  19. data/lib/webfontloader/demo/public/event-js-loading.html +60 -0
  20. data/lib/webfontloader/demo/public/events-variations.html +130 -0
  21. data/lib/webfontloader/demo/public/events.html +103 -0
  22. data/lib/webfontloader/demo/public/google-css.html +27 -0
  23. data/lib/webfontloader/demo/public/google.html +33 -0
  24. data/lib/webfontloader/demo/public/ie-fast-js.html +47 -0
  25. data/lib/webfontloader/demo/public/ie-slow-js.html +48 -0
  26. data/lib/webfontloader/demo/public/ie-slow-link.html +38 -0
  27. data/lib/webfontloader/demo/public/index.html +70 -0
  28. data/lib/webfontloader/demo/public/typekit-variations.html +50 -0
  29. data/lib/webfontloader/demo/public/typekit.html +41 -0
  30. data/lib/webfontloader/demo/server.rb +92 -0
  31. data/lib/webfontloader/modules.rb +44 -0
  32. data/src-test/ascender/ascender_script_test.js +48 -0
  33. data/src-test/core/cssclassnametest.js +42 -0
  34. data/src-test/core/cssfontfamilynametest.js +54 -0
  35. data/src-test/core/domhelpertest.js +81 -0
  36. data/src-test/core/eventdispatchertest.js +99 -0
  37. data/src-test/core/fontmoduleloadertest.js +30 -0
  38. data/src-test/core/fonttest.js +92 -0
  39. data/src-test/core/fontvariationdescriptiontest.js +76 -0
  40. data/src-test/core/fontwatchertest.js +510 -0
  41. data/src-test/core/useragenttest.js +395 -0
  42. data/src-test/custom/customcsstest.js +30 -0
  43. data/src-test/google/fontapiparsertest.js +92 -0
  44. data/src-test/google/fontapiurlbuildertest.js +28 -0
  45. data/src-test/google/googlefontapitest.js +173 -0
  46. data/src-test/typekit/typekit_script_test.js +171 -0
  47. data/src/ascender/ascender_script.js +84 -0
  48. data/src/async_load.js +3 -0
  49. data/src/closure.js +3 -0
  50. data/src/core/cssclassname.js +21 -0
  51. data/src/core/cssfontfamilyname.js +20 -0
  52. data/src/core/domhelper.js +103 -0
  53. data/src/core/eventdispatcher.js +78 -0
  54. data/src/core/font.js +84 -0
  55. data/src/core/fontmoduleloader.js +25 -0
  56. data/src/core/fontvariationdescription.js +112 -0
  57. data/src/core/fontwatcher.js +121 -0
  58. data/src/core/initialize.js +26 -0
  59. data/src/core/namespace.js +11 -0
  60. data/src/core/useragent.js +41 -0
  61. data/src/core/useragentparser.js +234 -0
  62. data/src/custom/customcss.js +37 -0
  63. data/src/google/fontapiparser.js +94 -0
  64. data/src/google/fontapiurlbuilder.js +39 -0
  65. data/src/google/googlefontapi.js +49 -0
  66. data/src/modules.yml +27 -0
  67. data/src/typekit/typekit_script.js +58 -0
  68. data/tools/compiler/compiler.jar +0 -0
  69. data/tools/jstestdriver/JsTestDriver-1.2.1.jar +0 -0
  70. data/webfontloader.gemspec +144 -0
  71. metadata +191 -0
@@ -0,0 +1,30 @@
1
+ var FontModuleLoaderTest = TestCase('FontModuleLoaderTest');
2
+
3
+ FontModuleLoaderTest.prototype.testGetProperModuleList = function() {
4
+ var fontModuleLoader = new webfont.FontModuleLoader();
5
+
6
+ fontModuleLoader.addModuleFactory('booh', function() { return { scary: true }; });
7
+ fontModuleLoader.addModuleFactory('haha', function() { return { funny: true }; });
8
+ fontModuleLoader.addModuleFactory('moo', function() { return { cowy: true }; });
9
+ var modules = fontModuleLoader.getModules({ booh: {}, moo: {}, nothing: {} });
10
+
11
+ assertNotNull(modules);
12
+ assertEquals(2, modules.length);
13
+ var module1 = modules[0];
14
+
15
+ assertNotNull(module1);
16
+ assertTrue(module1.scary || module1.cowy);
17
+ var module2 = modules[1];
18
+
19
+ assertNotNull(module2);
20
+ assertTrue(module2.scary || module2.cowy);
21
+ };
22
+
23
+ FontModuleLoaderTest.prototype.testNotModuleShouldHaveEmptyModuleList =
24
+ function() {
25
+ var fontModuleLoader = new webfont.FontModuleLoader();
26
+ var modules = fontModuleLoader.getModules();
27
+
28
+ assertNotNull(modules);
29
+ assertEquals(0, modules.length);
30
+ };
@@ -0,0 +1,92 @@
1
+ var FontTest = TestCase('FontTest');
2
+
3
+ FontTest.prototype.setUp = function() {
4
+ this.fakeHtmlElement_ = { className: '' };
5
+ this.fakeDomHelper_ = {
6
+ appendClassName: function() {},
7
+ createElement: function(name) {
8
+ return document.createElement(name);
9
+ },
10
+ insertInto: function() {},
11
+ removeElement: function() {}
12
+ };
13
+ this.fontModuleLoader_ = new webfont.FontModuleLoader();
14
+ };
15
+
16
+ FontTest.prototype.testFontLoad = function() {
17
+ var userAgent = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2',
18
+ 'Macintosh', '10.6', true);
19
+ var font = new webfont.WebFont(this.fakeDomHelper_, this.fontModuleLoader_,
20
+ this.fakeHtmlElement_, function(func, timeout) { func(); }, userAgent);
21
+ var self = this;
22
+ var testModule = null;
23
+
24
+ font.addModule('test', function(conf) {
25
+ testModule = new function() {
26
+ this.conf = conf;
27
+ this.loadCalled = false;
28
+ this.supportUserAgentCalled = false;
29
+ };
30
+ testModule.load = function(onReady) {
31
+ this.loadCalled = true;
32
+ onReady([]);
33
+ };
34
+ testModule.supportUserAgent = function(ua, support) {
35
+ this.supportUserAgentCalled = true;
36
+ support(true);
37
+ };
38
+ return testModule;
39
+ });
40
+ var loadingEventCalled = false;
41
+
42
+ assertEquals(0, font.moduleFailedLoading_);
43
+ assertEquals(0, font.moduleLoading_);
44
+
45
+ font.load({
46
+ test: {
47
+ somedata: 'in french a cow says meuh'
48
+ },
49
+ loading: function() {
50
+ loadingEventCalled = true;
51
+ }
52
+ });
53
+
54
+ assertEquals(1, font.moduleFailedLoading_);
55
+ assertEquals(0, font.moduleLoading_);
56
+ assertNotNull(testModule);
57
+ assertNotUndefined(testModule.conf);
58
+ assertNotNull(testModule.conf);
59
+ assertEquals('in french a cow says meuh', testModule.conf.somedata);
60
+ assertTrue(testModule.loadCalled);
61
+ assertTrue(testModule.supportUserAgentCalled);
62
+ assertTrue(loadingEventCalled);
63
+ };
64
+
65
+ FontTest.prototype.testFontInactive = function() {
66
+ var userAgent = new webfont.UserAgent('Firefox', '3.0', false);
67
+ var font = new webfont.WebFont(this.fakeDomHelper_, this.fontModuleLoader_,
68
+ this.fakeHtmlElement_, function(func, timeout) { func(); }, userAgent);
69
+ var self = this;
70
+ var testModule;
71
+
72
+ font.addModule('test', function(conf) {
73
+ testModule = new function() {
74
+ this.conf = conf;
75
+ this.loadCalled = false;
76
+ };
77
+ testModule.load = function(onReady) { };
78
+ return testModule;
79
+ });
80
+ var onInactiveCalled = false;
81
+
82
+ font.load({
83
+ test: {
84
+ somedata: 'in french a cow says meuh'
85
+ },
86
+ inactive: function() {
87
+ onInactiveCalled = true;
88
+ }
89
+ });
90
+ assertUndefined(testModule);
91
+ assertTrue(onInactiveCalled);
92
+ };
@@ -0,0 +1,76 @@
1
+ var FontVariationDescriptionTest = TestCase('FontVariationDescription');
2
+
3
+ FontVariationDescriptionTest.prototype.setUp = function() {
4
+ this.fvd_ = new webfont.FontVariationDescription();
5
+ };
6
+
7
+ FontVariationDescriptionTest.prototype.testCompactEmptyString = function() {
8
+ assertEquals('n4', this.fvd_.compact(''));
9
+ };
10
+
11
+ FontVariationDescriptionTest.prototype.testCompactFontStyle = function() {
12
+ assertEquals('n4', this.fvd_.compact('font-style: normal;'));
13
+ assertEquals('i4', this.fvd_.compact('font-style: italic;'));
14
+ assertEquals('o4', this.fvd_.compact('font-style: oblique;'));
15
+ };
16
+
17
+ FontVariationDescriptionTest.prototype.testCompactInvalidFontStyle = function() {
18
+ assertEquals('n4', this.fvd_.compact('font-style: other;'));
19
+ };
20
+
21
+ FontVariationDescriptionTest.prototype.testCompactFontWeight = function() {
22
+ assertEquals('n4', this.fvd_.compact('font-weight: normal;'));
23
+ assertEquals('n7', this.fvd_.compact('font-weight: bold;'));
24
+ assertEquals('n1', this.fvd_.compact('font-weight: 100;'));
25
+ assertEquals('n2', this.fvd_.compact('font-weight: 200;'));
26
+ assertEquals('n3', this.fvd_.compact('font-weight: 300;'));
27
+ assertEquals('n4', this.fvd_.compact('font-weight: 400;'));
28
+ assertEquals('n5', this.fvd_.compact('font-weight: 500;'));
29
+ assertEquals('n6', this.fvd_.compact('font-weight: 600;'));
30
+ assertEquals('n7', this.fvd_.compact('font-weight: 700;'));
31
+ assertEquals('n8', this.fvd_.compact('font-weight: 800;'));
32
+ assertEquals('n9', this.fvd_.compact('font-weight: 900;'));
33
+ };
34
+
35
+ FontVariationDescriptionTest.prototype.testCompactInvalidFontWeight = function() {
36
+ assertEquals('n4', this.fvd_.compact('font-weight: 140;'));
37
+ assertEquals('n4', this.fvd_.compact('font-weight: other;'));
38
+ };
39
+
40
+ FontVariationDescriptionTest.prototype.testCompactAllProperties = function() {
41
+ assertEquals('i7', this.fvd_.compact('font-style: italic; font-weight: bold;'));
42
+ assertEquals('i7', this.fvd_.compact('; font-style: italic; font-weight: bold'));
43
+ assertEquals('i7', this.fvd_.compact('font-style:italic;font-weight:bold;'));
44
+ assertEquals('i7', this.fvd_.compact(' font-style: italic ;\n\nfont-weight : bold; '));
45
+ };
46
+
47
+ FontVariationDescriptionTest.prototype.testInvalidProperties = function() {
48
+ assertEquals('n4', this.fvd_.compact('src: url(/font.otf);'));
49
+ assertEquals('n9', this.fvd_.compact('font-weight: 900; src: url(/font.otf);'));
50
+ assertEquals('n8', this.fvd_.compact('font-weight: 800; font-stretch:condensed'));
51
+ };
52
+
53
+ FontVariationDescriptionTest.prototype.testExpandFontStyle = function() {
54
+ assertEquals('font-style:normal;font-weight:400;', this.fvd_.expand('n4'));
55
+ assertEquals('font-style:italic;font-weight:400;', this.fvd_.expand('i4'));
56
+ assertEquals('font-style:oblique;font-weight:400;', this.fvd_.expand('o4'));
57
+ };
58
+
59
+ FontVariationDescriptionTest.prototype.testExpandFontWeight = function() {
60
+ assertEquals('font-style:normal;font-weight:100;', this.fvd_.expand('n1'));
61
+ assertEquals('font-style:normal;font-weight:200;', this.fvd_.expand('n2'));
62
+ assertEquals('font-style:normal;font-weight:300;', this.fvd_.expand('n3'));
63
+ assertEquals('font-style:normal;font-weight:400;', this.fvd_.expand('n4'));
64
+ assertEquals('font-style:normal;font-weight:500;', this.fvd_.expand('n5'));
65
+ assertEquals('font-style:normal;font-weight:600;', this.fvd_.expand('n6'));
66
+ assertEquals('font-style:normal;font-weight:700;', this.fvd_.expand('n7'));
67
+ assertEquals('font-style:normal;font-weight:800;', this.fvd_.expand('n8'));
68
+ assertEquals('font-style:normal;font-weight:900;', this.fvd_.expand('n9'));
69
+ };
70
+
71
+ FontVariationDescriptionTest.prototype.testExpandInvalid = function() {
72
+ assertEquals(null, this.fvd_.expand(''));
73
+ assertEquals(null, this.fvd_.expand('n'));
74
+ assertEquals(null, this.fvd_.expand('1'));
75
+ assertEquals(null, this.fvd_.expand('n1x'));
76
+ };
@@ -0,0 +1,510 @@
1
+ var FontWatcherTest = TestCase('FontWatcherTest');
2
+
3
+ FontWatcherTest.prototype.setUp = function() {
4
+ var self = this;
5
+
6
+ this.classNames_ = {};
7
+ this.classNamesCount_ = 0;
8
+ this.fakeDomHelper_ = {
9
+ removeElement: function() {},
10
+ createElement: function(name, attrs) {
11
+ var element = document.createElement(name);
12
+
13
+ for (var attr in attrs) {
14
+ element.setAttribute(attr, attrs[attr]);
15
+ }
16
+ return element;
17
+ },
18
+ insertInto: function() {},
19
+ appendClassName: function(e, name) {
20
+ self.classNames_[name] = true;
21
+ self.classNamesCount_++;
22
+ },
23
+ removeClassName: function(e, name) {
24
+ var exists = self.classNames_[name];
25
+ if (exists) {
26
+ self.classNames_[name] = false;
27
+ self.classNamesCount_--;
28
+ }
29
+ }
30
+ };
31
+ this.fakeHtmlElement_ = { className: '' };
32
+ this.loadingEventCalled_ = false;
33
+ this.fontLoadingEventCalled_ = 0;
34
+ this.fontLoading_ = [];
35
+ this.fontActiveEventCalled_ = 0;
36
+ this.fontActive_ = [];
37
+ this.fontInactvieEventCalled_ = 0;
38
+ this.fontInactive_ = [];
39
+ this.activeEventCalled_ = 0;
40
+
41
+ var namespace = 'ns';
42
+
43
+ this.callbacks_ = {
44
+ loading: function() {
45
+ self.loadingEventCalled_ = true;
46
+ },
47
+ active: function() {
48
+ self.activeEventCalled_++;
49
+ },
50
+ fontloading: function(fontFamily, fontDescription) {
51
+ self.fontLoadingEventCalled_++;
52
+ self.fontLoading_.push(fontFamily + ' ' + fontDescription);
53
+ },
54
+ fontactive: function(fontFamily, fontDescription) {
55
+ self.fontActiveEventCalled_++;
56
+ self.fontActive_.push(fontFamily + ' ' + fontDescription);
57
+ },
58
+ fontinactive: function(fontFamily, fontDescription) {
59
+ self.fontInactvieEventCalled_++;
60
+ self.fontInactive_.push(fontFamily + ' ' + fontDescription);
61
+ }
62
+ };
63
+ this.eventDispatcher_ = new webfont.EventDispatcher(this.fakeDomHelper_,
64
+ this.fakeHtmlElement_, this.callbacks_, namespace);
65
+ };
66
+
67
+ FontWatcherTest.prototype.testWatchOneFontAlreadyLoaded = function() {
68
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
69
+ this.eventDispatcher_, {
70
+ getWidth: function(element) {
71
+ var fontFamily = element.style.fontFamily;
72
+ var fonts = fontFamily.split(',');
73
+ var size = fonts.length;
74
+
75
+ if (size == 6) {
76
+ return 1;
77
+ } else {
78
+ return 2;
79
+ }
80
+ }
81
+ }, function() {}, function() { return 0; });
82
+ var fontFamilies = [ 'fontFamily1' ];
83
+
84
+ fontWatcher.watch(fontFamilies, {}, false);
85
+ assertEquals(1, this.fontLoadingEventCalled_);
86
+ assertEquals(1, this.fontLoading_.length);
87
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
88
+ assertEquals(1, this.fontActiveEventCalled_);
89
+ assertEquals(1, this.fontActive_.length);
90
+ assertEquals('fontFamily1 n4', this.fontActive_[0]);
91
+ };
92
+
93
+ FontWatcherTest.prototype.testWatchMultipleFontsAlreadyLoaded = function() {
94
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.eventDispatcher_,{
95
+ getWidth: function(element) {
96
+ var fontFamily = element.style.fontFamily;
97
+ var fonts = fontFamily.split(',');
98
+ var size = fonts.length;
99
+
100
+ if (size == 6) {
101
+ return 1;
102
+ } else {
103
+ return 2;
104
+ }
105
+ }
106
+ }, function() {}, function() { return 0; });
107
+ var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
108
+
109
+ fontWatcher.watch(fontFamilies, {}, false);
110
+ assertEquals(3, this.fontLoadingEventCalled_);
111
+ assertEquals(3, this.fontLoading_.length);
112
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
113
+ assertEquals('fontFamily2 n4', this.fontLoading_[1]);
114
+ assertEquals('fontFamily3 n4', this.fontLoading_[2]);
115
+ assertEquals(3, this.fontActiveEventCalled_);
116
+ assertEquals(3, this.fontActive_.length);
117
+ assertEquals('fontFamily1 n4', this.fontActive_[0]);
118
+ assertEquals('fontFamily2 n4', this.fontActive_[1]);
119
+ assertEquals('fontFamily3 n4', this.fontActive_[2]);
120
+ };
121
+
122
+ FontWatcherTest.prototype.testWatchOneFontWaitForLoad = function() {
123
+ var async = false;
124
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
125
+ this.eventDispatcher_, {
126
+
127
+ count_: 0,
128
+
129
+ getWidth: function(element) {
130
+ var fontFamily = element.style.fontFamily;
131
+ var fonts = fontFamily.split(',');
132
+ var size = fonts.length;
133
+
134
+ if (size == 6) {
135
+ return 1;
136
+ } else if (this.count_ == 0) {
137
+ this.count_++;
138
+ return 1;
139
+ } else if (this.count_ == 1) {
140
+ return 2;
141
+ }
142
+ }
143
+ }, function(func, timeout) {
144
+ async = true;
145
+ func();
146
+ }, function() { return 0; });
147
+ var fontFamilies = [ 'fontFamily1' ];
148
+
149
+ fontWatcher.watch(fontFamilies, {}, false);
150
+ assertTrue(async);
151
+ assertEquals(1, this.fontLoadingEventCalled_);
152
+ assertEquals(1, this.fontLoading_.length);
153
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
154
+ assertEquals(1, this.fontActiveEventCalled_);
155
+ assertEquals(1, this.fontActive_.length);
156
+ assertEquals('fontFamily1 n4', this.fontActive_[0]);
157
+ };
158
+
159
+ FontWatcherTest.prototype.testWatchMultipleFontsWaitForLoad = function() {
160
+ var async = 0;
161
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
162
+ this.eventDispatcher_, {
163
+
164
+ font1Count_: 0,
165
+ font2Count_: 0,
166
+ font3Count_: 0,
167
+
168
+ getWidth: function(element) {
169
+ var fontFamily = element.style.fontFamily;
170
+ var fonts = fontFamily.split(',');
171
+ var size = fonts.length;
172
+
173
+ if (size == 6) {
174
+ return 1;
175
+ } else if (fonts[0].indexOf("fontFamily1") != -1 &&
176
+ this.font1Count_ != 2) {
177
+ this.font1Count_++;
178
+ return 1;
179
+ } else if (fonts[0].indexOf("fontFamily2") != -1 &&
180
+ this.font2Count_ != 1) {
181
+ this.font2Count_++;
182
+ return 1;
183
+ } else if (fonts[0].indexOf("fontFamily3") != -1 &&
184
+ this.font3Count_ != 5) {
185
+ this.font3Count_++;
186
+ return 1;
187
+ } else {
188
+ return 2;
189
+ }
190
+ }
191
+ }, function(func, timeout) {
192
+ async++;
193
+ func();
194
+ }, function() { return 0; });
195
+ var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
196
+
197
+ fontWatcher.watch(fontFamilies, {}, false);
198
+ assertEquals(8, async);
199
+ assertEquals(3, this.fontLoadingEventCalled_);
200
+ assertEquals(3, this.fontLoading_.length);
201
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
202
+ assertEquals('fontFamily2 n4', this.fontLoading_[1]);
203
+ assertEquals('fontFamily3 n4', this.fontLoading_[2]);
204
+ assertEquals(3, this.fontActiveEventCalled_);
205
+ assertEquals(3, this.fontActive_.length);
206
+ assertEquals('fontFamily1 n4', this.fontActive_[0]);
207
+ assertEquals('fontFamily2 n4', this.fontActive_[1]);
208
+ assertEquals('fontFamily3 n4', this.fontActive_[2]);
209
+ };
210
+
211
+ FontWatcherTest.prototype.testWatchMultipleFontsWaitForLoadAndLoaded =
212
+ function() {
213
+ var async = 0;
214
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
215
+ this.eventDispatcher_, {
216
+
217
+ font1Count_: 0,
218
+ font3Count_: 0,
219
+
220
+ getWidth: function(element) {
221
+ var fontFamily = element.style.fontFamily;
222
+ var fonts = fontFamily.split(',');
223
+ var size = fonts.length;
224
+
225
+ if (size == 6) {
226
+ return 1;
227
+ } else if (fonts[0].indexOf("fontFamily1") != -1 &&
228
+ this.font1Count_ != 2) {
229
+ this.font1Count_++;
230
+ return 1;
231
+ } else if (fonts[0].indexOf("fontFamily3") != -1 &&
232
+ this.font3Count_ != 5) {
233
+ this.font3Count_++;
234
+ return 1;
235
+ } else {
236
+ return 2;
237
+ }
238
+ }
239
+ }, function(func, timeout) {
240
+ async++;
241
+ func();
242
+ }, function() { return 0; });
243
+ var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
244
+
245
+ fontWatcher.watch(fontFamilies, {}, false);
246
+ assertEquals(7, async);
247
+ assertEquals(3, this.fontLoadingEventCalled_);
248
+ assertEquals(3, this.fontLoading_.length);
249
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
250
+ assertEquals('fontFamily2 n4', this.fontLoading_[1]);
251
+ assertEquals('fontFamily3 n4', this.fontLoading_[2]);
252
+ assertEquals(3, this.fontActiveEventCalled_);
253
+ assertEquals(3, this.fontActive_.length);
254
+ assertEquals('fontFamily1 n4', this.fontActive_[0]);
255
+ assertEquals('fontFamily2 n4', this.fontActive_[1]);
256
+ assertEquals('fontFamily3 n4', this.fontActive_[2]);
257
+ };
258
+
259
+ FontWatcherTest.prototype.testWatchOneFontWaitForLoadInactive = function() {
260
+ var time = 0;
261
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
262
+ this.eventDispatcher_, {
263
+
264
+ count_: 0,
265
+
266
+ getWidth: function(element) {
267
+ return 1;
268
+ }
269
+ }, function(func, timeout) {
270
+ func();
271
+ }, function() {
272
+ time += 2500;
273
+ return time;
274
+ });
275
+ var fontFamilies = [ 'fontFamily1' ];
276
+
277
+ fontWatcher.watch(fontFamilies, {}, false);
278
+ assertEquals(1, this.fontLoadingEventCalled_);
279
+ assertEquals(1, this.fontLoading_.length);
280
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
281
+ assertEquals(1, this.fontInactvieEventCalled_);
282
+ assertEquals(1, this.fontInactive_.length);
283
+ assertEquals('fontFamily1 n4', this.fontInactive_[0]);
284
+ };
285
+
286
+ FontWatcherTest.prototype.testWatchMultipleFontsWaitForLoadAndInactive =
287
+ function() {
288
+ var count = 0;
289
+ var async = 0;
290
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
291
+ this.eventDispatcher_, {
292
+
293
+ font1Count_: 0,
294
+ font3Count_: 0,
295
+
296
+ getWidth: function(element) {
297
+ var fontFamily = element.style.fontFamily;
298
+ var fonts = fontFamily.split(',');
299
+ var size = fonts.length;
300
+
301
+ if (size == 6) {
302
+ return 1;
303
+ } else if (fonts[0].indexOf("fontFamily1") != -1 &&
304
+ this.font1Count_ != 2) {
305
+ this.font1Count_++;
306
+ return 1;
307
+ } else if (fonts[0].indexOf("fontFamily2") != -1) {
308
+ return 1;
309
+ } else if (fonts[0].indexOf("fontFamily3") != -1 &&
310
+ this.font3Count_ != 5) {
311
+ this.font3Count_++;
312
+ return 1;
313
+ } else {
314
+ return 2;
315
+ }
316
+ }
317
+ }, function(func, timeout) {
318
+ async++;
319
+ func();
320
+ }, function() {
321
+ if (++count == 5) {
322
+ return 5001;
323
+ }
324
+ return 0;
325
+ });
326
+ var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
327
+
328
+ fontWatcher.watch(fontFamilies, {}, false);
329
+ assertEquals(9, async);
330
+ assertEquals(3, this.fontLoadingEventCalled_);
331
+ assertEquals(3, this.fontLoading_.length);
332
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
333
+ assertEquals('fontFamily2 n4', this.fontLoading_[1]);
334
+ assertEquals('fontFamily3 n4', this.fontLoading_[2]);
335
+ assertEquals(2, this.fontActiveEventCalled_);
336
+ assertEquals(2, this.fontActive_.length);
337
+ assertEquals('fontFamily1 n4', this.fontActive_[0]);
338
+ assertEquals('fontFamily3 n4', this.fontActive_[1]);
339
+ assertEquals(1, this.fontInactvieEventCalled_);
340
+ assertEquals('fontFamily2 n4', this.fontInactive_[0]);
341
+ };
342
+
343
+ FontWatcherTest.prototype.testWatchMultipleFontsAlreadyLoadedAndLastBatchOnDone
344
+ = function() {
345
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_, this.eventDispatcher_,{
346
+ getWidth: function(element) {
347
+ var fontFamily = element.style.fontFamily;
348
+ var fonts = fontFamily.split(',');
349
+ var size = fonts.length;
350
+
351
+ if (size == 6) {
352
+ return 1;
353
+ } else {
354
+ return 2;
355
+ }
356
+ }
357
+ }, function() {}, function() { return 0; });
358
+ var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
359
+
360
+ fontWatcher.watch(fontFamilies, {}, true);
361
+ assertEquals(3, this.fontLoadingEventCalled_);
362
+ assertEquals(3, this.fontLoading_.length);
363
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
364
+ assertEquals('fontFamily2 n4', this.fontLoading_[1]);
365
+ assertEquals('fontFamily3 n4', this.fontLoading_[2]);
366
+ assertEquals(3, this.fontActiveEventCalled_);
367
+ assertEquals(3, this.fontActive_.length);
368
+ assertEquals('fontFamily1 n4', this.fontActive_[0]);
369
+ assertEquals('fontFamily2 n4', this.fontActive_[1]);
370
+ assertEquals('fontFamily3 n4', this.fontActive_[2]);
371
+ assertEquals(1, this.activeEventCalled_);
372
+ assertEquals(4, this.classNamesCount_);
373
+ assertEquals(true, this.classNames_['ns-fontfamily1-n4-active']);
374
+ assertEquals(true, this.classNames_['ns-fontfamily2-n4-active']);
375
+ assertEquals(true, this.classNames_['ns-fontfamily3-n4-active']);
376
+ assertEquals(true, this.classNames_['ns-active']);
377
+ };
378
+
379
+ FontWatcherTest.prototype.testWatchMultipleFontsWaitForLoadAndLastBatchOnDone =
380
+ function() {
381
+ var async = 0;
382
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
383
+ this.eventDispatcher_, {
384
+
385
+ font1Count_: 0,
386
+ font2Count_: 0,
387
+ font3Count_: 0,
388
+
389
+ getWidth: function(element) {
390
+ var fontFamily = element.style.fontFamily;
391
+ var fonts = fontFamily.split(',');
392
+ var size = fonts.length;
393
+
394
+ if (size == 6) {
395
+ return 1;
396
+ } else if (fonts[0].indexOf("fontFamily1") != -1 &&
397
+ this.font1Count_ != 2) {
398
+ this.font1Count_++;
399
+ return 1;
400
+ } else if (fonts[0].indexOf("fontFamily2") != -1 &&
401
+ this.font2Count_ != 1) {
402
+ this.font2Count_++;
403
+ return 1;
404
+ } else if (fonts[0].indexOf("fontFamily3") != -1 &&
405
+ this.font3Count_ != 5) {
406
+ this.font3Count_++;
407
+ return 1;
408
+ } else {
409
+ return 2;
410
+ }
411
+ }
412
+ }, function(func, timeout) {
413
+ async++;
414
+ func();
415
+ }, function() { return 0; });
416
+ var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
417
+
418
+ fontWatcher.watch(fontFamilies, {}, true);
419
+ assertEquals(8, async);
420
+ assertEquals(3, this.fontLoadingEventCalled_);
421
+ assertEquals(3, this.fontLoading_.length);
422
+ assertEquals('fontFamily1 n4', this.fontLoading_[0]);
423
+ assertEquals('fontFamily2 n4', this.fontLoading_[1]);
424
+ assertEquals('fontFamily3 n4', this.fontLoading_[2]);
425
+ assertEquals(3, this.fontActiveEventCalled_);
426
+ assertEquals(3, this.fontActive_.length);
427
+ assertEquals('fontFamily1 n4', this.fontActive_[0]);
428
+ assertEquals('fontFamily2 n4', this.fontActive_[1]);
429
+ assertEquals('fontFamily3 n4', this.fontActive_[2]);
430
+ assertEquals(1, this.activeEventCalled_);
431
+ assertEquals(true, this.classNames_['ns-fontfamily1-n4-active']);
432
+ assertEquals(true, this.classNames_['ns-fontfamily2-n4-active']);
433
+ assertEquals(true, this.classNames_['ns-fontfamily3-n4-active']);
434
+ assertEquals(true, this.classNames_['ns-active']);
435
+ assertEquals(4, this.classNamesCount_);
436
+ };
437
+
438
+ FontWatcherTest.prototype
439
+ .testWatchMultipleFontsWaitForLoadAndLastBatchOnDoneWithVariations =
440
+ function() {
441
+ var async = 0;
442
+ var fontWatcher = new webfont.FontWatcher(this.fakeDomHelper_,
443
+ this.eventDispatcher_, {
444
+
445
+ font1Count_: 0,
446
+ font2Count_: 0,
447
+ font3Count_: 0,
448
+
449
+ getWidth: function(element) {
450
+ var fontFamily = element.style.fontFamily;
451
+ var fonts = fontFamily.split(',');
452
+ var size = fonts.length;
453
+
454
+ if (size == 6) {
455
+ return 1;
456
+ } else if (fonts[0].indexOf("fontFamily1") != -1 &&
457
+ this.font1Count_ != 2) {
458
+ this.font1Count_++;
459
+ return 1;
460
+ } else if (fonts[0].indexOf("fontFamily2") != -1 &&
461
+ this.font2Count_ != 1) {
462
+ this.font2Count_++;
463
+ return 1;
464
+ } else if (fonts[0].indexOf("fontFamily3") != -1 &&
465
+ this.font3Count_ != 5) {
466
+ this.font3Count_++;
467
+ return 1;
468
+ } else {
469
+ return 2;
470
+ }
471
+ }
472
+ }, function(func, timeout) {
473
+ async++;
474
+ func();
475
+ }, function() { return 0; });
476
+ var fontFamilies = [ 'fontFamily1', 'fontFamily2', 'fontFamily3' ];
477
+
478
+ fontWatcher.watch(fontFamilies, {
479
+ 'fontFamily1': ['i7'],
480
+ 'fontFamily2': null,
481
+ 'fontFamily3': ['n4', 'i4', 'n7'] }, true);
482
+
483
+ assertEquals(8, async);
484
+ assertEquals(5, this.fontLoadingEventCalled_);
485
+ assertEquals(5, this.fontLoading_.length);
486
+ assertEquals('fontFamily1 i7',
487
+ this.fontLoading_[0]);
488
+ assertEquals('fontFamily2 n4', this.fontLoading_[1]);
489
+ assertEquals('fontFamily3 n4',
490
+ this.fontLoading_[2]);
491
+ assertEquals('fontFamily3 i4', this.fontLoading_[3]);
492
+ assertEquals('fontFamily3 n7', this.fontLoading_[4]);
493
+ assertEquals(5, this.fontActiveEventCalled_);
494
+ assertEquals(5, this.fontActive_.length);
495
+ assertEquals('fontFamily1 i7',
496
+ this.fontActive_[0]);
497
+ assertEquals('fontFamily2 n4', this.fontActive_[1]);
498
+ assertEquals('fontFamily3 n4',
499
+ this.fontActive_[2]);
500
+ assertEquals('fontFamily3 i4', this.fontActive_[3]);
501
+ assertEquals('fontFamily3 n7', this.fontActive_[4]);
502
+ assertEquals(1, this.activeEventCalled_);
503
+ assertEquals(6, this.classNamesCount_);
504
+ assertEquals(true, this.classNames_['ns-fontfamily1-i7-active']);
505
+ assertEquals(true, this.classNames_['ns-fontfamily2-n4-active']);
506
+ assertEquals(true, this.classNames_['ns-fontfamily3-n4-active']);
507
+ assertEquals(true, this.classNames_['ns-fontfamily3-i4-active']);
508
+ assertEquals(true, this.classNames_['ns-fontfamily3-n7-active']);
509
+ assertEquals(true, this.classNames_['ns-active']);
510
+ };