Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / data / webui / context_menu_handler_test.html
blob366ed0e9e8835b49efedc8b2a31d38f749ac9f95
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script>
6 function testShowAndHideEvents() {
7 // Keep original Date.now not to affect other code.
8 var originalDateNow = Date.now;
10 // Initial value is 1 since 0 becomes false.
11 var currentTime = 1;
13 // Overrides Date.now to simulate time.
14 Date.now = function() { return currentTime; };
16 var cmh = cr.ui.contextMenuHandler;
18 // Create context menu.
19 var menu = document.createElement('div');
20 cr.ui.decorate(menu, cr.ui.Menu);
21 document.body.appendChild(menu);
23 var menuItem = document.createElement('div');
24 menu.addMenuItem(menuItem);
26 // Create target elements.
27 var elem1 = document.createElement('div');
28 var elem2 = document.createElement('div');
30 cmh.setContextMenu(elem1, menu);
31 cmh.setContextMenu(elem2, menu);
33 var events = [];
34 cmh.addEventListener('show', function(e) { events.push(e); });
35 cmh.addEventListener('hide', function(e) { events.push(e); });
37 // Show context menu of elem1.
38 elem1.dispatchEvent(new MouseEvent('contextmenu'));
39 assertEquals(1, events.length);
40 assertEquals('show', events[0].type);
41 assertEquals(elem1, events[0].element);
42 assertEquals(menu, events[0].menu);
44 // Show context menu of elem2.
45 document.dispatchEvent(new MouseEvent('mousedown'));
47 // On Windows to prevent context menu show again by mouse right button up,
48 // we need to wait at least 50ms from the last hide of context menu.
49 currentTime += 51; // ms
51 elem2.dispatchEvent(new MouseEvent('contextmenu'));
52 assertEquals(3, events.length);
53 assertEquals('hide', events[1].type);
54 assertEquals(elem1, events[1].element);
55 assertEquals(menu, events[1].menu);
56 assertEquals('show', events[2].type);
57 assertEquals(elem2, events[2].element);
58 assertEquals(menu, events[2].menu);
60 Date.now = originalDateNow;
63 </script>
64 </body>
65 </html>