/******************************************************************************* * Copyright (c) 2002, 2015 Innoopract Informationssysteme GmbH 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: * Innoopract Informationssysteme GmbH - initial API and implementation * EclipseSource - ongoing development ******************************************************************************/ package org.eclipse.swt.internal.widgets.sashkit; import static org.eclipse.rap.rwt.internal.lifecycle.WidgetUtil.getId; import static org.eclipse.rap.rwt.internal.protocol.RemoteObjectFactory.getRemoteObject; import static org.eclipse.rap.rwt.testfixture.internal.TestMessage.getParent; import static org.eclipse.rap.rwt.testfixture.internal.TestMessage.getStyles; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import java.io.IOException; import org.eclipse.rap.json.JsonObject; import org.eclipse.rap.json.JsonValue; import org.eclipse.rap.rwt.internal.protocol.Operation.CreateOperation; import org.eclipse.rap.rwt.internal.remote.RemoteObjectRegistry; import org.eclipse.rap.rwt.remote.OperationHandler; import org.eclipse.rap.rwt.testfixture.internal.Fixture; import org.eclipse.rap.rwt.testfixture.internal.TestMessage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.internal.widgets.controlkit.ControlLCATestUtil; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Sash; import org.eclipse.swt.widgets.Shell; import org.junit.After; import org.junit.Before; import org.junit.Test; public class SashLCA_Test { private Display display; private Shell shell; private Sash sash; private SashLCA lca; @Before public void setUp() { Fixture.setUp(); display = new Display(); shell = new Shell( display ); sash = new Sash( shell, SWT.NONE ); lca = SashLCA.INSTANCE; } @After public void tearDown() { Fixture.tearDown(); } @Test public void testCommonControlProperties() throws IOException { ControlLCATestUtil.testCommonControlProperties( sash ); } @Test public void testRenderCreate() throws IOException { lca.renderInitialization( sash ); TestMessage message = Fixture.getProtocolMessage(); CreateOperation operation = message.findCreateOperation( sash ); assertEquals( "rwt.widgets.Sash", operation.getType() ); } @Test public void testRenderInitialization_setsOperationHandler() throws IOException { String id = getId( sash ); lca.renderInitialization( sash ); OperationHandler handler = RemoteObjectRegistry.getInstance().get( id ).getHandler(); assertTrue( handler instanceof SashOperationHandler ); } @Test public void testReadData_usesOperationHandler() { SashOperationHandler handler = spy( new SashOperationHandler( sash ) ); getRemoteObject( getId( sash ) ).setHandler( handler ); Fixture.fakeNotifyOperation( getId( sash ), "Help", new JsonObject() ); lca.readData( sash ); verify( handler ).handleNotifyHelp( sash, new JsonObject() ); } @Test public void testRenderParent() throws IOException { lca.renderInitialization( sash ); TestMessage message = Fixture.getProtocolMessage(); CreateOperation operation = message.findCreateOperation( sash ); assertEquals( getId( sash.getParent() ), getParent( operation ) ); } @Test public void testRenderCreateWithHorizontal() throws IOException { sash = new Sash( shell, SWT.HORIZONTAL ); lca.renderInitialization( sash ); TestMessage message = Fixture.getProtocolMessage(); CreateOperation operation = message.findCreateOperation( sash ); assertTrue( getStyles( operation ).contains( "HORIZONTAL" ) ); } @Test public void testRenderAddSelectionListener() throws Exception { Fixture.markInitialized( display ); Fixture.markInitialized( sash ); Fixture.preserveWidgets(); sash.addListener( SWT.Selection, mock( Listener.class ) ); lca.renderChanges( sash ); TestMessage message = Fixture.getProtocolMessage(); assertEquals( JsonValue.TRUE, message.findListenProperty( sash, "Selection" ) ); } @Test public void testRenderRemoveSelectionListener() throws Exception { Listener listener = mock( Listener.class ); sash.addListener( SWT.Selection, listener ); Fixture.markInitialized( display ); Fixture.markInitialized( sash ); Fixture.preserveWidgets(); sash.removeListener( SWT.Selection, listener ); lca.renderChanges( sash ); TestMessage message = Fixture.getProtocolMessage(); assertEquals( JsonValue.FALSE, message.findListenProperty( sash, "Selection" ) ); } @Test public void testRenderSelectionListenerUnchanged() throws Exception { Fixture.markInitialized( display ); Fixture.markInitialized( sash ); Fixture.preserveWidgets(); sash.addSelectionListener( new SelectionAdapter() { } ); Fixture.preserveWidgets(); lca.renderChanges( sash ); TestMessage message = Fixture.getProtocolMessage(); assertNull( message.findListenOperation( sash, "Selection" ) ); } }