summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/webui/inline_editable_list_test.html
blob: 39e63d500a6bd210abcd0a66ce30b0f1a568feee (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
<!DOCTYPE html>
<html>
<body>

<script>

function setUp() {
  loadTimeData.data = { 
    'deletableItemDeleteButtonTitle': 'test_deletable_button_title'
  };
  
  cr.define('options', function() {
    /** @const */ var InlineEditableItemList = options.InlineEditableItemList;
    /** @const */ var InlineEditableItem = options.InlineEditableItem;
  
    /**
     * Creates a test list item.
     * @param {string} name This item's name.
     * @constructor
     * @extends {options.InlineEditableItem}
     */
    function TestItem(name) {
      var el = cr.doc.createElement('div');
      el.name_ = name;
      TestItem.decorate(el);
      return el;
    }
    
    /**
     * Decorates an element as a test list item.
     * @param {!HTMLElement} el The element to decorate.
     */
    TestItem.decorate = function(el) {
      el.__proto__ = TestItem.prototype;
      el.decorate();
    };
    
    TestItem.prototype = {
      __proto__: InlineEditableItem.prototype,
    
      /**
       * Item name. Used to set the item's text fields.
       * @type {string}
       * @private
       */
      name_: null,
    
      /** @override */
      decorate: function() {
        InlineEditableItem.prototype.decorate.call(this);
  
        var fieldEl = this.createEditableTextCell(this.name_);
        this.contentElement.appendChild(fieldEl);
  
        fieldEl = this.createEditableTextCell(this.name_ + '_two');
        this.contentElement.appendChild(fieldEl);
      },
    };
  
    /**
     * @constructor
     * @extends {options.InlineEditableItemList}
     */
    var TestItemList = cr.ui.define('list');
    
     TestItemList.prototype = {
      __proto__: InlineEditableItemList.prototype,
    
      /**
       * @override
       * @param {string} name
       */
      createItem: function(name) {
        return new TestItem(name);
      },
  
      /**
       * @param {!Element} el
       * @return {boolean} True if |el| or any of its children are focusable. 
       * @private
       */
      hasFocusableElement_: function(el) {
        return el.querySelectorAll('[tabindex="0"]').length > 0;
      },
  
      /**
       * @param {number} itemIndex
       * @return {boolean} True if item at |itemIndex| has a focusable element
       *     and no other items have focusable elements.
       */      
      hasExactlyOneItemFocusable: function(itemIndex) {
        var length = this.items.length;
        for(var i = 0; i < length; ++i) {
          if (this.hasFocusableElement_(this.items[i]) != (i == itemIndex))
            return false;
        }
        return true;
      },
    };
  
    // Export.
    return {
      TestItemList: TestItemList
    };
  
  })
}

/**
 * @param {!EventTarget} target Where to send the event. 
 * @param {!string} keyIdentifier Which key to send. 
 */      
function sendKeyDownEvent(target, keyIdentifier) {
  var event = document.createEvent('KeyboardEvent');
  event.initKeyboardEvent('keydown', true, true, window, keyIdentifier);
  assertEquals(keyIdentifier, event.keyIdentifier);
  target.dispatchEvent(event);  
}

/**
 * Test that exactly one item in the list is focusable after navigating the
 * list using up and down arrow keys.
 */
function testUpDownFocus() {
  var list = document.createElement('ul');
  list.style.position = 'absolute';
  list.style.width = '800px';
  list.style.height = '800px';
  options.TestItemList.decorate(list);
  document.body.appendChild(list);

  var model = new cr.ui.ArrayDataModel(['itemA', 'itemB', 'itemC']);
  list.dataModel = model;
  list.selectionModel.setIndexSelected(0, true);
  list.selectionModel.leadIndex = 0;

  assertTrue(list.hasExactlyOneItemFocusable(0));
  sendKeyDownEvent(list, 'Down');
  assertTrue(list.hasExactlyOneItemFocusable(1));
  sendKeyDownEvent(list, 'Down');
  assertTrue(list.hasExactlyOneItemFocusable(2));
  sendKeyDownEvent(list, 'Up');
  assertTrue(list.hasExactlyOneItemFocusable(1));
  sendKeyDownEvent(list, 'Up');
  assertTrue(list.hasExactlyOneItemFocusable(0));
  sendKeyDownEvent(list, 'Down');
  assertTrue(list.hasExactlyOneItemFocusable(1));
}

</script>

</body>
</html>