CQ-4081: Initial import
diff --git a/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.classpath b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.classpath
new file mode 100644
index 0000000..8a8f166
--- /dev/null
+++ b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<classpath>

+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>

+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>

+	<classpathentry kind="src" path="src"/>

+	<classpathentry kind="output" path="bin"/>

+</classpath>

diff --git a/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.project b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.project
new file mode 100644
index 0000000..3002c38
--- /dev/null
+++ b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<projectDescription>

+	<name>org.eclipse.e4.ui.deeplink.typehandler.perspective.test</name>

+	<comment></comment>

+	<projects>

+	</projects>

+	<buildSpec>

+		<buildCommand>

+			<name>org.eclipse.jdt.core.javabuilder</name>

+			<arguments>

+			</arguments>

+		</buildCommand>

+		<buildCommand>

+			<name>org.eclipse.pde.ManifestBuilder</name>

+			<arguments>

+			</arguments>

+		</buildCommand>

+		<buildCommand>

+			<name>org.eclipse.pde.SchemaBuilder</name>

+			<arguments>

+			</arguments>

+		</buildCommand>

+	</buildSpec>

+	<natures>

+		<nature>org.eclipse.pde.PluginNature</nature>

+		<nature>org.eclipse.jdt.core.javanature</nature>

+	</natures>

+</projectDescription>

diff --git a/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.settings/org.eclipse.jdt.core.prefs b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..45e2549
--- /dev/null
+++ b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+#Thu Feb 18 16:48:50 GMT 2010

+eclipse.preferences.version=1

+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6

+org.eclipse.jdt.core.compiler.compliance=1.6

+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error

+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error

+org.eclipse.jdt.core.compiler.source=1.6

diff --git a/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/META-INF/MANIFEST.MF b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..c61ba38
--- /dev/null
+++ b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/META-INF/MANIFEST.MF
@@ -0,0 +1,11 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Test Fragment
+Bundle-SymbolicName: org.eclipse.e4.ui.deeplink.typehandler.perspective.test
+Bundle-Version: 4.6.0.qualifier
+Fragment-Host: org.eclipse.e4.ui.deeplink.typehandler.perspective;bundle-version="1.0.0"
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Require-Bundle: org.easymock,
+ org.junit;bundle-version="3.8.2",
+ javax.servlet;bundle-version="2.4.0",
+ org.eclipse.e4.ui.test.utils
diff --git a/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/build.properties b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/build.properties
new file mode 100644
index 0000000..41eb6ad
--- /dev/null
+++ b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/build.properties
@@ -0,0 +1,4 @@
+source.. = src/

+output.. = bin/

+bin.includes = META-INF/,\

+               .

