Bug 546259: SWTBotEditor.isActive() returns true when not active part

SWTBotEditor.isActive() now returns true only if the editor is the
active part.

SWTBotEditor.isActiveEditor() is added and returns true if the editor is
the active editor, even if it is not the active part.

SWTWorkbenchBot.activePart() is added and returns the active part, which
may be a view or an editor.

Change-Id: I696da9f9462b8dbeca51ea5a0b4ca52142b81a66
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
diff --git a/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseEditorTest.java b/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseEditorTest.java
index 0015987..9e0e7bb 100644
--- a/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseEditorTest.java
+++ b/org.eclipse.swtbot.eclipse.finder.test/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseEditorTest.java
@@ -91,11 +91,33 @@
 		javaClass.createClass("com.foo.example", "BazClass");
 
 		assertTrue(bot.activeEditor().isActive());
+		assertTrue(bot.activeEditor().isActiveEditor());
 		assertFalse(bot.editorByTitle("FooClass.java").isActive());
+		assertFalse(bot.editorByTitle("FooClass.java").isActiveEditor());
 		assertFalse(bot.editorByTitle("BarClass.java").isActive());
+		assertFalse(bot.editorByTitle("BarClass.java").isActiveEditor());
 		assertTrue(bot.editorByTitle("BazClass.java").isActive());
+		assertTrue(bot.editorByTitle("BazClass.java").isActiveEditor());
 	}
-	
+
+	@Test
+	public void isActiveIsFalseForActiveEditor() {
+		javaClass.createClass("com.foo.example", "FooClass");
+		javaClass.createClass("com.foo.example", "BarClass");
+		javaClass.createClass("com.foo.example", "BazClass");
+
+		bot.viewByTitle("Package Explorer").setFocus();
+
+		assertFalse(bot.activeEditor().isActive());
+		assertTrue(bot.activeEditor().isActiveEditor());
+		assertFalse(bot.editorByTitle("FooClass.java").isActive());
+		assertFalse(bot.editorByTitle("FooClass.java").isActiveEditor());
+		assertFalse(bot.editorByTitle("BarClass.java").isActive());
+		assertFalse(bot.editorByTitle("BarClass.java").isActiveEditor());
+		assertFalse(bot.editorByTitle("BazClass.java").isActive());
+		assertTrue(bot.editorByTitle("BazClass.java").isActiveEditor());
+	}
+
 	@Test
 	public void contextMenu() {
 		editor.contextMenu("Copy Qualified Name").click();
diff --git a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/SWTWorkbenchBot.java b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/SWTWorkbenchBot.java
index 6fadff4..70cb869 100644
--- a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/SWTWorkbenchBot.java
+++ b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/SWTWorkbenchBot.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009,2010 SWTBot Committers and others
+ * Copyright (c) 2009, 2019 SWTBot Committers 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
@@ -14,9 +14,9 @@
 
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartId;
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;
-import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withTitle;
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPerspectiveId;
 import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPerspectiveLabel;
+import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withTitle;
 import static org.eclipse.swtbot.eclipse.finder.waits.Conditions.waitForEditor;
 import static org.eclipse.swtbot.eclipse.finder.waits.Conditions.waitForView;
 import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec;
@@ -32,6 +32,7 @@
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotMultiPageEditor;
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotPerspective;
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotWorkbenchPart;
 import org.eclipse.swtbot.swt.finder.SWTBot;
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 import org.eclipse.swtbot.swt.finder.results.Result;
@@ -39,6 +40,7 @@
 import org.eclipse.ui.IPerspectiveDescriptor;
 import org.eclipse.ui.IViewReference;
 import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPartReference;
 import org.eclipse.ui.PlatformUI;
 import org.hamcrest.Matcher;
 import org.hamcrest.Matchers;
@@ -120,6 +122,23 @@
 	}
 
 	/**
+	 * Returns the active workbench part
+	 * 
+	 * @return the active part, if any
+	 * @throws WidgetNotFoundException if there is no active part
+	 * @since 2.8
+	 */
+	public SWTBotWorkbenchPart<?> activePart() {
+		IWorkbenchPartReference part = workbenchContentsFinder.findActivePart();
+		if (part instanceof IViewReference) {
+			return new SWTBotView((IViewReference) part, this);
+		} else if (part instanceof IEditorReference) {
+			return new SWTBotEditor((IEditorReference) part, this);
+		}
+		throw new WidgetNotFoundException("There is no active part"); //$NON-NLS-1$
+	}
+
+	/**
 	 * Waits for a view matching the given matcher to appear in the active workbench page and returns it
 	 * 
 	 * @param matcher the matcher used to match views
@@ -263,7 +282,8 @@
 	}
 
 	/**
-	 * Returns the active workbench editor part
+	 * Returns the active workbench editor part, which is not necessarily the active
+	 * workbench part
 	 * 
 	 * @return the active editor, if any
 	 * @throws WidgetNotFoundException if there is no active view
diff --git a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/finders/WorkbenchContentsFinder.java b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/finders/WorkbenchContentsFinder.java
index cac2b3d..bf4acba 100644
--- a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/finders/WorkbenchContentsFinder.java
+++ b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/finders/WorkbenchContentsFinder.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009 SWTBot Committers and others
+ * Copyright (c) 2009, 2019 SWTBot Committers 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
@@ -126,6 +126,19 @@
 	}
 
 	/**
+	 * @return the active part.
+	 * @since 2.8
+	 */
+	public IWorkbenchPartReference findActivePart() {
+		return syncExec(new Result<IWorkbenchPartReference>() {
+			@Override
+			public IWorkbenchPartReference run() {
+				return activePageInternal().getActivePartReference();
+			}
+		});
+	}
+
+	/**
 	 * @return the active view.
 	 */
 	public IViewReference findActiveView() {
diff --git a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEditor.java b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEditor.java
index e5daefa..950ca67 100644
--- a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEditor.java
+++ b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEditor.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008,2009,2010 Ketan Padegaonkar and others.
+ * Copyright (c) 2008, 2019 Ketan Padegaonkar 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
@@ -58,8 +58,21 @@
 		super(editorReference, bot, description);
 	}
 
+	/**
+	 * @see #isActiveEditor()
+	 */
 	@Override
 	public boolean isActive() {
+		return partReference.getPage().getActivePartReference() == partReference;
+	}
+
+	/**
+	 * @return <code>true</code> if the editor is the active editor, which is not
+	 *         necessarily the active part.
+	 * @since 2.8
+	 * @see #isActive()
+	 */
+	public boolean isActiveEditor() {
 		return bot.activeEditor().partReference == partReference;
 	}