| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 | <title>dijit.Tree / dijit.tree.ObjectStoreModel automated test</title> |
| 6 | |
| 7 | <script type="text/javascript" src="../boilerplate.js"></script> |
| 8 | |
| 9 | <script type="text/javascript"> |
| 10 | require([ |
| 11 | "doh/runner", |
| 12 | "dojo/_base/array", |
| 13 | "dojo/aspect", |
| 14 | "dojo/cookie", |
| 15 | "dojo/dom-class", |
| 16 | "dojo/_base/lang", |
| 17 | "dojo/on", |
| 18 | "dojo/_base/window", |
| 19 | "dojo/promise/all", |
| 20 | "dojo/store/Memory", |
| 21 | "dojo/store/Observable", |
| 22 | "dijit/focus", |
| 23 | "dijit/registry", |
| 24 | "dijit/Tree", |
| 25 | "dijit/tree/ObjectStoreModel", |
| 26 | "dijit/tree/dndSource", |
| 27 | "dijit/tests/_data/geography", |
| 28 | "dijit/tests/AsyncMemoryStore", |
| 29 | "dijit/tests/helpers", // functions to help test |
| 30 | "dojo/domReady!" |
| 31 | ], function(doh, array, aspect, cookie, domClass, lang, on, win, all, Memory, Observable, |
| 32 | focus, registry, Tree, ObjectStoreModel, dndSource, data, AsyncMemoryStore, helpers){ |
| 33 | |
| 34 | var myStore, myModel, myTree, myTree2, cache; |
| 35 | |
| 36 | // Create / reset the data store and model, setting global-ish variables myStore and myModel |
| 37 | function setupStoreAndModel(/*Function*/ Store, /*Boolean*/ isObservable, idDataType){ |
| 38 | if(!Store){ |
| 39 | Store = Memory; |
| 40 | } |
| 41 | |
| 42 | // Clone to sandbox tests that change data |
| 43 | var datacopy = lang.clone(data); |
| 44 | |
| 45 | // id datatype switch: |
| 46 | // assume fetched data id is of type 'string', overwrite if needed |
| 47 | // store original id in a cache, so it can be retrieved later |
| 48 | var _id, i=0, l=datacopy.length; |
| 49 | cache = {}; |
| 50 | for(i=0;i<l;i++){ |
| 51 | _id = datacopy[i].id; |
| 52 | cache[_id] = idDataType=="float" ? Math.random() : idDataType=="int" ? i+1 : _id; |
| 53 | datacopy[i].id = cache[_id]; |
| 54 | } |
| 55 | // replace parent |
| 56 | for(i=0;i<l;i++){ |
| 57 | if(datacopy[i].hasOwnProperty("parent")){ |
| 58 | datacopy[i].parent = cache[datacopy[i].parent]; |
| 59 | } |
| 60 | } |
| 61 | // Create test store. |
| 62 | myStore = new Store({ |
| 63 | data: datacopy |
| 64 | }); |
| 65 | |
| 66 | // Since dojo.store.Memory doesn't have various store methods we need, we have to add them manually |
| 67 | myStore.getChildren = function(object){ |
| 68 | // Add a getChildren() method to store for the data model where |
| 69 | // children objects point to their parent (aka relational model) |
| 70 | return this.query({parent: this.getIdentity(object)}); |
| 71 | }; |
| 72 | |
| 73 | if(isObservable){ |
| 74 | aspect.around(myStore, "put", function(originalPut){ |
| 75 | // To support DnD, the store must support put(child, {parent: parent}). |
| 76 | // Since our store is relational, that just amounts to setting child.parent |
| 77 | // to the parent's id. |
| 78 | return function(obj, options){ |
| 79 | if(options && options.parent){ |
| 80 | obj.parent = options.parent.id; |
| 81 | } |
| 82 | return originalPut.call(myStore, obj, options); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | // Wrap the store in Observable so that updates to the store are reflected |
| 87 | myStore = new Observable(myStore); |
| 88 | } |
| 89 | |
| 90 | doh.t(myStore, "store created"); |
| 91 | |
| 92 | // Create the model |
| 93 | myModel = new ObjectStoreModel({store: myStore, query: {id: cache["earth"]}}); |
| 94 | doh.t(myModel, "model created"); |
| 95 | } |
| 96 | |
| 97 | // Register tests for specified store / observable |
| 98 | function registerBasicTests(/*Boolean*/ async, /*Boolean*/ isObservable){ |
| 99 | var category = (async ? "async " : "sync ") + (isObservable ? "observable ": "") + "store: "; |
| 100 | |
| 101 | doh.register(category + "setup", function(){ |
| 102 | setupStoreAndModel(async ? AsyncMemoryStore : Memory, isObservable); |
| 103 | }); |
| 104 | |
| 105 | doh.register(category + "basic", [ |
| 106 | { |
| 107 | name: "create", |
| 108 | timeout: 5000, |
| 109 | runTest: function(){ |
| 110 | myTree = new Tree({ |
| 111 | id: "myTree", |
| 112 | model: myModel, |
| 113 | persist: false, // persist==true is too hard to test |
| 114 | dndController: dndSource |
| 115 | }).placeAt(win.body()); |
| 116 | doh.t(myTree, "tree created"); |
| 117 | |
| 118 | myTree.startup(); |
| 119 | |
| 120 | // Give the tree time to load, and the do checks that it loaded correctly |
| 121 | return myTree.onLoadDeferred.then(function(){ |
| 122 | var world = myTree.rootNode; |
| 123 | doh.t(world, "root node exists"); |
| 124 | doh.is("The earth", world.label, "world node label"); |
| 125 | doh.t(world.isExpanded, "root node is expanded"); |
| 126 | doh.t(domClass.contains(world.domNode, "dijitTreeIsRoot"), "root node has dijitTreeIsRoot"); |
| 127 | |
| 128 | var children = world.getChildren(); |
| 129 | doh.is(6, children.length, "six children"); |
| 130 | doh.is("Africa", children[0].label, "first child"); |
| 131 | doh.f(domClass.contains(children[0].domNode, "dijitTreeIsRoot"), "first child doesn't have dijitTreeIsRoot"); |
| 132 | doh.f(children[0].isExpanded, "first child not expanded"); |
| 133 | doh.is("South America", children[5].label, "last child"); |
| 134 | |
| 135 | // Last child has special CSS for drawing the grid lines |
| 136 | doh.f(domClass.contains(children[3].domNode, "dijitTreeIsLast"), "middle node doesn't have dijitTreeIsLast"); |
| 137 | doh.t(domClass.contains(children[5].domNode, "dijitTreeIsLast"), "last node has dijitTreeIsLast"); |
| 138 | }); |
| 139 | } |
| 140 | }, |
| 141 | |
| 142 | function openEurope(){ |
| 143 | var d = new doh.Deferred(); |
| 144 | |
| 145 | var world = myTree.rootNode; |
| 146 | var europe = world.getChildren()[3]; |
| 147 | |
| 148 | doh.is("Europe", europe.label, "europe node label"); |
| 149 | doh.t(europe, "europe node exists"); |
| 150 | |
| 151 | // Click on Europe to open it |
| 152 | on.emit(europe.expandoNode, "click", {bubbles: true}); |
| 153 | |
| 154 | // Give the children time to load, and the do checks that they |
| 155 | // loaded correctly |
| 156 | setTimeout(d.getTestCallback(function(){ |
| 157 | doh.t(europe.isExpanded, "europe node is expanded"); |
| 158 | |
| 159 | var children = europe.getChildren(); |
| 160 | doh.is(4, children.length, "children count"); |
| 161 | doh.is("Germany", children[0].label, "first child"); |
| 162 | doh.f(children[0].isExpanded, "first child not expanded"); |
| 163 | doh.is("Italy", children[3].label, "last child"); |
| 164 | |
| 165 | // Last child has special CSS for drawing the grid lines |
| 166 | doh.f(domClass.contains(children[1].domNode, "dijitTreeIsLast"), "middle node doesn't have dijitTreeIsLast"); |
| 167 | doh.t(domClass.contains(children[3].domNode, "dijitTreeIsLast"), "last node has dijitTreeIsLast"); |
| 168 | }), 750); |
| 169 | return d; |
| 170 | }, |
| 171 | |
| 172 | function openAsia(){ |
| 173 | var d = new doh.Deferred(); |
| 174 | |
| 175 | var world = myTree.rootNode; |
| 176 | var asia = world.getChildren()[1]; |
| 177 | doh.is("Asia", asia.label, "asia node label"); |
| 178 | doh.t(asia, "asia node exists"); |
| 179 | |
| 180 | // Click on Europe to open it |
| 181 | on.emit(asia.expandoNode, "click", {bubbles: true}); |
| 182 | |
| 183 | // Give the children time to load, and the do checks that they |
| 184 | // loaded correctly |
| 185 | setTimeout(d.getTestCallback(function(){ |
| 186 | doh.t(asia.isExpanded, "asia node is expanded"); |
| 187 | |
| 188 | var children = asia.getChildren(); |
| 189 | doh.is(4, children.length, "children count"); |
| 190 | doh.is("China", children[0].label, "first child"); |
| 191 | doh.is("Mongolia", children[3].label, "last child"); |
| 192 | }), 750); |
| 193 | return d; |
| 194 | } |
| 195 | ]); |
| 196 | |
| 197 | if(isObservable){ |
| 198 | doh.register(category + "data store binding", [ |
| 199 | function itemUpdate(){ |
| 200 | // Test that Tree notices when data store items change, and updates accordingly |
| 201 | |
| 202 | myStore.put({ id: "ES", name: "España", type: "country", parent: "EU" }); |
| 203 | |
| 204 | var world = myTree.rootNode; |
| 205 | var europe = world.getChildren()[3]; |
| 206 | doh.is("España", helpers.innerText(europe.getChildren()[2].labelNode), "label changed"); |
| 207 | }, |
| 208 | |
| 209 | function topLevelItemDelete(){ |
| 210 | // Delete a top level item. |
| 211 | |
| 212 | // Remove "South America" |
| 213 | myStore.remove("SA"); |
| 214 | |
| 215 | var world = myTree.rootNode; |
| 216 | var children = world.getChildren(); |
| 217 | doh.is(5, children.length, "five children"); |
| 218 | doh.is("North America", children[4].label, "last child"); |
| 219 | doh.t(domClass.contains(children[4].domNode, "dijitTreeIsLast"), |
| 220 | "North america has become the last node so it gets the CSS class for that"); |
| 221 | }, |
| 222 | |
| 223 | function nestedItemDelete(){ |
| 224 | // Delete a nested item |
| 225 | |
| 226 | // Remove "China" |
| 227 | myStore.remove("CN"); |
| 228 | |
| 229 | var world = myTree.rootNode; |
| 230 | var asia = world.getChildren()[1]; |
| 231 | var children = asia.getChildren(); |
| 232 | doh.is(3, children.length, "three children"); |
| 233 | }, |
| 234 | |
| 235 | function topLevelItemInsert(){ |
| 236 | // Create a new top level item as last child. |
| 237 | // ForestStoreModel needs to realize that the top level children have changed and notify Tree. |
| 238 | |
| 239 | myStore.add({ |
| 240 | id: "PA", |
| 241 | name: "Pacifica", |
| 242 | type: "continent", |
| 243 | parent: "earth" |
| 244 | }); |
| 245 | |
| 246 | var world = myTree.rootNode; |
| 247 | var children = world.getChildren(); |
| 248 | |
| 249 | doh.is(6, children.length, "six children"); |
| 250 | doh.is("Pacifica", children[5].label, "last child"); |
| 251 | doh.f(domClass.contains(children[4].domNode, "dijitTreeIsLast"), |
| 252 | "North America no longer last child"); |
| 253 | doh.t(domClass.contains(children[5].domNode, "dijitTreeIsLast"), |
| 254 | "Pacifica is last child"); |
| 255 | }, |
| 256 | |
| 257 | function topLevelItemModify(){ |
| 258 | // Modify a top level item so it's no longer top level. |
| 259 | |
| 260 | myStore.put({ |
| 261 | id: "PA", |
| 262 | name: "Pacifica", |
| 263 | type: "continent", |
| 264 | parent: "AS" |
| 265 | }); |
| 266 | |
| 267 | var world = myTree.rootNode; |
| 268 | doh.is(5, world.getChildren().length, "world children"); |
| 269 | |
| 270 | var asia = world.getChildren()[1]; |
| 271 | doh.is(4, asia.getChildren().length, "asia children"); |
| 272 | }, |
| 273 | |
| 274 | function nestedItemModify(){ |
| 275 | // Modify a nested item so it matches the query for top level items in the tree. |
| 276 | |
| 277 | myStore.put({ |
| 278 | id: "PA", |
| 279 | name: "Pacifica", |
| 280 | type: "continent", |
| 281 | parent: "earth" |
| 282 | }); |
| 283 | |
| 284 | var world = myTree.rootNode; |
| 285 | var asia = world.getChildren()[1]; |
| 286 | doh.is(3, asia.getChildren().length, "asia children"); |
| 287 | } |
| 288 | ]); |
| 289 | |
| 290 | doh.register(category + "DnD", [ |
| 291 | // Drag Germany from Europe to Asia |
| 292 | function dragGermanyToAsia(){ |
| 293 | return all([myStore.get("AS"), myStore.get("EU"), myStore.get("DE")]).then(function(items){ |
| 294 | var asiaItem = items[0], |
| 295 | europeItem = items[1], |
| 296 | germanyItem = items[2]; |
| 297 | return myModel.pasteItem(germanyItem, europeItem, asiaItem); |
| 298 | }).then(function(){ |
| 299 | // Check tree state |
| 300 | var world = myTree.rootNode; |
| 301 | var asia = world.getChildren()[1]; |
| 302 | var europe = world.getChildren()[3]; |
| 303 | doh.is(3, europe.getChildren().length, "europe children"); |
| 304 | doh.is(4, asia.getChildren().length, "asia children"); |
| 305 | doh.is("Germany", asia.getChildren()[3].label, "last child of asia"); |
| 306 | }); |
| 307 | }, |
| 308 | |
| 309 | // Drag Germany from Asia to Europe |
| 310 | function dragGermanyToEurope(){ |
| 311 | return all([myStore.get("AS"), myStore.get("EU"), myStore.get("DE")]).then(function(items){ |
| 312 | var asiaItem = items[0], |
| 313 | europeItem = items[1], |
| 314 | germanyItem = items[2]; |
| 315 | return myModel.pasteItem(germanyItem, asiaItem, europeItem); |
| 316 | }).then(function(){ |
| 317 | // Check tree state |
| 318 | var world = myTree.rootNode; |
| 319 | var asia = world.getChildren()[1]; |
| 320 | var europe = world.getChildren()[3]; |
| 321 | doh.is(3, asia.getChildren().length, "asia children"); |
| 322 | doh.is(4, europe.getChildren().length, "europe children"); |
| 323 | doh.is("Germany", europe.getChildren()[3].label, "last child of europe"); |
| 324 | }); |
| 325 | }, |
| 326 | |
| 327 | function openAfrica(){ |
| 328 | var d = new doh.Deferred(); |
| 329 | |
| 330 | var world = myTree.rootNode; |
| 331 | var africa = world.getChildren()[0]; |
| 332 | doh.is("Africa", africa.label, "africa node label"); |
| 333 | doh.t(africa, "africa node exists"); |
| 334 | |
| 335 | // Click on Africa to open it |
| 336 | on.emit(africa.expandoNode, "click", {bubbles: true}); |
| 337 | |
| 338 | // Give the children time to load, and the do checks that they |
| 339 | // loaded correctly |
| 340 | setTimeout(d.getTestCallback(function(){ |
| 341 | doh.t(africa.isExpanded, "node is expanded"); |
| 342 | |
| 343 | var children = africa.getChildren(); |
| 344 | doh.is(3, children.length, "children count"); |
| 345 | }), 750); |
| 346 | |
| 347 | return d; |
| 348 | }, |
| 349 | |
| 350 | function openKenya(){ |
| 351 | var d = new doh.Deferred(); |
| 352 | |
| 353 | var world = myTree.rootNode; |
| 354 | var africa = world.getChildren()[0]; |
| 355 | var kenya = africa.getChildren()[1]; |
| 356 | doh.t(kenya, "kenya node exists"); |
| 357 | doh.is("Kenya", kenya.label, "kenya node label"); |
| 358 | |
| 359 | // Click on Kenya to open it |
| 360 | on.emit(kenya.expandoNode, "click", {bubbles: true}); |
| 361 | |
| 362 | // Give the children time to load, and the do checks that they |
| 363 | // loaded correctly |
| 364 | setTimeout(d.getTestCallback(function(){ |
| 365 | doh.t(kenya.isExpanded, "node is expanded"); |
| 366 | |
| 367 | var children = kenya.getChildren(); |
| 368 | doh.is(2, children.length, "children count"); |
| 369 | }), 750); |
| 370 | |
| 371 | return d; |
| 372 | }, |
| 373 | |
| 374 | // Dragging open node to make sure it stays open. |
| 375 | function dragKenyaToAsia(){ |
| 376 | // Get some pointers to TreeNodes |
| 377 | var world = myTree.rootNode; |
| 378 | var africa = world.getChildren()[0]; |
| 379 | var asia = world.getChildren()[1]; |
| 380 | var kenya = africa.getChildren()[1]; |
| 381 | |
| 382 | return all([myStore.get("AS"), myStore.get("AF"), myStore.get("KE")]).then(function(items){ |
| 383 | var asiaItem = items[0], |
| 384 | africaItem = items[1], |
| 385 | kenyaItem = items[2]; |
| 386 | return myModel.pasteItem(kenyaItem, africaItem, asiaItem); |
| 387 | }).then(function(){ |
| 388 | // Confirm Tree state |
| 389 | doh.is(4, asia.getChildren().length, "asia children"); |
| 390 | doh.is("Kenya", asia.getChildren()[3].label, "last child of asia"); |
| 391 | doh.is(kenya, asia.getChildren()[3], "same TreeNode as before"); |
| 392 | doh.t(kenya.isExpanded, "node is still expanded"); |
| 393 | doh.is(2, kenya.getChildren().length, "same children count as before"); |
| 394 | }); |
| 395 | }, |
| 396 | |
| 397 | // Put Kenya back in Africa |
| 398 | function dragKenyaToAfrica(){ |
| 399 | return all([myStore.get("AS"), myStore.get("AF"), myStore.get("KE")]).then(function(items){ |
| 400 | var asiaItem = items[0], |
| 401 | africaItem = items[1], |
| 402 | kenyaItem = items[2]; |
| 403 | return myModel.pasteItem(kenyaItem, asiaItem, africaItem); |
| 404 | }).then(function(){ |
| 405 | var world = myTree.rootNode; |
| 406 | var africa = world.getChildren()[0]; |
| 407 | var asia = world.getChildren()[1]; |
| 408 | doh.is(3, asia.getChildren().length, "asia children"); |
| 409 | doh.is(3, africa.getChildren().length, "africa children"); |
| 410 | }); |
| 411 | }, |
| 412 | |
| 413 | // Drag Europe onto root, to make sure it doesn't disappear (#16692) |
| 414 | function dragEuropeToRoot(){ |
| 415 | var world = myTree.rootNode; |
| 416 | doh.is(6, world.getChildren().length, "world children before paste"); |
| 417 | return all([myStore.get("earth"), myStore.get("EU")]).then(function(items){ |
| 418 | var worldItem = items[0], |
| 419 | europeItem = items[1]; |
| 420 | return myModel.pasteItem(europeItem, worldItem, worldItem); |
| 421 | }).then(function(){ |
| 422 | doh.is(6, world.getChildren().length, "world children after paste"); |
| 423 | }); |
| 424 | } |
| 425 | ]); |
| 426 | } |
| 427 | doh.register(category + "tearDown", function destroyTree(){ |
| 428 | myTree.destroy(); |
| 429 | myModel.destroy(); |
| 430 | }); |
| 431 | } |
| 432 | |
| 433 | registerBasicTests(false, true); |
| 434 | registerBasicTests(true, true); |
| 435 | registerBasicTests(true, false); |
| 436 | |
| 437 | // Other tests, currently only run on synchronous observable store |
| 438 | function registerPathsTests(datatype){ |
| 439 | // use datatype to switch between int|float|string (default) |
| 440 | // cleanup: |
| 441 | datatype = datatype == "float" ? "float" : datatype == "int" ? "int" : "string"; |
| 442 | function typePath(path){ |
| 443 | if(datatype!="string"){ |
| 444 | return array.map(path,function(item){ |
| 445 | return cache[item]; |
| 446 | }); |
| 447 | } |
| 448 | return path; |
| 449 | } |
| 450 | // return original id from cache |
| 451 | function fromCache(val){ |
| 452 | for(var k in cache){ |
| 453 | if(cache[k]===val){ |
| 454 | return k; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | doh.register("paths_"+datatype, [ |
| 459 | function reset(){ |
| 460 | setupStoreAndModel(Memory, true, datatype); |
| 461 | }, |
| 462 | |
| 463 | { |
| 464 | name: "getPath", |
| 465 | timeout: 5000, |
| 466 | runTest: function(){ |
| 467 | myTree = new Tree({ |
| 468 | id: "myTree", |
| 469 | model: myModel, |
| 470 | persist: false, // persist==true is too hard to test |
| 471 | dndController: dndSource, |
| 472 | autoExpand: true |
| 473 | }).placeAt(win.body()); |
| 474 | doh.t(myTree, "tree created"); |
| 475 | |
| 476 | myTree.startup(); |
| 477 | |
| 478 | return myTree.onLoadDeferred.then(function (){ |
| 479 | // Select the node. can't emit "click" because dndSelector listens for mousedown & mouseup. |
| 480 | // And before the mousedown, we need a mouseover event, so _dndContainer.js sets this.current. |
| 481 | var europe = myTree.rootNode.getChildren()[3]; |
| 482 | var france = europe.getChildren()[1]; |
| 483 | on.emit(france.labelNode, navigator.pointerEnabled ? "pointerover" : navigator.msPointerEnabled ? "MSPointerOver" : "mouseover", {bubbles: true}); |
| 484 | on.emit(france.labelNode, navigator.pointerEnabled ? "pointerdown" : navigator.msPointerEnabled ? "MSPointerDown" : "mousedown", {button: dojo.mouseButtons.LEFT, bubbles: true}); |
| 485 | on.emit(france.labelNode, navigator.pointerEnabled ? "pointerup" : navigator.msPointerEnabled ? "MSPointerUp" : "mouseup", {button: dojo.mouseButtons.LEFT, bubbles: true}); |
| 486 | |
| 487 | var path = myTree.get("path"); |
| 488 | doh.is("earth, EU, FR", array.map(myTree.get("path"), function (obj){ |
| 489 | return fromCache(obj.id); |
| 490 | }).join(", "), "serialized path"); |
| 491 | }); |
| 492 | } |
| 493 | }, |
| 494 | |
| 495 | { |
| 496 | name: "createWithPath", |
| 497 | timeout: 5000, |
| 498 | runTest: function(){ |
| 499 | myTree2 = new Tree({ |
| 500 | id: "myTree2", |
| 501 | model: myModel, |
| 502 | persist: false, // persist==true is too hard to test |
| 503 | dndController: dndSource, |
| 504 | path: typePath(["earth", "EU", "IT"]) |
| 505 | }).placeAt(win.body()); |
| 506 | doh.t(myTree2, "myTree2 created"); |
| 507 | |
| 508 | myTree2.startup(); |
| 509 | |
| 510 | return myTree2.onLoadDeferred.then(function(){ |
| 511 | doh.t(myTree2.rootNode, "root node exists"); |
| 512 | doh.t(myTree2.rootNode.isExpanded, "root node is expanded"); |
| 513 | doh.t(myTree2.rootNode.getChildren()[3].isExpanded, "europe node is expanded"); |
| 514 | doh.is("Italy", myTree2.selectedNode.label, "selected correct node"); |
| 515 | }); |
| 516 | } |
| 517 | }, |
| 518 | |
| 519 | function copyPath(){ |
| 520 | var world = myTree.rootNode; |
| 521 | |
| 522 | return myTree.set("path", myTree2.get("path")).then(function(items){ |
| 523 | doh.is("earth, EU, IT", array.map(items, function(item){ |
| 524 | return fromCache(item.id); |
| 525 | }).join(", ")); |
| 526 | doh.t(world.isExpanded, "root node is expanded"); |
| 527 | doh.t(world.getChildren()[3].isExpanded, "europe node is expanded"); |
| 528 | doh.is("Italy", myTree.get("selectedNode").label, "selected correct node"); |
| 529 | }); |
| 530 | }, |
| 531 | |
| 532 | { |
| 533 | name: "copyPathByIds", |
| 534 | timeout: 5000, |
| 535 | runTest: function(){ |
| 536 | var testpath = typePath(["earth", "NA", "CA", "Ottawa"]); |
| 537 | |
| 538 | return myTree.set("path", testpath).then(function(){ |
| 539 | var path = array.map(myTree.get("path"), function(item){ return item.id; }); |
| 540 | doh.is(testpath, path, "path got set on myTree"); |
| 541 | |
| 542 | return myTree2.set("path", path).then(function(){ |
| 543 | doh.t(myTree2.rootNode.isExpanded, "root node is expanded"); |
| 544 | doh.t(myTree2.rootNode.getChildren()[4].isExpanded, "north america node is expanded"); |
| 545 | doh.t(myTree2.rootNode.getChildren()[4].getChildren()[1].isExpanded, "canada node is expanded"); |
| 546 | doh.is("Ottawa", myTree2.get("selectedNode").label, "selected correct node"); |
| 547 | }); |
| 548 | }); |
| 549 | } |
| 550 | }, |
| 551 | |
| 552 | function setPathToNull(){ |
| 553 | return myTree2.set("path", []).then(function(){ |
| 554 | doh.is(null, myTree2.get("selectedNode"), "no selected node"); |
| 555 | }); |
| 556 | }, |
| 557 | |
| 558 | function setPathToRoot(){ |
| 559 | return myTree2.set("path", typePath(["earth"])).then(function(){ |
| 560 | doh.is(myTree2.rootNode, myTree2.get("selectedNode"), "selected root node"); |
| 561 | }); |
| 562 | }, |
| 563 | |
| 564 | { |
| 565 | name: "setPaths", |
| 566 | timeout: 5000, |
| 567 | runTest: function(){ |
| 568 | return myTree2.set("paths", [ |
| 569 | typePath(["earth", "AF", "KE", "Nairobi"]), |
| 570 | typePath(["earth", "NA", "MX", "Guadalajara"]) |
| 571 | ]).then(function(paths){ |
| 572 | doh.is("earth, AF, KE, Nairobi", array.map(paths[0], function(item){ |
| 573 | return fromCache(item.id); |
| 574 | }).join(", ")); |
| 575 | doh.is("earth, NA, MX, Guadalajara", array.map(paths[1], function(item){ |
| 576 | return fromCache(item.id); |
| 577 | }).join(", ")); |
| 578 | var ids = array.map(myTree2.selectedItems, function(x){ |
| 579 | return myTree2.model.getIdentity(x); |
| 580 | }).sort(); |
| 581 | doh.is(typePath(["Guadalajara", "Nairobi"]).sort(), ids); |
| 582 | }); |
| 583 | } |
| 584 | }, |
| 585 | |
| 586 | function setEmptyPath(){ |
| 587 | // setting an empty path is interpreted as selecting nothing |
| 588 | return myTree2.set("path", []).then(function(){ |
| 589 | doh.is(0, myTree2.get("selectedNodes").length, "no nodes selected"); |
| 590 | }); |
| 591 | }, |
| 592 | function setInvalidPath(){ |
| 593 | var d = new doh.Deferred(); |
| 594 | |
| 595 | myTree2.set("path", typePath(["earth", "AF", "KE", "Narnia"])).then( |
| 596 | function(){ |
| 597 | d.errback("Should have gotten error trying to set invalid path"); |
| 598 | }, |
| 599 | d.getTestCallback(function(e){ |
| 600 | doh.t(e instanceof Tree.PathError, "got PathError"); |
| 601 | }) |
| 602 | ); |
| 603 | |
| 604 | return d; |
| 605 | }, |
| 606 | |
| 607 | function destroyTree(){ |
| 608 | myTree.destroy(); |
| 609 | myTree2.destroy(); |
| 610 | } |
| 611 | ]); |
| 612 | } |
| 613 | |
| 614 | registerPathsTests("string"); |
| 615 | registerPathsTests("int"); |
| 616 | registerPathsTests("float"); |
| 617 | |
| 618 | doh.register("delete selected node", [ |
| 619 | function reset(){ |
| 620 | setupStoreAndModel(Memory, true); |
| 621 | }, |
| 622 | |
| 623 | { |
| 624 | name: "create", |
| 625 | timeout: 10000, |
| 626 | runTest: function(){ |
| 627 | myTree = new Tree({ |
| 628 | id: "myTree", |
| 629 | model: myModel, |
| 630 | persist: false, // persist==true is too hard to test |
| 631 | path: ["earth", "EU", "IT"] |
| 632 | }).placeAt(win.body()); |
| 633 | doh.t(myTree, "tree created"); |
| 634 | |
| 635 | myTree.startup(); |
| 636 | |
| 637 | return myTree.onLoadDeferred.then(function(){ |
| 638 | doh.is("IT", myTree.get("selectedItem").id); |
| 639 | }); |
| 640 | } |
| 641 | }, |
| 642 | |
| 643 | function deleteSelectedItem(){ |
| 644 | myStore.remove("IT"); |
| 645 | }, |
| 646 | |
| 647 | function selectNewItem(){ |
| 648 | // Since EU is already open this set("path", ...) should execute immediately |
| 649 | myTree.set("path", ["earth", "EU", "FR"]); |
| 650 | doh.is("FR", myTree.get("selectedItem").id); |
| 651 | }, |
| 652 | |
| 653 | function tearDown(){ |
| 654 | myTree.destroy(); |
| 655 | myModel.destroy(); |
| 656 | } |
| 657 | ]); |
| 658 | |
| 659 | |
| 660 | doh.register("nobidi", [ |
| 661 | function reset(){ |
| 662 | setupStoreAndModel(Memory, true); |
| 663 | }, |
| 664 | |
| 665 | { |
| 666 | name: "create", |
| 667 | timeout: 10000, |
| 668 | runTest: function(){ |
| 669 | myTree = new Tree({ |
| 670 | id: "myTree", |
| 671 | model: myModel, |
| 672 | persist: false, // persist==true is too hard to test |
| 673 | path: ["earth", "EU", "IT"] |
| 674 | }).placeAt(win.body()); |
| 675 | doh.t(myTree, "tree created"); |
| 676 | } |
| 677 | }, |
| 678 | |
| 679 | // Make sure that Tree doesn't have spurious lang="" dir="" on nodes |
| 680 | function noLangDir(){ |
| 681 | doh.t(myTree.rootNode, "root node exists"); |
| 682 | doh.t(myTree.rootNode.isExpanded, "root node is expanded"); |
| 683 | doh.f(domClass.contains(myTree.rootNode, "lang"), "no (empty) lang attribute on root TreeNode"); |
| 684 | doh.f(domClass.contains(myTree.rootNode, "dir"), "no (empty) dir attribute on root TreeNode"); |
| 685 | |
| 686 | var children = myTree.rootNode.getChildren(); |
| 687 | doh.f(domClass.contains(children[2], "lang"), "no (empty) lang attribute on child TreeNode"); |
| 688 | doh.f(domClass.contains(children[2], "dir"), "no (empty) dir attribute on child TreeNode"); |
| 689 | }, |
| 690 | |
| 691 | function tearDown(){ |
| 692 | myTree.destroy(); |
| 693 | myModel.destroy(); |
| 694 | } |
| 695 | ]); |
| 696 | |
| 697 | doh.register("expand/contract", [ |
| 698 | function reset(){ |
| 699 | setupStoreAndModel(Memory, true); |
| 700 | }, |
| 701 | |
| 702 | { |
| 703 | name: "initiallyExpanded", |
| 704 | timeout: 10000, |
| 705 | runTest: function(){ |
| 706 | myTree = new Tree({ |
| 707 | id: "myTreeExpand", |
| 708 | model: myModel, |
| 709 | persist: false, // persist==true is too hard to test |
| 710 | autoExpand: true |
| 711 | }).placeAt(win.body()); |
| 712 | doh.t(myTree, "tree created"); |
| 713 | |
| 714 | myTree.startup(); |
| 715 | |
| 716 | return myTree.onLoadDeferred.then(function(){ |
| 717 | var world = myTree.rootNode; |
| 718 | doh.t(world, "root node exists"); |
| 719 | doh.t(world.isExpanded, "root node is expanded"); |
| 720 | |
| 721 | var children = world.getChildren(); |
| 722 | doh.is(6, children.length, "world children"); |
| 723 | doh.t(children[0].isExpanded, "Africa expanded"); |
| 724 | doh.t(children[0].getChildren()[1].isExpanded, "Kenya expanded too"); |
| 725 | doh.is(2, children[0].getChildren()[1].getChildren().length, "Kenya children"); |
| 726 | doh.t(children[4].isExpanded, "North America expanded"); |
| 727 | doh.is(3, children[4].getChildren().length, "North America children"); |
| 728 | }); |
| 729 | } |
| 730 | }, |
| 731 | { |
| 732 | name: "collapseAll", |
| 733 | timeout: 10000, |
| 734 | runTest: function(){ |
| 735 | return myTree.collapseAll().then(function(){ |
| 736 | var world = myTree.rootNode; |
| 737 | doh.t(world, "root node exists"); |
| 738 | doh.f(world.isExpanded, "root node collapsed"); |
| 739 | |
| 740 | var children = world.getChildren(); |
| 741 | doh.is(6, children.length, "world children"); |
| 742 | doh.f(children[0].isExpanded, "Africa collapsed"); |
| 743 | doh.f(children[0].getChildren()[1].iscollapsed, "Kenya collapsed too"); |
| 744 | doh.is(2, children[0].getChildren()[1].getChildren().length, "Kenya children"); |
| 745 | doh.f(children[4].isExpanded, "North America collapsed"); |
| 746 | doh.is(3, children[4].getChildren().length, "North America children"); |
| 747 | }); |
| 748 | } |
| 749 | }, |
| 750 | { |
| 751 | name: "expandAll", |
| 752 | timeout: 10000, |
| 753 | runTest: function(){ |
| 754 | myTree.destroy(); |
| 755 | |
| 756 | myTree = new Tree({ |
| 757 | id: "myTreeExpand", |
| 758 | model: myModel, |
| 759 | persist: false, // persist==true is too hard to test |
| 760 | autoExpand: false |
| 761 | }).placeAt(win.body()); |
| 762 | doh.t(myTree, "tree created"); |
| 763 | |
| 764 | myTree.startup(); |
| 765 | |
| 766 | return myTree.onLoadDeferred.then(function(){ |
| 767 | var world = myTree.rootNode; |
| 768 | doh.t(world, "root node exists"); |
| 769 | doh.t(world.isExpanded, "root node is expanded"); |
| 770 | |
| 771 | var children = world.getChildren(); |
| 772 | doh.is(6, children.length, "world children"); |
| 773 | doh.f(children[0].isExpanded, "Africa collapsed"); |
| 774 | |
| 775 | return myTree.expandAll().then(function(){ |
| 776 | var children = world.getChildren(); |
| 777 | doh.t(children[0].isExpanded, "Africa expanded"); |
| 778 | doh.t(children[0].getChildren()[2].isExpanded, "Kenya expanded too"); |
| 779 | doh.t(children[4].isExpanded, "North America expanded"); |
| 780 | doh.is(3, children[4].getChildren().length, "North America children"); |
| 781 | }); |
| 782 | }); |
| 783 | } |
| 784 | }, |
| 785 | { |
| 786 | name: "collapseShowRootFalseTree", |
| 787 | timeout: 5000, |
| 788 | runTest: function(){ |
| 789 | myTree.destroy(); |
| 790 | |
| 791 | myTree = new Tree({ |
| 792 | id: "myTreeExpand", |
| 793 | model: myModel, |
| 794 | persist: false, // persist==true is too hard to test |
| 795 | showRoot: false |
| 796 | }).placeAt(win.body()); |
| 797 | doh.t(myTree, "tree created"); |
| 798 | |
| 799 | myTree.startup(); |
| 800 | |
| 801 | return myTree.onLoadDeferred.then(function(){ |
| 802 | var world = myTree.rootNode; |
| 803 | return myTree._expandNode(world.getChildren()[0]).then(function(){ |
| 804 | var children = world.getChildren(); |
| 805 | doh.t(children[0].isExpanded, "Africa expanded"); |
| 806 | |
| 807 | // While we are here, check that dijitTreeIsRoot class applied to first level of |
| 808 | // children, since the actual root node is hidden. |
| 809 | doh.t(domClass.contains(children[0].domNode, "dijitTreeIsRoot"), |
| 810 | "first level children have dijitTreeIsRoot"); |
| 811 | |
| 812 | return myTree.collapseAll().then(function(){ |
| 813 | doh.t(world, "root node exists"); |
| 814 | doh.t(world.isExpanded, "root node expanded (because it's hidden"); |
| 815 | |
| 816 | doh.f(children[0].isExpanded, "Africa collapsed"); |
| 817 | doh.t(helpers.isVisible(children[0]), "Africa node visible"); |
| 818 | }); |
| 819 | }); |
| 820 | }); |
| 821 | } |
| 822 | }, |
| 823 | |
| 824 | function tearDown(){ |
| 825 | myTree.destroy(); |
| 826 | myModel.destroy(); |
| 827 | } |
| 828 | ]); |
| 829 | |
| 830 | doh.register("persistence", [ |
| 831 | // Reset the data to original values |
| 832 | function reset(){ |
| 833 | setupStoreAndModel(); |
| 834 | }, |
| 835 | |
| 836 | { |
| 837 | name: "create", |
| 838 | timeout: 5000, |
| 839 | runTest: function(){ |
| 840 | if(registry.byId("myTree")){ |
| 841 | registry.byId("myTree").destroy(); |
| 842 | } |
| 843 | cookie("myTreeSaveStateCookie", ""); |
| 844 | |
| 845 | myTree = new Tree({ |
| 846 | id: "myTree", |
| 847 | model: myModel, |
| 848 | persist: true |
| 849 | }).placeAt(win.body()); |
| 850 | doh.t(myTree, "tree created"); |
| 851 | |
| 852 | myTree.startup(); |
| 853 | |
| 854 | return myTree.onLoadDeferred; |
| 855 | } |
| 856 | }, |
| 857 | |
| 858 | { |
| 859 | name: "expand and contract", |
| 860 | timeout: 5000, |
| 861 | runTest: function(){ |
| 862 | var d = new doh.Deferred(); |
| 863 | |
| 864 | var world = myTree.rootNode; |
| 865 | na = world.getChildren()[4]; |
| 866 | doh.is("North America", na.label, "North America node label"); |
| 867 | |
| 868 | // Click on North America to open it |
| 869 | on.emit(na.expandoNode, "click", {bubbles: true}); |
| 870 | |
| 871 | // Wait for animation to complete |
| 872 | setTimeout(d.getTestErrback(function(){ |
| 873 | mx = na.getChildren()[0]; |
| 874 | doh.is("Mexico", mx.label, "Mexico node label"); |
| 875 | |
| 876 | // Click on Mexico to open it |
| 877 | on.emit(mx.expandoNode, "click", {bubbles: true}); |
| 878 | |
| 879 | // Wait for animation to complete. |
| 880 | setTimeout(d.getTestErrback(function(){ |
| 881 | // Click on North America again, to close it |
| 882 | on.emit(na.expandoNode, "click", {bubbles: true}); |
| 883 | setTimeout(d.getTestCallback(function(){ |
| 884 | // Wait for close animation to finish and then return success |
| 885 | }), 500); |
| 886 | }), 500); |
| 887 | }), 500); |
| 888 | |
| 889 | return d; |
| 890 | } |
| 891 | }, |
| 892 | |
| 893 | { |
| 894 | name: "recreate tree", |
| 895 | timeout: 5000, |
| 896 | runTest: function(){ |
| 897 | myTree.destroy(); |
| 898 | |
| 899 | myTree = new Tree({ |
| 900 | id: "myTree", |
| 901 | model: myModel, |
| 902 | persist: true |
| 903 | }).placeAt(win.body()); |
| 904 | doh.t(myTree, "tree created"); |
| 905 | |
| 906 | myTree.startup(); |
| 907 | |
| 908 | return myTree.onLoadDeferred; |
| 909 | } |
| 910 | }, |
| 911 | |
| 912 | { |
| 913 | name: "check tree state", |
| 914 | timeout: 5000, |
| 915 | runTest: function(){ |
| 916 | var d = new doh.Deferred(); |
| 917 | |
| 918 | var world = myTree.rootNode; |
| 919 | var na = world.getChildren()[4]; |
| 920 | doh.is("North America", na.label, "North America node label"); |
| 921 | |
| 922 | doh.f(na.isExpanded, "North America collapsed"); |
| 923 | |
| 924 | // Click on North America to open it |
| 925 | on.emit(na.expandoNode, "click", {bubbles: true}); |
| 926 | |
| 927 | setTimeout(d.getTestCallback(function(){ |
| 928 | doh.t(na.isExpanded, "North America expanded"); |
| 929 | mx = na.getChildren()[0]; |
| 930 | doh.is("Mexico", mx.label, "Mexico node label"); |
| 931 | doh.t(mx.isExpanded, "Mexico expanded"); |
| 932 | }), 500); |
| 933 | |
| 934 | return d; |
| 935 | } |
| 936 | }, |
| 937 | |
| 938 | function tearDown(){ |
| 939 | myTree.destroy(); |
| 940 | myModel.destroy(); |
| 941 | } |
| 942 | ]); |
| 943 | |
| 944 | // Tests that TreeNodes that are destroyed don't linger in selection, etc. |
| 945 | doh.register("destroyed nodes", [ |
| 946 | // Reset the data to original values |
| 947 | function reset(){ |
| 948 | setupStoreAndModel(Memory, true); |
| 949 | }, |
| 950 | |
| 951 | { |
| 952 | name: "create", |
| 953 | timeout: 5000, |
| 954 | runTest: function(){ |
| 955 | cookie("myDeleteTreeSaveStateCookie", ""); |
| 956 | myTree = new Tree({ |
| 957 | id: "myDeleteTree", |
| 958 | model: myModel, |
| 959 | persist: true |
| 960 | }).placeAt(win.body()); |
| 961 | doh.t(myTree, "tree created"); |
| 962 | |
| 963 | myTree.startup(); |
| 964 | doh.is("earth", cookie("myDeleteTreeSaveStateCookie"), "initial persistence cookie just earth"); |
| 965 | } |
| 966 | }, |
| 967 | |
| 968 | { |
| 969 | name: "removedFromSelection", |
| 970 | timeout: 10000, |
| 971 | runTest: function(){ |
| 972 | var d = new doh.Deferred(); |
| 973 | // Select Mexico city |
| 974 | myTree.set("path", ["earth", "NA", "MX", "Mexico City"]).then(d.getTestErrback(function(){ |
| 975 | doh.is(1, myTree.get("paths").length, "one selected node"); |
| 976 | var selectedNode = myTree.get("selectedNode"); |
| 977 | |
| 978 | // Ancestors of the selected node should be expanded, and that should be saved |
| 979 | // in the cookie |
| 980 | doh.is("earth,earth/NA,earth/NA/MX", cookie("myDeleteTreeSaveStateCookie"), |
| 981 | "Earth, North America, Mexico should be listed as expanded"); |
| 982 | |
| 983 | // Remove selection's ancestor |
| 984 | myStore.remove("NA"); |
| 985 | |
| 986 | // use setTimeout() to give Tree time to realize this isn't a drag & drop, but that |
| 987 | // the North America subtree was truly deleted |
| 988 | setTimeout(d.getTestCallback(function(){ |
| 989 | doh.t(selectedNode._destroyed, "the selected node was destroyed (along with it's ancestor)"); |
| 990 | doh.is(0, myTree.get("paths").length, "no selected nodes"); |
| 991 | doh.is("earth", cookie("myDeleteTreeSaveStateCookie"), |
| 992 | "North America and Mexico should have been removed from list of expanded nodes") |
| 993 | }), 100); |
| 994 | })); |
| 995 | return d; |
| 996 | } |
| 997 | }, |
| 998 | |
| 999 | { |
| 1000 | name: "reinsert deleted descendants", |
| 1001 | timeout: 5000, |
| 1002 | runTest: function(){ |
| 1003 | myStore.put({ id: "NA", name:"North America", type:"continent", parent: "earth" }); |
| 1004 | myStore.put({ id: "MX", name:"Mexico", type:"country", population:"108 million", area:"1,972,550 sq km", |
| 1005 | parent: "NA" }); |
| 1006 | myStore.put({ id: "Mexico City", name:"Mexico City", type:"city", population:"19 million", timezone:"-6 UTC", parent: "MX"}); |
| 1007 | myStore.put({ id: "Guadalajara", name:"Guadalajara", type:"city", population:"4 million", timezone:"-6 UTC", parent: "MX" }); |
| 1008 | |
| 1009 | return myTree.set("path", ["earth", "NA", "MX", "Mexico City"]).then(function(){ |
| 1010 | var node = myTree.get("selectedNode"); |
| 1011 | doh.isNot(null, node, "got a selected node"); |
| 1012 | doh.f(node._destroyed, "not destroyed"); |
| 1013 | doh.f(node.getParent()._destroyed, "parent not destroyed"); |
| 1014 | doh.f(node.getParent().getParent()._destroyed, "grandparent not destroyed"); |
| 1015 | }); |
| 1016 | } |
| 1017 | }, |
| 1018 | |
| 1019 | function tearDown(){ |
| 1020 | myTree.destroy(); |
| 1021 | myModel.destroy(); |
| 1022 | } |
| 1023 | ]); |
| 1024 | |
| 1025 | doh.register("HTML tree", { |
| 1026 | name: "create", |
| 1027 | timeout: 10000, |
| 1028 | runTest: function(){ |
| 1029 | var d = new doh.Deferred(); |
| 1030 | |
| 1031 | // Create test store. |
| 1032 | htmlData = array.map(data, function(o){ |
| 1033 | var n = lang.clone(o); |
| 1034 | n.name = "<i>" + o.name + "</i>"; |
| 1035 | return n; |
| 1036 | }); |
| 1037 | htmlStore = new Memory({ |
| 1038 | data: htmlData |
| 1039 | }); |
| 1040 | |
| 1041 | // Since dojo.store.Memory doesn't have various store methods we need, we have to add them manually |
| 1042 | htmlStore.getChildren = function(object){ |
| 1043 | // Add a getChildren() method to store for the data model where |
| 1044 | // children objects point to their parent (aka relational model) |
| 1045 | return this.query({parent: this.getIdentity(object)}); |
| 1046 | }; |
| 1047 | |
| 1048 | // Create the model |
| 1049 | htmlModel = new ObjectStoreModel({ |
| 1050 | store: htmlStore, |
| 1051 | labelType: "html", |
| 1052 | query: {id: "earth"} |
| 1053 | }); |
| 1054 | doh.t(htmlModel, "model created"); |
| 1055 | |
| 1056 | htmlTree = new Tree({ |
| 1057 | id: "htmlTree", |
| 1058 | model: htmlModel |
| 1059 | }).placeAt(win.body()); |
| 1060 | doh.t(htmlTree, "tree created"); |
| 1061 | |
| 1062 | htmlTree.startup(); |
| 1063 | |
| 1064 | world = htmlTree.rootNode; |
| 1065 | |
| 1066 | return htmlTree.expandAll().then(function(){ |
| 1067 | doh.t(world, "root node exists"); |
| 1068 | doh.is("<i>The earth</i>", world.label, "world node label"); |
| 1069 | doh.is("<i>the earth</i>", world.labelNode.innerHTML.toLowerCase(), "world node labelNode.innerHTML"); |
| 1070 | |
| 1071 | var children = world.getChildren(); |
| 1072 | doh.is("<i>Africa</i>", children[0].label, "first child"); |
| 1073 | doh.is("<i>africa</i>", children[0].labelNode.innerHTML.toLowerCase(), "first child labelNode.innerHTML"); |
| 1074 | }); |
| 1075 | }, |
| 1076 | |
| 1077 | tearDown: function(){ |
| 1078 | htmlTree.destroy(); |
| 1079 | htmlModel.destroy(); |
| 1080 | } |
| 1081 | }); |
| 1082 | |
| 1083 | doh.register("plain text tree", { |
| 1084 | name: "create", |
| 1085 | timeout: 10000, |
| 1086 | runTest: function(){ |
| 1087 | var d = new doh.Deferred(); |
| 1088 | |
| 1089 | // Create test store. |
| 1090 | plainTextData = array.map(data, function(o){ |
| 1091 | var n = lang.clone(o); |
| 1092 | n.name = "<>&" + o.name; |
| 1093 | return n; |
| 1094 | }); |
| 1095 | plainTextStore = new Memory({ |
| 1096 | data: plainTextData |
| 1097 | }); |
| 1098 | |
| 1099 | // Since dojo.store.Memory doesn't have various store methods we need, we have to add them manually |
| 1100 | plainTextStore.getChildren = function(object){ |
| 1101 | // Add a getChildren() method to store for the data model where |
| 1102 | // children objects point to their parent (aka relational model) |
| 1103 | return this.query({parent: this.getIdentity(object)}); |
| 1104 | }; |
| 1105 | |
| 1106 | // Create the model |
| 1107 | plainTextModel = new ObjectStoreModel({ |
| 1108 | store: plainTextStore, |
| 1109 | labelType: "text", |
| 1110 | query: {id: "earth"} |
| 1111 | }); |
| 1112 | doh.t(plainTextModel, "model created"); |
| 1113 | |
| 1114 | plainTextTree = new Tree({ |
| 1115 | id: "plainTextTree", |
| 1116 | model: plainTextModel |
| 1117 | }).placeAt(win.body()); |
| 1118 | doh.t(plainTextTree, "tree created"); |
| 1119 | |
| 1120 | plainTextTree.startup(); |
| 1121 | |
| 1122 | world = plainTextTree.rootNode; |
| 1123 | |
| 1124 | return plainTextTree.expandAll().then(function(){ |
| 1125 | doh.t(world, "root node exists"); |
| 1126 | doh.is("<>&The earth", world.label, "world node label"); |
| 1127 | doh.is("<>&The earth", helpers.innerText(world.labelNode), "world node labelNode.innerText"); |
| 1128 | |
| 1129 | var children = world.getChildren(); |
| 1130 | doh.is("<>&Africa", helpers.innerText(children[0].labelNode), "first child"); |
| 1131 | }); |
| 1132 | }, |
| 1133 | |
| 1134 | tearDown: function(){ |
| 1135 | plainTextTree.destroy(); |
| 1136 | plainTextModel.destroy(); |
| 1137 | } |
| 1138 | }); |
| 1139 | |
| 1140 | // Repeat of suite in Tree_a11y.html, but here to run against ObjectStoreModel instead of TreeStoreModel. |
| 1141 | var myTree_before = document.createElement("input"); |
| 1142 | doh.register("destroy and focus", [ |
| 1143 | { |
| 1144 | name: "create", |
| 1145 | timeout: 5000, |
| 1146 | runTest: function(){ |
| 1147 | setupStoreAndModel(Memory, true); |
| 1148 | |
| 1149 | win.body().appendChild(myTree_before); |
| 1150 | myTree = new Tree({ |
| 1151 | id: "myTree", |
| 1152 | model: myModel, |
| 1153 | persist: false, // persist==true is too hard to test |
| 1154 | dndController: dndSource |
| 1155 | }).placeAt(win.body()); |
| 1156 | doh.t(myTree, "tree created"); |
| 1157 | |
| 1158 | myTree.startup(); |
| 1159 | |
| 1160 | return myTree.expandAll(); |
| 1161 | } |
| 1162 | }, |
| 1163 | |
| 1164 | { |
| 1165 | name: "destroy previously focused node", |
| 1166 | timeout: 20000, |
| 1167 | runTest: function(){ |
| 1168 | var d = new doh.Deferred(); |
| 1169 | |
| 1170 | // Go to Africa node |
| 1171 | var rootNode = registry.byId("myTree").getChildren()[0]; |
| 1172 | rootNode.getChildren()[0].focus(); |
| 1173 | |
| 1174 | setTimeout(d.getTestErrback(function(){ |
| 1175 | doh.is("Africa", myTree.focusedChild.label, "focus"); |
| 1176 | var previouslyFocusedChild = myTree.focusedChild; |
| 1177 | |
| 1178 | // Tab out of Tree, and then destroy previously focused node. |
| 1179 | myTree_before.focus(); |
| 1180 | setTimeout(d.getTestErrback(function(){ |
| 1181 | var store = myTree.model.store; |
| 1182 | store.remove("AF"); |
| 1183 | |
| 1184 | // Refocus Tree. Since previously focused node was destroyed, |
| 1185 | // focus should go to The earth. |
| 1186 | setTimeout(d.getTestErrback(function(){ |
| 1187 | myTree.focus(); |
| 1188 | setTimeout(d.getTestCallback(function(){ |
| 1189 | doh.is("The earth", focus.curNode.innerHTML, "focus"); |
| 1190 | }), 500); |
| 1191 | }), 500); |
| 1192 | }), 500); |
| 1193 | }), 500); |
| 1194 | |
| 1195 | return d; |
| 1196 | } |
| 1197 | }, |
| 1198 | |
| 1199 | { |
| 1200 | name: "destroy previously focused node's ancestor", |
| 1201 | timeout: 20000, |
| 1202 | runTest: function(){ |
| 1203 | var d = new doh.Deferred(); |
| 1204 | |
| 1205 | // Go to China node |
| 1206 | var rootNode = registry.byId("myTree").getChildren()[0]; |
| 1207 | rootNode.getChildren()[0].getChildren()[0].focus(); |
| 1208 | |
| 1209 | setTimeout(d.getTestErrback(function(){ |
| 1210 | doh.is("China", myTree.focusedChild.label, "focus"); |
| 1211 | var previouslyFocusedChild = myTree.focusedChild; |
| 1212 | |
| 1213 | // Tab out of Tree, and then destroy previously focused node's ancestor |
| 1214 | myTree_before.focus(); |
| 1215 | setTimeout(d.getTestErrback(function(){ |
| 1216 | var store = myTree.model.store; |
| 1217 | store.remove("AS"); |
| 1218 | |
| 1219 | // Refocus Tree. Since previously focused node was destroyed, |
| 1220 | // focus should go to The earth. |
| 1221 | setTimeout(d.getTestErrback(function(){ |
| 1222 | myTree.focus(); |
| 1223 | setTimeout(d.getTestCallback(function(){ |
| 1224 | doh.is("The earth", focus.curNode.innerHTML, "focus"); |
| 1225 | }), 500); |
| 1226 | }), 500); |
| 1227 | }), 500); |
| 1228 | }), 500); |
| 1229 | |
| 1230 | return d; |
| 1231 | } |
| 1232 | }, |
| 1233 | |
| 1234 | { |
| 1235 | name: "destroy currently focused node", |
| 1236 | timeout: 4000, |
| 1237 | runTest: function(){ |
| 1238 | var d = new doh.Deferred(); |
| 1239 | |
| 1240 | // Go to Oceania node |
| 1241 | var rootNode = registry.byId("myTree").getChildren()[0]; |
| 1242 | rootNode.getChildren()[0].focus(); |
| 1243 | |
| 1244 | setTimeout(d.getTestErrback(function(){ |
| 1245 | doh.is("Oceania", myTree.focusedChild.label, "focus"); |
| 1246 | |
| 1247 | // Destroy currently focused node |
| 1248 | setTimeout(d.getTestErrback(function(){ |
| 1249 | var store = myTree.model.store; |
| 1250 | store.remove("OC"); |
| 1251 | |
| 1252 | // Focus should go to The earth |
| 1253 | setTimeout(d.getTestCallback(function(){ |
| 1254 | doh.is("The earth", focus.curNode.innerHTML, "focus"); |
| 1255 | }), 500); |
| 1256 | }), 500); |
| 1257 | }), 500); |
| 1258 | |
| 1259 | return d; |
| 1260 | } |
| 1261 | }, |
| 1262 | |
| 1263 | { |
| 1264 | name: "destroy currently focused node's ancestor", |
| 1265 | timeout: 4000, |
| 1266 | runTest: function(){ |
| 1267 | var d = new doh.Deferred(); |
| 1268 | |
| 1269 | // Go to Germany node |
| 1270 | var rootNode = registry.byId("myTree").getChildren()[0]; |
| 1271 | rootNode.getChildren()[0].getChildren()[0].focus(); |
| 1272 | |
| 1273 | setTimeout(d.getTestErrback(function(){ |
| 1274 | doh.is("Germany", myTree.focusedChild.label, "focus"); |
| 1275 | |
| 1276 | // Destroy currently focused node's ancestor |
| 1277 | setTimeout(d.getTestErrback(function(){ |
| 1278 | var store = myTree.model.store; |
| 1279 | store.remove("EU"); |
| 1280 | |
| 1281 | // Focus should go to The earth |
| 1282 | setTimeout(d.getTestCallback(function(){ |
| 1283 | doh.is("The earth", focus.curNode.innerHTML, "focus"); |
| 1284 | }), 500); |
| 1285 | }), 500); |
| 1286 | }), 500); |
| 1287 | |
| 1288 | return d; |
| 1289 | } |
| 1290 | } |
| 1291 | ]); |
| 1292 | |
| 1293 | doh.run(); |
| 1294 | |
| 1295 | }); |
| 1296 | </script> |
| 1297 | |
| 1298 | </head> |
| 1299 | <body class="claro" role="main"> |
| 1300 | |
| 1301 | <h1 class="testTitle">dijit/Tree using dijit/tree/ObjectStore (against dojo/store) Automated Test</h1> |
| 1302 | |
| 1303 | </body> |
| 1304 | </html> |