summaryrefslogtreecommitdiffstats
path: root/toolkit/components/prompts/test/test_modal_select.html
blob: 1e008d0f449514ed1d135e9eaaa2ee09e670a51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE HTML>
<html>
<head>
  <title>Modal Prompts Test</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="prompt_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Prompter tests: modal prompts
<p id="display"></p>

<div id="content" style="display: none">
  <iframe id="iframe"></iframe>
</div>

<pre id="test">
<script class="testbody" type="text/javascript;version=1.8">

let prompter = Cc["@mozilla.org/embedcomp/prompt-service;1"].
               getService(Ci.nsIPromptService2);

function checkPromptState(promptState, expectedState) {
    // XXX check title? OS X has title in content
    // XXX check focused element
    // XXX check button labels?

    is(promptState.msg, expectedState.msg, "Checking expected message");

    // Compare listbox contents
    is(promptState.items.length, expectedState.items.length, "Checking listbox length");

    if (promptState.items.length)
      is(promptState.selectedIndex, 0, "Checking selected index");

    for (let i = 0; i < promptState.items; i++) {
        is(promptState.items[i], expectedState.items[i], "Checking list item #" + i);
    }
}

let selectVal = {};
let isOK;
let state, action;

function handlePrompt(state, action) {
  return new Promise(resolve => {
    gChromeScript.addMessageListener("promptHandled", function handled(msg) {
      gChromeScript.removeMessageListener("promptHandled", handled);
      checkPromptState(msg.promptState, state);
      resolve(true);
    });
    gChromeScript.sendAsyncMessage("handlePrompt", { action: action, isSelect: true});
  });
}


// =====
add_task(function* test_select_empty_list() {
    info("Starting test: Select (0 items, ok)");
    state = {
        msg   : "This is the select text.",
        title : "TestTitle",
        items : [],
    };
    action = {
        buttonClick: "ok",
    };
    promptDone = handlePrompt(state, action);
    items = [];
    selectVal.value = null; // outparam, just making sure.
    isOK = prompter.select(window, "TestTitle", "This is the select text.", items.length, items, selectVal);
    is(isOK, true, "checked expected retval");
    is(selectVal.value, -1, "checking selected index");

    yield promptDone;
});

// =====
add_task(function* test_select_ok() {
    info("Starting test: Select (3 items, ok)");
    state = {
        msg   : "This is the select text.",
        title : "TestTitle",
        items : ["one", "two", "three"],
    };
    action = {
        buttonClick: "ok",
    };
    promptDone = handlePrompt(state, action);
    items = ["one", "two", "three"];
    selectVal.value = null; // outparam, just making sure.
    isOK = prompter.select(window, "TestTitle", "This is the select text.", items.length, items, selectVal);
    is(isOK, true, "checked expected retval");
    is(selectVal.value, 0, "checking selected index");

    yield promptDone;
});

// =====
add_task(function* test_select_item() {
    info("Starting test: Select (3 items, selection changed, ok)");
    state = {
        msg   : "This is the select text.",
        title : "TestTitle",
        items : ["one", "two", "three"],
    };
    action = {
        buttonClick: "ok",
        selectItem: 1,
    };
    promptDone = handlePrompt(state, action);
    items = ["one", "two", "three"];
    selectVal.value = null; // outparam, just making sure.
    isOK = prompter.select(window, "TestTitle", "This is the select text.", items.length, items, selectVal);
    is(isOK, true, "checked expected retval");
    is(selectVal.value, 1, "checking selected index");

    yield promptDone;
});

// =====
add_task(function* test_cancel_prompt() {
    info("Starting test: Select (3 items, cancel)");
    state = {
        msg   : "This is the select text.",
        title : "TestTitle",
        items : ["one", "two", "three"],
    };
    action = {
        buttonClick: "cancel",
    };
    promptDone = handlePrompt(state, action);
    items = ["one", "two", "three"];
    selectVal.value = null; // outparam, just making sure.
    isOK = prompter.select(window, "TestTitle", "This is the select text.", items.length, items, selectVal);
    is(isOK, false, "checked expected retval");
    is(selectVal.value, 0, "checking selected index");

    yield promptDone;
});

</script>
</pre>
</body>
</html>