1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/content/tests/unit/test_contentAreaUtils.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +const Ci = Components.interfaces; 1.10 +const Cc = Components.classes; 1.11 +const Cr = Components.results; 1.12 + 1.13 +function loadUtilsScript() { 1.14 + var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]. 1.15 + getService(Ci.mozIJSSubScriptLoader); 1.16 + loader.loadSubScript("chrome://global/content/contentAreaUtils.js"); 1.17 +} 1.18 + 1.19 +function test_urlSecurityCheck() { 1.20 + var nullPrincipal = Cc["@mozilla.org/nullprincipal;1"]. 1.21 + createInstance(Ci.nsIPrincipal); 1.22 + 1.23 + const HTTP_URI = "http://www.mozilla.org/"; 1.24 + const CHROME_URI = "chrome://browser/content/browser.xul"; 1.25 + const DISALLOW_INHERIT_PRINCIPAL = 1.26 + Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL; 1.27 + 1.28 + try { 1.29 + urlSecurityCheck(makeURI(HTTP_URI), nullPrincipal, 1.30 + DISALLOW_INHERIT_PRINCIPAL); 1.31 + } 1.32 + catch(ex) { 1.33 + do_throw("urlSecurityCheck should not throw when linking to a http uri with a null principal"); 1.34 + } 1.35 + 1.36 + // urlSecurityCheck also supports passing the url as a string 1.37 + try { 1.38 + urlSecurityCheck(HTTP_URI, nullPrincipal, 1.39 + DISALLOW_INHERIT_PRINCIPAL); 1.40 + } 1.41 + catch(ex) { 1.42 + do_throw("urlSecurityCheck failed to handle the http URI as a string (uri spec)"); 1.43 + } 1.44 + 1.45 + let shouldThrow = true; 1.46 + try { 1.47 + urlSecurityCheck(CHROME_URI, nullPrincipal, 1.48 + DISALLOW_INHERIT_PRINCIPAL); 1.49 + } 1.50 + catch(ex) { 1.51 + shouldThrow = false; 1.52 + } 1.53 + if (shouldThrow) 1.54 + do_throw("urlSecurityCheck should throw when linking to a chrome uri with a null principal"); 1.55 +} 1.56 + 1.57 +function test_stringBundle() { 1.58 + // This test verifies that the elements that can be used as file picker title 1.59 + // keys in the save* functions are actually present in the string bundle. 1.60 + // These keys are part of the contentAreaUtils.js public API. 1.61 + var validFilePickerTitleKeys = [ 1.62 + "SaveImageTitle", 1.63 + "SaveVideoTitle", 1.64 + "SaveAudioTitle", 1.65 + "SaveLinkTitle", 1.66 + ]; 1.67 + 1.68 + for (let [, filePickerTitleKey] in Iterator(validFilePickerTitleKeys)) { 1.69 + // Just check that the string exists 1.70 + try { 1.71 + ContentAreaUtils.stringBundle.GetStringFromName(filePickerTitleKey); 1.72 + } catch (e) { 1.73 + do_throw("Error accessing file picker title key: " + filePickerTitleKey); 1.74 + } 1.75 + } 1.76 +} 1.77 + 1.78 +function run_test() 1.79 +{ 1.80 + loadUtilsScript(); 1.81 + test_urlSecurityCheck(); 1.82 + test_stringBundle(); 1.83 +}