Bug 814382 - Can't change encoding for source view in TB17. r+a=standard8
authorMagnus Melin <mkmelin+mozilla@iki.fi>
Thu, 29 Aug 2013 21:35:28 +0300
changeset 15993 24b90b84740fed265466883aa8f34817624c0d42
parent 15992 2bb1ce0050b7d2c14cacb3a15cac158d64a71215
child 15994 a7ff46d7c19240439bcaadb0e6334d3efdb4e4b1
push id994
push userbugzilla@standard8.plus.com
push dateMon, 09 Sep 2013 21:26:50 +0000
treeherdercomm-beta@340434e8f9e7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs814382
Bug 814382 - Can't change encoding for source view in TB17. r+a=standard8
mail/test/mozmill/content-policy/test-view-source.js
mailnews/base/src/nsMsgContentPolicy.cpp
new file mode 100644
--- /dev/null
+++ b/mail/test/mozmill/content-policy/test-view-source.js
@@ -0,0 +1,101 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Test that view-source content can be reloaded to change encoding.
+ */
+
+// make SOLO_TEST=content-policy/test-view-source.js mozmill-one
+
+const MODULE_NAME = "test-view-source";
+
+const RELATIVE_ROOT = "../shared-modules";
+const MODULE_REQUIRES = ["folder-display-helpers", "window-helpers"];
+
+const elib = {};
+Components.utils.import("resource://mozmill/modules/elementslib.js", elib);
+
+Components.utils.import("resource://gre/modules/Services.jsm");
+
+var folder = null;
+
+function setupModule(module) {
+  for (let dep of MODULE_REQUIRES) {
+    collector.getModule(dep).installInto(module);
+  }
+
+  folder = create_folder("viewsource");
+
+  // Skip on mac, as we can't click the (native) menus to make it work.
+  if (mc.mozmillModule.isMac)
+    test_view_source_reload.__force_skip__ = true;
+}
+
+function addToFolder(aSubject, aBody, aFolder) {
+  let msgId = Components.classes["@mozilla.org/uuid-generator;1"]
+                          .getService(Components.interfaces.nsIUUIDGenerator)
+                          .generateUUID() +"@invalid";
+
+  let source = "From - Sat Nov  1 12:39:54 2008\n" +
+               "X-Mozilla-Status: 0001\n" +
+               "X-Mozilla-Status2: 00000000\n" +
+               "Message-ID: <" + msgId + ">\n" +
+               "Date: Wed, 11 Jun 2008 20:32:02 -0400\n" +
+               "From: Tester <tests@mozillamessaging.invalid>\n" +
+               "MIME-Version: 1.0\n" +
+               "To: anna@example.com\n" +
+               "Subject: " + aSubject + "\n" +
+               "Content-Type: text/plain; charset=ISO-8859-1\n" +
+               "Content-Transfer-Encoding: 7bit\n" +
+               "\n" + aBody + "\n";
+
+  aFolder.QueryInterface(Components.interfaces.nsIMsgLocalMailFolder);
+  aFolder.addMessage(source);
+
+  return aFolder.msgDatabase.getMsgHdrForMessageID(msgId);
+}
+
+/**
+ * Test that the view source character encoding can be changed,
+ * which requires content policy is correct for view-source:.
+ */
+function test_view_source_reload() {
+  be_in_folder(folder);
+
+  let contentLatin1 = "Testar, ett två tre.";
+  let contentUTF8 = "Testar, ett tv� tre";
+  let msg = addToFolder("view-source reload test123?", contentLatin1, folder);
+
+  let selMsg = select_click_row(0);
+  assert_true(msg == selMsg, "Selected msg isn't the same as the generated one.");
+
+  plan_for_new_window("navigator:view-source");
+  mc.keypress(null, "U", {shiftKey: false, accelKey: true});
+  let vsc = wait_for_new_window("navigator:view-source");
+
+  vsc.waitFor(function() vsc.e("content").contentDocument.querySelector("pre") != null,
+              "Timeout waiting for the latin1 view-source document to load.");
+
+  let source = vsc.e("content").contentDocument.querySelector("pre").textContent;
+  if (!source.contains(contentLatin1))
+    throw new Error("View source didn't contain the latin1 text;\n" +
+                    contentLatin1 + "\n" + source);
+
+  let doc = vsc.e("content").contentDocument; // keep a ref to the latin1 doc
+
+  vsc.click_menus_in_sequence(vsc.e("viewmenu-popup"),
+    [{id: "charsetMenu"}, {label: "Unicode (UTF-8)"}]);
+
+  vsc.waitFor(function() vsc.e("content").contentDocument != doc &&
+                         vsc.e("content").contentDocument.querySelector("pre") != null,
+              "Timeout waiting utf-8 encoded view-source document to load.");
+
+  source = vsc.e("content").contentDocument.querySelector("pre").textContent;
+  if (!source.contains(contentUTF8))
+    throw new Error("View source didn't contain the utf-8 text;\n" + 
+                    contentUTF8 + "\n" + source);
+
+  close_window(vsc);
+}
+
--- a/mailnews/base/src/nsMsgContentPolicy.cpp
+++ b/mailnews/base/src/nsMsgContentPolicy.cpp
@@ -331,42 +331,44 @@ nsMsgContentPolicy::ShouldLoad(uint32_t 
  * app/user's control - so file, about, chrome etc.
  */
 bool
 nsMsgContentPolicy::IsSafeRequestingLocation(nsIURI *aRequestingLocation)
 {
   if (!aRequestingLocation)
     return false;
 
-  // if aRequestingLocation is chrome, resource about or file, allow
-  // aContentLocation to load
+  // If aRequestingLocation is one of chrome, resource, file or view-source,
+  // allow aContentLocation to load.
   bool isChrome;
   bool isRes;
-  bool isAbout;
   bool isFile;
+  bool isViewSource;
 
   nsresult rv = aRequestingLocation->SchemeIs("chrome", &isChrome);
   NS_ENSURE_SUCCESS(rv, false);
   rv = aRequestingLocation->SchemeIs("resource", &isRes);
   NS_ENSURE_SUCCESS(rv, false);
   rv = aRequestingLocation->SchemeIs("file", &isFile);
   NS_ENSURE_SUCCESS(rv, false);
+  rv = aRequestingLocation->SchemeIs("view-source", &isViewSource);
+  NS_ENSURE_SUCCESS(rv, false);
 
-  if (isChrome || isRes || isFile)
+  if (isChrome || isRes || isFile || isViewSource)
     return true;
 
   // Only allow about: to load anything if the requesting location is not the
   // special about:blank one.
+  bool isAbout;
   rv = aRequestingLocation->SchemeIs("about", &isAbout);
   NS_ENSURE_SUCCESS(rv, false);
 
   if (!isAbout)
     return false;
 
-
   nsCString fullSpec;
   rv = aRequestingLocation->GetSpec(fullSpec);
   NS_ENSURE_SUCCESS(rv, false);
 
   return !fullSpec.EqualsLiteral("about:blank");
 }
 
 /**