| 1 | <!DOCTYPE HTML> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta http-equiv="content-type"ibm content="text/html; charset=utf-8"/> |
| 5 | <meta name="viewport" |
| 6 | content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" /> |
| 7 | <meta name="apple-mobile-web-app-capable" content="yes" /> |
| 8 | <title>Combo Box Tests</title> |
| 9 | <script type="text/javascript" src="../../../deviceTheme.js"></script> |
| 10 | <script type="text/javascript" src="../../../../../dojo/dojo.js" |
| 11 | data-dojo-config="async: true, parseOnLoad: true"></script> |
| 12 | <script type="text/javascript" src="../TestUtil.js"></script> |
| 13 | <script language="JavaScript" type="text/javascript"> |
| 14 | var timeoutInterval = 1000; |
| 15 | require([ |
| 16 | "dojo/_base/connect", |
| 17 | "dojo/_base/lang", |
| 18 | "dojo/sniff", |
| 19 | "dojo/Deferred", |
| 20 | "dojo/dom", |
| 21 | "dojo/dom-construct", // dojo.place |
| 22 | "dojo/dom-class", // dojo.hasClass |
| 23 | "dojo/dom-style", |
| 24 | "dojo/store/Memory", // dojo.store.Memory |
| 25 | "dojo/ready", // dojo.ready |
| 26 | "dijit/registry", // dijit.byId |
| 27 | "doh/runner", //doh functions |
| 28 | "dojox/mobile/ComboBox", |
| 29 | "dojox/mobile", // This is a mobile app. |
| 30 | "dojox/mobile/View", // This mobile app uses mobile view |
| 31 | "dojox/mobile/compat", // This mobile app supports running on desktop browsers |
| 32 | "dojox/mobile/parser" // This mobile app uses declarative programming with fast mobile parser |
| 33 | ], function(connect, lang, has, Deferred, dom, domConst, domClass, domStyle, |
| 34 | Memory, ready, registry, runner, ComboBox){ |
| 35 | |
| 36 | var FRUITS = new Memory({ |
| 37 | idProperty:"name", |
| 38 | data:[ |
| 39 | {name:"Apple"}, |
| 40 | {name:"Banana"}, |
| 41 | {name:"Cherry"} |
| 42 | ]}); |
| 43 | |
| 44 | function _createComboBoxDeclaratively(id) { |
| 45 | return registry.byId(id); |
| 46 | }; |
| 47 | |
| 48 | function _createComboBoxProgrammatically(placeHolderId){ |
| 49 | var widget = new ComboBox({store:FRUITS, id:placeHolderId, value:'Apple'}); |
| 50 | runner.assertNotEqual(null, widget); |
| 51 | domConst.place(widget.domNode, placeHolderId, "replace"); |
| 52 | widget.startup(); |
| 53 | return widget; |
| 54 | }; |
| 55 | |
| 56 | function _createComboBoxProgrammaticallyWithSourceNodeReference(id){ |
| 57 | var widget = new ComboBox({store:FRUITS, value:'Apple'}, id); |
| 58 | widget.startup(); |
| 59 | return widget; |
| 60 | }; |
| 61 | |
| 62 | function _assertCorrectComboBox(comboBox, def){ |
| 63 | runner.assertNotEqual(null, comboBox, "combobox shouldn't be null"); |
| 64 | runner.assertEqual(FRUITS.data[0].name, comboBox.domNode.value, comboBox.toString() + |
| 65 | " value is not the expected one at init"); |
| 66 | // Display the dropdown |
| 67 | fireOnClick(comboBox.domNode); |
| 68 | setTimeout(def.getTestErrback(function(){ |
| 69 | var dropDownNode = dom.byId(comboBox.id + "_dropdown"); |
| 70 | runner.assertNotEqual(null, dropDownNode, |
| 71 | "can't find dropdown node for " + comboBox.toString()); |
| 72 | runner.assertEqual("block", domStyle.get(dropDownNode, "display"), |
| 73 | " dropdown node for " + comboBox.toString() + " should be visible after click"); |
| 74 | // Verify available options |
| 75 | _checkComboBoxOptions(comboBox, |
| 76 | [{containsClass: "mblComboBoxMenuPreviousButton", displayed: false}, |
| 77 | {label: "Apple", displayed: true}, |
| 78 | {label: "Banana", displayed: true}, |
| 79 | {label: "Cherry", displayed: true}, |
| 80 | {containsClass: "mblComboBoxMenuNextButton", displayed: false}]); |
| 81 | // Type letter c in the input field |
| 82 | comboBox.domNode.value = "c"; |
| 83 | fireOnInput(comboBox.domNode); |
| 84 | setTimeout(def.getTestErrback(function(){ |
| 85 | // Verify available options |
| 86 | _checkComboBoxOptions(comboBox, |
| 87 | [{containsClass: "mblComboBoxMenuPreviousButton", displayed: false}, |
| 88 | {label: "Cherry", displayed: true}, |
| 89 | {containsClass: "mblComboBoxMenuNextButton", displayed: false}]); |
| 90 | // click in the dropdown to select an option |
| 91 | fireOnClick(dom.byId(comboBox.id + "_popup0")); |
| 92 | setTimeout(def.getTestCallback(function(){ |
| 93 | // check that the dropdown is not visible anymore |
| 94 | runner.assertEqual("none", domStyle.get(dropDownNode, "display"), |
| 95 | " dropdown node for " + comboBox.toString() + " should be invisible after click"); |
| 96 | // check the clicked option value is now the input field value |
| 97 | runner.assertEqual("Cherry", comboBox.domNode.value, comboBox.toString() + |
| 98 | " value is not the expected one after clicking the option"); |
| 99 | // programmatically set value |
| 100 | comboBox.set("value", FRUITS.data[1].name); |
| 101 | runner.assertEqual(FRUITS.data[1].name, comboBox.domNode.value, comboBox.toString() + |
| 102 | " value is not the expected one after programmatic set"); |
| 103 | }), 500); |
| 104 | }), 500); |
| 105 | }), 500); |
| 106 | }; |
| 107 | |
| 108 | function _checkComboBoxOptions(comboBox, expected){ |
| 109 | var options = dom.byId(comboBox.id + "_dropdown").firstChild.firstChild.children; |
| 110 | var i = 0; |
| 111 | runner.assertEqual(expected.length, options.length, |
| 112 | "The number of options is not the expected one for " + comboBox.id); |
| 113 | for(i = 0; i < options.length; i++){ |
| 114 | if(expected[i].label){ |
| 115 | runner.assertEqual(expected[i].label, options[i].innerText || options[i].textContent, |
| 116 | "label for option " + i + " of " + comboBox.id + " is not the expected one"); |
| 117 | } |
| 118 | if(expected[i].containsClass){ |
| 119 | runner.assertTrue(domClass.contains(options[i], expected[i].containsClass), |
| 120 | "class for option " + i + " of " + comboBox.id + " is not the expected one"); |
| 121 | } |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | function _showView2(){ |
| 126 | var view1 = registry.byId("view1"); |
| 127 | view1.performTransition("view2", 1, "none"); |
| 128 | }; |
| 129 | |
| 130 | ready(function(){ |
| 131 | runner.register("dojox.mobile.test.doh.ComboBoxTests", [ |
| 132 | { |
| 133 | name: "test view1-comboBox1", |
| 134 | timeout: 4000, |
| 135 | runTest: function(){ |
| 136 | |
| 137 | var d = new runner.Deferred(); |
| 138 | var comboBox1 = _createComboBoxDeclaratively("view1-comboBox1"); |
| 139 | setTimeout(d.getTestErrback(function(){ |
| 140 | _assertCorrectComboBox(comboBox1, d); |
| 141 | }), timeoutInterval); |
| 142 | return d; |
| 143 | } |
| 144 | }, |
| 145 | { |
| 146 | name: "test view1-comboBox2", |
| 147 | timeout: 4000, |
| 148 | runTest: function(){ |
| 149 | |
| 150 | var d = new runner.Deferred(); |
| 151 | var comboBox2 = _createComboBoxProgrammatically("view1-comboBox2"); |
| 152 | setTimeout(d.getTestErrback(function(){ |
| 153 | _assertCorrectComboBox(comboBox2, d); |
| 154 | }), timeoutInterval); |
| 155 | return d; |
| 156 | } |
| 157 | }, |
| 158 | { |
| 159 | name: "test view1-comboBox3", |
| 160 | timeout: 4000, |
| 161 | runTest: function(){ |
| 162 | |
| 163 | var d = new runner.Deferred(); |
| 164 | var comboBox3 = _createComboBoxProgrammaticallyWithSourceNodeReference("view1-comboBox3"); |
| 165 | setTimeout(d.getTestErrback(function(){ |
| 166 | _assertCorrectComboBox(comboBox3, d); |
| 167 | }), timeoutInterval); |
| 168 | return d; |
| 169 | } |
| 170 | }, |
| 171 | { |
| 172 | name: "test view2-comboBox1", |
| 173 | timeout: 4000, |
| 174 | runTest: function(){ |
| 175 | |
| 176 | var d = new runner.Deferred(); |
| 177 | var comboBox1 = _createComboBoxDeclaratively("view2-comboBox1"); |
| 178 | _createComboBoxProgrammatically("view2-comboBox2"); |
| 179 | _createComboBoxProgrammaticallyWithSourceNodeReference("view2-comboBox3"); |
| 180 | _showView2(); |
| 181 | setTimeout(d.getTestErrback(function(){ |
| 182 | _assertCorrectComboBox(comboBox1, d); |
| 183 | }), timeoutInterval); |
| 184 | return d; |
| 185 | } |
| 186 | }, |
| 187 | { |
| 188 | name: "test view2-comboBox2", |
| 189 | timeout: 4000, |
| 190 | runTest: function(){ |
| 191 | |
| 192 | var d = new runner.Deferred(); |
| 193 | var comboBox2 = registry.byId("view2-comboBox2"); |
| 194 | setTimeout(d.getTestErrback(function(){ |
| 195 | _assertCorrectComboBox(comboBox2, d); |
| 196 | }), timeoutInterval); |
| 197 | return d; |
| 198 | } |
| 199 | }, |
| 200 | { |
| 201 | name: "test view2-comboBox3", |
| 202 | timeout: 4000, |
| 203 | runTest: function(){ |
| 204 | |
| 205 | var d = new runner.Deferred(); |
| 206 | var comboBox3 = registry.byId("view2-comboBox3"); |
| 207 | setTimeout(d.getTestErrback(function(){ |
| 208 | _assertCorrectComboBox(comboBox3, d); |
| 209 | }), timeoutInterval); |
| 210 | return d; |
| 211 | } |
| 212 | }, |
| 213 | { |
| 214 | // Test case for #17944. |
| 215 | name: "test view2-trac-ticket-17944", |
| 216 | timeout: 4000, |
| 217 | runTest: function(){ |
| 218 | |
| 219 | var d = new runner.Deferred(); |
| 220 | var comboBox1 = registry.byId("view2-comboBox1"); |
| 221 | var comboBox2 = registry.byId("view2-comboBox2"); |
| 222 | var errorCounter = 0; |
| 223 | var errorMsg; |
| 224 | // Check that no error is thrown when opening the dropdown |
| 225 | // of a combobox while the dropdown of another combobox is opening. |
| 226 | // To test it, we cannot use a simple try-catch, because |
| 227 | // the error is thrown asynchronously. Hence: |
| 228 | window.onerror = function(msg, url, lineNumber){ |
| 229 | errorCounter++; |
| 230 | errorMsg = "After firing click: " + msg + "\nURL: " + url + |
| 231 | "\nLine number: " + lineNumber; |
| 232 | console.log(errorMsg); |
| 233 | }; |
| 234 | fireOnClick(comboBox1.domNode); |
| 235 | fireOnClick(comboBox2.domNode); |
| 236 | // Check that no error has been thrown |
| 237 | setTimeout(d.getTestCallback(function(){ |
| 238 | runner.assertEqual(0, errorCounter, errorMsg); |
| 239 | }), 2000); // smaller than the total timeout of the test case |
| 240 | |
| 241 | return d; |
| 242 | } |
| 243 | } |
| 244 | ]); |
| 245 | |
| 246 | runner.run(); |
| 247 | }); |
| 248 | }); |
| 249 | </script> |
| 250 | </head> |
| 251 | <body> |
| 252 | <div id="view1" data-dojo-type="dojox/mobile/View" selected="true"> |
| 253 | <h1>View 1</h1> |
| 254 | <datalist id="fruits"> |
| 255 | <select data-dojo-type="dijit/form/DataList" data-dojo-props="id:'fruits'"> |
| 256 | <option value="fruit1">Apple</option> |
| 257 | <option value="fruit2">Banana</option> |
| 258 | <option value="fruit3">Cherry</option> |
| 259 | </select> |
| 260 | </datalist> |
| 261 | <input id="view1-comboBox1" type="text" data-dojo-type="dojox/mobile/ComboBox" |
| 262 | data-dojo-props="value:'Apple', list:'fruits'"></input> |
| 263 | <div id="view1-comboBox2"></div> |
| 264 | <input id="view1-comboBox3" type="text"></input> |
| 265 | </div> |
| 266 | <div id="view2" data-dojo-type="dojox/mobile/View"> |
| 267 | <h1>View 2</h1> |
| 268 | <select data-dojo-type="dijit/form/DataList" data-dojo-props="id:'fruits2'"> |
| 269 | <option>Apple</option> |
| 270 | <option>Banana</option> |
| 271 | <option>Cherry</option> |
| 272 | </select> |
| 273 | <input id="view2-comboBox1" type="text" data-dojo-type="dojox/mobile/ComboBox" |
| 274 | data-dojo-props="value:'Apple', list:'fruits2'"></input> |
| 275 | <div id="view2-comboBox2"></div> |
| 276 | <input id="view2-comboBox3" type="text"></input> |
| 277 | </div> |
| 278 | </body> |
| 279 | </html> |