diff --git a/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/src/org/eclipse/e4/ui/deeplink/typehandler/perspective/DeepLinkPerspectiveTypeHandlerTest.java b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/src/org/eclipse/e4/ui/deeplink/typehandler/perspective/DeepLinkPerspectiveTypeHandlerTest.java
new file mode 100644
index 0000000..0d12c28
--- /dev/null
+++ b/tests/org.eclipse.e4.ui.deeplink.typehandler.perspective.test/src/org/eclipse/e4/ui/deeplink/typehandler/perspective/DeepLinkPerspectiveTypeHandlerTest.java
@@ -0,0 +1,265 @@
+/******************************************************************************
+ * Copyright (c) David Orme 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:
+ *    David Orme - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.e4.ui.deeplink.typehandler.perspective;

+

+import org.easymock.EasyMock;

+//import org.easymock.classextension.EasyMock;

+import org.eclipse.core.runtime.ILog;

+import org.eclipse.core.runtime.Status;

+import org.eclipse.e4.core.deeplink.api.IStatusFactory;
+import org.eclipse.e4.ui.deeplink.typehandler.perspective.DeepLinkPerspectiveTypeHandler;
+import org.eclipse.e4.ui.test.utils.SWTTestCase;
+import org.eclipse.jface.preference.IPreferenceStore;

+import org.eclipse.ui.IPerspectiveDescriptor;

+import org.eclipse.ui.IPerspectiveRegistry;

+import org.eclipse.ui.IWorkbench;

+import org.eclipse.ui.IWorkbenchPage;

+import org.eclipse.ui.IWorkbenchPreferenceConstants;

+import org.eclipse.ui.IWorkbenchWindow;

+import org.eclipse.ui.WorkbenchException;

+

+

+public class DeepLinkPerspectiveTypeHandlerTest extends SWTTestCase {

+

+	public static final String PLUGIN_ID = "org.eclipse.e4.enterprise.utils.app.skeleton.example";

+	

+	private DeepLinkPerspectiveTypeHandler testee;

+	

+	public void setUp() throws Exception {

+		super.setUp();

+			

+		testee = new DeepLinkPerspectiveTypeHandler();

+		shell.setMinimized(true);

+	}

+

+	

+	// ---------------------------------------------------------------------	

+	

+	private Status makeErrorStatus(String errorMessage) {

+		Status errorStatus = new Status(Status.ERROR, PLUGIN_ID, errorMessage);

+		IStatusFactory statusFactory = EasyMock.createMock(IStatusFactory.class);

+		EasyMock.expect(statusFactory.error(errorMessage)).andReturn(errorStatus);

+		EasyMock.replay(statusFactory);

+		testee.statusFactory = statusFactory;

+		return errorStatus;

+	}

+	

+	private Status makeErrorStatus(String errorMessage, Throwable t) {

+		Status errorStatus = new Status(Status.ERROR, PLUGIN_ID, errorMessage, t);

+		IStatusFactory statusFactory = EasyMock.createMock(IStatusFactory.class);

+		EasyMock.expect(statusFactory.error(errorMessage, t)).andReturn(errorStatus);

+		EasyMock.replay(statusFactory);

+		testee.statusFactory = statusFactory;

+		return errorStatus;

+	}

+	

+	private Status makeInfoStatus(String message) {

+		Status status = new Status(Status.INFO, PLUGIN_ID, message);

+		IStatusFactory statusFactory = EasyMock.createMock(IStatusFactory.class);

+		EasyMock.expect(statusFactory.info(message)).andReturn(status);

+		EasyMock.replay(statusFactory);

+		testee.statusFactory = statusFactory;

+		return status;

+	}

+	

+	private ILog makeLogger(Status status) {

+		ILog logger = EasyMock.createMock(ILog.class);

+		logger.log(status);

+		EasyMock.replay(logger);

+		return logger;

+	}

+	

+	private ILog makeLoggerExpectingError(String errorMessage) {

+		Status errorStatus = makeErrorStatus(errorMessage);

+		return makeLogger(errorStatus);

+	}

+

+	private ILog makeLoggerExpectingError(String errorMessage, Throwable t) {

+		Status errorStatus = makeErrorStatus(errorMessage, t);

+		return makeLogger(errorStatus);

+	}

+

+	private ILog makeLoggerExpectingInfo(String message) {

+		Status status = makeInfoStatus(message);

+		return makeLogger(status);

+	}

+

+	private IWorkbench makeWorkbenchWithPerspectiveID(String perspID, IPerspectiveDescriptor perspective) {

+		IPerspectiveRegistry reg = EasyMock.createMock(IPerspectiveRegistry.class);

+		EasyMock.expect(reg.findPerspectiveWithId(perspID)).andReturn(perspective);

+		EasyMock.replay(reg);

+		

+		IWorkbench workbench = EasyMock.createMock(IWorkbench.class);

+		EasyMock.expect(workbench.getPerspectiveRegistry()).andReturn(reg);

+		

+		return workbench;

+	}

+

+	private void expectWorkbenchWindowGetShell(IWorkbench workbench) {

+		IWorkbenchWindow workbenchWindow = EasyMock.createMock(IWorkbenchWindow.class);

+		EasyMock.expect(workbenchWindow.getShell()).andReturn(shell);

+		EasyMock.expect(workbench.getActiveWorkbenchWindow()).andReturn(workbenchWindow);

+		EasyMock.replay(workbenchWindow);

+	}

+

+	private IPreferenceStore makePreferenceStoreReturning(String returnValue) {

+		IPreferenceStore store = EasyMock.createMock(IPreferenceStore.class);

+		EasyMock.expect(store.getString(IWorkbenchPreferenceConstants.OPEN_NEW_PERSPECTIVE)).andReturn(returnValue);

+		EasyMock.replay(store);

+		return store;

+	}

+	

+	//-----------------------------------------------------------------------

+	

+	public void testOpenPerspective_nonexistantPerspective_logsAndReturnsFalse() throws Exception {

+		String perspID = "com.nonexistant.perspective";

+

+		IWorkbench workbench = makeWorkbenchWithPerspectiveID(perspID, null);

+		EasyMock.replay(workbench);

+		

+		// Expectations

+		ILog logger = makeLoggerExpectingError("Unable to open perspective: " + perspID);

+		

+		boolean result = testee.openPerspective(perspID, null, workbench, null, logger);

+		

+		EasyMock.verify(logger);

+		assertFalse(result);

+	}

+	

+	public void testOpenPerspective_openInNewWindow_success() throws Exception {

+		String perspID = "com.some.perspective";

+

+		// Dependencies/prereqs

+		IPreferenceStore store = makePreferenceStoreReturning(IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_WINDOW);

+		IPerspectiveDescriptor perspective = EasyMock.createMock(IPerspectiveDescriptor.class);

+

+		// Expectations

+		ILog logger = makeLoggerExpectingInfo("Opened perspective: " + perspID);

+

+		IWorkbench workbench = makeWorkbenchWithPerspectiveID(perspID, perspective);

+		EasyMock.expect(workbench.openWorkbenchWindow(perspID, null)).andReturn(null);

+		expectWorkbenchWindowGetShell(workbench);

+		EasyMock.replay(workbench);

+		

+		boolean result = testee.openPerspective(perspID, null, workbench, store, logger);

+		

+		EasyMock.verify(workbench, logger);

+		assertTrue(shell.isFocusControl());	// Prove the Shell got activated

+		assertTrue(result);

+	}

+

+	public void testOpenPerspective_openInNewWindow_failure_logsErrorAndReturnsFalse() throws Exception {

+		String perspID = "com.some.perspective";

+		WorkbenchException e = new WorkbenchException("test error");

+

+		// Dependencies/prereqs

+		IPreferenceStore store = makePreferenceStoreReturning(IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_WINDOW);

+		IPerspectiveDescriptor perspective = EasyMock.createMock(IPerspectiveDescriptor.class);

+

+		// Expectations

+		ILog logger = makeLoggerExpectingError("Error opening perspective: " + e.getMessage(), e);

+		IWorkbench workbench = makeWorkbenchWithPerspectiveID(perspID, perspective);

+		EasyMock.expect(workbench.openWorkbenchWindow(perspID, null)).andThrow(e);

+		EasyMock.replay(workbench);

+		

+		boolean result = testee.openPerspective(perspID, null, workbench, store, logger);

+		

+		EasyMock.verify(workbench);

+		EasyMock.verify(logger);

+		assertFalse(result);

+	}

+

+	public void testOpenPerspective_openInCurrentWindowWithNewWorkbenchPage_failure_logsErrorAndReturnsFalse() throws Exception {

+		String perspID = "com.some.perspective";

+		WorkbenchException e = new WorkbenchException("test error");

+

+		// Dependencies/prereqs

+		IPreferenceStore store = makePreferenceStoreReturning(IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE);

+		IPerspectiveDescriptor perspective = EasyMock.createMock(IPerspectiveDescriptor.class);

+

+		IWorkbenchWindow window = EasyMock.createMock(IWorkbenchWindow.class);

+		EasyMock.expect(window.getActivePage()).andReturn(null);

+		EasyMock.expect(window.openPage(perspID, null)).andThrow(e);

+		EasyMock.replay(window);

+		

+		// Expectations

+		ILog logger = makeLoggerExpectingError("Error opening perspective: " + e.getMessage(), e);

+

+		IWorkbench workbench = makeWorkbenchWithPerspectiveID(perspID, perspective);

+		EasyMock.expect(workbench.getActiveWorkbenchWindow()).andReturn(window);

+		EasyMock.replay(workbench);

+		

+		boolean result = testee.openPerspective(perspID, null, workbench, store, logger);

+		

+		EasyMock.verify(workbench, window, logger);

+		assertFalse(result);

+	}

+

+	public void testOpenPerspective_openInCurrentWindowWithNewWorkbenchPage_success_ReturnsTrueAndActivatesShell() throws Exception {

+		String perspID = "com.some.perspective";

+

+		// Dependencies/prereqs

+		IPreferenceStore store = makePreferenceStoreReturning(IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE);

+		IPerspectiveDescriptor perspective = EasyMock.createMock(IPerspectiveDescriptor.class);

+

+		IWorkbenchWindow window = EasyMock.createMock(IWorkbenchWindow.class);

+		EasyMock.expect(window.getActivePage()).andReturn(null);

+		EasyMock.expect(window.openPage(perspID, null)).andReturn(null);

+		EasyMock.expect(window.getShell()).andReturn(shell);

+		EasyMock.replay(window);

+		

+		// Expectations

+		ILog logger = makeLoggerExpectingInfo("Opened perspective: " + perspID);

+		

+		IWorkbench workbench = makeWorkbenchWithPerspectiveID(perspID, perspective);

+		EasyMock.expect(workbench.getActiveWorkbenchWindow()).andReturn(window);

+		EasyMock.expect(workbench.getActiveWorkbenchWindow()).andReturn(window);

+		EasyMock.replay(workbench);

+		

+		boolean result = testee.openPerspective(perspID, null, workbench, store, logger);

+		

+		EasyMock.verify(workbench, window, logger);

+		assertTrue(shell.isFocusControl());	// Prove the Shell got activated

+		assertTrue(result);

+	}

+

+	public void testOpenPerspective_openInCurrentWindowWithExistingWorkbenchPage_success_ReturnsTrueAndActivatesShell() throws Exception {

+		String perspID = "com.some.perspective";

+

+		// Dependencies/prereqs

+		IPreferenceStore store = makePreferenceStoreReturning(IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE);

+		IPerspectiveDescriptor perspective = EasyMock.createMock(IPerspectiveDescriptor.class);

+

+		IWorkbenchPage page = EasyMock.createMock(IWorkbenchPage.class);

+		page.setPerspective(perspective);

+		EasyMock.replay(page);

+		

+		IWorkbenchWindow window = EasyMock.createMock(IWorkbenchWindow.class);

+		EasyMock.expect(window.getActivePage()).andReturn(page);

+		EasyMock.expect(window.getShell()).andReturn(shell);

+		EasyMock.replay(window);

+		

+		// Expectations

+		ILog logger = makeLoggerExpectingInfo("Opened perspective: " + perspID);

+		

+		IWorkbench workbench = makeWorkbenchWithPerspectiveID(perspID, perspective);

+		EasyMock.expect(workbench.getActiveWorkbenchWindow()).andReturn(window);

+		EasyMock.expect(workbench.getActiveWorkbenchWindow()).andReturn(window);

+		EasyMock.replay(workbench);

+		

+		boolean result = testee.openPerspective(perspID, null, workbench, store, logger);

+		

+		EasyMock.verify(workbench, window, page, logger);

+		assertTrue(shell.isFocusControl());	// Prove the Shell got activated

+		assertTrue(result);

+	}

+

+}