Skip to content

Commit

Permalink
feat: support GUI unit test
Browse files Browse the repository at this point in the history
- refacotr main gui classes to allow GUI test
    - MainWindowMenuHandler class inherited from BaseMenuHanlder
      abstract class
    - Tester should implement MenuHandler class for test inherited
      from BaseMenuHandler class
- Add testGui configuration on gradle
- Set gui test source under test/gui
- Introduce TestCoreGUI class
- Add BasicMenuItemTest

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed Feb 18, 2024
1 parent 2f99f45 commit 6fafc3b
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 11 deletions.
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ sourceSets {
srcDir 'test/fixtures'
}
}
testGui {
java {
srcDir 'test/gui'
}
}
testIntegration {
java {
srcDir 'test-integration/src'
Expand Down Expand Up @@ -351,6 +356,11 @@ dependencies {
// genMac only
genMac(libs.omegat.appbundler)

testGuiImplementation sourceSets.main.output
testGuiImplementation(testFixtures(project.rootProject))
testGuiImplementation 'org.assertj:assertj-swing-junit:3.17.1'
testGuiImplementation(libs.bundles.jackson)

testIntegrationImplementation sourceSets.main.output, sourceSets.test.output
testIntegrationImplementation(testFixtures(project.rootProject))
testIntegrationRuntimeOnly(libs.slf4j.jdk14)
Expand Down
7 changes: 3 additions & 4 deletions src/org/omegat/gui/accesstool/AccessTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@
import org.omegat.core.Core;
import org.omegat.core.CoreEvents;
import org.omegat.core.events.IProjectEventListener;
import org.omegat.gui.main.BaseMainWindowMenuHandler;
import org.omegat.gui.main.MainMenuIcons;
import org.omegat.gui.main.MainWindow;
import org.omegat.gui.main.MainWindowMenuHandler;
import org.omegat.gui.main.ProjectUICommands;
import org.omegat.util.OConsts;
import org.omegat.util.OStrings;
Expand All @@ -72,14 +71,14 @@ public class AccessTools extends JPanel {

private ProjectComboBoxModel projectComboBoxModel;
private SourceComboBoxModel sourceComboBoxModel;
private final MainWindowMenuHandler mainWindowMenuHandler;
private final BaseMainWindowMenuHandler mainWindowMenuHandler;

private URI selectedProject = null;

private static final int MAX_PATH_LENGTH_SHOWN = 25;
private static final float CHECKBOX_HEIGHT_RATIO = 1.8f;

public AccessTools(final MainWindow mainWindow, final MainWindowMenuHandler mainWindowMenuHandler) {
public AccessTools(BaseMainWindowMenuHandler mainWindowMenuHandler) {
this.mainWindowMenuHandler = mainWindowMenuHandler;
initComponents();
}
Expand Down
10 changes: 6 additions & 4 deletions src/org/omegat/gui/main/BaseMainWindowMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
Expand Down Expand Up @@ -111,18 +112,17 @@ public abstract class BaseMainWindowMenu implements ActionListener, MenuListener
private static final Logger LOGGER = Logger.getLogger(BaseMainWindowMenu.class.getName());

/** MainWindow instance. */
protected final MainWindow mainWindow;
protected final JFrame mainWindow;

/** menu bar instance */
protected final JMenuBar mainMenu = new JMenuBar();

/** MainWindow menu handler instance. */
protected final MainWindowMenuHandler mainWindowMenuHandler;
protected final BaseMainWindowMenuHandler mainWindowMenuHandler;

private final Map<MenuExtender.MenuKey, JMenu> menus = new EnumMap<>(MenuExtender.MenuKey.class);

public BaseMainWindowMenu(final MainWindow mainWindow,
final MainWindowMenuHandler mainWindowMenuHandler) {
public BaseMainWindowMenu(JFrame mainWindow, BaseMainWindowMenuHandler mainWindowMenuHandler) {
this.mainWindow = mainWindow;
this.mainWindowMenuHandler = mainWindowMenuHandler;
}
Expand Down Expand Up @@ -738,6 +738,7 @@ protected JMenu createMenu(String titleKey) {

protected JMenu createMenu(String titleKey, MenuExtender.MenuKey menuKey) {
JMenu result = new JMenu();
result.setName(titleKey);
Mnemonics.setLocalizedText(result, OStrings.getString(titleKey));
result.addMenuListener(this);
if (menuKey != null) {
Expand All @@ -755,6 +756,7 @@ protected JMenu createMenu(String titleKey, MenuExtender.MenuKey menuKey) {
*/
protected JMenuItem createMenuItem(String titleKey) {
JMenuItem result = new JMenuItem();
result.setName(titleKey);
Mnemonics.setLocalizedText(result, OStrings.getString(titleKey));
result.addActionListener(this);
return result;
Expand Down
77 changes: 77 additions & 0 deletions src/org/omegat/gui/main/BaseMainWindowMenuHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2000-2006 Keith Godfrey, Maxym Mykhalchuk, Henry Pijffers,
Benjamin Siband, and Kim Bruning
2007 Zoltan Bartko
2008 Andrzej Sawula, Alex Buloichik
2009 Didier Briel, Alex Buloichik
2010 Wildrich Fourie, Didier Briel
2012 Wildrich Fourie, Guido Leenders, Didier Briel
2013 Zoltan Bartko, Didier Briel, Yu Tang
2014 Aaron Madlon-Kay
2015 Yu Tang, Aaron Madlon-Kay, Didier Briel
2017 Didier Briel
2019 Thomas Cordonnier
Home page: https://www.omegat.org/
Support center: https://omegat.org/support
This file is part of OmegaT.
OmegaT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmegaT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**************************************************************************/
package org.omegat.gui.main;

/**
* @author Hiroshi Miura
*/
public abstract class BaseMainWindowMenuHandler {

/**
* Common base class for menu handler.
* <p>
* There should be a mandatory methods for mandatory
* commands.
*/
public BaseMainWindowMenuHandler() {
}

public void projectExitMenuItemActionPerformed() {
System.exit(0);
}

public void editFindInProjectMenuItemActionPerformed() {
}

public void optionsPreferencesMenuItemActionPerformed() {
}

public void projectNewMenuItemActionPerformed() {
}

public void projectOpenMenuItemActionPerformed() {
}

public void projectTeamNewMenuItemActionPerformed() {
}

public void findInProjectReuseLastWindow() {
}

public void helpAboutMenuItemActionPerformed() {
}

}
2 changes: 1 addition & 1 deletion src/org/omegat/gui/main/MainWindowBurgerMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ void createMenuBar() {
burgerMenu.add(helpMenu);
burgerMenu.add(burgerMenu);
mainMenu.add(burgerMenu);
mainMenu.add(new AccessTools(mainWindow, mainWindowMenuHandler));
mainMenu.add(new AccessTools(mainWindowMenuHandler));
}
}
4 changes: 2 additions & 2 deletions src/org/omegat/gui/main/MainWindowMenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
* @author Yu Tang
* @author Aaron Madlon-Kay
*/
public final class MainWindowMenuHandler {
public final class MainWindowMenuHandler extends BaseMainWindowMenuHandler {
private static final String PROP_ORIGIN = ProjectTMX.PROP_ORIGIN;

private final MainWindow mainWindow;
Expand Down Expand Up @@ -574,7 +574,7 @@ public void editFindInProjectMenuItemActionPerformed() {
search.makeVisible(getTrimmedSelectedTextInMainWindow());
}

void findInProjectReuseLastWindow() {
public void findInProjectReuseLastWindow() {
if (!Core.getProject().isProjectLoaded()) {
return;
}
Expand Down
71 changes: 71 additions & 0 deletions test/gui/org/omegat/gui/main/BasicMenuItemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2024 Hiroshi Miura
Home page: https://www.omegat.org/
Support center: https://omegat.org/support
This file is part of OmegaT.
OmegaT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmegaT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**************************************************************************/
package org.omegat.gui.main;

import javax.swing.JFrame;

import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
import org.junit.Assert;
import org.junit.Test;

import org.omegat.util.TestPreferencesInitializer;

public class BasicMenuItemTest extends TestCoreGUI {

private static boolean quited = false;

@Test
public void testMainWindowTitle() {
window.requireTitle("OmegaT 6.1.0");
}

@Test
public void testExit() {
window.menuItem("TF_MENU_FILE").click();
window.menuItem("TF_MENU_FILE_QUIT").click();
Assert.assertTrue(quited);
}

@Override
protected void onSetUp() throws Exception {
TestPreferencesInitializer.init();
JFrame frame = GuiActionRunner.execute(() -> new TestMainWindow(new TestMainWindowMenuHandler()));
window = new FrameFixture(robot(), frame);
window.show();
}

static class TestMainWindowMenuHandler extends BaseMainWindowMenuHandler {
public TestMainWindowMenuHandler() {
}

@Override
public void projectExitMenuItemActionPerformed() {
quited = true;
}

}

}
Loading

0 comments on commit 6fafc3b

Please sign in to comment.