Skip to content

Commit

Permalink
Make SWTBot tests on dirty state more robust
Browse files Browse the repository at this point in the history
We should retest the dirty state a few times, since the update of the
dirty state takes place in an asynchronous way

Change-Id: I6886b361e921389d3acdaf941fd3e88b4962ab07
  • Loading branch information
LorenzoBettini committed Dec 16, 2016
1 parent 74a8159 commit 2763c7b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
Expand Up @@ -33,11 +33,13 @@
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.results.ListResult;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.ISaveablePart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.PlatformUI;
import org.junit.AfterClass;
import org.junit.Assert;
Expand Down Expand Up @@ -110,9 +112,9 @@ public void testDirtyForSelectedBook() {
libraryNode.select("Book Domain Specific Languages");
botView.bot().textWithLabel("Title").setText("My Tyltle");
someSleep();
Assert.assertTrue(isLibraryViewDirty(TEST_CDO_FORM_VIEW));
assertLibraryViewDirty(TEST_CDO_FORM_VIEW, true);
forceSaveView(botView);
Assert.assertFalse(isLibraryViewDirty(TEST_CDO_FORM_VIEW));
assertLibraryViewDirty(TEST_CDO_FORM_VIEW, false);
}

private void someSleep() {
Expand Down Expand Up @@ -156,8 +158,21 @@ public static void checkCDOServer(String host, String user, String password, Str
session.close();
}

private boolean isLibraryViewDirty(String id) {
return getLibraryView(id).getViewReference().isDirty();
private void assertLibraryViewDirty(final String id, final boolean expectedDirty) {
final IViewReference viewReference = getLibraryView(id).getViewReference();
bot.waitUntil(new DefaultCondition() {
@Override
public boolean test() throws Exception {
boolean dirty = viewReference.isDirty();
return dirty == expectedDirty;
}

@Override
public String getFailureMessage() {
return "expected dirty: " + expectedDirty +
", but was " + viewReference.isDirty();
}
});
}

private static SWTBotView openTestView(String libraryView) {
Expand Down
Expand Up @@ -73,6 +73,7 @@
import org.eclipse.swtbot.swt.finder.results.ListResult;
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
Expand Down Expand Up @@ -1188,14 +1189,28 @@ protected void assertDialogControlsOfWriterNode(boolean editable) {
}

protected SWTBotEditor assertEditorNotDirty(String editorName) {
SWTBotEditor editor = getEditor(editorName);
assertTrue("editor should NOT be in dirty state", !editor.isDirty());
return editor;
return assertEditorDirtyState(editorName, false);
}

protected SWTBotEditor assertEditorDirty(String editorName) {
SWTBotEditor editor = getEditor(editorName);
assertTrue("editor should be in dirty state", editor.isDirty());
return assertEditorDirtyState(editorName, true);
}

private SWTBotEditor assertEditorDirtyState(String editorName, final boolean expectedDirtyState) {
final SWTBotEditor editor = getEditor(editorName);
bot.waitUntil(new DefaultCondition() {
@Override
public boolean test() throws Exception {
boolean dirty = editor.isDirty();
return dirty == expectedDirtyState;
}

@Override
public String getFailureMessage() {
return "expected dirty: " + expectedDirtyState +
", but was " + editor.isDirty();
}
});
return editor;
}

Expand All @@ -1212,9 +1227,21 @@ protected void assertDirtyThenSaveAndAssertNotDirty(String viewName) {
saveViewAndAssertNotDirty(viewName);
}

protected void assertSaveableViewIsDirty(boolean isDirty, String viewName) {
ISaveablePart viewAsSaveablePart = getViewAsSaveablePart(viewName);
assertEquals(isDirty, viewAsSaveablePart.isDirty());
protected void assertSaveableViewIsDirty(final boolean expectedDirty, String viewName) {
final ISaveablePart viewAsSaveablePart = getViewAsSaveablePart(viewName);
bot.waitUntil(new DefaultCondition() {
@Override
public boolean test() throws Exception {
boolean dirty = viewAsSaveablePart.isDirty();
return dirty == expectedDirty;
}

@Override
public String getFailureMessage() {
return "expected dirty: " + expectedDirty +
", but was " + viewAsSaveablePart.isDirty();
}
});
}

protected ISaveablePart getViewAsSaveablePart(String viewName) {
Expand Down

0 comments on commit 2763c7b

Please sign in to comment.