/******************************************************************************* * Copyright (c) 2009, 2014 EclipseSource and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * EclipseSource - initial API and implementation ******************************************************************************/ package org.eclipse.swt.internal.events; import static org.eclipse.rap.rwt.internal.protocol.ClientMessageConst.EVENT_SELECTION; import static org.eclipse.rap.rwt.internal.protocol.RemoteObjectFactory.getRemoteObject; import static org.eclipse.rap.rwt.internal.lifecycle.WidgetUtil.getId; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import org.eclipse.rap.json.JsonObject; import org.eclipse.rap.rwt.internal.lifecycle.PhaseId; import org.eclipse.rap.rwt.scripting.ClientListener; import org.eclipse.rap.rwt.testfixture.internal.Fixture; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.internal.widgets.buttonkit.ButtonOperationHandler; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; public class EventLCAUtil_Test { private Display display; private Shell shell; @Before public void setUp() { Fixture.setUp(); display = new Display(); shell = new Shell( display ); Fixture.fakeNewRequest(); Fixture.fakePhase( PhaseId.PROCESS_ACTION ); } @After public void tearDown() { Fixture.tearDown(); } @Test public void testWidgetDefaultSeletedModifiers() { Button button = new Button( shell, SWT.PUSH ); getRemoteObject( button ).setHandler( new ButtonOperationHandler( button ) ); SelectionListener listener = mock( SelectionListener.class ); button.addSelectionListener( listener ); JsonObject parameters = new JsonObject() .add( "altKey", true ) .add( "ctrlKey", false ) .add( "shiftKey", false ); Fixture.fakeNotifyOperation( getId( button ), EVENT_SELECTION, parameters ); Fixture.readDataAndProcessAction( button ); ArgumentCaptor<SelectionEvent> captor = ArgumentCaptor.forClass( SelectionEvent.class ); verify( listener, times( 1 ) ).widgetSelected( captor.capture() ); SelectionEvent event = captor.getValue(); assertTrue( ( event.stateMask & SWT.ALT ) != 0 ); assertTrue( ( event.stateMask & SWT.CTRL ) == 0 ); assertTrue( ( event.stateMask & SWT.SHIFT ) == 0 ); } @Test public void testTranslateModifier() { int stateMask = EventLCAUtil.translateModifier( false, false, false ); assertEquals( 0, stateMask & SWT.MODIFIER_MASK ); assertEquals( 0, stateMask & SWT.CTRL ); assertEquals( 0, stateMask & SWT.SHIFT ); assertEquals( 0, stateMask & SWT.ALT ); // Shift stateMask = EventLCAUtil.translateModifier( false, false, true ); assertTrue( ( stateMask & SWT.MODIFIER_MASK ) != 0 ); assertEquals( 0, stateMask & SWT.CTRL ); assertTrue( ( stateMask & SWT.SHIFT ) != 0 ); assertEquals( 0, stateMask & SWT.ALT ); // Alt stateMask = EventLCAUtil.translateModifier( true, false, false ); assertTrue( ( stateMask & SWT.MODIFIER_MASK ) != 0 ); assertEquals( 0, stateMask & SWT.CTRL ); assertEquals( 0, stateMask & SWT.SHIFT ); assertTrue( ( stateMask & SWT.ALT ) != 0 ); // Shift + Ctrl + Alt stateMask = EventLCAUtil.translateModifier( true, true, true ); assertEquals( SWT.SHIFT | SWT.CTRL | SWT.ALT, stateMask & SWT.MODIFIER_MASK ); assertEquals( stateMask, stateMask & SWT.MODIFIER_MASK ); } @Test public void testTranslateButton() { int button = EventLCAUtil.translateButton( 0 ); assertEquals( 0, button & SWT.BUTTON_MASK ); assertEquals( 0, button & SWT.BUTTON1 ); assertEquals( 0, button & SWT.BUTTON2 ); assertEquals( 0, button & SWT.BUTTON3 ); assertEquals( 0, button & SWT.BUTTON4 ); assertEquals( 0, button & SWT.BUTTON5 ); button = EventLCAUtil.translateButton( 1 ); assertTrue( ( button & SWT.BUTTON_MASK ) != 0 ); assertTrue( ( button & SWT.BUTTON1 ) != 0 ); assertEquals( 0, button & SWT.BUTTON2 ); assertEquals( 0, button & SWT.BUTTON3 ); assertEquals( 0, button & SWT.BUTTON4 ); assertEquals( 0, button & SWT.BUTTON5 ); button = EventLCAUtil.translateButton( 2 ); assertTrue( ( button & SWT.BUTTON_MASK ) != 0 ); assertEquals( 0, button & SWT.BUTTON1 ); assertTrue( ( button & SWT.BUTTON2 ) != 0 ); assertEquals( 0, button & SWT.BUTTON3 ); assertEquals( 0, button & SWT.BUTTON4 ); assertEquals( 0, button & SWT.BUTTON5 ); button = EventLCAUtil.translateButton( 3 ); assertTrue( ( button & SWT.BUTTON_MASK ) != 0 ); assertEquals( 0, button & SWT.BUTTON1 ); assertEquals( 0, button & SWT.BUTTON2 ); assertTrue( ( button & SWT.BUTTON3 ) != 0 ); assertEquals( 0, button & SWT.BUTTON4 ); assertEquals( 0, button & SWT.BUTTON5 ); button = EventLCAUtil.translateButton( 4 ); assertTrue( ( button & SWT.BUTTON_MASK ) != 0 ); assertEquals( 0, button & SWT.BUTTON1 ); assertEquals( 0, button & SWT.BUTTON2 ); assertEquals( 0, button & SWT.BUTTON3 ); assertTrue( ( button & SWT.BUTTON4 ) != 0 ); assertEquals( 0, button & SWT.BUTTON5 ); button = EventLCAUtil.translateButton( 5 ); assertTrue( ( button & SWT.BUTTON_MASK ) != 0 ); assertEquals( 0, button & SWT.BUTTON1 ); assertEquals( 0, button & SWT.BUTTON2 ); assertEquals( 0, button & SWT.BUTTON3 ); assertEquals( 0, button & SWT.BUTTON4 ); assertTrue( ( button & SWT.BUTTON5 ) != 0 ); } @Test public void testIsListening_withoutListeners() { Button button = new Button( shell, SWT.PUSH ); assertFalse( EventLCAUtil.isListening( button, SWT.Selection ) ); } @Test public void testIsListening_withSWTListenerOnly() { Button button = new Button( shell, SWT.PUSH ); button.addListener( SWT.Selection, mock( Listener.class ) ); assertTrue( EventLCAUtil.isListening( button, SWT.Selection ) ); } @Test public void testIsListening_withClientListenerOnly() { Button button = new Button( shell, SWT.PUSH ); button.addListener( SWT.Selection, mock( ClientListener.class ) ); assertFalse( EventLCAUtil.isListening( button, SWT.Selection ) ); } @Test public void testIsListening_withClientListenerAndSWTListener() { Button button = new Button( shell, SWT.PUSH ); button.addListener( SWT.Selection, mock( Listener.class ) ); button.addListener( SWT.Selection, mock( ClientListener.class ) ); assertTrue( EventLCAUtil.isListening( button, SWT.Selection ) ); } @Test public void testGetEventMask() { long eventMask = EventLCAUtil.getEventMask( 23 ); assertTrue( EventLCAUtil.containsEvent( eventMask, 23 ) ); } @Test public void testGetEventMask_ignoresCustomEvents() { long eventMask = EventLCAUtil.getEventMask( 100 ); assertFalse( EventLCAUtil.containsEvent( eventMask, 100 ) ); } @Test public void testContainsEvent_ignoresCustomEvents() { long eventMask = EventLCAUtil.getEventMask( 1 ); assertFalse( EventLCAUtil.containsEvent( eventMask, 65 ) ); } }