From 6affd5f4855ee3c40a7423a9df1e4b30c570a9a3 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Tue, 22 Nov 2011 17:20:36 +0000
Subject: [PATCH] Fixing the teardown problem of onWinUnload not knowing what
 self is.

---
 resources/static/dialog/controllers/dialog.js |  5 +-
 .../qunit/controllers/dialog_unit_test.js     | 73 +++++++++++++++++--
 2 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/resources/static/dialog/controllers/dialog.js b/resources/static/dialog/controllers/dialog.js
index 991069116..eef6fa57d 100644
--- a/resources/static/dialog/controllers/dialog.js
+++ b/resources/static/dialog/controllers/dialog.js
@@ -79,6 +79,7 @@ BrowserID.Modules.Dialog = (function() {
 
   function onWinUnload() {
     // do this only if something else hasn't declared success
+    var self=this;
     if (!self.success) {
       bid.Storage.setStagedOnBehalfOf("");
       self.doCancel();
@@ -256,7 +257,9 @@ BrowserID.Modules.Dialog = (function() {
               self.publish("auth");
             }
           }, self.getErrorDialog(errors.checkAuthentication));
-    }
+    },
+
+    doWinUnload: onWinUnload
 
   });
 
diff --git a/resources/static/test/qunit/controllers/dialog_unit_test.js b/resources/static/test/qunit/controllers/dialog_unit_test.js
index 8a4ef0bea..33ed6facb 100644
--- a/resources/static/test/qunit/controllers/dialog_unit_test.js
+++ b/resources/static/test/qunit/controllers/dialog_unit_test.js
@@ -37,9 +37,14 @@
 (function() {
   "use strict";
 
-  var controller,
+  var bid = BrowserID,
+      mediator = bid.Mediator,
+      channel = bid.Channel,
+      controller,
       el,
-      channelError = false;
+      channelError = false,
+      winMock,
+      navMock;
 
   function reset() {
     el = $("#controller_head");
@@ -50,13 +55,41 @@
     channelError = false;
   }
 
-  function createController(config) {
-    var config = $.extend(config, {
-      window: {
-        setupChannel: function() {
-          if (channelError) throw "Channel error";
+  function WinMock() {
+    this.location.hash = "#1234";  
+  }
+
+  WinMock.prototype = {
+    setupChannel: function() {
+      if (channelError) throw "Channel error";
+    },
+
+    // Oh so beautiful.
+    opener: {
+      frames: {
+        1234: {
+          BrowserID: {
+            Relay: {
+              registerClient: function() {
+              },
+
+              unregisterClient: function() {
+              }
+            }
+          }
         }
       }
+    },
+
+    location: {
+    }
+
+
+  }
+
+  function createController(config) {
+    var config = $.extend(config, {
+      window: winMock
     });
 
     controller = BrowserID.Modules.Dialog.create(config);
@@ -64,12 +97,21 @@
 
   module("controllers/dialog_controller", {
     setup: function() {
+      winMock = new WinMock();
+      channel.init({
+        window: winMock,
+        navigator: navMock
+      });
       reset();
     },
 
     teardown: function() {
       controller.destroy();
       reset();
+      channel.init({
+        window: window,
+        navigator: navigator
+      });
     }
   });
 
@@ -152,5 +194,22 @@
   });
 */
 
+  test("doWinUnload", function() {
+    createController({
+      requiredEmail: "registered@testuser.com"
+    });
+
+    var error;
+
+    try {
+      controller.doWinUnload();
+    }
+    catch(e) {
+      error = e;
+    }
+
+    equal(typeof error, "undefined", "no error thrown when unloading window");
+  });
+
 }());
 
-- 
GitLab