summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/selectable.js
blob: e71fca489b3b44a39c8db7a3ffbba0b40efa414a (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
/* import-globals-from common.js */
/* import-globals-from states.js */

/**
 * Test selection getter methods of nsIAccessibleSelectable.
 *
 * @param aIdentifier        [in] selectable container accessible
 * @param aSelectedChildren  [in] array of selected children
 */
function testSelectableSelection(aIdentifier, aSelectedChildren, aMsg) {
  var acc = getAccessible(aIdentifier, [nsIAccessibleSelectable]);
  if (!acc) {
    return;
  }

  var msg = aMsg ? aMsg : "";
  var len = aSelectedChildren.length;

  // getSelectedChildren
  var selectedChildren = acc.selectedItems;
  is(
    selectedChildren ? selectedChildren.length : 0,
    len,
    msg +
      "getSelectedChildren: wrong selected children count for " +
      prettyName(aIdentifier)
  );

  for (let idx = 0; idx < len; idx++) {
    let expectedAcc = getAccessible(aSelectedChildren[idx]);
    var actualAcc = selectedChildren.queryElementAt(idx, nsIAccessible);
    is(
      actualAcc,
      expectedAcc,
      msg +
        "getSelectedChildren: wrong selected child at index " +
        idx +
        " for " +
        prettyName(aIdentifier) +
        " { actual : " +
        prettyName(actualAcc) +
        ", expected: " +
        prettyName(expectedAcc) +
        "}"
    );
  }

  // selectedItemCount
  is(
    acc.selectedItemCount,
    aSelectedChildren.length,
    "selectedItemCount: wrong selected children count for " +
      prettyName(aIdentifier)
  );

  // getSelectedItemAt
  for (let idx = 0; idx < len; idx++) {
    let expectedAcc = getAccessible(aSelectedChildren[idx]);
    is(
      acc.getSelectedItemAt(idx),
      expectedAcc,
      msg +
        "getSelectedItemAt: wrong selected child at index " +
        idx +
        " for " +
        prettyName(aIdentifier)
    );
  }

  // isItemSelected
  testIsItemSelected(acc, acc, { value: 0 }, aSelectedChildren, msg);
}

/**
 * Test isItemSelected method, helper for testSelectableSelection
 */
function testIsItemSelected(
  aSelectAcc,
  aTraversedAcc,
  aIndexObj,
  aSelectedChildren,
  aMsg
) {
  var childCount = aTraversedAcc.childCount;
  for (var idx = 0; idx < childCount; idx++) {
    var child = aTraversedAcc.getChildAt(idx);
    var [state /* extraState */] = getStates(child);
    if (state & STATE_SELECTABLE) {
      var isSelected = false;
      var len = aSelectedChildren.length;
      for (var jdx = 0; jdx < len; jdx++) {
        if (child == getAccessible(aSelectedChildren[jdx])) {
          isSelected = true;
          break;
        }
      }

      // isItemSelected
      is(
        aSelectAcc.isItemSelected(aIndexObj.value++),
        isSelected,
        aMsg +
          "isItemSelected: wrong selected child " +
          prettyName(child) +
          " for " +
          prettyName(aSelectAcc)
      );

      // selected state
      testStates(
        child,
        isSelected ? STATE_SELECTED : 0,
        0,
        !isSelected ? STATE_SELECTED : 0,
        0
      );

      continue;
    }

    testIsItemSelected(aSelectAcc, child, aIndexObj, aSelectedChildren);
  }
}