browser/metro/base/tests/mochitest/browser_urlbar_highlightURLs.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/tests/mochitest/browser_urlbar_highlightURLs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,171 @@
     1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* Any copyright is dedicated to the Public Domain.
     1.7 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.8 +
     1.9 +"use strict";
    1.10 +
    1.11 +const kHighlightPref = "browser.urlbar.formatting.enabled";
    1.12 +var gHighlightPrefValue;
    1.13 +
    1.14 +function test() {
    1.15 +  runTests();
    1.16 +}
    1.17 +
    1.18 +function setUp() {
    1.19 +  gHighlightPrefValue = SpecialPowers.getBoolPref(kHighlightPref);
    1.20 +  SpecialPowers.setBoolPref(kHighlightPref, true);
    1.21 +  yield addTab("about:blank");
    1.22 +}
    1.23 +
    1.24 +function tearDown() {
    1.25 +  SpecialPowers.setBoolPref(kHighlightPref, gHighlightPrefValue);
    1.26 +  Browser.closeTab(Browser.selectedTab, { forceClose: true });
    1.27 +}
    1.28 +
    1.29 +function testHighlight(aExpected) {
    1.30 +  let urlbar = BrowserUI._edit;
    1.31 +  urlbar.value = aExpected.replace(/[<>]/g, "");
    1.32 +
    1.33 +  let selectionController = urlbar.editor.selectionController;
    1.34 +  let selection = selectionController.getSelection(selectionController.SELECTION_URLSECONDARY);
    1.35 +  let value = urlbar.editor.rootElement.textContent;
    1.36 +
    1.37 +  let result = "";
    1.38 +  for (let i = 0; i < selection.rangeCount; i++) {
    1.39 +    let range = selection.getRangeAt(i).toString();
    1.40 +    let pos = value.indexOf(range);
    1.41 +    result += value.substring(0, pos) + "<" + range + ">";
    1.42 +    value = value.substring(pos + range.length);
    1.43 +  }
    1.44 +  result += value;
    1.45 +  is(result, aExpected, "test highight");
    1.46 +}
    1.47 +
    1.48 +gTests.push({
    1.49 +  desc: "Domain-based URIs (not in editing mode)",
    1.50 +  setUp: setUp,
    1.51 +  tearDown: tearDown,
    1.52 +  run: function () {
    1.53 +    let testcases = [
    1.54 +      "<https://>mozilla.org",
    1.55 +      "<https://>mözilla.org",
    1.56 +      "<https://>mozilla.imaginatory",
    1.57 +
    1.58 +      "<https://www.>mozilla.org",
    1.59 +      "<https://sub.>mozilla.org",
    1.60 +      "<https://sub1.sub2.sub3.>mozilla.org",
    1.61 +      "<www.>mozilla.org",
    1.62 +      "<sub.>mozilla.org",
    1.63 +      "<sub1.sub2.sub3.>mozilla.org",
    1.64 +
    1.65 +      "<http://ftp.>mozilla.org",
    1.66 +      "<ftp://ftp.>mozilla.org",
    1.67 +
    1.68 +      "<https://sub.>mozilla.org",
    1.69 +      "<https://sub1.sub2.sub3.>mozilla.org",
    1.70 +      "<https://user:pass@sub1.sub2.sub3.>mozilla.org",
    1.71 +      "<https://user:pass@>mozilla.org",
    1.72 +
    1.73 +      "<https://>mozilla.org</file.ext>",
    1.74 +      "<https://>mozilla.org</sub/file.ext>",
    1.75 +      "<https://>mozilla.org</sub/file.ext?foo>",
    1.76 +      "<https://>mozilla.org</sub/file.ext?foo&bar>",
    1.77 +      "<https://>mozilla.org</sub/file.ext?foo&bar#top>",
    1.78 +      "<https://>mozilla.org</sub/file.ext?foo&bar#top>",
    1.79 +
    1.80 +      "<https://sub.>mozilla.org<:666/file.ext>",
    1.81 +      "<sub.>mozilla.org<:666/file.ext>",
    1.82 +      "localhost<:666/file.ext>",
    1.83 +
    1.84 +      "mailto:admin@mozilla.org",
    1.85 +      "gopher://mozilla.org/",
    1.86 +      "about:config",
    1.87 +      "jar:http://mozilla.org/example.jar!/",
    1.88 +      "view-source:http://mozilla.org/",
    1.89 +      "foo9://mozilla.org/",
    1.90 +      "foo+://mozilla.org/",
    1.91 +      "foo.://mozilla.org/",
    1.92 +      "foo-://mozilla.org/"
    1.93 +    ];
    1.94 +
    1.95 +    testcases.forEach(testHighlight);
    1.96 +  }
    1.97 +});
    1.98 +
    1.99 +gTests.push({
   1.100 +  desc: "IP-based URIs (not in editing mode)",
   1.101 +  setUp: setUp,
   1.102 +  tearDown: tearDown,
   1.103 +  run: function () {
   1.104 +    let ips = [
   1.105 +      "192.168.1.1",
   1.106 +      "[::]",
   1.107 +      "[::1]",
   1.108 +      "[1::]",
   1.109 +      "[::]",
   1.110 +      "[::1]",
   1.111 +      "[1::]",
   1.112 +      "[1:2:3:4:5:6:7::]",
   1.113 +      "[::1:2:3:4:5:6:7]",
   1.114 +      "[1:2:a:B:c:D:e:F]",
   1.115 +      "[1::8]",
   1.116 +      "[1:2::8]",
   1.117 +      "[fe80::222:19ff:fe11:8c76]",
   1.118 +      "[0000:0123:4567:89AB:CDEF:abcd:ef00:0000]",
   1.119 +      "[::192.168.1.1]",
   1.120 +      "[1::0.0.0.0]",
   1.121 +      "[1:2::255.255.255.255]",
   1.122 +      "[1:2:3::255.255.255.255]",
   1.123 +      "[1:2:3:4::255.255.255.255]",
   1.124 +      "[1:2:3:4:5::255.255.255.255]",
   1.125 +      "[1:2:3:4:5:6:255.255.255.255]"
   1.126 +    ];
   1.127 +
   1.128 +    let formats = [
   1.129 +      "{ip}</file.ext>",
   1.130 +      "{ip}<:666/file.ext>",
   1.131 +      "<https://>{ip}",
   1.132 +      "<https://>{ip}</file.ext>",
   1.133 +      "<https://user:pass@>{ip}<:666/file.ext>",
   1.134 +      "<http://user:pass@>{ip}<:666/file.ext>"
   1.135 +    ];
   1.136 +
   1.137 +    function testHighlightAllFormats(aIP) {
   1.138 +      formats.forEach((aFormat) => testHighlight(aFormat.replace("{ip}", aIP)));
   1.139 +    }
   1.140 +
   1.141 +    ips.forEach(testHighlightAllFormats);
   1.142 +  }
   1.143 +});
   1.144 +
   1.145 +gTests.push({
   1.146 +  desc: "no highlighting (in editing mode)",
   1.147 +  setUp: setUp,
   1.148 +  tearDown: tearDown,
   1.149 +  run: function () {
   1.150 +    testHighlight("<https://>mozilla.org");
   1.151 +
   1.152 +    BrowserUI._edit.focus();
   1.153 +    testHighlight("https://mozilla.org");
   1.154 +
   1.155 +    Browser.selectedBrowser.focus();
   1.156 +    testHighlight("<https://>mozilla.org");
   1.157 +  }
   1.158 +});
   1.159 +
   1.160 +gTests.push({
   1.161 +  desc: "no higlighting (pref disabled)",
   1.162 +  setUp: setUp,
   1.163 +  tearDown: tearDown,
   1.164 +  run: function () {
   1.165 +    testHighlight("<https://>mozilla.org");
   1.166 +
   1.167 +    SpecialPowers.setBoolPref(kHighlightPref, false);
   1.168 +    testHighlight("https://mozilla.org");
   1.169 +
   1.170 +    SpecialPowers.setBoolPref(kHighlightPref, true);
   1.171 +    testHighlight("<https://>mozilla.org");
   1.172 +  }
   1.173 +});
   1.174 +

mercurial