/*******************************************************************************
* Copyright (c) 2013, 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.widgets.tabfolderkit;
import static org.eclipse.rap.rwt.internal.protocol.ClientMessageConst.EVENT_SELECTION;
import static org.eclipse.rap.rwt.internal.lifecycle.WidgetUtil.getId;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
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.testfixture.internal.Fixture;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
public class TabFolderOperationHandler_Test {
private TabFolder folder;
private TabFolderOperationHandler handler;
@Before
public void setUp() {
Fixture.setUp();
Fixture.fakePhase( PhaseId.PROCESS_ACTION );
Display display = new Display();
Shell shell = new Shell( display, SWT.NONE );
folder = new TabFolder( shell, SWT.NONE );
createTabFolderItems( folder, 3 );
handler = new TabFolderOperationHandler( folder );
}
@After
public void tearDown() {
Fixture.tearDown();
}
@Test
public void testHandleSetSelection() {
TabItem item = folder.getItem( 1 );
handler.handleSet( new JsonObject().add( "selection", getId( item ) ) );
assertArrayEquals( new TabItem[] { item }, folder.getSelection() );
}
@Test
public void testHandleNotifySelection() {
TabItem item = folder.getItem( 1 );
Listener listener = mock( Listener.class );
folder.addListener( SWT.Selection, listener );
JsonObject properties = new JsonObject()
.add( "altKey", true )
.add( "shiftKey", true )
.add( "item", getId( item ) );
handler.handleNotify( EVENT_SELECTION, properties );
ArgumentCaptor<Event> captor = ArgumentCaptor.forClass( Event.class );
verify( listener ).handleEvent( captor.capture() );
Event event = captor.getValue();
assertEquals( SWT.ALT | SWT.SHIFT, event.stateMask );
assertSame( item, event.item );
}
private static void createTabFolderItems( TabFolder folder, int number ) {
for( int i = 0; i < number; i++ ) {
TabItem item = new TabItem( folder, SWT.NONE );
item.setText( "item " + i );
}
}
}