Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import static org.eclipse.ui.tests.harness.util.UITestUtil.openTestWindow;
import static org.eclipse.ui.tests.harness.util.UITestUtil.processEventsUntil;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.Arrays;
Expand Down Expand Up @@ -47,18 +47,17 @@
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.quickaccess.QuickAccessDialog;
import org.eclipse.ui.internal.quickaccess.QuickAccessMessages;
import org.eclipse.ui.tests.harness.util.CloseTestWindowsExtension;
import org.eclipse.ui.tests.harness.util.CloseTestWindowsRule;
import org.eclipse.ui.tests.harness.util.DisplayHelper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
* Tests the quick access UI
* @since 3.4
*/
@ExtendWith(CloseTestWindowsExtension.class)
public class QuickAccessDialogTest {

private class TestQuickAccessDialog extends QuickAccessDialog {
Expand All @@ -73,6 +72,9 @@ protected IDialogSettings getDialogSettings() {
}
}

@Rule
public CloseTestWindowsRule closeTestWindows = new CloseTestWindowsRule();

private static final int TIMEOUT = 5000;
// As defined in QuickAccessDialog and in SearchField
private static final int MAXIMUM_NUMBER_OF_ELEMENTS = 60;
Expand All @@ -82,7 +84,7 @@ protected IDialogSettings getDialogSettings() {
private IWorkbenchWindow activeWorkbenchWindow;


@BeforeEach
@Before
public void setUp() throws Exception {
Arrays.stream(Display.getDefault().getShells()).filter(isQuickAccessShell).forEach(Shell::close);
dialogSettings = new DialogSettings("QuickAccessDialogTest" + System.currentTimeMillis());
Expand All @@ -97,7 +99,7 @@ static Optional<QuickAccessDialog> findQuickAccessDialog() {
.map(QuickAccessDialog.class::cast);
}

@AfterEach
@After
public void tearDown() throws Exception {
Arrays.stream(Display.getDefault().getShells()).filter(isQuickAccessShell)
.forEach(Shell::close);
Expand Down Expand Up @@ -128,24 +130,23 @@ public void testTextFilter(){
dialog.open();
Text text = dialog.getQuickAccessContents().getFilterText();
Table table = dialog.getQuickAccessContents().getTable();
assertTrue(text.getText().isEmpty(), "Quick access filter should be empty");
assertEquals(0, table.getItemCount(), "Quick access table should be empty");
assertTrue("Quick access filter should be empty", text.getText().isEmpty());
assertEquals("Quick access table should be empty", 0, table.getItemCount());

text.setText("T");
processEventsUntil(() -> table.getItemCount() > 1, TIMEOUT);
int oldCount = table.getItemCount();
assertTrue(oldCount > 3, "Not enough quick access items for simple filter");
assertTrue(oldCount < MAXIMUM_NUMBER_OF_ELEMENTS, "Too many quick access items for size of table");
assertTrue("Not enough quick access items for simple filter", oldCount > 3);
assertTrue("Too many quick access items for size of table", oldCount < MAXIMUM_NUMBER_OF_ELEMENTS);
final String oldFirstItemText = table.getItem(0).getText(1);

text.setText("B"); // The letter mustn't be part of the previous 1st proposal
assertTrue(DisplayHelper.waitForCondition(table.getDisplay(),
assertTrue("The quick access items should have changed", DisplayHelper.waitForCondition(table.getDisplay(),
TIMEOUT,
() -> table.getItemCount() > 1 && !table.getItem(0).getText(1).equals(oldFirstItemText)),
"The quick access items should have changed");
() -> table.getItemCount() > 1 && !table.getItem(0).getText(1).equals(oldFirstItemText)));
int newCount = table.getItemCount();
assertTrue(newCount > 3, "Not enough quick access items for simple filter");
assertTrue(newCount < MAXIMUM_NUMBER_OF_ELEMENTS, "Too many quick access items for size of table");
assertTrue("Not enough quick access items for simple filter", newCount > 3);
assertTrue("Too many quick access items for size of table", newCount < MAXIMUM_NUMBER_OF_ELEMENTS);
}

@Test
Expand All @@ -154,13 +155,12 @@ public void testContributedElement() {
dialog.open();
final Table table = dialog.getQuickAccessContents().getTable();
Text text = dialog.getQuickAccessContents().getFilterText();
assertTrue(text.getText().isEmpty(), "Quick access filter should be empty");
assertEquals(0, table.getItemCount(), "Quick access table should be empty");
assertTrue("Quick access filter should be empty", text.getText().isEmpty());
assertEquals("Quick access table should be empty", 0, table.getItemCount());

text.setText(TestQuickAccessComputer.TEST_QUICK_ACCESS_PROPOSAL_LABEL);
assertTrue(DisplayHelper.waitForCondition(dialog.getShell().getDisplay(), TIMEOUT, () ->
dialogContains(dialog, TestQuickAccessComputer.TEST_QUICK_ACCESS_PROPOSAL_LABEL)),
"Missing contributed element"
assertTrue("Missing contributed element", DisplayHelper.waitForCondition(dialog.getShell().getDisplay(), TIMEOUT, () ->
dialogContains(dialog, TestQuickAccessComputer.TEST_QUICK_ACCESS_PROPOSAL_LABEL))
);
}

Expand All @@ -172,11 +172,11 @@ public void testLongRunningComputerDoesntFreezeUI() {
Text text = dialog.getQuickAccessContents().getFilterText();
long duration = System.currentTimeMillis();
text.setText(TestLongRunningQuickAccessComputer.THE_ELEMENT.getId());
assertTrue(System.currentTimeMillis() - duration < TestLongRunningQuickAccessComputer.DELAY,
"UI Frozen on text change");
assertTrue(DisplayHelper.waitForCondition(dialog.getShell().getDisplay(), TestLongRunningQuickAccessComputer.DELAY + TIMEOUT, () ->
assertTrue("UI Frozen on text change",
System.currentTimeMillis() - duration < TestLongRunningQuickAccessComputer.DELAY);
assertTrue("Missing contributed element", DisplayHelper.waitForCondition(dialog.getShell().getDisplay(), TestLongRunningQuickAccessComputer.DELAY + TIMEOUT, () ->
dialogContains(dialog, TestLongRunningQuickAccessComputer.THE_ELEMENT.getLabel())
), "Missing contributed element");
));
table.select(0);
activateCurrentElement(dialog);
duration = System.currentTimeMillis();
Expand All @@ -185,15 +185,15 @@ public void testLongRunningComputerDoesntFreezeUI() {
assertTrue(System.currentTimeMillis() - duration < TestLongRunningQuickAccessComputer.DELAY);
AtomicLong tick = new AtomicLong(System.currentTimeMillis());
AtomicLong maxBlockedUIThread = new AtomicLong();
assertTrue(DisplayHelper.waitForCondition(
assertTrue("Missing contributed element as previous pick", DisplayHelper.waitForCondition(
secondDialog.getShell().getDisplay(), TestLongRunningQuickAccessComputer.DELAY + TIMEOUT, () -> {
long currentTick = System.currentTimeMillis();
long previousTick = tick.getAndSet(currentTick);
long currentDelayInUIThread = currentTick - previousTick;
maxBlockedUIThread.set(Math.max(maxBlockedUIThread.get(), currentDelayInUIThread));
return dialogContains(secondDialog,
TestLongRunningQuickAccessComputer.THE_ELEMENT.getLabel());
}), "Missing contributed element as previous pick");
}));
assertTrue(maxBlockedUIThread.get() < TestLongRunningQuickAccessComputer.DELAY);
}

Expand All @@ -208,15 +208,15 @@ public void testShowAll() throws Exception {
dialog.open();
Text text = dialog.getQuickAccessContents().getFilterText();
final Table table = dialog.getQuickAccessContents().getTable();
assertTrue(text.getText().isEmpty(), "Quick access filter should be empty");
assertEquals(0, table.getItemCount(), "Quick access table should be empty");
assertTrue("Quick access filter should be empty", text.getText().isEmpty());
assertEquals("Quick access table should be empty", 0, table.getItemCount());

// Set a filter to get some items
text.setText("T");
processEventsUntil(() -> table.getItemCount() > 1, TIMEOUT);
final int defaultCount = table.getItemCount();
assertTrue(defaultCount > 3, "Not enough quick access items for simple filter");
assertTrue(defaultCount < MAXIMUM_NUMBER_OF_ELEMENTS, "Too many quick access items for size of table");
assertTrue("Not enough quick access items for simple filter", defaultCount > 3);
assertTrue("Too many quick access items for size of table", defaultCount < MAXIMUM_NUMBER_OF_ELEMENTS);
final String oldFirstItemText = table.getItem(0).getText(1);

IHandlerService handlerService = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
Expand All @@ -225,21 +225,21 @@ public void testShowAll() throws Exception {
handlerService.executeCommand("org.eclipse.ui.window.quickAccess", null); //$NON-NLS-1$
processEventsUntil(() -> table.getItemCount() != defaultCount, TIMEOUT);
final int allCount = table.getItemCount();
assertTrue(allCount > defaultCount, "Turning on show all should display more items");
assertEquals(oldFirstItemText, table.getItem(0).getText(1), "Turning on show all should not change the top item");
assertTrue("Turning on show all should display more items", allCount > defaultCount);
assertEquals("Turning on show all should not change the top item", oldFirstItemText, table.getItem(0).getText(1));

// Run the handler to turn off show all
handlerService.executeCommand("org.eclipse.ui.window.quickAccess", null); //$NON-NLS-1$
processEventsUntil(() -> table.getItemCount() != allCount, TIMEOUT);
// Note: The table count may one off from the old count because of shell resizing (scroll bars being added then removed)
assertTrue(table.getItemCount() < allCount, "Turning off show all should limit items shown");
assertEquals(oldFirstItemText, table.getItem(0).getText(1), "Turning off show all should not change the top item");
assertTrue("Turning off show all should limit items shown", table.getItemCount() < allCount);
assertEquals("Turning off show all should not change the top item", oldFirstItemText, table.getItem(0).getText(1));

// Run the handler to turn on show all
handlerService.executeCommand("org.eclipse.ui.window.quickAccess", null); //$NON-NLS-1$
processEventsUntil(() -> table.getItemCount() == allCount, TIMEOUT);
assertEquals(allCount, table.getItemCount(), "Turning on show all twice shouldn't change the items");
assertEquals(oldFirstItemText, table.getItem(0).getText(1), "Turning on show all twice shouldn't change the top item");
assertEquals("Turning on show all twice shouldn't change the items", allCount, table.getItemCount());
assertEquals("Turning on show all twice shouldn't change the top item", oldFirstItemText, table.getItem(0).getText(1));

// Close and reopen the shell
dialog.close();
Expand All @@ -250,8 +250,8 @@ public void testShowAll() throws Exception {
text.setText("T");
processEventsUntil(() -> newTable.getItemCount() > 1, TIMEOUT);
// Note: The table count may one off from the old count because of shell resizing (scroll bars being added then removed)
assertTrue(newTable.getItemCount() < allCount,
"Show all should be turned off when the shell is closed and reopened");
assertTrue("Show all should be turned off when the shell is closed and reopened",
newTable.getItemCount() < allCount);
}

@Test
Expand All @@ -263,9 +263,9 @@ public void testPreviousChoicesAvailable() {
Table firstTable = dialog.getQuickAccessContents().getTable();
String quickAccessElementText = "Project Explorer";
text.setText(quickAccessElementText);
assertTrue(DisplayHelper.waitForCondition(firstTable.getDisplay(),
assertTrue("Missing entry", DisplayHelper.waitForCondition(firstTable.getDisplay(),
TIMEOUT,
() -> dialogContains(dialog, quickAccessElementText)), "Missing entry");
() -> dialogContains(dialog, quickAccessElementText)));
firstTable.select(0);
activateCurrentElement(dialog);
assertNotEquals(0, dialogSettings.getArray("orderedElements").length);
Expand All @@ -278,9 +278,9 @@ public void testPreviousChoicesAvailable() {
// then try in a new SearchField
QuickAccessDialog secondDialog = new TestQuickAccessDialog(activeWorkbenchWindow, null);
secondDialog.open();
assertTrue(DisplayHelper.waitForCondition(secondDialog.getShell().getDisplay(), TIMEOUT,
() -> dialogContains(secondDialog, quickAccessElementText)),
"Missing entry in previous pick");
assertTrue("Missing entry in previous pick",
DisplayHelper.waitForCondition(secondDialog.getShell().getDisplay(), TIMEOUT,
() -> dialogContains(secondDialog, quickAccessElementText)));
}

private void activateCurrentElement(QuickAccessDialog dialog) {
Expand All @@ -299,18 +299,18 @@ public void testPreviousChoicesAvailableForExtension() {
Text text = dialog.getQuickAccessContents().getFilterText();
text.setText(TestQuickAccessComputer.TEST_QUICK_ACCESS_PROPOSAL_LABEL);
final Table firstTable = dialog.getQuickAccessContents().getTable();
assertTrue(DisplayHelper.waitForCondition(text.getDisplay(), TIMEOUT,
() -> dialogContains(dialog, TestQuickAccessComputer.TEST_QUICK_ACCESS_PROPOSAL_LABEL)),
"Unexpected dialog contents: " + getAllEntries(dialog.getQuickAccessContents().getTable()));
assertTrue("Unexpected dialog contents: " + getAllEntries(dialog.getQuickAccessContents().getTable()),
DisplayHelper.waitForCondition(text.getDisplay(), TIMEOUT,
() -> dialogContains(dialog, TestQuickAccessComputer.TEST_QUICK_ACCESS_PROPOSAL_LABEL)));
firstTable.select(0);
activateCurrentElement(dialog);
// then try in a new SearchField
QuickAccessDialog secondDialog = new TestQuickAccessDialog(activeWorkbenchWindow, null);
secondDialog.open();
assertTrue(DisplayHelper.waitForCondition(secondDialog.getShell().getDisplay(), TIMEOUT,
assertTrue("Contributed item not found in previous choices",
DisplayHelper.waitForCondition(secondDialog.getShell().getDisplay(), TIMEOUT,
() -> getAllEntries(secondDialog.getQuickAccessContents().getTable()).stream()
.anyMatch(TestQuickAccessComputer::isContributedItem)),
"Contributed item not found in previous choices");
.anyMatch(TestQuickAccessComputer::isContributedItem)));
}

@Test
Expand All @@ -331,10 +331,11 @@ public void testPreviousChoicesAvailableForIncrementalExtension() {
dialog = new TestQuickAccessDialog(activeWorkbenchWindow, null);
dialog.open();
final Table secondTable = dialog.getQuickAccessContents().getTable();
assertTrue(DisplayHelper.waitForCondition(secondTable.getDisplay(), TIMEOUT, //
assertTrue("Contributed item not found in previous choices",
DisplayHelper.waitForCondition(secondTable.getDisplay(), TIMEOUT, //
() -> getAllEntries(secondTable).stream()
.anyMatch(TestIncrementalQuickAccessComputer::isContributedItem)),
"Contributed item not found in previous choices");
.anyMatch(TestIncrementalQuickAccessComputer::isContributedItem)
));
}

@Test
Expand All @@ -344,9 +345,9 @@ public void testPrefixMatchHavePriority() {
Text text = dialog.getQuickAccessContents().getFilterText();
Table table = dialog.getQuickAccessContents().getTable();
text.setText("P");
assertTrue(DisplayHelper.waitForCondition(table.getDisplay(), TIMEOUT, () -> table.getItemCount() > 3),
"Not enough quick access items for simple filter");
assertTrue(table.getItem(0).getText(1).toLowerCase().startsWith("p"), "Non-prefix match first");
assertTrue("Not enough quick access items for simple filter",
DisplayHelper.waitForCondition(table.getDisplay(), TIMEOUT, () -> table.getItemCount() > 3));
assertTrue("Non-prefix match first", table.getItem(0).getText(1).toLowerCase().startsWith("p"));
}

@Test
Expand All @@ -365,9 +366,9 @@ public void testCommandEnableContext() throws Exception {
Text text = dialog.getQuickAccessContents().getFilterText();
Table table = dialog.getQuickAccessContents().getTable();
text.setText("Toggle Split");
assertTrue(DisplayHelper.waitForCondition(table.getDisplay(), TIMEOUT, () -> table.getItemCount() > 1),
"Not enough quick access items for simple filter");
assertTrue(table.getItem(0).getText(1).toLowerCase().startsWith("toggle split"), "Non-prefix match first");
assertTrue("Not enough quick access items for simple filter",
DisplayHelper.waitForCondition(table.getDisplay(), TIMEOUT, () -> table.getItemCount() > 1));
assertTrue("Non-prefix match first", table.getItem(0).getText(1).toLowerCase().startsWith("toggle split"));
}

private List<String> getAllEntries(Table table) {
Expand Down
Loading