startup_page_list_browsertest.js revision 1e9bf3e0803691d0a228da41fc608347b6db4340
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * Fixture for startup pages WebUI tests.
7 * @extends {testing.Test}
8 * @constructor
9 */
10function StartupPageListWebUITest() {}
11
12StartupPageListWebUITest.prototype = {
13  __proto__: testing.Test.prototype,
14
15  /**
16   * Browse to the options page & call our preLoad().
17   * @override
18   */
19  browsePreload: 'chrome://settings-frame/startup',
20
21  /** @override */
22  setUp: function() {
23    StartupOverlay.updateStartupPages(this.fakeStartupList);
24    // 1 item for entering data, 1+ from |this.fakeStartupList|.
25    assertGE(this.getList().items.length, 2);
26  },
27
28  /**
29   * Returns the list to be tested.
30   * @return {Element}
31   * @protected
32   */
33  getList: function() {
34    return $('startupPagesList');
35  },
36
37  /**
38   * Register a mock handler to ensure expectations are met and options pages
39   * behave correctly.
40   * @override
41   */
42  preLoad: function() {
43    this.makeAndRegisterMockHandler(['addStartupPage',
44                                     'dragDropStartupPage']);
45  },
46
47  /**
48   * A fake list of startup pages to send to the overlay.
49   * @protected
50   */
51  fakeStartupList: [
52    {
53      title: 'Yahoo!',
54      url: 'http://yahoo.com',
55      tooltip: 'Yahoo! homepage',
56      modelIndex: 0
57    },
58    {
59      title: 'Facebook',
60      url: 'http://facebook.com',
61      tooltip: 'Facebook :: Sign In',
62      modelIndex: 1
63    }
64  ],
65};
66
67(function() {
68
69/**
70 * A mock data transfer object for drag/drop events.
71 * @constructor
72 */
73function MockDataTransfer() {
74  /**
75   * The data this dataTransfer object knows about.
76   * @type {!Object.<string, string>}
77   * @private
78   */
79  this.data_ = {};
80}
81
82/**
83 * Installs a lazily created MockDataTransfer on event#dataTransfer.
84 * @param {!Event} event An event to modify.
85 */
86MockDataTransfer.install = function(event) {
87  event.__defineGetter__('dataTransfer', function() {
88    event.dataTransfer_ = event.dataTransfer_ || new MockDataTransfer;
89    return event.dataTransfer_;
90  });
91};
92
93MockDataTransfer.prototype = {
94  /**
95   * The URL data in this mock drop event.
96   * @param {string} type The text of data being set.
97   * @param {*} val The data to set. Will be stringified.
98   */
99  setData: function(type, val) {
100    this.data_[type] = String(val);
101  },
102
103  /**
104   * Gets data associated with this fake data transfer.
105   * @param {string} type The type of data to get.
106   * @returns {string} The requested type of data or '' if not set.
107   */
108  getData: function(type) {
109    return this.data_[type] || '';
110  },
111};
112
113/**
114 * Creates a fake bubbling, cancelable mouse event with a mock data transfer
115 * installed.
116 * @param {string} type A type of mouse event (e.g. 'drop').
117 * @return {!Event} A fake mouse event.
118 */
119function createMouseEvent(type) {
120  var event = new MouseEvent(type, {bubbles: true, cancelable: true});
121  MockDataTransfer.install(event);
122  return event;
123}
124
125TEST_F('StartupPageListWebUITest', 'testDropFromOutsideSource', function() {
126  /** @const */ var NEW_PAGE = 'http://google.com';
127
128  var mockDropEvent = createMouseEvent('drop');
129  mockDropEvent.dataTransfer.setData('url', NEW_PAGE);
130
131  this.mockHandler.expects(once()).addStartupPage([NEW_PAGE, 0]);
132
133  this.getList().items[0].dispatchEvent(mockDropEvent);
134
135  expectTrue(mockDropEvent.defaultPrevented);
136});
137
138TEST_F('StartupPageListWebUITest', 'testDropToReorder', function() {
139  // TODO(dbeam): mock4js doesn't handle complex arguments well. Fix this.
140  this.mockHandler.expects(once()).dragDropStartupPage([0, [1].join()]);
141
142  this.getList().selectionModel.selectedIndex = 1;
143  expectEquals(1, this.getList().selectionModel.selectedIndexes.length);
144
145  this.getList().items[0].dispatchEvent(createMouseEvent('drop'));
146});
147
148}());
149