summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/treeview.js
blob: 5dc3b595d82c539bf2288ad2d5e12e35a1993369 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* import-globals-from common.js */
/* import-globals-from events.js */

/**
 * Helper method to start a single XUL tree test.
 */
function loadXULTreeAndDoTest(aDoTestFunc, aTreeID, aTreeView) {
  var doTestFunc = aDoTestFunc ? aDoTestFunc : gXULTreeLoadContext.doTestFunc;
  var treeID = aTreeID ? aTreeID : gXULTreeLoadContext.treeID;
  var treeView = aTreeView ? aTreeView : gXULTreeLoadContext.treeView;

  let treeNode = getNode(treeID);

  gXULTreeLoadContext.queue = new eventQueue();
  gXULTreeLoadContext.queue.push({
    treeNode,

    eventSeq: [new invokerChecker(EVENT_REORDER, treeNode)],

    invoke() {
      this.treeNode.view = treeView;
    },

    getID() {
      return "Load XUL tree " + prettyName(treeID);
    },
  });
  gXULTreeLoadContext.queue.onFinish = function () {
    SimpleTest.executeSoon(doTestFunc);
    return DO_NOT_FINISH_TEST;
  };
  gXULTreeLoadContext.queue.invoke();
}

/**
 * Analogy of addA11yLoadEvent, nice helper to load XUL tree and start the test.
 */
function addA11yXULTreeLoadEvent(aDoTestFunc, aTreeID, aTreeView) {
  gXULTreeLoadContext.doTestFunc = aDoTestFunc;
  gXULTreeLoadContext.treeID = aTreeID;
  gXULTreeLoadContext.treeView = aTreeView;

  addA11yLoadEvent(loadXULTreeAndDoTest);
}

function nsTableTreeView(aRowCount) {
  this.__proto__ = new nsTreeView();

  for (var idx = 0; idx < aRowCount; idx++) {
    this.mData.push(new treeItem("row" + String(idx) + "_"));
  }
}

function nsTreeTreeView() {
  this.__proto__ = new nsTreeView();

  this.mData = [
    new treeItem("row1"),
    new treeItem("row2_", true, [
      new treeItem("row2.1_"),
      new treeItem("row2.2_"),
    ]),
    new treeItem("row3_", false, [
      new treeItem("row3.1_"),
      new treeItem("row3.2_"),
    ]),
    new treeItem("row4"),
  ];
}

function nsTreeView() {
  this.mTree = null;
  this.mData = [];
}

