From 1e79cd43a9b45bbdcad715dc245a2c4d0cde19a2 Mon Sep 17 00:00:00 2001 From: Charles Vazac Date: Fri, 30 Mar 2018 12:37:48 -0400 Subject: [PATCH] Errors: Check context in wrapFn --- plugins/errors.js | 6 +++++- tests/boomerang-test-framework.js | 2 +- .../14-errors/40-wrap-context.html | 14 +++++++++++++ .../14-errors/40-wrap-context.js | 21 +++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/page-templates/14-errors/40-wrap-context.html create mode 100644 tests/page-templates/14-errors/40-wrap-context.js diff --git a/plugins/errors.js b/plugins/errors.js index 27e8e25b1..adeb8da3b 100644 --- a/plugins/errors.js +++ b/plugins/errors.js @@ -1316,7 +1316,11 @@ try { var args = Array.prototype.slice.call(arguments); var callbackFn = args[callbackIndex]; - var targetObj = useCallingObject ? this : that; + + // Determine the calling object: if 'this' is the Boomerang frame, we should swap it + // to the correct top level window context. If Boomerang isn't running in a frame, + // BOOMR.window will still point to the top-level window. + var targetObj = useCallingObject ? (this === window ? BOOMR.window : this) : that; var wrappedFn = impl.wrap(callbackFn, targetObj, via); args[callbackIndex] = wrappedFn; diff --git a/tests/boomerang-test-framework.js b/tests/boomerang-test-framework.js index 1d1094aa0..052129177 100644 --- a/tests/boomerang-test-framework.js +++ b/tests/boomerang-test-framework.js @@ -259,7 +259,7 @@ t.configureTestEnvironment = function() { // setup Mocha - window.mocha.globals(["BOOMR", "PageGroupVariable", "mochaResults", "BOOMR_configt"]); + window.mocha.globals(["BOOMR", "PageGroupVariable", "mochaResults", "BOOMR_configt", "_bmrEvents"]); window.mocha.checkLeaks(); // set globals diff --git a/tests/page-templates/14-errors/40-wrap-context.html b/tests/page-templates/14-errors/40-wrap-context.html new file mode 100644 index 000000000..ff0b6c166 --- /dev/null +++ b/tests/page-templates/14-errors/40-wrap-context.html @@ -0,0 +1,14 @@ +<%= header %> +<%= boomerangSnippet %> + + +<%= footer %> diff --git a/tests/page-templates/14-errors/40-wrap-context.js b/tests/page-templates/14-errors/40-wrap-context.js new file mode 100644 index 000000000..cbac088c4 --- /dev/null +++ b/tests/page-templates/14-errors/40-wrap-context.js @@ -0,0 +1,21 @@ +/*eslint-env mocha*/ + +describe("e2e/14-errors/40-wrap-context", function() { + it("Should infer BOOMR.window as context for unbound addEventListener calls", function(done) { + if (typeof window.EventTarget === "undefined") { + return this.skip(); + } + + assert.equal(BOOMR.window, top); + assert.equal(BOOMR.window, window.parent); + (function(){ + addEventListener("foo", function(){ + // this assert is redundant, if we get our callback, it means it fired on the right window + assert.equal(this, BOOMR.window); + done(); + }); + this.dispatchEvent(new Event("foo")); + }).apply(BOOMR.window); + }); + +});