/*******************************************************************************
* Copyright (c) 2013 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 com.eclipsesource.tabris.internal.ui;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.Serializable;
import org.junit.Test;
import com.eclipsesource.tabris.ui.Action;
public class ActionOperatorImplTest {
@Test
public void testIsSerializable() {
assertTrue( Serializable.class.isAssignableFrom( ActionOperatorImpl.class ) );
}
@Test( expected = IllegalArgumentException.class )
public void testFailsWithNullController() {
new ActionOperatorImpl( null );
}
@Test( expected = IllegalArgumentException.class )
public void testgetActionFailsWithNullId() {
ActionOperatorImpl actionOperator = new ActionOperatorImpl( mock( Controller.class ) );
actionOperator.getAction( null );
}
@Test( expected = IllegalArgumentException.class )
public void testgetActionFailsWithEmptyId() {
ActionOperatorImpl actionOperator = new ActionOperatorImpl( mock( Controller.class ) );
actionOperator.getAction( "" );
}
@Test
public void testFindsAction() {
Controller controller = mock( Controller.class );
RemoteAction remoteAction = mock( RemoteAction.class );
ActionDescriptor descriptor = mock( ActionDescriptor.class );
Action action = mock( Action.class );
when( descriptor.getAction() ).thenReturn( action );
when( remoteAction.getDescriptor() ).thenReturn( descriptor );
when( controller.findRemoteAction( "foo" ) ).thenReturn( remoteAction );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
Action actualAction = actionOperator.getAction( "foo" );
assertSame( action, actualAction );
}
@Test( expected = IllegalArgumentException.class )
public void testExistsFailsWithNullId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.exists( null );
}
@Test( expected = IllegalArgumentException.class )
public void testExistsFailsWithEmptyId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.exists( "" );
}
@Test
public void testActionExistsIfRegistered() {
Controller controller = mock( Controller.class );
doReturn( Boolean.TRUE ).when( controller ).hasAction( "foo" );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
boolean exists = actionOperator.exists( "foo" );
assertTrue( exists );
}
@Test
public void testActionDoesNotExistIfNotRegistered() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
boolean exists = actionOperator.exists( "foo" );
assertFalse( exists );
}
@Test
public void testEnablesActionsByDefault() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
boolean enabled = actionOperator.isActionEnabled( "foo" );
assertTrue( enabled );
}
@Test
public void testDisbalesActions() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.setActionEnabled( "foo", false );
boolean enabled = actionOperator.isActionEnabled( "foo" );
assertFalse( enabled );
verify( controller ).setActionEnabled( "foo", false );
}
@Test( expected = IllegalArgumentException.class )
public void testDisbalesActionFailsForNullId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.setActionEnabled( null, false );
}
@Test( expected = IllegalArgumentException.class )
public void testDisbalesActionFailsForEmptyId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.setActionEnabled( "", false );
}
@Test
public void testActionIsVisibleByDefault() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
boolean visible = actionOperator.isActionVisible( "foo" );
assertTrue( visible );
}
@Test( expected = IllegalArgumentException.class )
public void testActionIsVisibleFailsWithNullId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.isActionVisible( null );
}
@Test( expected = IllegalArgumentException.class )
public void testActionIsVisibleFailsWithEmptyId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.isActionVisible( "" );
}
@Test( expected = IllegalArgumentException.class )
public void testActionIsEnabledFailsWithNullId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.isActionEnabled( null );
}
@Test( expected = IllegalArgumentException.class )
public void testActionIsEnabledFailsWithEmptyId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.isActionEnabled( "" );
}
@Test( expected = IllegalArgumentException.class )
public void testSetEnabledFailsWithNullId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.setActionEnabled( null, true );
}
@Test( expected = IllegalArgumentException.class )
public void testSetEnabledFailsWithEmptyId() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.setActionEnabled( "", true );
}
@Test
public void testHidesAction() {
Controller controller = mock( Controller.class );
ActionOperatorImpl actionOperator = new ActionOperatorImpl( controller );
actionOperator.setActionVisible( "foo", false );
boolean visible = actionOperator.isActionVisible( "foo" );
assertFalse( visible );
verify( controller ).setActionVisible( "foo", false );
}
}