toolkit/content/tests/chrome/test_textbox_search.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/tests/chrome/test_textbox_search.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
     1.7 +<!--
     1.8 +  XUL Widget Test for search textbox
     1.9 +  -->
    1.10 +<window title="Search textbox test" width="500" height="600"
    1.11 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.12 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.13 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    1.14 +
    1.15 +  <hbox>
    1.16 +    <textbox id="searchbox"
    1.17 +             type="search"
    1.18 +             oncommand="doSearch(this.value);"
    1.19 +             placeholder="random placeholder"
    1.20 +             timeout="1"/>
    1.21 +  </hbox>
    1.22 +
    1.23 +  <!-- test results are displayed in the html:body -->
    1.24 +  <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>
    1.25 +
    1.26 +  <!-- test code goes here -->
    1.27 +  <script type="application/javascript"><![CDATA[
    1.28 +
    1.29 +SimpleTest.waitForExplicitFinish();
    1.30 +
    1.31 +var gExpectedValue;
    1.32 +var gLastTest;
    1.33 +
    1.34 +function doTests() {
    1.35 +  var textbox = $("searchbox");
    1.36 +  var icons = document.getAnonymousElementByAttribute(textbox, "anonid", "search-icons");
    1.37 +  var searchIcon = document.getAnonymousElementByAttribute(textbox, "class", "textbox-search-icon");
    1.38 +  var clearIcon = document.getAnonymousElementByAttribute(textbox, "class", "textbox-search-clear");
    1.39 +
    1.40 +  ok(icons, "icon deck found");
    1.41 +  ok(searchIcon, "search icon found");
    1.42 +  ok(clearIcon, "clear icon found");
    1.43 +  is(icons.selectedPanel, searchIcon, "search icon is displayed");
    1.44 +
    1.45 +  is(textbox.placeholder, "random placeholder", "search textbox supports placeholder");
    1.46 +  is(textbox.value, "", "placeholder doesn't interfere with the real value");
    1.47 +
    1.48 +  function iconClick(aIcon) {
    1.49 +    is(icons.selectedPanel, aIcon, aIcon.className + " icon must be displayed in order to be clickable");
    1.50 +
    1.51 +    //XXX synthesizeMouse worked on Linux but failed on Windows an Mac
    1.52 +    //    for unknown reasons. Manually dispatch the event for now.
    1.53 +    //synthesizeMouse(aIcon, 0, 0, {});
    1.54 +
    1.55 +    var event = document.createEvent("MouseEvent");
    1.56 +    event.initMouseEvent("click", true, true, window, 1,
    1.57 +                         0, 0, 0, 0,
    1.58 +                         false, false, false, false,
    1.59 +                         0, null);
    1.60 +    aIcon.dispatchEvent(event);
    1.61 +  }
    1.62 +
    1.63 +  iconClick(searchIcon);
    1.64 +  is(textbox.getAttribute("focused"), "true", "clicking the search icon focuses the textbox");
    1.65 +
    1.66 +  textbox.value = "foo";
    1.67 +  is(icons.selectedPanel, clearIcon, "clear icon is displayed when setting a value");
    1.68 +
    1.69 +  textbox.reset();
    1.70 +  is(textbox.defaultValue, "", "defaultValue is empty");
    1.71 +  is(textbox.value, "", "reset method clears the textbox");
    1.72 +  is(icons.selectedPanel, searchIcon, "search icon is displayed after textbox.reset()");
    1.73 +
    1.74 +  textbox.value = "foo";
    1.75 +  gExpectedValue = "";
    1.76 +  iconClick(clearIcon);
    1.77 +  is(textbox.value, "", "clicking the clear icon clears the textbox");
    1.78 +  ok(gExpectedValue == null, "search triggered when clearing the textbox with the clear icon");
    1.79 +
    1.80 +  textbox.value = "foo";
    1.81 +  gExpectedValue = "";
    1.82 +  synthesizeKey("VK_ESCAPE", {});
    1.83 +  is(textbox.value, "", "escape key clears the textbox");
    1.84 +  ok(gExpectedValue == null, "search triggered when clearing the textbox with the escape key");
    1.85 +
    1.86 +  textbox.value = "bar";
    1.87 +  gExpectedValue = "bar";
    1.88 +  textbox.doCommand();
    1.89 +  ok(gExpectedValue == null, "search triggered with doCommand");
    1.90 +
    1.91 +  gExpectedValue = "bar";
    1.92 +  synthesizeKey("VK_RETURN", {});
    1.93 +  ok(gExpectedValue == null, "search triggered with enter key");
    1.94 +
    1.95 +  textbox.value = "";
    1.96 +  textbox.searchButton = true;
    1.97 +  is(textbox.getAttribute("searchbutton"), "true", "searchbutton attribute set on the textbox");
    1.98 +  is(searchIcon.getAttribute("searchbutton"), "true", "searchbutton attribute inherited to the search icon");
    1.99 +
   1.100 +  textbox.value = "foo";
   1.101 +  is(icons.selectedPanel, searchIcon, "search icon displayed in search button mode if there's a value");
   1.102 +
   1.103 +  gExpectedValue = "foo";
   1.104 +  iconClick(searchIcon);
   1.105 +  ok(gExpectedValue == null, "search triggered when clicking the search icon in search button mode");
   1.106 +  is(icons.selectedPanel, clearIcon, "clear icon displayed in search button mode after submitting");
   1.107 +
   1.108 +  synthesizeKey("o", {});
   1.109 +  is(icons.selectedPanel, searchIcon, "search icon displayed in search button mode when typing a key");
   1.110 +
   1.111 +  gExpectedValue = "fooo";
   1.112 +  iconClick(searchIcon); // display the clear icon (tested above)
   1.113 +
   1.114 +  textbox.value = "foo";
   1.115 +  is(icons.selectedPanel, searchIcon, "search icon displayed in search button mode when the value is changed");
   1.116 +
   1.117 +  gExpectedValue = "foo";
   1.118 +  synthesizeKey("VK_RETURN", {});
   1.119 +  ok(gExpectedValue == null, "search triggered with enter key in search button mode");
   1.120 +  is(icons.selectedPanel, clearIcon, "clear icon displayed in search button mode after submitting with enter key");
   1.121 +
   1.122 +  textbox.value = "x";
   1.123 +  synthesizeKey("VK_BACK_SPACE", {});
   1.124 +  is(icons.selectedPanel, searchIcon, "search icon displayed in search button mode when deleting the value with the backspace key");
   1.125 +
   1.126 +  gExpectedValue = "";
   1.127 +  synthesizeKey("VK_RETURN", {});
   1.128 +  ok(gExpectedValue == null, "search triggered with enter key in search button mode");
   1.129 +  is(icons.selectedPanel, searchIcon, "search icon displayed in search button mode after submitting an empty string");
   1.130 +
   1.131 +  textbox.readOnly = true;
   1.132 +  gExpectedValue = "foo";
   1.133 +  textbox.value = "foo";
   1.134 +  iconClick(searchIcon);
   1.135 +  ok(gExpectedValue == null, "search triggered when clicking the search icon in search button mode while the textbox is read-only");
   1.136 +  is(icons.selectedPanel, searchIcon, "search icon persists in search button mode after submitting while the textbox is read-only");
   1.137 +  textbox.readOnly = false;
   1.138 +
   1.139 +  textbox.disabled = true;
   1.140 +  is(searchIcon.getAttribute("disabled"), "true", "disabled attribute inherited to the search icon");
   1.141 +  is(clearIcon.getAttribute("disabled"), "true", "disabled attribute inherited to the clear icon");
   1.142 +  gExpectedValue = false;
   1.143 +  textbox.value = "foo";
   1.144 +  iconClick(searchIcon);
   1.145 +  ok(gExpectedValue == false, "search *not* triggered when clicking the search icon in search button mode while the textbox is disabled");
   1.146 +  is(icons.selectedPanel, searchIcon, "search icon persists in search button mode when trying to submit while the textbox is disabled");
   1.147 +  textbox.disabled = false;
   1.148 +  ok(!searchIcon.hasAttribute("disabled"), "disabled attribute removed from the search icon");
   1.149 +  ok(!clearIcon.hasAttribute("disabled"), "disabled attribute removed from the clear icon");
   1.150 +
   1.151 +  textbox.searchButton = false;
   1.152 +  ok(!textbox.hasAttribute("searchbutton"), "searchbutton attribute removed from the textbox");
   1.153 +  ok(!searchIcon.hasAttribute("searchbutton"), "searchbutton attribute removed from the search icon");
   1.154 +
   1.155 +  gLastTest = true;
   1.156 +  gExpectedValue = "123";
   1.157 +  textbox.value = "1";
   1.158 +  synthesizeKey("2", {});
   1.159 +  synthesizeKey("3", {});
   1.160 +}
   1.161 +
   1.162 +function doSearch(aValue) {
   1.163 +  is(aValue, gExpectedValue, "search triggered with expected value");
   1.164 +  gExpectedValue = null;
   1.165 +  if (gLastTest)
   1.166 +    SimpleTest.finish();
   1.167 +}
   1.168 +
   1.169 +SimpleTest.waitForFocus(doTests);
   1.170 +
   1.171 +  ]]></script>
   1.172 +
   1.173 +</window>

mercurial