1 // Copyright 2014 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.
9 /** @type {(sinon.Spy|function():void)} */
11 /** @type {(sinon.Spy|function():void)} */
13 /** @type {remoting.MenuButton} */
14 var menuButton = null;
16 QUnit.module('MenuButton', {
17 beforeEach: function() {
18 var fixture = document.getElementById('qunit-fixture');
20 '<span class="menu-button" id="menu-button-container">' +
21 '<button class="menu-button-activator">Click me</button>' +
23 '<li id="menu-option-1">Option 1</li>' +
26 onShow = /** @type {(sinon.Spy|function():void)} */ (sinon.spy());
27 onHide = /** @type {(sinon.Spy|function():void)} */ (sinon.spy());
28 menuButton = new remoting.MenuButton(
29 document.getElementById('menu-button-container'),
30 /** @type {function():void} */ (onShow),
31 /** @type {function():void} */ (onHide));
33 afterEach: function() {
40 QUnit.test('should display on click', function(assert) {
41 var menu = menuButton.menu();
42 assert.ok(menu.offsetWidth == 0 && menu.offsetHeight == 0);
43 menuButton.button().click();
44 assert.ok(menu.offsetWidth != 0 && menu.offsetHeight != 0);
47 QUnit.test('should dismiss when the menu is clicked', function(assert) {
48 var menu = menuButton.menu();
49 menuButton.button().click();
51 assert.ok(menu.offsetWidth == 0 && menu.offsetHeight == 0);
54 QUnit.test('should dismiss when anything outside the menu is clicked',
56 var menu = menuButton.menu();
57 menuButton.button().click();
58 var x = menu.offsetRight + 1;
59 var y = menu.offsetBottom + 1;
60 var notMenu = document.elementFromPoint(x, y);
61 console.assert(notMenu != menu, 'Unable to click outside menu.');
63 assert.ok(menu.offsetWidth == 0 && menu.offsetHeight == 0);
66 QUnit.test('should dismiss when menu item is clicked', function(assert) {
67 var menu = menuButton.menu();
68 menuButton.button().click();
69 var element = document.getElementById('menu-option-1');
71 assert.ok(menu.offsetWidth == 0 && menu.offsetHeight == 0);
74 QUnit.test('should invoke callbacks', function(assert) {
75 assert.ok(!onShow.called);
76 menuButton.button().click();
77 assert.ok(onShow.called);
78 assert.ok(!onHide.called);
79 menuButton.menu().click();
80 assert.ok(onHide.called);
83 QUnit.test('select method should set/unset background image', function(assert) {
84 var element = document.getElementById('menu-option-1');
85 var style = window.getComputedStyle(element);
86 assert.ok(style.backgroundImage == 'none');
87 remoting.MenuButton.select(element, true);
88 style = window.getComputedStyle(element);
89 assert.ok(style.backgroundImage != 'none');
90 remoting.MenuButton.select(element, false);
91 style = window.getComputedStyle(element);
92 assert.ok(style.backgroundImage == 'none');