From 281993748ffb429d33df26f6e8bd2a733f445af7 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Thu, 27 Oct 2011 12:50:14 +0100
Subject: [PATCH] Remember the last selected email for a site.

Store off the email in localStorage, when we display the list of emails, highlight the previously selected one.

close #1
---
 .../dialog/controllers/dialog_controller.js   |  7 +-
 .../controllers/pickemail_controller.js       | 13 ++-
 browserid/static/dialog/css/popup.css         |  4 +
 .../pickemail_controller_unit_test.js         | 96 +++++++++++++++++++
 browserid/static/dialog/test/qunit/qunit.js   |  6 +-
 browserid/static/dialog/views/pickemail.ejs   |  7 +-
 6 files changed, 126 insertions(+), 7 deletions(-)
 create mode 100644 browserid/static/dialog/test/qunit/controllers/pickemail_controller_unit_test.js

diff --git a/browserid/static/dialog/controllers/dialog_controller.js b/browserid/static/dialog/controllers/dialog_controller.js
index 49560e08d..1c7a39c83 100644
--- a/browserid/static/dialog/controllers/dialog_controller.js
+++ b/browserid/static/dialog/controllers/dialog_controller.js
@@ -180,7 +180,12 @@
       },
 
       doPickEmail: function() {
-        this.element.pickemail();
+        this.element.pickemail({
+          // XXX ideal is to get rid of this and have a User function 
+          // that takes care of getting email addresses AND the last used email 
+          // for this site.
+          origin: user.getHostname()
+        });
       },
 
       doAuthenticate: function(info) {
diff --git a/browserid/static/dialog/controllers/pickemail_controller.js b/browserid/static/dialog/controllers/pickemail_controller.js
index 1b3679c13..a9197a4c6 100644
--- a/browserid/static/dialog/controllers/pickemail_controller.js
+++ b/browserid/static/dialog/controllers/pickemail_controller.js
@@ -41,6 +41,8 @@
       bid = BrowserID,
       user = bid.User,
       errors = bid.Errors,
+      storage = bid.Storage,
+      origin,
       body = $("body"),
       animationComplete = body.innerWidth() < 640,
       assertion;
@@ -109,6 +111,9 @@
     // the animation, hopefully this minimizes the delay the user notices.
     var self=this;
     user.getAssertion(email, function(assert) {
+      // XXX make a user api call that gets the assertion and sets the site 
+      // email as well.
+      storage.setSiteEmail(origin, email);
       assertion = assert || null;
       startAnimation.call(self);
     }, self.getErrorDialog(errors.getAssertion));
@@ -176,10 +181,16 @@
 
   PageController.extend("Pickemail", {}, {
     init: function(el, options) {
+      origin = options.origin;
+
       this._super(el, {
         bodyTemplate: "pickemail.ejs",
         bodyVars: {
-          identities: user.getStoredEmailKeypairs()
+          identities: user.getStoredEmailKeypairs(),
+          // XXX ideal is to get rid of this and have a User function 
+          // that takes care of getting email addresses AND the last used email 
+          // for this site.
+          siteemail: storage.getSiteEmail(options.origin)
         }
       });
 
diff --git a/browserid/static/dialog/css/popup.css b/browserid/static/dialog/css/popup.css
index e5f4503f2..72f7272fa 100644
--- a/browserid/static/dialog/css/popup.css
+++ b/browserid/static/dialog/css/popup.css
@@ -300,6 +300,10 @@ label {
     color: #333;
 }
 
+.inputs > li > label.preselected {
+    font-weight: bold;
+}
+
 .inputs > li:only-child > label {
     cursor: default;
 }
diff --git a/browserid/static/dialog/test/qunit/controllers/pickemail_controller_unit_test.js b/browserid/static/dialog/test/qunit/controllers/pickemail_controller_unit_test.js
new file mode 100644
index 000000000..25730ca41
--- /dev/null
+++ b/browserid/static/dialog/test/qunit/controllers/pickemail_controller_unit_test.js
@@ -0,0 +1,96 @@
+/*jshint browsers:true, forin: true, laxbreak: true */
+/*global steal: true, test: true, start: true, stop: true, module: true, ok: true, equal: true, BrowserID:true */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla BrowserID.
+ *
+ * The Initial Developer of the Original Code is Mozilla.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+steal.plugins("jquery").then("/dialog/controllers/page_controller", "/dialog/controllers/pickemail_controller", function() {
+  "use strict";
+
+  var controller, 
+      el = $("body"),
+      storage = BrowserID.Storage;
+
+  function reset() {
+    el = $("#controller_head");
+    el.find("#formWrap .contents").html("");
+    el.find("#wait .contents").html("");
+    el.find("#error .contents").html("");
+  }
+
+  module("controllers/pickemail_controller", {
+    setup: function() {
+      reset();
+      storage.clear();
+    },
+
+    teardown: function() {
+      if (controller) {
+        controller.destroy();
+      }    
+      reset();
+      storage.clear();
+    } 
+  });
+
+
+  test("pickemail controller with email associated with site", function() {
+    storage.addEmail("testuser@testuser.com", {priv: "priv", pub: "pub"});
+    storage.addEmail("testuser2@testuser.com", {priv: "priv", pub: "pub"});
+    storage.setSiteEmail("browserid.org", "testuser2@testuser.com");
+
+    controller = el.pickemail({origin: "browserid.org"}).controller();
+    ok(controller, "controller created");
+
+    var radioButton = $("input[type=radio]").eq(1);
+    ok(radioButton.is(":checked"), "the email address we specified is checked");
+
+    var label = radioButton.parent();;
+    ok(label.hasClass("preselected"), "the label has the preselected class");
+  });
+
+  test("pickemail controller without email associated with site", function() {
+    storage.addEmail("testuser@testuser.com", {priv: "priv", pub: "pub"});
+
+    controller = el.pickemail({origin: "browserid.org"}).controller();
+    ok(controller, "controller created");
+
+    var radioButton = $("input[type=radio]").eq(0);
+    equal(radioButton.is(":checked"), false, "The email address is not checked");
+
+    var label = radioButton.parent();
+    equal(label.hasClass("preselected"), false, "the label has no class");
+  });
+
+});
+
diff --git a/browserid/static/dialog/test/qunit/qunit.js b/browserid/static/dialog/test/qunit/qunit.js
index 450add4ea..909773ac0 100644
--- a/browserid/static/dialog/test/qunit/qunit.js
+++ b/browserid/static/dialog/test/qunit/qunit.js
@@ -12,8 +12,8 @@ steal("/dialog/resources/browserid.js",
     "jquery/view/ejs",
     "funcunit/qunit")
 	.views('testBodyTemplate.ejs')
-	.views('wait.ejs')
-  .then("/dialog/controllers/page_controller.js")
+	.views('wait.ejs',
+         'pickemail.ejs')
   .then("browserid_unit_test")
   .then("include_unit_test")
   .then("pages/add_email_address_test")
@@ -23,5 +23,5 @@ steal("/dialog/resources/browserid.js",
   .then("resources/network_unit_test")
   .then("resources/user_unit_test")
   .then("controllers/page_controller_unit_test")
-  .then("controllers/page_controller_unit_test")
+  .then("controllers/pickemail_controller_unit_test")
 
diff --git a/browserid/static/dialog/views/pickemail.ejs b/browserid/static/dialog/views/pickemail.ejs
index de6582dad..8e0b16691 100644
--- a/browserid/static/dialog/views/pickemail.ejs
+++ b/browserid/static/dialog/views/pickemail.ejs
@@ -4,8 +4,11 @@
       <ul class="inputs">
           <% _.each(identities, function(email_obj, email_address) { %>
               <li>
-                  <label for="<%= email_address %>" class="serif">
-                    <input type="radio" name="email" id="<%= email_address %>" value="<%= email_address %>" />
+
+                  <label for="<%= email_address %>" class="serif<% if (email_address === siteemail) { %> preselected<% } %>">
+                    <input type="radio" name="email" id="<%= email_address %>" value="<%= email_address %>" 
+                      <% if (email_address === siteemail) { %> checked="checked" <% } %> 
+                    />
                     <%= email_address %>
                   </label>
               </li>
-- 
GitLab