toolkit/components/prompts/test/test_modal_select.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/prompts/test/test_modal_select.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,192 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Modal Prompts Test</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <script type="text/javascript" src="prompt_common.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body>
    1.13 +Prompter tests: modal prompts
    1.14 +<p id="display"></p>
    1.15 +
    1.16 +<div id="content" style="display: none">
    1.17 +  <iframe id="iframe"></iframe>
    1.18 +</div>
    1.19 +
    1.20 +<pre id="test">
    1.21 +<script class="testbody" type="text/javascript;version=1.8">
    1.22 +
    1.23 +let prompter = Cc["@mozilla.org/embedcomp/prompt-service;1"].
    1.24 +               getService(Ci.nsIPromptService2);
    1.25 +let ioService = Cc["@mozilla.org/network/io-service;1"].
    1.26 +                getService(Ci.nsIIOService);
    1.27 +let pollTimer;
    1.28 +
    1.29 +function pollDialog(dialog) {
    1.30 +    if (dialog.getButton("accept").disabled)
    1.31 +        return;
    1.32 +
    1.33 +    ok(true, "dialog button is enabled now");
    1.34 +    pollTimer.cancel();
    1.35 +    pollTimer = null;
    1.36 +    dialog.acceptDialog();
    1.37 +    didDialog = true;
    1.38 +}
    1.39 +
    1.40 +function checkExpectedSelectState(doc, state) {
    1.41 +    let msg = doc.getElementById("info.txt").value;
    1.42 +    // XXX check title? OS X has title in content
    1.43 +    let listbox = doc.getElementById("list");
    1.44 +
    1.45 +    is(msg, state.msg, "Checking expected message");
    1.46 +    // XXX check title? OS X has title in content
    1.47 +    // Compare listbox contents
    1.48 +    let count = listbox.itemCount;
    1.49 +    is(count, state.items.length, "Checking listbox length");
    1.50 +    if (count)
    1.51 +        is(listbox.selectedIndex, 0, "Checking selected index");
    1.52 +
    1.53 +    for (let i = 0; i < count; i++) {
    1.54 +        let item = listbox.getItemAtIndex(i).label;
    1.55 +        is(item, items[i], "Checking item #" + i + " label");
    1.56 +    }
    1.57 +}
    1.58 +
    1.59 +/*
    1.60 + * handleDialog
    1.61 + *
    1.62 + * Invoked a short period of time after calling startCallbackTimer(), and
    1.63 + * allows testing the actual prompt dialog while it's being displayed. Tests
    1.64 + * should call startCallbackTimer() each time the auth dialog is expected (the
    1.65 + * timer is a one-shot).
    1.66 + */
    1.67 +function handleDialog(doc, testNum) {
    1.68 +    ok(true, "--- handleDialog for test " + testNum + " ---");
    1.69 +
    1.70 +    let dialog    = doc.getElementsByTagName("dialog")[0];
    1.71 +    let listbox   = doc.getElementById("list");
    1.72 +    let clickOK   = true;
    1.73 +    let state;
    1.74 +
    1.75 +    // XXX check focused element
    1.76 +    // XXX check text/passbox labels?
    1.77 +    // XXX check button labels?
    1.78 +
    1.79 +    switch(testNum) {
    1.80 +      case 1:
    1.81 +        // Select (0 items)
    1.82 +        state = {
    1.83 +            msg   : "This is the select text.",
    1.84 +            title : "TestTitle",
    1.85 +            items : [],
    1.86 +        };
    1.87 +        checkExpectedSelectState(doc, state);
    1.88 +        break;
    1.89 +
    1.90 +      case 2:
    1.91 +        // Select (3 items, default ok)
    1.92 +        state = {
    1.93 +            msg   : "This is the select text.",
    1.94 +            title : "TestTitle",
    1.95 +            items : ["one", "two", "three"],
    1.96 +        };
    1.97 +        checkExpectedSelectState(doc, state);
    1.98 +        break;
    1.99 +
   1.100 +      case 3:
   1.101 +        // Select (3 items, change selection, ok)
   1.102 +        state = {
   1.103 +            msg   : "This is the select text.",
   1.104 +            title : "TestTitle",
   1.105 +            items : ["one", "two", "three"],
   1.106 +        };
   1.107 +        checkExpectedSelectState(doc, state);
   1.108 +        // XXX need to trigger old code's click listener
   1.109 +        listbox.selectedIndex = 1;
   1.110 +        //listbox.getItemAtIndex(1).click();
   1.111 +        break;
   1.112 +
   1.113 +      case 4:
   1.114 +        // Select (3 items, cancel)
   1.115 +        state = {
   1.116 +            msg   : "This is the select text.",
   1.117 +            title : "TestTitle",
   1.118 +            items : ["one", "two", "three"],
   1.119 +        };
   1.120 +        checkExpectedSelectState(doc, state);
   1.121 +        clickOK = false;
   1.122 +        break;
   1.123 +
   1.124 +      default:
   1.125 +        ok(false, "Uhh, unhandled switch for testNum #" + testNum);
   1.126 +        break;
   1.127 +    }
   1.128 +
   1.129 +
   1.130 +    if (clickOK)
   1.131 +        dialog.acceptDialog();
   1.132 +    else
   1.133 +        dialog.cancelDialog();
   1.134 +
   1.135 +    ok(true, "handleDialog done");
   1.136 +    didDialog = true;
   1.137 +}
   1.138 +
   1.139 +let testNum   = 0;
   1.140 +let selectVal = {};
   1.141 +let isOK;
   1.142 +
   1.143 +isSelectDialog = true;
   1.144 +isTabModal = false;
   1.145 +usePromptService = true;
   1.146 +
   1.147 +// ===== test 1 =====
   1.148 +// Select (0 items, ok)
   1.149 +testNum++;
   1.150 +startCallbackTimer();
   1.151 +items = [];
   1.152 +selectVal.value = null; // outparam, just making sure.
   1.153 +isOK = prompter.select(window, "TestTitle", "This is the select text.", items.length, items, selectVal);
   1.154 +is(isOK, true, "checked expected retval");
   1.155 +is(selectVal.value, -1, "checking selected index");
   1.156 +ok(didDialog, "handleDialog was invoked");
   1.157 +
   1.158 +// ===== test 2 =====
   1.159 +// Select (3 items, ok)
   1.160 +testNum++;
   1.161 +startCallbackTimer();
   1.162 +items = ["one", "two", "three"];
   1.163 +selectVal.value = null; // outparam, just making sure.
   1.164 +isOK = prompter.select(window, "TestTitle", "This is the select text.", items.length, items, selectVal);
   1.165 +is(isOK, true, "checked expected retval");
   1.166 +is(selectVal.value, 0, "checking selected index");
   1.167 +ok(didDialog, "handleDialog was invoked");
   1.168 +
   1.169 +// ===== test 3 =====
   1.170 +// Select (3 items, selection changed, ok)
   1.171 +testNum++;
   1.172 +startCallbackTimer();
   1.173 +items = ["one", "two", "three"];
   1.174 +selectVal.value = null; // outparam, just making sure.
   1.175 +isOK = prompter.select(window, "TestTitle", "This is the select text.", items.length, items, selectVal);
   1.176 +is(isOK, true, "checked expected retval");
   1.177 +is(selectVal.value, 1, "checking selected index");
   1.178 +ok(didDialog, "handleDialog was invoked");
   1.179 +
   1.180 +// ===== test 4 =====
   1.181 +// Select (3 items, cancel)
   1.182 +testNum++;
   1.183 +startCallbackTimer();
   1.184 +items = ["one", "two", "three"];
   1.185 +selectVal.value = null; // outparam, just making sure.
   1.186 +isOK = prompter.select(window, "TestTitle", "This is the select text.", items.length, items, selectVal);
   1.187 +is(isOK, false, "checked expected retval");
   1.188 +is(selectVal.value, 0, "checking selected index");
   1.189 +ok(didDialog, "handleDialog was invoked");
   1.190 +
   1.191 +
   1.192 +</script>
   1.193 +</pre>
   1.194 +</body>
   1.195 +</html>

mercurial