blob: 97d1fb4e5a5e49283053827f38d48ae8d48c8097 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010 SAP AG, Walldorf.
* 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:
* SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.platform.discovery.ui.test.comp.internal;
import java.util.Arrays;
import java.util.List;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.eclipse.platform.discovery.runtime.api.ISearchDestination;
import org.eclipse.platform.discovery.runtime.api.ISearchSubdestination;
import org.eclipse.platform.discovery.runtime.internal.ISearchProviderConfiguration;
import org.eclipse.platform.discovery.runtime.internal.ProviderNotFoundException;
import org.eclipse.platform.discovery.runtime.internal.model.descriptions.IDestinationCategoryDescription;
import org.eclipse.platform.discovery.runtime.internal.model.descriptions.IObjectTypeDescription;
import org.eclipse.platform.discovery.runtime.internal.model.descriptions.ISearchProviderDescription;
import org.eclipse.platform.discovery.testutils.utils.abbot.AbstractUITest;
import org.eclipse.platform.discovery.testutils.utils.abbot.IAbbotTestUiStarter;
import org.eclipse.platform.discovery.testutils.utils.abbot.matchers.EnhancedTextMatcher;
import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.Assertions;
import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.ConditionCheckException;
import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.IWaitCondition;
import org.eclipse.platform.discovery.ui.api.ISearchParametersUI.IConsoleContext;
import org.eclipse.platform.discovery.ui.internal.view.impl.SubdestinationsSelectedListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;
import abbot.swt.finder.generic.MultipleFoundException;
import abbot.swt.finder.generic.NotFoundException;
import abbot.swt.tester.ButtonTester;
import abbot.swt.tester.ShellTester;
public class SubdestinationsSelectedListenerTest extends AbstractUITest
{
private ISearchProviderConfiguration providerConfiguration;
private IObjectTypeDescription searchObjectType;
private IDestinationCategoryDescription destinationCategory;
private ISearchProviderDescription searchProviderDescription;
private ISearchSubdestination subDestination_1;
private ISearchSubdestination subDestination_2;
private ISearchSubdestination subDestination_3;
private static final String SUBDESTINATION_NAME_1 = "Subdestination 1";
private static final String SUBDESTINATION_NAME_2 = "Subdestination 2";
private static final String SUBDESTINATION_NAME_3 = "Subdestination 3";
private ISearchDestination searchDestination;
private SubdestinationsSelectedListener testListener;
private ChangeChecker changeChecker;
private FormToolkit formToolkit;
@Override
protected IAbbotTestUiStarter getUiStarter()
{
return new IAbbotTestUiStarter()
{
public void closeEnclosingDialog(Display display)
{
TESTER.key(SWT.ESC);
}
};
}
@Override
public Display setUpPDE() throws Exception
{
super.setUpPDE();
searchObjectType = EasyMock.createNiceMock(IObjectTypeDescription.class);
destinationCategory = EasyMock.createNiceMock(IDestinationCategoryDescription.class);
searchDestination = EasyMock.createNiceMock(ISearchDestination.class);
changeChecker = new ChangeChecker(null, null, null, false){
public void checkChange(IObjectTypeDescription searchObjectType, ISearchDestination searchDestination, ISearchSubdestination subDestination, boolean newActivationState) {
};
};
setupSubdestinations();
setupProviderConfiguration();
getDisplay().syncExec(new Runnable()
{
public void run()
{
formToolkit = new FormToolkit(getDisplay());
testListener = createTestedListener(activeShell(), formToolkit);
}
});
return getDisplay();
}
@Override
public void tearDownPDE() throws Exception
{
super.tearDownPDE();
formToolkit.dispose();
}
private SubdestinationsSelectedListener createTestedListener(final Control parent, final FormToolkit formToolkit)
{
final IConsoleContext context = EasyMock.createMock(IConsoleContext.class);
EasyMock.expect(context.destinationCategory()).andStubReturn(destinationCategory);
EasyMock.expect(context.searchDestination()).andStubReturn(searchDestination);
EasyMock.expect(context.searchProvider()).andStubReturn(searchProviderDescription);
EasyMock.expect(context.searchProviderConfiguration()).andStubReturn(providerConfiguration);
EasyMock.expect(context.selectedObjectType()).andStubReturn(searchObjectType);
context.notifyComplete(EasyMock.anyBoolean());
EasyMock.expectLastCall().asStub();
// The mocked context expects notifySubdestinationActivationChange calls and verifies input parameters via changeChecker
context.notifySubdestinationActivationChange(EasyMock.isA(ISearchSubdestination.class), EasyMock.anyBoolean());
EasyMock.expectLastCall().andStubAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
final ISearchSubdestination subDestination = (ISearchSubdestination)EasyMock.getCurrentArguments()[0];
final boolean newState = (Boolean)EasyMock.getCurrentArguments()[1];
changeChecker.checkChange(searchObjectType, searchDestination, subDestination, newState);
return null;
}
});
EasyMock.replay(context);
return new SubdestinationsSelectedListener(parent, context);
}
private void setupSubdestinations()
{
subDestination_1 = EasyMock.createNiceMock(ISearchSubdestination.class);
EasyMock.expect(subDestination_1.getDisplayName()).andStubReturn(SUBDESTINATION_NAME_1);
subDestination_2 = EasyMock.createNiceMock(ISearchSubdestination.class);
EasyMock.expect(subDestination_2.getDisplayName()).andStubReturn(SUBDESTINATION_NAME_2);
subDestination_3 = EasyMock.createNiceMock(ISearchSubdestination.class);
EasyMock.expect(subDestination_3.getDisplayName()).andStubReturn(SUBDESTINATION_NAME_3);
EasyMock.replay(subDestination_1, subDestination_2, subDestination_3);
}
private void setupProviderConfiguration() throws ProviderNotFoundException
{
searchProviderDescription = EasyMock.createNiceMock(ISearchProviderDescription.class);
providerConfiguration = EasyMock.createNiceMock(ISearchProviderConfiguration.class);
final List<ISearchSubdestination> subDestinations = Arrays.asList(new ISearchSubdestination[] { subDestination_1, subDestination_2, subDestination_3 });
EasyMock.expect(providerConfiguration.getAvailableSearchSubdestinations(searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(subDestinations);
EasyMock.replay(searchProviderDescription);
}
private void providerConfigurationDone()
{
EasyMock.replay(providerConfiguration);
}
private SelectionEvent createSelectionEvent()
{
final Shell s = activeShell();
final Rectangle rect = ShellTester.getShellTester().getClientArea(s);
final Event e = new Event();
e.x = rect.x + (rect.width / 2);
e.y = rect.y + (rect.height / 2);
e.widget = s;
return new SelectionEvent(e);
}
private Shell getCurrentActiveShell()
{
final Shell[] result = new Shell[1];
getDisplay().syncExec(new Runnable()
{
public void run()
{
result[0] = getDisplay().getActiveShell();
}
});
return result[0];
}
private Shell activeShell()
{
final Shell[] result = new Shell[1];
Assertions.waitAssert(new IWaitCondition()
{
@Override
public boolean checkCondition() throws ConditionCheckException
{
result[0] = getCurrentActiveShell();
return result[0] != null;
}
}, "Could not find active shell");
return result[0];
}
private void widgetSelected(final SubdestinationsSelectedListener listener, final SelectionEvent event)
{
getDisplay().syncExec(new Runnable()
{
public void run()
{
listener.widgetSelected(event);
}
});
}
public void testAllSubdestinationsAreShown() throws NotFoundException, MultipleFoundException
{
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_1, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(true);
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_2, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(false);
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_3, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(true);
providerConfigurationDone();
widgetSelected(testListener, createSelectionEvent());
FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_1));
FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_2));
FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_3));
}
public void testInitiallySelectedSubdestinationsAreChecked() throws NotFoundException, MultipleFoundException
{
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_1, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(true);
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_2, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(true);
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_3, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(false);
providerConfigurationDone();
widgetSelected(testListener, createSelectionEvent());
final Button subdestB1 = (Button) FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_1));
assertTrue("Subdestination 1 should be selected", this.isSelected(subdestB1));
final Button subdestB2 = (Button) FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_2));
assertTrue("Subdestination 2 should be selected", this.isSelected(subdestB2));
final Button subdestB3 = (Button) FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_3));
assertFalse("Subdestination 3 should not be selected", this.isSelected(subdestB3));
}
public void testSelectingSubdestinations() throws NotFoundException, MultipleFoundException
{
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_1, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(true);
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_3, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(false);
providerConfigurationDone();
widgetSelected(testListener, createSelectionEvent());
final ButtonTester tester = ButtonTester.getButtonTester();
final Button subdestB1 = (Button) FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_1));
final Button subdestB3 = (Button) FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_3));
tester.actionClick(subdestB1);
tester.actionClick(subdestB3);
EasyMock.verify(providerConfiguration);
}
public void testSubdestChangeListenerNotifiedOnSubdestSelection() throws NotFoundException, MultipleFoundException
{
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_1, searchObjectType, destinationCategory, searchProviderDescription)).andStubReturn(false);
changeChecker = new ChangeChecker(searchObjectType, searchDestination, subDestination_1, true);
providerConfigurationDone();
widgetSelected(testListener, createSelectionEvent());
final ButtonTester tester = ButtonTester.getButtonTester();
final Button subdestB1 = (Button) FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_1));
tester.actionClick(subdestB1);
forceAsynchronousEventsToBeProcessed();
assertTrue("Event was not handled", changeChecker.changeChecked());
}
private void forceAsynchronousEventsToBeProcessed() {
getDisplay().syncExec(new Runnable()
{
public void run()
{
}
});
}
public void testSelectWithConflicting() throws NotFoundException, MultipleFoundException{
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_1, searchObjectType, destinationCategory, searchProviderDescription)).andReturn(true);
EasyMock.expect(providerConfiguration.isSubdestinationActive(subDestination_2, searchObjectType, destinationCategory, searchProviderDescription)).andReturn(false);
providerConfigurationDone();
widgetSelected(testListener, createSelectionEvent());
final ButtonTester tester = ButtonTester.getButtonTester();
Button subd1 = (Button) FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_1));
Button subd2 = (Button) FINDER.find(new EnhancedTextMatcher(SUBDESTINATION_NAME_2));
tester.actionClick(subd2);
forceAsynchronousEventsToBeProcessed();
assertFalse("Button for subd1 should be deselected because it conflicts with subd2",tester.getSelection(subd1));
assertTrue("Button for subd2 should be selected", tester.getSelection(subd2));
}
private class ChangeChecker
{
private final ISearchDestination expectedDest;
private final IObjectTypeDescription expectedObjType;
private final ISearchSubdestination expectedSubDest;
private final boolean expectedState;
private boolean checked;
public ChangeChecker(final IObjectTypeDescription expectedObjType, final ISearchDestination expectedDest, final ISearchSubdestination expectedSubDest, final boolean expectedState)
{
this.expectedDest = expectedDest;
this.expectedObjType = expectedObjType;
this.expectedSubDest = expectedSubDest;
this.expectedState = expectedState;
checked = false;
}
public void checkChange(final IObjectTypeDescription searchObjectType, final ISearchDestination searchDestination, final ISearchSubdestination subDestination, final boolean newActivationState)
{
assertSame("Unexpected object type", this.expectedObjType, searchObjectType);
assertSame("Unexpected search destination", this.expectedDest, searchDestination);
assertSame("Unexpected subdestination", this.expectedSubDest, subDestination);
assertSame("Unexpected state", this.expectedState, newActivationState);
checked = true;
}
public boolean changeChecked()
{
return checked;
}
}
}