1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/accessible/tests/mochitest/treeview.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,295 @@ 1.4 +/** 1.5 + * Helper method to start a single XUL tree test. 1.6 + */ 1.7 +function loadXULTreeAndDoTest(aDoTestFunc, aTreeID, aTreeView) 1.8 +{ 1.9 + var doTestFunc = aDoTestFunc ? aDoTestFunc : gXULTreeLoadContext.doTestFunc; 1.10 + var treeID = aTreeID ? aTreeID : gXULTreeLoadContext.treeID; 1.11 + var treeView = aTreeView ? aTreeView : gXULTreeLoadContext.treeView; 1.12 + 1.13 + function loadXULTree(aTreeID, aTreeView) 1.14 + { 1.15 + this.treeNode = getNode(aTreeID); 1.16 + 1.17 + this.eventSeq = [ 1.18 + new invokerChecker(EVENT_REORDER, this.treeNode) 1.19 + ]; 1.20 + 1.21 + this.invoke = function loadXULTree_invoke() 1.22 + { 1.23 + this.treeNode.treeBoxObject.view = aTreeView; 1.24 + } 1.25 + 1.26 + this.getID = function loadXULTree_getID() 1.27 + { 1.28 + return "Load XUL tree " + prettyName(aTreeID); 1.29 + } 1.30 + } 1.31 + 1.32 + gXULTreeLoadContext.queue = new eventQueue(); 1.33 + gXULTreeLoadContext.queue.push(new loadXULTree(treeID, treeView)); 1.34 + gXULTreeLoadContext.queue.onFinish = function() 1.35 + { 1.36 + SimpleTest.executeSoon(doTestFunc); 1.37 + return DO_NOT_FINISH_TEST; 1.38 + } 1.39 + gXULTreeLoadContext.queue.invoke(); 1.40 +} 1.41 + 1.42 +/** 1.43 + * Analogy of addA11yLoadEvent, nice helper to load XUL tree and start the test. 1.44 + */ 1.45 +function addA11yXULTreeLoadEvent(aDoTestFunc, aTreeID, aTreeView) 1.46 +{ 1.47 + gXULTreeLoadContext.doTestFunc = aDoTestFunc; 1.48 + gXULTreeLoadContext.treeID = aTreeID; 1.49 + gXULTreeLoadContext.treeView = aTreeView; 1.50 + 1.51 + addA11yLoadEvent(loadXULTreeAndDoTest); 1.52 +} 1.53 + 1.54 + 1.55 +function nsTableTreeView(aRowCount) 1.56 +{ 1.57 + this.__proto__ = new nsTreeView(); 1.58 + 1.59 + for (var idx = 0; idx < aRowCount; idx++) 1.60 + this.mData.push(new treeItem("row" + String(idx) + "_")); 1.61 +} 1.62 + 1.63 +function nsTreeTreeView() 1.64 +{ 1.65 + this.__proto__ = new nsTreeView(); 1.66 + 1.67 + this.mData = [ 1.68 + new treeItem("row1"), 1.69 + new treeItem("row2_", true, [new treeItem("row2.1_"), new treeItem("row2.2_")]), 1.70 + new treeItem("row3_", false, [new treeItem("row3.1_"), new treeItem("row3.2_")]), 1.71 + new treeItem("row4") 1.72 + ]; 1.73 +} 1.74 + 1.75 +function nsTreeView() 1.76 +{ 1.77 + this.mTree = null; 1.78 + this.mData = []; 1.79 +} 1.80 + 1.81 +nsTreeView.prototype = 1.82 +{ 1.83 + ////////////////////////////////////////////////////////////////////////////// 1.84 + // nsITreeView implementation 1.85 + 1.86 + get rowCount() 1.87 + { 1.88 + return this.getRowCountIntl(this.mData); 1.89 + }, 1.90 + setTree: function setTree(aTree) 1.91 + { 1.92 + this.mTree = aTree; 1.93 + }, 1.94 + getCellText: function getCellText(aRow, aCol) 1.95 + { 1.96 + var data = this.getDataForIndex(aRow); 1.97 + if (aCol.id in data.colsText) 1.98 + return data.colsText[aCol.id]; 1.99 + 1.100 + return data.text + aCol.id; 1.101 + }, 1.102 + getCellValue: function getCellValue(aRow, aCol) 1.103 + { 1.104 + var data = this.getDataForIndex(aRow); 1.105 + return data.value; 1.106 + }, 1.107 + getRowProperties: function getRowProperties(aIndex) { return ""; }, 1.108 + getCellProperties: function getCellProperties(aIndex, aCol) 1.109 + { 1.110 + if (!aCol.cycler) 1.111 + return ""; 1.112 + 1.113 + var data = this.getDataForIndex(aIndex); 1.114 + return this.mCyclerStates[data.cyclerState]; 1.115 + }, 1.116 + getColumnProperties: function getColumnProperties(aCol) { return ""; }, 1.117 + getParentIndex: function getParentIndex(aRowIndex) 1.118 + { 1.119 + var info = this.getInfoByIndex(aRowIndex); 1.120 + return info.parentIndex; 1.121 + }, 1.122 + hasNextSibling: function hasNextSibling(aRowIndex, aAfterIndex) { }, 1.123 + getLevel: function getLevel(aIndex) 1.124 + { 1.125 + var info = this.getInfoByIndex(aIndex); 1.126 + return info.level; 1.127 + }, 1.128 + getImageSrc: function getImageSrc(aRow, aCol) {}, 1.129 + getProgressMode: function getProgressMode(aRow, aCol) {}, 1.130 + isContainer: function isContainer(aIndex) 1.131 + { 1.132 + var data = this.getDataForIndex(aIndex); 1.133 + return data.open != undefined; 1.134 + }, 1.135 + isContainerOpen: function isContainerOpen(aIndex) 1.136 + { 1.137 + var data = this.getDataForIndex(aIndex); 1.138 + return data.open; 1.139 + }, 1.140 + isContainerEmpty: function isContainerEmpty(aIndex) 1.141 + { 1.142 + var data = this.getDataForIndex(aIndex); 1.143 + return data.open == undefined; 1.144 + }, 1.145 + isSeparator: function isSeparator(aIndex) {}, 1.146 + isSorted: function isSorted() {}, 1.147 + toggleOpenState: function toggleOpenState(aIndex) 1.148 + { 1.149 + var data = this.getDataForIndex(aIndex); 1.150 + 1.151 + data.open = !data.open; 1.152 + var rowCount = this.getRowCountIntl(data.children); 1.153 + 1.154 + if (data.open) 1.155 + this.mTree.rowCountChanged(aIndex + 1, rowCount); 1.156 + else 1.157 + this.mTree.rowCountChanged(aIndex + 1, -rowCount); 1.158 + }, 1.159 + selectionChanged: function selectionChanged() {}, 1.160 + cycleHeader: function cycleHeader(aCol) {}, 1.161 + cycleCell: function cycleCell(aRow, aCol) 1.162 + { 1.163 + var data = this.getDataForIndex(aRow); 1.164 + data.cyclerState = (data.cyclerState + 1) % 3; 1.165 + 1.166 + this.mTree.invalidateCell(aRow, aCol); 1.167 + }, 1.168 + isEditable: function isEditable(aRow, aCol) 1.169 + { 1.170 + return true; 1.171 + }, 1.172 + isSelectable: function isSelectable(aRow, aCol) {}, 1.173 + setCellText: function setCellText(aRow, aCol, aValue) 1.174 + { 1.175 + var data = this.getDataForIndex(aRow); 1.176 + data.colsText[aCol.id] = aValue; 1.177 + }, 1.178 + setCellValue: function setCellValue(aRow, aCol, aValue) 1.179 + { 1.180 + var data = this.getDataForIndex(aRow); 1.181 + data.value = aValue; 1.182 + 1.183 + this.mTree.invalidateCell(aRow, aCol); 1.184 + }, 1.185 + performAction: function performAction(aAction) {}, 1.186 + performActionOnRow: function performActionOnRow(aAction, aRow) {}, 1.187 + performActionOnCell: function performActionOnCell(aAction, aRow, aCol) {}, 1.188 + 1.189 + ////////////////////////////////////////////////////////////////////////////// 1.190 + // public implementation 1.191 + 1.192 + appendItem: function appendItem(aText) 1.193 + { 1.194 + this.mData.push(new treeItem(aText)); 1.195 + }, 1.196 + 1.197 + ////////////////////////////////////////////////////////////////////////////// 1.198 + // private implementation 1.199 + 1.200 + getDataForIndex: function getDataForIndex(aRowIndex) 1.201 + { 1.202 + return this.getInfoByIndex(aRowIndex).data; 1.203 + }, 1.204 + 1.205 + getInfoByIndex: function getInfoByIndex(aRowIndex) 1.206 + { 1.207 + var info = { 1.208 + data: null, 1.209 + parentIndex: -1, 1.210 + level: 0, 1.211 + index: -1 1.212 + }; 1.213 + 1.214 + this.getInfoByIndexIntl(aRowIndex, info, this.mData, 0); 1.215 + return info; 1.216 + }, 1.217 + 1.218 + getRowCountIntl: function getRowCountIntl(aChildren) 1.219 + { 1.220 + var rowCount = 0; 1.221 + for (var childIdx = 0; childIdx < aChildren.length; childIdx++) { 1.222 + rowCount++; 1.223 + 1.224 + var data = aChildren[childIdx]; 1.225 + if (data.open) 1.226 + rowCount += this.getRowCountIntl(data.children); 1.227 + } 1.228 + 1.229 + return rowCount; 1.230 + }, 1.231 + 1.232 + getInfoByIndexIntl: function getInfoByIndexIntl(aRowIdx, aInfo, 1.233 + aChildren, aLevel) 1.234 + { 1.235 + var rowIdx = aRowIdx; 1.236 + for (var childIdx = 0; childIdx < aChildren.length; childIdx++) { 1.237 + var data = aChildren[childIdx]; 1.238 + 1.239 + aInfo.index++; 1.240 + 1.241 + if (rowIdx == 0) { 1.242 + aInfo.data = data; 1.243 + aInfo.level = aLevel; 1.244 + return -1; 1.245 + } 1.246 + 1.247 + if (data.open) { 1.248 + var parentIdx = aInfo.index; 1.249 + rowIdx = this.getInfoByIndexIntl(rowIdx - 1, aInfo, data.children, 1.250 + aLevel + 1); 1.251 + 1.252 + if (rowIdx == -1) { 1.253 + if (aInfo.parentIndex == -1) 1.254 + aInfo.parentIndex = parentIdx; 1.255 + return 0; 1.256 + } 1.257 + } else { 1.258 + rowIdx--; 1.259 + } 1.260 + } 1.261 + 1.262 + return rowIdx; 1.263 + }, 1.264 + 1.265 + mCyclerStates: [ 1.266 + "cyclerState1", 1.267 + "cyclerState2", 1.268 + "cyclerState3" 1.269 + ] 1.270 +}; 1.271 + 1.272 +function treeItem(aText, aOpen, aChildren) 1.273 +{ 1.274 + this.text = aText; 1.275 + this.colsText = {}; 1.276 + this.open = aOpen; 1.277 + this.value = "true"; 1.278 + this.cyclerState = 0; 1.279 + if (aChildren) 1.280 + this.children = aChildren; 1.281 +} 1.282 + 1.283 +function createAtom(aName) 1.284 +{ 1.285 + return Components.classes["@mozilla.org/atom-service;1"] 1.286 + .getService(Components.interfaces.nsIAtomService).getAtom(aName); 1.287 +} 1.288 + 1.289 +/** 1.290 + * Used in conjunction with loadXULTreeAndDoTest and addA11yXULTreeLoadEvent. 1.291 + */ 1.292 +var gXULTreeLoadContext = 1.293 +{ 1.294 + doTestFunc: null, 1.295 + treeID: null, 1.296 + treeView: null, 1.297 + queue: null 1.298 +};