toolkit/content/tests/unit/test_contentAreaUtils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 const Ci = Components.interfaces;
     7 const Cc = Components.classes;
     8 const Cr = Components.results;
    10 function loadUtilsScript() {
    11   var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
    12                getService(Ci.mozIJSSubScriptLoader);
    13   loader.loadSubScript("chrome://global/content/contentAreaUtils.js");
    14 }
    16 function test_urlSecurityCheck() {
    17   var nullPrincipal = Cc["@mozilla.org/nullprincipal;1"].
    18                       createInstance(Ci.nsIPrincipal);
    20   const HTTP_URI = "http://www.mozilla.org/";
    21   const CHROME_URI = "chrome://browser/content/browser.xul";
    22   const DISALLOW_INHERIT_PRINCIPAL =
    23     Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL;
    25   try {
    26     urlSecurityCheck(makeURI(HTTP_URI), nullPrincipal,
    27                      DISALLOW_INHERIT_PRINCIPAL);
    28   }
    29   catch(ex) {
    30     do_throw("urlSecurityCheck should not throw when linking to a http uri with a null principal");
    31   }
    33   // urlSecurityCheck also supports passing the url as a string
    34   try {
    35     urlSecurityCheck(HTTP_URI, nullPrincipal,
    36                      DISALLOW_INHERIT_PRINCIPAL);
    37   }
    38   catch(ex) {
    39     do_throw("urlSecurityCheck failed to handle the http URI as a string (uri spec)");
    40   }
    42   let shouldThrow = true;
    43   try {
    44     urlSecurityCheck(CHROME_URI, nullPrincipal,
    45                      DISALLOW_INHERIT_PRINCIPAL);
    46   }
    47   catch(ex) { 
    48     shouldThrow = false;
    49   }
    50   if (shouldThrow)
    51     do_throw("urlSecurityCheck should throw when linking to a chrome uri with a null principal");
    52 }
    54 function test_stringBundle() {
    55   // This test verifies that the elements that can be used as file picker title
    56   //  keys in the save* functions are actually present in the string bundle.
    57   //  These keys are part of the contentAreaUtils.js public API.
    58   var validFilePickerTitleKeys = [
    59     "SaveImageTitle",
    60     "SaveVideoTitle",
    61     "SaveAudioTitle",
    62     "SaveLinkTitle",
    63   ];
    65   for (let [, filePickerTitleKey] in Iterator(validFilePickerTitleKeys)) {
    66     // Just check that the string exists
    67     try {
    68       ContentAreaUtils.stringBundle.GetStringFromName(filePickerTitleKey);
    69     } catch (e) {
    70       do_throw("Error accessing file picker title key: " + filePickerTitleKey);
    71     }
    72   }
    73 }
    75 function run_test()
    76 {
    77   loadUtilsScript();
    78   test_urlSecurityCheck();
    79   test_stringBundle();
    80 }

mercurial