Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add test for using custom context with web font loader

  • Loading branch information...
commit d691b6f560f257f1f99fac2ab4bbe115f59e2173 1 parent beb6d91
Sean McBride authored
View
65 src-test/core/fonttest.js
@@ -7,32 +7,32 @@ FontTest.prototype.setUp = function() {
FontTest.prototype.testFontLoad = function() {
var userAgent = new webfont.UserAgent('Firefox', '3.6', 'Gecko', '1.9.2',
'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);
- var self = this;
var testModule = null;
- font.addModule('test', function(conf) {
+ font.addModule('test', function(conf, domHelper) {
testModule = new function() {
this.conf = conf;
+ this.domHelper = domHelper;
this.loadCalled = false;
this.supportUserAgentCalled = false;
- };
- testModule.load = function(onReady) {
- this.loadCalled = true;
- onReady([]);
};
- testModule.supportUserAgent = function(ua, support) {
- this.supportUserAgentCalled = true;
- support(true);
- };
- return testModule;
+ testModule.load = function(onReady) {
+ this.loadCalled = true;
+ onReady([]);
+ };
+ testModule.supportUserAgent = function(ua, support) {
+ this.supportUserAgentCalled = true;
+ support(true);
+ };
+ return testModule;
});
- var loadingEventCalled = false;
assertEquals(0, font.moduleFailedLoading_);
assertEquals(0, font.moduleLoading_);
+ var loadingEventCalled = false;
font.load({
test: {
somedata: 'in french a cow says meuh'
@@ -47,17 +47,52 @@ FontTest.prototype.testFontLoad = function() {
assertNotNull(testModule);
assertNotUndefined(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);
assertTrue(testModule.loadCalled);
assertTrue(testModule.supportUserAgentCalled);
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() {
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);
- var self = this;
var testModule;
font.addModule('test', function(conf) {
View
15 src/core/font.js
@@ -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
*/
-webfont.WebFont = function(fontModuleLoader, asyncCall, userAgent) {
+webfont.WebFont = function(mainWindow, fontModuleLoader, asyncCall, userAgent) {
+ this.mainWindow_ = mainWindow;
this.fontModuleLoader_ = fontModuleLoader;
this.asyncCall_ = asyncCall;
this.userAgent_ = userAgent;
@@ -14,12 +20,11 @@ webfont.WebFont.prototype.addModule = function(name, factory) {
};
webfont.WebFont.prototype.load = function(configuration, opt_context) {
- var context = opt_context || window;
- this.domHelper_ = new webfont.DomHelper(window, context);
- this.htmlElement_ = context.document.documentElement;
+ var context = opt_context || this.mainWindow_;
+ this.domHelper_ = new webfont.DomHelper(this.mainWindow_, context);
var eventDispatcher = new webfont.EventDispatcher(
- this.domHelper_, this.htmlElement_, configuration);
+ this.domHelper_, context.document.documentElement, configuration);
if (this.userAgent_.isSupportingWebFont()) {
this.load_(eventDispatcher, configuration);
View
3  src/core/initialize.js
@@ -5,9 +5,10 @@ var globalName = 'WebFont';
var globalNamespaceObject = window[globalName] = (function() {
var userAgentParser = new webfont.UserAgentParser(navigator.userAgent, document);
var userAgent = userAgentParser.parse();
+ var fontModuleLoader = new webfont.FontModuleLoader();
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.
Please sign in to comment.
Something went wrong with that request. Please try again.