nsTreeView.prototype = {
  // ////////////////////////////////////////////////////////////////////////////
  // nsITreeView implementation

  get rowCount() {
    return this.getRowCountIntl(this.mData);
  },
  setTree: function setTree(aTree) {
    this.mTree = aTree;
  },
  getCellText: function getCellText(aRow, aCol) {
    var data = this.getDataForIndex(aRow);
    if (aCol.id in data.colsText) {
      return data.colsText[aCol.id];
    }

    return data.text + aCol.id;
  },
  getCellValue: function getCellValue(aRow, aCol) {
    var data = this.getDataForIndex(aRow);
    return data.value;
  },
  getRowProperties: function getRowProperties(aIndex) {
    return "";
  },
  getCellProperties: function getCellProperties(aIndex, aCol) {
    if (!aCol.cycler) {
      return "";
    }

    var data = this.getDataForIndex(aIndex);
    return this.mCyclerStates[data.cyclerState];
  },
  getColumnProperties: function getColumnProperties(aCol) {
    return "";
  },
  getParentIndex: function getParentIndex(aRowIndex) {
    var info = this.getInfoByIndex(aRowIndex);
    return info.parentIndex;
  },
  hasNextSibling: function hasNextSibling(aRowIndex, aAfterIndex) {},
  getLevel: function getLevel(aIndex) {
    var info = this.getInfoByIndex(aIndex);
    return info.level;
  },
  getImageSrc: function getImageSrc(aRow, aCol) {},
  isContainer: function isContainer(aIndex) {
    var data = this.getDataForIndex(aIndex);
    return data.open != undefined;
  },
  isContainerOpen: function isContainerOpen(aIndex) {
    var data = this.getDataForIndex(aIndex);
    return data.open;
  },
  isContainerEmpty: function isContainerEmpty(aIndex) {
    var data = this.getDataForIndex(aIndex);
    return data.open == undefined;
  },
  isSeparator: function isSeparator(aIndex) {},
  isSorted: function isSorted() {},
  toggleOpenState: function toggleOpenState(aIndex) {
    var data = this.getDataForIndex(aIndex);

    data.open = !data.open;
    var rowCount = this.getRowCountIntl(data.children);

    if (data.open) {
      this.mTree.rowCountChanged(aIndex + 1, rowCount);
    } else {
      this.mTree.rowCountChanged(aIndex + 1, -rowCount);
    }
  },
  selectionChanged: function selectionChanged() {},
  cycleHeader: function cycleHeader(aCol) {},
  cycleCell: function cycleCell(aRow, aCol) {
    var data = this.getDataForIndex(aRow);
    data.cyclerState = (data.cyclerState + 1) % 3;

    this.mTree.invalidateCell(aRow, aCol);
  },
  isEditable: function isEditable(aRow, aCol) {
    return true;
  },
  setCellText: function setCellText(aRow, aCol, aValue) {
    var data = this.getDataForIndex(aRow);
    data.colsText[aCol.id] = aValue;
  },
  setCellValue: function setCellValue(aRow, aCol, aValue) {
    var data = this.getDataForIndex(aRow);
    data.value = aValue;

    this.mTree.invalidateCell(aRow, aCol);
  },

  // ////////////////////////////////////////////////////////////////////////////
  // public implementation

  appendItem: function appendItem(aText) {
    this.mData.push(new treeItem(aText));
  },

  // ////////////////////////////////////////////////////////////////////////////
  // private implementation

  getDataForIndex: function getDataForIndex(aRowIndex) {
    return this.getInfoByIndex(aRowIndex).data;
  },

  getInfoByIndex: function getInfoByIndex(aRowIndex) {
    var info = {
      data: null,
      parentIndex: -1,
      level: 0,
      index: -1,
    };

    this.getInfoByIndexIntl(aRowIndex, info, this.mData, 0);
    return info;
  },

  getRowCountIntl: function getRowCountIntl(aChildren) {
    var rowCount = 0;
    for (var childIdx = 0; childIdx < aChildren.length; childIdx++) {
      rowCount++;

      var data = aChildren[childIdx];
      if (data.open) {
        rowCount += this.getRowCountIntl(data.children);
      }
    }

    return rowCount;
  },

  getInfoByIndexIntl: function getInfoByIndexIntl(
    aRowIdx,
    aInfo,
    aChildren,
    aLevel
  ) {
    var rowIdx = aRowIdx;
    for (var childIdx = 0; childIdx < aChildren.length; childIdx++) {
      var data = aChildren[childIdx];

      aInfo.index++;

      if (rowIdx == 0) {
        aInfo.data = data;
        aInfo.level = aLevel;
        return -1;
      }

      if (data.open) {
        var parentIdx = aInfo.index;
        rowIdx = this.getInfoByIndexIntl(
          rowIdx - 1,
          aInfo,
          data.children,
          aLevel + 1
        );

        if (rowIdx == -1) {
          if (aInfo.parentIndex == -1) {
            aInfo.parentIndex = parentIdx;
          }
          return 0;
        }
      } else {
        rowIdx--;
      }
    }

    return rowIdx;
  },

  mCyclerStates: ["cyclerState1", "cyclerState2", "cyclerState3"],
};

function treeItem(aText, aOpen, aChildren) {
  this.text = aText;
  this.colsText = {};
  this.open = aOpen;
  this.value = "true";
  this.cyclerState = 0;
  if (aChildren) {
    this.children = aChildren;
  }
}

/**
 * Used in conjunction with loadXULTreeAndDoTest and addA11yXULTreeLoadEvent.
 */
var gXULTreeLoadContext = {
  doTestFunc: null,
  treeID: null,
  treeView: null,
  queue: null,
};