blob: 13cced96524fa2c1b8208bc3805aaf9de70e58a4 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2012 SAP AG 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:
* SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.platform.discovery.destprefs.test.unit.prefpage.ui;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.platform.discovery.destprefs.internal.prefpage.ui.CategoryDestinationProviderPair;
import org.eclipse.platform.discovery.destprefs.internal.prefpage.ui.DestinationConfiguratorSelection;
import org.eclipse.platform.discovery.destprefs.internal.prefpage.ui.IDestinationConfiguratorsPresenter;
import org.eclipse.platform.discovery.runtime.api.IDestinationsProvider;
import org.eclipse.platform.discovery.runtime.api.ISearchDestination;
import org.eclipse.platform.discovery.runtime.internal.model.descriptions.IDestinationCategoryDescription;
import org.eclipse.platform.discovery.runtime.internal.model.descriptions.IDestinationsProviderDescription;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class DestinationsPreferencePageTest {
private static final String CATEGORY_ID = "my.category.id";
private static final String CATEGORY_NAME = "My Category";
private static final String DESTINATION_PROVIDER_ID = "my.dest.provider.id";
private static final String DESTINATION_NAME = "My Destination";
private DestinationsPreferencePagePageObject pageObject;
@Mock
private IDestinationConfiguratorsPresenter presenter;
@Mock
private IDestinationCategoryDescription category;
@Mock
private IDestinationsProviderDescription destinationProviderDescr;
@Mock
private IDestinationsProvider destinationProvider;
@Mock
private ISearchDestination destination;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
stubDestinations();
pageObject = new DestinationsPreferencePagePageObject(presenter);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
pageObject.getView().setAddEnabled(true);
pageObject.getView().setRemoveEnabled(true);
pageObject.getView().setEditEnabled(true);
pageObject.getView().setTestEnabled(true);
return null;
}
}).when(presenter).selectionChanged(Mockito.any(DestinationConfiguratorSelection.class));
pageObject.open();
pageObject.setInput(createPageInput());
}
private void stubDestinations() {
Mockito.stub(destination.getDisplayName()).toReturn(DESTINATION_NAME);
Mockito.stub(destinationProvider.getSearchDestinations()).toReturn(new HashSet<ISearchDestination>(Arrays.asList(new ISearchDestination[]{destination})));
Mockito.stub(destinationProviderDescr.createProvider()).toReturn(destinationProvider);
Mockito.stub(destinationProviderDescr.getId()).toReturn(DESTINATION_PROVIDER_ID);
Mockito.stub(category.getId()).toReturn(CATEGORY_ID);
Mockito.stub(category.getDisplayName()).toReturn(CATEGORY_NAME);
}
private Collection<CategoryDestinationProviderPair> createPageInput() {
final Collection<CategoryDestinationProviderPair> result = new ArrayList<CategoryDestinationProviderPair>();
result.add(new CategoryDestinationProviderPair(category, destinationProviderDescr));
return result;
}
@After
public void tearDown() {
pageObject.close();
}
@Test
public void testNoDestinationsSelected() {
assertFalse("Adding destination is unexpectedly enabled", pageObject.canAddDestination());
assertFalse("Removing destination is unexpectedly enabled", pageObject.canRemoveDestination());
assertFalse("Editing destination is unexpectedly enabled", pageObject.canEditDestination());
assertFalse("Testing destination is unexpectedly enabled", pageObject.canTestDestination());
}
@Test
public void testEnableAdd() {
pageObject.setAddEnabled(true);
assertTrue("Adding destination is unexpectedly disabled", pageObject.canAddDestination());
}
@Test
public void testEnableEdit() {
pageObject.setEditEnabled(true);
assertTrue("Editing destination is unexpectedly disabled", pageObject.canEditDestination());
}
@Test
public void testEnableRemove() {
pageObject.setRemoveEnabled(true);
assertTrue("Removing destination is unexpectedly disabled", pageObject.canRemoveDestination());
}
@Test
public void testEnableTest() {
pageObject.setTestEnabled(true);
assertTrue("Testingdestination is unexpectedly disabled", pageObject.canTestDestination());
}
@Test
public void testSetErrorStatus() throws InterruptedException {
setStatusTest(createStatus(IStatus.ERROR, "Some error"), IMessageProvider.ERROR);
}
@Test
public void testSetWarningStatus() throws InterruptedException {
setStatusTest(createStatus(IStatus.WARNING, "Some warning"), IMessageProvider.WARNING);
}
@Test
public void testSetCancelStatus() throws InterruptedException {
setStatusTest(createStatus(IStatus.CANCEL, "Cancelled"), IMessageProvider.INFORMATION);
}
@Test
public void testSetOkStatus() throws InterruptedException {
setStatusTest(createStatus(IStatus.OK, "Success"), IMessageProvider.INFORMATION);
}
@Test
public void testSelectCategory() {
pageObject.selectCategory(CATEGORY_NAME);
Mockito.verify(presenter, Mockito.atLeastOnce()).selectionChanged(Mockito.argThat(selectionMatcher(DESTINATION_PROVIDER_ID, null)));
}
@Test
public void testSelectDestination() {
pageObject.selectDestination(CATEGORY_NAME, DESTINATION_NAME);
Mockito.verify(presenter, Mockito.atLeastOnce()).selectionChanged(Mockito.argThat(selectionMatcher(DESTINATION_PROVIDER_ID, destination)));
}
@Test
public void testAddDestination() {
pageObject.selectDestination(CATEGORY_NAME, DESTINATION_NAME);
pageObject.addDestination();
Mockito.verify(presenter).addDestination();
}
@Test
public void testEditDestination() {
pageObject.selectDestination(CATEGORY_NAME, DESTINATION_NAME);
pageObject.editDestination();
Mockito.verify(presenter).editDestination();
}
@Test
public void testRemoveDestination() {
pageObject.selectDestination(CATEGORY_NAME, DESTINATION_NAME);
pageObject.removeDestination();
Mockito.verify(presenter).removeDestination();
}
@Test
public void testTestDestination() {
pageObject.selectDestination(CATEGORY_NAME, DESTINATION_NAME);
pageObject.testDestination();
Mockito.verify(presenter).testDestination();
}
private Matcher<DestinationConfiguratorSelection> selectionMatcher(String destinationProviderId, final ISearchDestination destination) {
return new BaseMatcher<DestinationConfiguratorSelection>() {
@Override
public boolean matches(Object item) {
final DestinationConfiguratorSelection selection = (DestinationConfiguratorSelection)item;
return selection.destProviderId.equals(DESTINATION_PROVIDER_ID) && (selection.destination == destination);
}
@Override
public void describeTo(Description description) {
}
};
}
private void setStatusTest(final IStatus status, int expectedMessageType) {
pageObject.getView().setStatus(status);
assertEquals("Unexpected message type", expectedMessageType, pageObject.getMessageType());
assertEquals("Unexpected message ", status.getMessage(), pageObject.getMessage());
}
private IStatus createStatus(int severity, String message) {
final IStatus status = Mockito.mock(IStatus.class);
Mockito.stub(status.getSeverity()).toReturn(severity);
Mockito.stub(status.getMessage()).toReturn(message);
return status;
}
}