Code re-factoring and test extends * move handling of Help event to ControlOperationHandler * move test for creating of selection event to WidgetOperationHandler_Test * move handling of Selection/DefaultSelection to corresponding widget/control operation handler
diff --git a/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/protocol/ControlOperationHandler.java b/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/protocol/ControlOperationHandler.java index 09720a1..b495d2f 100644 --- a/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/protocol/ControlOperationHandler.java +++ b/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/protocol/ControlOperationHandler.java
@@ -51,6 +51,8 @@ handleNotifyKeyDown( control, properties ); } else if( "MenuDetect".equals( eventName ) ) { handleNotifyMenuDetect( control, properties ); + } else if( "Help".equals( eventName ) ) { + handleNotifyHelp( control, properties ); } else { super.handleNotify( control, eventName, properties ); } @@ -152,7 +154,14 @@ control.notifyListeners( SWT.MenuDetect, createMenuDetectEvent( properties ) ); } - static void processMouseEvent( int eventType, Control control, JsonObject properties ) { + /* + * PROTOCOL NOTIFY Help + */ + public void handleNotifyHelp( T widget, JsonObject properties ) { + widget.notifyListeners( SWT.Help, new Event() ); + } + + private static void processMouseEvent( int eventType, Control control, JsonObject properties ) { Event event = createMouseEvent( eventType, control, properties ); boolean pass = false; if( control instanceof Scrollable ) { @@ -183,7 +192,7 @@ return event; } - static void processTraverseEvent( Control control, JsonObject properties ) { + private static void processTraverseEvent( Control control, JsonObject properties ) { int keyCode = properties.get( EVENT_PARAM_KEY_CODE ).asInt(); int charCode = properties.get( EVENT_PARAM_CHAR_CODE ).asInt(); int stateMask = readStateMask( properties );
diff --git a/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/protocol/WidgetOperationHandler.java b/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/protocol/WidgetOperationHandler.java index 0d5d253..acd6387 100644 --- a/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/protocol/WidgetOperationHandler.java +++ b/bundles/org.eclipse.rap.rwt/src/org/eclipse/rap/rwt/internal/protocol/WidgetOperationHandler.java
@@ -34,11 +34,6 @@ } @Override - public void handleNotify( String eventName, JsonObject properties ) { - handleNotify( widget, eventName, properties ); - } - - @Override public void handleSet( JsonObject properties ) { handleSet( widget, properties ); } @@ -48,6 +43,11 @@ handleCall( widget, method, parameters ); } + @Override + public void handleNotify( String eventName, JsonObject properties ) { + handleNotify( widget, eventName, properties ); + } + public void handleSet( T widget, JsonObject properties ) { throw new UnsupportedOperationException( "set operations not supported by this handler" ); } @@ -57,46 +57,7 @@ } public void handleNotify( T widget, String eventName, JsonObject properties ) { - if( "Help".equals( eventName ) ) { - handleNotifyHelp( widget, properties ); - } else if( "Selection".equals( eventName ) ) { - handleNotifySelection( widget, properties ); - } else if( "DefaultSelection".equals( eventName ) ) { - handleNotifyDefaultSelection( widget, properties ); - } else { - throw new UnsupportedOperationException( "notify operations not supported by this handler" ); - } - } - - /* - * PROTOCOL NOTIFY Selection - * - * @param altKey (boolean) true if the ALT key was pressed - * @param ctrlKey (boolean) true if the CTRL key was pressed - * @param shiftKey (boolean) true if the SHIFT key was pressed - */ - public void handleNotifySelection( T widget, JsonObject properties ) { - Event event = createSelectionEvent( SWT.Selection, properties ); - widget.notifyListeners( SWT.Selection, event ); - } - - /* - * PROTOCOL NOTIFY DefaultSelection - * - * @param altKey (boolean) true if the ALT key was pressed - * @param ctrlKey (boolean) true if the CTRL key was pressed - * @param shiftKey (boolean) true if the SHIFT key was pressed - */ - public void handleNotifyDefaultSelection( T widget, JsonObject properties ) { - Event event = createSelectionEvent( SWT.DefaultSelection, properties ); - widget.notifyListeners( SWT.DefaultSelection, event ); - } - - /* - * PROTOCOL NOTIFY Help - */ - public void handleNotifyHelp( T widget, JsonObject properties ) { - widget.notifyListeners( SWT.Help, new Event() ); + throw new UnsupportedOperationException( "notify operations not supported by this handler" ); } protected static Event createSelectionEvent( int eventType, JsonObject properties ) {
diff --git a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/buttonkit/ButtonOperationHandler.java b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/buttonkit/ButtonOperationHandler.java index 99a7d4f..e782670 100644 --- a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/buttonkit/ButtonOperationHandler.java +++ b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/buttonkit/ButtonOperationHandler.java
@@ -32,22 +32,6 @@ } /* - * PROTOCOL NOTIFY Selection - * - * @param altKey (boolean) true if the ALT key was pressed - * @param ctrlKey (boolean) true if the CTRL key was pressed - * @param shiftKey (boolean) true if the SHIFT key was pressed - */ - @Override - public void handleNotifySelection( Button button, JsonObject properties ) { - Event event = createSelectionEvent( SWT.Selection, properties ); - if( ( button.getStyle() & SWT.RADIO ) != 0 && !button.getSelection() ) { - event.time = -1; - } - button.notifyListeners( SWT.Selection, event ); - } - - /* * PROTOCOL SET selection * * @param selection (boolean) true if the button was selected, otherwise false @@ -59,4 +43,42 @@ } } + @Override + public void handleNotify( Button button, String eventName, JsonObject properties ) { + if( "Selection".equals( eventName ) ) { + handleNotifySelection( button, properties ); + } else if( "DefaultSelection".equals( eventName ) ) { + handleNotifyDefaultSelection( button, properties ); + } else { + super.handleNotify( button, eventName, properties ); + } + } + + /* + * PROTOCOL NOTIFY Selection + * + * @param altKey (boolean) true if the ALT key was pressed + * @param ctrlKey (boolean) true if the CTRL key was pressed + * @param shiftKey (boolean) true if the SHIFT key was pressed + */ + public void handleNotifySelection( Button button, JsonObject properties ) { + Event event = createSelectionEvent( SWT.Selection, properties ); + if( ( button.getStyle() & SWT.RADIO ) != 0 && !button.getSelection() ) { + event.time = -1; + } + button.notifyListeners( SWT.Selection, event ); + } + + /* + * PROTOCOL NOTIFY DefaultSelection + * + * @param altKey (boolean) true if the ALT key was pressed + * @param ctrlKey (boolean) true if the CTRL key was pressed + * @param shiftKey (boolean) true if the SHIFT key was pressed + */ + public void handleNotifyDefaultSelection( Button button, JsonObject properties ) { + Event event = createSelectionEvent( SWT.DefaultSelection, properties ); + button.notifyListeners( SWT.DefaultSelection, event ); + } + }
diff --git a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/combokit/ComboOperationHandler.java b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/combokit/ComboOperationHandler.java index e425b20..437d7bf 100644 --- a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/combokit/ComboOperationHandler.java +++ b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/combokit/ComboOperationHandler.java
@@ -20,6 +20,7 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Event; public class ComboOperationHandler extends ControlOperationHandler<Combo> { @@ -44,7 +45,11 @@ @Override public void handleNotify( Combo combo, String eventName, JsonObject properties ) { - if( "Modify".equals( eventName ) ) { + if( "Selection".equals( eventName ) ) { + handleNotifySelection( combo, properties ); + } else if( "DefaultSelection".equals( eventName ) ) { + handleNotifyDefaultSelection( combo, properties ); + } else if( "Modify".equals( eventName ) ) { handleNotifyModify( combo, properties ); } else { super.handleNotify( combo, eventName, properties ); @@ -52,10 +57,34 @@ } /* + * PROTOCOL NOTIFY Selection + * + * @param altKey (boolean) true if the ALT key was pressed + * @param ctrlKey (boolean) true if the CTRL key was pressed + * @param shiftKey (boolean) true if the SHIFT key was pressed + */ + public void handleNotifySelection( Combo combo, JsonObject properties ) { + Event event = createSelectionEvent( SWT.Selection, properties ); + combo.notifyListeners( SWT.Selection, event ); + } + + /* + * PROTOCOL NOTIFY DefaultSelection + * + * @param altKey (boolean) true if the ALT key was pressed + * @param ctrlKey (boolean) true if the CTRL key was pressed + * @param shiftKey (boolean) true if the SHIFT key was pressed + */ + public void handleNotifyDefaultSelection( Combo combo, JsonObject properties ) { + Event event = createSelectionEvent( SWT.DefaultSelection, properties ); + combo.notifyListeners( SWT.DefaultSelection, event ); + } + + /* * PROTOCOL NOTIFY Modify * ignored, Modify event is fired when set text */ - public void handleNotifyModify( Combo control, JsonObject properties ) { + public void handleNotifyModify( Combo combo, JsonObject properties ) { } /*
diff --git a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/treecolumnkit/TreeColumnOperationHandler.java b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/treecolumnkit/TreeColumnOperationHandler.java index bfc78f9..1a86775 100644 --- a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/treecolumnkit/TreeColumnOperationHandler.java +++ b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/treecolumnkit/TreeColumnOperationHandler.java
@@ -17,7 +17,9 @@ import org.eclipse.rap.json.JsonObject; import org.eclipse.rap.rwt.internal.protocol.WidgetOperationHandler; import org.eclipse.rap.rwt.lifecycle.ProcessActionRunner; +import org.eclipse.swt.SWT; import org.eclipse.swt.internal.widgets.ITreeAdapter; +import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeColumn; @@ -42,6 +44,17 @@ } } + @Override + public void handleNotify( TreeColumn column, String eventName, JsonObject properties ) { + if( "Selection".equals( eventName ) ) { + handleNotifySelection( column, properties ); + } else if( "DefaultSelection".equals( eventName ) ) { + handleNotifyDefaultSelection( column, properties ); + } else { + super.handleNotify( column, eventName, properties ); + } + } + /* * PROTOCOL CALL move * @@ -70,6 +83,30 @@ } ); } + /* + * PROTOCOL NOTIFY Selection + * + * @param altKey (boolean) true if the ALT key was pressed + * @param ctrlKey (boolean) true if the CTRL key was pressed + * @param shiftKey (boolean) true if the SHIFT key was pressed + */ + public void handleNotifySelection( TreeColumn column, JsonObject properties ) { + Event event = createSelectionEvent( SWT.Selection, properties ); + column.notifyListeners( SWT.Selection, event ); + } + + /* + * PROTOCOL NOTIFY DefaultSelection + * + * @param altKey (boolean) true if the ALT key was pressed + * @param ctrlKey (boolean) true if the CTRL key was pressed + * @param shiftKey (boolean) true if the SHIFT key was pressed + */ + public void handleNotifyDefaultSelection( TreeColumn column, JsonObject properties ) { + Event event = createSelectionEvent( SWT.DefaultSelection, properties ); + column.notifyListeners( SWT.DefaultSelection, event ); + } + static void moveColumn( TreeColumn column, int newLeft ) { Tree tree = column.getParent(); int targetColumn = findMoveTarget( tree, newLeft );
diff --git a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/treekit/TreeOperationHandler.java b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/treekit/TreeOperationHandler.java index 88d065e..bc1ecdb 100644 --- a/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/treekit/TreeOperationHandler.java +++ b/bundles/org.eclipse.rap.rwt/widgetkits/org/eclipse/swt/internal/widgets/treekit/TreeOperationHandler.java
@@ -71,60 +71,6 @@ } /* - * PROTOCOL NOTIFY Selection - * - * @param altKey (boolean) true if the ALT key was pressed - * @param ctrlKey (boolean) true if the CTRL key was pressed - * @param shiftKey (boolean) true if the SHIFT key was pressed - * @param detail (string) "check" is checkbox is selected - * @item item (string) id of selected item - */ - @Override - public void handleNotifySelection( Tree tree, JsonObject properties ) { - Event event = createSelectionEvent( SWT.Selection, properties ); - event.item = getItem( tree, properties.get( EVENT_PARAM_ITEM ).asString() ); - tree.notifyListeners( SWT.Selection, event ); - } - - /* - * PROTOCOL NOTIFY DefaultSelection - * - * @param altKey (boolean) true if the ALT key was pressed - * @param ctrlKey (boolean) true if the CTRL key was pressed - * @param shiftKey (boolean) true if the SHIFT key was pressed - * @param detail (string) "check" is checkbox is selected - * @item item (string) id of selected item - */ - @Override - public void handleNotifyDefaultSelection( Tree tree, JsonObject properties ) { - Event event = createSelectionEvent( SWT.DefaultSelection, properties ); - event.item = getItem( tree, properties.get( EVENT_PARAM_ITEM ).asString() ); - tree.notifyListeners( SWT.DefaultSelection, event ); - } - - /* - * PROTOCOL NOTIFY Expand - * - * @item item (string) id of expanded item - */ - public void handleNotifyExpand( Tree tree, JsonObject properties ) { - Event event = new Event(); - event.item = getItem( tree, properties.get( EVENT_PARAM_ITEM ).asString() ); - tree.notifyListeners( SWT.Expand, event ); - } - - /* - * PROTOCOL NOTIFY Collapse - * - * @item item (string) id of collapsed item - */ - public void handleNotifyCollapse( Tree tree, JsonObject properties ) { - Event event = new Event(); - event.item = getItem( tree, properties.get( EVENT_PARAM_ITEM ).asString() ); - tree.notifyListeners( SWT.Collapse, event ); - } - - /* * PROTOCOL SET selection * * @param selection ([string]) array with ids of selected items @@ -196,6 +142,58 @@ } } + /* + * PROTOCOL NOTIFY Selection + * + * @param altKey (boolean) true if the ALT key was pressed + * @param ctrlKey (boolean) true if the CTRL key was pressed + * @param shiftKey (boolean) true if the SHIFT key was pressed + * @param detail (string) "check" is checkbox is selected + * @item item (string) id of selected item + */ + public void handleNotifySelection( Tree tree, JsonObject properties ) { + Event event = createSelectionEvent( SWT.Selection, properties ); + event.item = getItem( tree, properties.get( EVENT_PARAM_ITEM ).asString() ); + tree.notifyListeners( SWT.Selection, event ); + } + + /* + * PROTOCOL NOTIFY DefaultSelection + * + * @param altKey (boolean) true if the ALT key was pressed + * @param ctrlKey (boolean) true if the CTRL key was pressed + * @param shiftKey (boolean) true if the SHIFT key was pressed + * @param detail (string) "check" is checkbox is selected + * @item item (string) id of selected item + */ + public void handleNotifyDefaultSelection( Tree tree, JsonObject properties ) { + Event event = createSelectionEvent( SWT.DefaultSelection, properties ); + event.item = getItem( tree, properties.get( EVENT_PARAM_ITEM ).asString() ); + tree.notifyListeners( SWT.DefaultSelection, event ); + } + + /* + * PROTOCOL NOTIFY Expand + * + * @item item (string) id of expanded item + */ + public void handleNotifyExpand( Tree tree, JsonObject properties ) { + Event event = new Event(); + event.item = getItem( tree, properties.get( EVENT_PARAM_ITEM ).asString() ); + tree.notifyListeners( SWT.Expand, event ); + } + + /* + * PROTOCOL NOTIFY Collapse + * + * @item item (string) id of collapsed item + */ + public void handleNotifyCollapse( Tree tree, JsonObject properties ) { + Event event = new Event(); + event.item = getItem( tree, properties.get( EVENT_PARAM_ITEM ).asString() ); + tree.notifyListeners( SWT.Collapse, event ); + } + private TreeItem getItem( Tree tree, String itemId ) { TreeItem item = null; String[] idParts = itemId.split( "#" );
diff --git a/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/protocol/ControlOperationHandler_Test.java b/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/protocol/ControlOperationHandler_Test.java index 4c9e4f7..5f697b5 100644 --- a/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/protocol/ControlOperationHandler_Test.java +++ b/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/protocol/ControlOperationHandler_Test.java
@@ -10,13 +10,11 @@ ******************************************************************************/ package org.eclipse.rap.rwt.internal.protocol; +import static org.eclipse.rap.rwt.internal.protocol.ClientMessageConst.EVENT_KEY_DOWN; import static org.eclipse.rap.rwt.internal.protocol.ControlOperationHandler.createKeyEvent; import static org.eclipse.rap.rwt.internal.protocol.ControlOperationHandler.createMenuDetectEvent; import static org.eclipse.rap.rwt.internal.protocol.ControlOperationHandler.createMouseEvent; -import static org.eclipse.rap.rwt.internal.protocol.ControlOperationHandler.createSelectionEvent; import static org.eclipse.rap.rwt.internal.protocol.ControlOperationHandler.getTraverseKey; -import static org.eclipse.rap.rwt.internal.protocol.ControlOperationHandler.processMouseEvent; -import static org.eclipse.rap.rwt.internal.protocol.ControlOperationHandler.processTraverseEvent; import static org.eclipse.rap.rwt.internal.protocol.ControlOperationHandler.translateKeyCode; import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; @@ -29,7 +27,6 @@ import org.eclipse.rap.json.JsonObject; import org.eclipse.rap.rwt.testfixture.Fixture; import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; @@ -46,6 +43,7 @@ private Shell shell; private Button control; private Control mockedControl; + private ControlOperationHandler<Control> handler; @Before public void setUp() { @@ -55,6 +53,7 @@ control = new Button( shell, SWT.NONE ); control.setBounds( 10, 10, 100, 20 ); mockedControl = mock( Control.class ); + handler = new ControlOperationHandler<Control>( mockedControl ) {}; } @After @@ -63,44 +62,6 @@ } @Test - public void testCreateSelectionEvent_withoutProperties() { - Event event = createSelectionEvent( SWT.Selection, new JsonObject() ); - - assertEquals( SWT.Selection, event.type ); - } - - @Test - public void testCreateSelectionEvent_withStateMask() { - JsonObject properties = new JsonObject().add( "altKey", true ).add( "ctrlKey", true ); - - Event event = createSelectionEvent( SWT.Selection, properties ); - - assertEquals( SWT.ALT | SWT.CTRL, event.stateMask ); - } - - @Test - public void testCreateSelectionEvent_withDetail() { - JsonObject properties = new JsonObject().add( "detail", "check" ); - - Event event = createSelectionEvent( SWT.Selection, properties ); - - assertEquals( SWT.CHECK, event.detail ); - } - - @Test - public void testCreateSelectionEvent_withBounds() { - JsonObject properties = new JsonObject() - .add( "x", 1 ) - .add( "y", 2 ) - .add( "width", 3 ) - .add( "height", 4 ); - - Event event = createSelectionEvent( SWT.Selection, properties ); - - assertEquals( new Rectangle( 1, 2, 3, 4 ), event.getBounds() ); - } - - @Test public void testCreateMouseEvent() { JsonObject properties = new JsonObject() .add( "button", 1 ) @@ -143,92 +104,6 @@ } @Test - public void testProcessMouseEvent_onControl_valid() { - Control spyControl = spy( control ); - JsonObject properties = new JsonObject() - .add( "button", 1 ) - .add( "x", 15 ) - .add( "y", 20 ) - .add( "time", 4 ); - - processMouseEvent( SWT.MouseDown, spyControl, properties ); - - verify( spyControl ).notifyListeners( eq( SWT.MouseDown ), any( Event.class ) ); - } - - @Test - public void testProcessMouseEvent_onControl_invalid() { - Control spyControl = spy( control ); - JsonObject properties = new JsonObject() - .add( "button", 1 ) - .add( "x", -10 ) - .add( "y", 3 ) - .add( "time", 4 ); - - processMouseEvent( SWT.MouseDown, spyControl, properties ); - - verify( spyControl, never() ).notifyListeners( eq( SWT.MouseDown ), any( Event.class ) ); - } - - @Test - public void testProcessMouseEvent_onScrollable_valid() { - Control spyControl = spy( shell ); - JsonObject properties = new JsonObject() - .add( "button", 1 ) - .add( "x", 20 ) - .add( "y", 60 ) - .add( "time", 4 ); - - processMouseEvent( SWT.MouseDown, spyControl, properties ); - - verify( spyControl ).notifyListeners( eq( SWT.MouseDown ), any( Event.class ) ); - } - - @Test - public void testProcessMouseEvent_onScrollable_invalid() { - Control spyControl = spy( shell ); - JsonObject properties = new JsonObject() - .add( "button", 1 ) - .add( "x", 2 ) - .add( "y", 3 ) - .add( "time", 4 ); - - processMouseEvent( SWT.MouseDown, spyControl, properties ); - - verify( spyControl, never() ).notifyListeners( eq( SWT.MouseDown ), any( Event.class ) ); - } - - @Test - public void testProcessTraverseEvent() { - JsonObject properties = new JsonObject() - .add( "shiftKey", true ) - .add( "keyCode", 9 ) - .add( "charCode", 0 ); - - processTraverseEvent( mockedControl, properties ); - - ArgumentCaptor<Event> captor = ArgumentCaptor.forClass( Event.class ); - verify( mockedControl ).notifyListeners( eq( SWT.Traverse ), captor.capture() ); - Event event = captor.getValue(); - assertEquals( SWT.SHIFT, event.stateMask ); - assertEquals( 9, event.keyCode ); - assertEquals( 9, event.character ); - assertEquals( SWT.TRAVERSE_TAB_PREVIOUS, event.detail ); - } - - @Test - public void testProcessTraverseEvent_wrongKeyModifier() { - JsonObject properties = new JsonObject() - .add( "ctrlKey", true ) - .add( "keyCode", 9 ) - .add( "charCode", 0 ); - - processTraverseEvent( mockedControl, properties ); - - verify( mockedControl, never() ).notifyListeners( eq( SWT.Traverse ), any( Event.class ) ); - } - - @Test public void testGetTraverseKey() { int traverseKey; traverseKey = getTraverseKey( 13, 0 ); @@ -336,4 +211,161 @@ assertEquals( 2, event.y ); } + @Test + public void testHandleNotify_processesFocusIn() { + JsonObject properties = new JsonObject(); + + handler.handleNotify( "FocusIn", properties ); + + verify( mockedControl ).notifyListeners( eq( SWT.FocusIn ), any( Event.class ) ); + } + + @Test + public void testHandleNotify_processesFocusOut() { + JsonObject properties = new JsonObject(); + + handler.handleNotify( "FocusOut", properties ); + + verify( mockedControl ).notifyListeners( eq( SWT.FocusOut ), any( Event.class ) ); + } + + @Test + public void testHandleNotify_processesMouseDown_onControl_valid() { + Control spyControl = spy( control ); + handler = new ControlOperationHandler<Control>( spyControl ) {}; + JsonObject properties = new JsonObject() + .add( "button", 1 ) + .add( "x", 15 ) + .add( "y", 20 ) + .add( "time", 4 ); + + handler.handleNotifyMouseDown( spyControl, properties ); + + verify( spyControl ).notifyListeners( eq( SWT.MouseDown ), any( Event.class ) ); + } + + @Test + public void testHandleNotify_processesMouseDown_onControl_invalid() { + Control spyControl = spy( control ); + JsonObject properties = new JsonObject() + .add( "button", 1 ) + .add( "x", -10 ) + .add( "y", 3 ) + .add( "time", 4 ); + + handler.handleNotifyMouseDown( spyControl, properties ); + + verify( spyControl, never() ).notifyListeners( eq( SWT.MouseDown ), any( Event.class ) ); + } + + @Test + public void testHandleNotify_processesMouseDown_onScrollable_valid() { + Control spyControl = spy( shell ); + JsonObject properties = new JsonObject() + .add( "button", 1 ) + .add( "x", 20 ) + .add( "y", 60 ) + .add( "time", 4 ); + + handler.handleNotifyMouseDown( spyControl, properties ); + + verify( spyControl ).notifyListeners( eq( SWT.MouseDown ), any( Event.class ) ); + } + + @Test + public void testHandleNotify_processesMouseDown_onScrollable_invalid() { + Control spyControl = spy( shell ); + JsonObject properties = new JsonObject() + .add( "button", 1 ) + .add( "x", 2 ) + .add( "y", 3 ) + .add( "time", 4 ); + + handler.handleNotifyMouseDown( spyControl, properties ); + + verify( spyControl, never() ).notifyListeners( eq( SWT.MouseDown ), any( Event.class ) ); + } + + @Test + public void testHandleNotify_processesKeyDown() { + JsonObject properties = new JsonObject() + .add( "shiftKey", true ) + .add( "keyCode", 65 ) + .add( "charCode", 97 ); + + handler.handleNotify( EVENT_KEY_DOWN, properties ); + + ArgumentCaptor<Event> captor = ArgumentCaptor.forClass( Event.class ); + verify( mockedControl ).notifyListeners( eq( SWT.KeyDown ), captor.capture() ); + Event event = captor.getValue(); + assertEquals( SWT.SHIFT, event.stateMask ); + assertEquals( 97, event.keyCode ); + assertEquals( 'a', event.character ); + } + + @Test + public void testHandleNotify_processesKeyUp() { + JsonObject properties = new JsonObject() + .add( "shiftKey", true ) + .add( "keyCode", 65 ) + .add( "charCode", 97 ); + + handler.handleNotify( EVENT_KEY_DOWN, properties ); + + ArgumentCaptor<Event> captor = ArgumentCaptor.forClass( Event.class ); + verify( mockedControl ).notifyListeners( eq( SWT.KeyUp ), captor.capture() ); + Event event = captor.getValue(); + assertEquals( SWT.SHIFT, event.stateMask ); + assertEquals( 97, event.keyCode ); + assertEquals( 'a', event.character ); + } + + @Test + public void testHandleNotify_processesTraverse() { + JsonObject properties = new JsonObject() + .add( "shiftKey", true ) + .add( "keyCode", 9 ) + .add( "charCode", 0 ); + + handler.handleNotify( "Traverse", properties ); + + ArgumentCaptor<Event> captor = ArgumentCaptor.forClass( Event.class ); + verify( mockedControl ).notifyListeners( eq( SWT.Traverse ), captor.capture() ); + Event event = captor.getValue(); + assertEquals( SWT.SHIFT, event.stateMask ); + assertEquals( 9, event.keyCode ); + assertEquals( 9, event.character ); + assertEquals( SWT.TRAVERSE_TAB_PREVIOUS, event.detail ); + } + + @Test + public void testHandleNotify_processesTraverse_wrongKeyModifier() { + JsonObject properties = new JsonObject() + .add( "ctrlKey", true ) + .add( "keyCode", 9 ) + .add( "charCode", 0 ); + + handler.handleNotify( "Traverse", properties ); + + verify( mockedControl, never() ).notifyListeners( eq( SWT.Traverse ), any( Event.class ) ); + } + + @Test + public void testHandleNotify_processesMenuDetect() { + JsonObject properties = new JsonObject().add( "x", 1 ).add( "y", 2 ); + + handler.handleNotify( "MenuDetect", properties ); + + verify( mockedControl ).notifyListeners( eq( SWT.MenuDetect ), any( Event.class ) ); + } + + @Test + public void testHandleNotify_processesHelp() { + JsonObject properties = new JsonObject(); + + handler.handleNotify( "Help", properties ); + + verify( mockedControl ).notifyListeners( eq( SWT.Help ), any( Event.class ) ); + } + }
diff --git a/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/protocol/WidgetOperationHandler_Test.java b/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/protocol/WidgetOperationHandler_Test.java index 62168d7..f6df7be 100644 --- a/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/protocol/WidgetOperationHandler_Test.java +++ b/tests/org.eclipse.rap.rwt.test/src/org/eclipse/rap/rwt/internal/protocol/WidgetOperationHandler_Test.java
@@ -10,24 +10,72 @@ ******************************************************************************/ package org.eclipse.rap.rwt.internal.protocol; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; +import static org.eclipse.rap.rwt.internal.protocol.WidgetOperationHandler.createSelectionEvent; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import org.eclipse.rap.json.JsonObject; import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Widget; +import org.junit.Before; import org.junit.Test; public class WidgetOperationHandler_Test { + private Widget widget; + private WidgetOperationHandler<Widget> handler; + + @Before + public void setUp() { + widget = mock( Widget.class ); + handler = new WidgetOperationHandler<Widget>( widget ) {}; + } + + @Test + public void testCreateSelectionEvent_withoutProperties() { + Event event = createSelectionEvent( SWT.Selection, new JsonObject() ); + + assertEquals( SWT.Selection, event.type ); + } + + @Test + public void testCreateSelectionEvent_withStateMask() { + JsonObject properties = new JsonObject().add( "altKey", true ).add( "ctrlKey", true ); + + Event event = createSelectionEvent( SWT.Selection, properties ); + + assertEquals( SWT.ALT | SWT.CTRL, event.stateMask ); + } + + @Test + public void testCreateSelectionEvent_withDetail() { + JsonObject properties = new JsonObject().add( "detail", "check" ); + + Event event = createSelectionEvent( SWT.Selection, properties ); + + assertEquals( SWT.CHECK, event.detail ); + } + + @Test + public void testCreateSelectionEvent_withBounds() { + JsonObject properties = new JsonObject() + .add( "x", 1 ) + .add( "y", 2 ) + .add( "width", 3 ) + .add( "height", 4 ); + + Event event = createSelectionEvent( SWT.Selection, properties ); + + assertEquals( new Rectangle( 1, 2, 3, 4 ), event.getBounds() ); + } + @Test public void testHandleSet_delegatesToHandleSetWithWidget() { - Widget widget = mock( Widget.class ); JsonObject properties = new JsonObject(); WidgetOperationHandler<Widget> handler = spy( new TestWidgetOperationHandler( widget ) ); @@ -38,16 +86,13 @@ @Test( expected = UnsupportedOperationException.class ) public void testHandleSet_throwsExceptionIfNotSupported() { - Widget widget = mock( Widget.class ); JsonObject properties = new JsonObject(); - WidgetOperationHandler<Widget> handler = spy( new WidgetOperationHandler<Widget>( widget ) {} ); handler.handleSet( properties ); } @Test public void testHandleCall_delegatesToHandleCallWithWidget() { - Widget widget = mock( Widget.class ); JsonObject properties = new JsonObject(); WidgetOperationHandler<Widget> handler = spy( new TestWidgetOperationHandler( widget ) ); @@ -58,9 +103,7 @@ @Test( expected = UnsupportedOperationException.class ) public void testHandleCall_throwsExceptionIfNotSupported() { - Widget widget = mock( Widget.class ); JsonObject properties = new JsonObject(); - WidgetOperationHandler<Widget> handler = spy( new WidgetOperationHandler<Widget>( widget ) {} ); handler.handleCall( "foo", properties ); } @@ -78,24 +121,11 @@ @Test( expected = UnsupportedOperationException.class ) public void testHandleNotify_throwsExceptionIfNotSupported() { - Widget widget = mock( Widget.class ); JsonObject properties = new JsonObject(); - WidgetOperationHandler<Widget> handler = spy( new WidgetOperationHandler<Widget>( widget ) {} ); handler.handleNotify( "foo", properties ); } - @Test - public void testHandleNotify_processesHelp() { - Widget widget = mock( Widget.class ); - JsonObject properties = new JsonObject(); - WidgetOperationHandler<Widget> handler = new WidgetOperationHandler<Widget>( widget ) {}; - - handler.handleNotify( "Help", properties ); - - verify( widget ).notifyListeners( eq( SWT.Help ), any( Event.class ) ); - } - private static class TestWidgetOperationHandler extends WidgetOperationHandler<Widget> { private TestWidgetOperationHandler( Widget widget ) {