Skip to content

Commit

Permalink
Add test for using custom context with web font loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean McBride committed Dec 5, 2012
1 parent beb6d91 commit d691b6f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
65 changes: 50 additions & 15 deletions src-test/core/fonttest.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ FontTest.prototype.setUp = function() {
FontTest.prototype.testFontLoad = function() { FontTest.prototype.testFontLoad = function() {
var userAgent = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2', var userAgent = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2',
'Macintosh', '10.6', undefined, true); 'Macintosh', '10.6', undefined, true);
var font = new webfont.WebFont(this.fontModuleLoader_, var font = new webfont.WebFont(window, this.fontModuleLoader_,
function(func, timeout) { func(); }, userAgent); function(func, timeout) { func(); }, userAgent);
var self = this;
var testModule = null; var testModule = null;


font.addModule('test', function(conf) { font.addModule('test', function(conf, domHelper) {
testModule = new function() { testModule = new function() {
this.conf = conf; this.conf = conf;
this.domHelper = domHelper;
this.loadCalled = false; this.loadCalled = false;
this.supportUserAgentCalled = false; this.supportUserAgentCalled = false;
};
testModule.load = function(onReady) {
this.loadCalled = true;
onReady([]);
}; };
testModule.supportUserAgent = function(ua, support) { testModule.load = function(onReady) {
this.supportUserAgentCalled = true; this.loadCalled = true;
support(true); onReady([]);
}; };
return testModule; testModule.supportUserAgent = function(ua, support) {
this.supportUserAgentCalled = true;
support(true);
};
return testModule;
}); });
var loadingEventCalled = false;


assertEquals(0, font.moduleFailedLoading_); assertEquals(0, font.moduleFailedLoading_);
assertEquals(0, font.moduleLoading_); assertEquals(0, font.moduleLoading_);


var loadingEventCalled = false;
font.load({ font.load({
test: { test: {
somedata: 'in french a cow says meuh' somedata: 'in french a cow says meuh'
Expand All @@ -47,17 +47,52 @@ FontTest.prototype.testFontLoad = function() {
assertNotNull(testModule); assertNotNull(testModule);
assertNotUndefined(testModule.conf); assertNotUndefined(testModule.conf);
assertNotNull(testModule.conf); assertNotNull(testModule.conf);
assertNotUndefined(testModule.domHelper);
assertNotNull(testModule.domHelper);
assertSame(window, testModule.domHelper.getMainWindow());
assertSame(window, testModule.domHelper.getLoadWindow());
assertEquals('in french a cow says meuh', testModule.conf.somedata); assertEquals('in french a cow says meuh', testModule.conf.somedata);
assertTrue(testModule.loadCalled); assertTrue(testModule.loadCalled);
assertTrue(testModule.supportUserAgentCalled); assertTrue(testModule.supportUserAgentCalled);
assertTrue(loadingEventCalled); assertTrue(loadingEventCalled);
}; };


FontTest.prototype.testFontLoadWithContext = function() {
var fakeMainWindow = {};

var userAgent = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2',
'Macintosh', '10.6', undefined, true);
var font = new webfont.WebFont(fakeMainWindow, this.fontModuleLoader_,
function(func, timeout) { func(); }, userAgent);
var testModule = null;

font.addModule('test', function(conf, domHelper) {
testModule = new function() {
this.domHelper = domHelper;
};
testModule.load = function() {};
testModule.supportUserAgent = function(ua, support) {
support(true);
};
return testModule;
});

font.load({
test: {
somedata: 'in french a cow says meuh'
}
}, window);

assertNotUndefined(testModule.domHelper);
assertNotNull(testModule.domHelper);
assertSame(fakeMainWindow, testModule.domHelper.getMainWindow());
assertSame(window, testModule.domHelper.getLoadWindow());
};

FontTest.prototype.testFontInactive = function() { FontTest.prototype.testFontInactive = function() {
var userAgent = new webfont.UserAgent('Firefox', '3.0', false); var userAgent = new webfont.UserAgent('Firefox', '3.0', false);
var font = new webfont.WebFont(this.fontModuleLoader_, var font = new webfont.WebFont(window, this.fontModuleLoader_,
function(func, timeout) { func(); }, userAgent); function(func, timeout) { func(); }, userAgent);
var self = this;
var testModule; var testModule;


font.addModule('test', function(conf) { font.addModule('test', function(conf) {
Expand Down
15 changes: 10 additions & 5 deletions src/core/font.js
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,13 @@
/** /**
* @param {Window} mainWindow The main application window containing
* webfontloader.js.
* @param {webfont.FontModuleLoader} fontModuleLoader A loader instance to use.
* @param {function(function(), number=)} asyncCall An async function to use.
* @param {webfont.UserAgent} userAgent The detected user agent to load for.
* @constructor * @constructor
*/ */
webfont.WebFont = function(fontModuleLoader, asyncCall, userAgent) { webfont.WebFont = function(mainWindow, fontModuleLoader, asyncCall, userAgent) {
this.mainWindow_ = mainWindow;
this.fontModuleLoader_ = fontModuleLoader; this.fontModuleLoader_ = fontModuleLoader;
this.asyncCall_ = asyncCall; this.asyncCall_ = asyncCall;
this.userAgent_ = userAgent; this.userAgent_ = userAgent;
Expand All @@ -14,12 +20,11 @@ webfont.WebFont.prototype.addModule = function(name, factory) {
}; };


webfont.WebFont.prototype.load = function(configuration, opt_context) { webfont.WebFont.prototype.load = function(configuration, opt_context) {
var context = opt_context || window; var context = opt_context || this.mainWindow_;
this.domHelper_ = new webfont.DomHelper(window, context); this.domHelper_ = new webfont.DomHelper(this.mainWindow_, context);
this.htmlElement_ = context.document.documentElement;


var eventDispatcher = new webfont.EventDispatcher( var eventDispatcher = new webfont.EventDispatcher(
this.domHelper_, this.htmlElement_, configuration); this.domHelper_, context.document.documentElement, configuration);


if (this.userAgent_.isSupportingWebFont()) { if (this.userAgent_.isSupportingWebFont()) {
this.load_(eventDispatcher, configuration); this.load_(eventDispatcher, configuration);
Expand Down
3 changes: 2 additions & 1 deletion src/core/initialize.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ var globalName = 'WebFont';
var globalNamespaceObject = window[globalName] = (function() { var globalNamespaceObject = window[globalName] = (function() {
var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document); var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
var userAgent = userAgentParser.parse(); var userAgent = userAgentParser.parse();
var fontModuleLoader = new webfont.FontModuleLoader();
var asyncCall = function(func, timeout) { setTimeout(func, timeout); }; var asyncCall = function(func, timeout) { setTimeout(func, timeout); };


return new webfont.WebFont(new webfont.FontModuleLoader(), asyncCall, userAgent); return new webfont.WebFont(window, fontModuleLoader, asyncCall, userAgent);
})(); })();


// Export the public API. // Export the public API.
Expand Down

0 comments on commit d691b6f

Please sign in to comment.