diff --git a/test/jdk/jb/java/jcef/MouseEventAfterHideAndShowBrowserTest.java b/test/jdk/jb/java/jcef/MouseEventAfterHideAndShowBrowserTest.java new file mode 100644 index 000000000000..7e6410aff0a6 --- /dev/null +++ b/test/jdk/jb/java/jcef/MouseEventAfterHideAndShowBrowserTest.java @@ -0,0 +1,39 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + +import java.awt.*; +import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +/** + * @test + * @key headful + * @summary Regression test for JBR-2412. The test checks that mouse actions are handled on jcef browser after hide and show it. + * @run main/othervm MouseEventAfterHideAndShowBrowserTest + */ + +public class MouseEventAfterHideAndShowBrowserTest { + + public static void main(String[] args) throws InvocationTargetException, InterruptedException { + + MouseEventScenario scenario = new MouseEventScenario(); + try { + + scenario.initUI(); + scenario.mouseMove(scenario.getBrowserFrame().getFrameCenter()); + + MouseEventScenario.latch = new CountDownLatch(1); + scenario.getBrowserFrame().hideAndShowBrowser(); + MouseEventScenario.latch.await(2, TimeUnit.SECONDS); + + //mouseEntered and mouseExited events work unstable. These actions are not tested. + scenario.doMouseActions(); + + System.out.println("Test PASSED"); + } catch (AWTException e) { + e.printStackTrace(); + } finally { + scenario.disposeBrowserFrame(); + } + } +} \ No newline at end of file diff --git a/test/jdk/jb/java/jcef/MouseEventScenario.java b/test/jdk/jb/java/jcef/MouseEventScenario.java new file mode 100644 index 000000000000..a12b599014de --- /dev/null +++ b/test/jdk/jb/java/jcef/MouseEventScenario.java @@ -0,0 +1,243 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + + +public class MouseEventScenario { + public static CountDownLatch latch; + + static TestStage testStage; + + private static final int WIDTH = 400; + private static final int HEIGHT = 400; + private Robot robot; + private CefBrowserFrame browserFrame = new CefBrowserFrame(WIDTH, HEIGHT); + + public void initUI() throws AWTException, InvocationTargetException, InterruptedException { + robot = new Robot(); + SwingUtilities.invokeAndWait(browserFrame::initUI); + robot.waitForIdle(); + } + + + public void doMouseActions() throws InterruptedException { + Point frameCenter = browserFrame.getFrameCenter(); + + mouseMove(frameCenter); + + //mouseEntered and mouseExited events work unstable. These actions are not tested. + + testStage = TestStage.MOUSE_PRESSED; + System.out.println("Stage: " + testStage.name()); + latch = new CountDownLatch(1); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + latch.await(2, TimeUnit.SECONDS); + checkActionHandler(); + + testStage = TestStage.MOUSE_DRAGGED; + System.out.println("Stage: " + testStage.name()); + latch = new CountDownLatch(1); + robot.mouseMove(frameCenter.x + 1, frameCenter.y); + latch.await(2, TimeUnit.SECONDS); + checkActionHandler(); + + testStage = TestStage.MOUSE_RELEASED; + System.out.println("Stage: " + testStage.name()); + latch = new CountDownLatch(1); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + latch.await(2, TimeUnit.SECONDS); + checkActionHandler(); + + testStage = TestStage.MOUSE_CLICKED; + System.out.println("Stage: " + testStage.name()); + latch = new CountDownLatch(1); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + latch.await(2, TimeUnit.SECONDS); + checkActionHandler(); + + testStage = TestStage.MOUSE_MOVED; + System.out.println("Stage: " + testStage.name()); + latch = new CountDownLatch(1); + robot.mouseMove(frameCenter.x + 2, frameCenter.y); + latch.await(2, TimeUnit.SECONDS); + checkActionHandler(); + + testStage = TestStage.MOUSE_WHEEL_MOVED; + latch = new CountDownLatch(1); + robot.mouseWheel(1); + latch.await(2, TimeUnit.SECONDS); + checkActionHandler(); + + } + + public void mouseMove(Point p) throws InterruptedException { + testStage = TestStage.MOUSE_MOVED; + latch = new CountDownLatch(1); + robot.mouseMove(p.x, p.y); + latch.await(2, TimeUnit.SECONDS); + browserFrame.resetMouseActionPerformedFlag(); + } + + public void disposeBrowserFrame() throws InvocationTargetException, InterruptedException { + getBrowserFrame().getBrowser().dispose(); + JBCefApp.getInstance().getCefApp().dispose(); + SwingUtilities.invokeAndWait(getBrowserFrame()::dispose); + } + + private void checkActionHandler() { + if(!browserFrame.isMouseActionPerformed()) { + throw new RuntimeException("ERROR: " + testStage.name() + " action was not handled."); + } + browserFrame.resetMouseActionPerformedFlag(); + } + + public CefBrowserFrame getBrowserFrame() { + return browserFrame; + } + +} + +enum TestStage { + MOUSE_ENTERED, + MOUSE_EXITED, + MOUSE_MOVED, + MOUSE_DRAGGED, + MOUSE_CLICKED, + MOUSE_PRESSED, + MOUSE_RELEASED, + MOUSE_WHEEL_MOVED +} + +class CefBrowserFrame extends JFrame { + private final JBCefBrowser browser = new JBCefBrowser(); + private final int width, height; + private volatile boolean mouseActionPerformed = false; + + private MouseAdapter mouseAdapter = new MouseAdapter() { + @Override + public void mouseDragged(MouseEvent e) { + if(MouseEventScenario.testStage == TestStage.MOUSE_DRAGGED) { + System.out.println("mouseDragged"); + mouseActionPerformed = true; + MouseEventScenario.latch.countDown(); + } + } + + @Override + public void mouseMoved(MouseEvent e) { + if(MouseEventScenario.testStage == TestStage.MOUSE_MOVED) { + System.out.println("mouseMoved"); + mouseActionPerformed = true; + MouseEventScenario.latch.countDown(); + } + } + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + if(MouseEventScenario.testStage == TestStage.MOUSE_WHEEL_MOVED) { + System.out.println("mouseWheelMoved"); + mouseActionPerformed = true; + MouseEventScenario.latch.countDown(); + } + } + + @Override + public void mouseClicked(MouseEvent e) { + if(MouseEventScenario.testStage == TestStage.MOUSE_CLICKED) { + System.out.println("mouseClicked"); + mouseActionPerformed = true; + MouseEventScenario.latch.countDown(); + } + } + + @Override + public void mousePressed(MouseEvent e) { + if(MouseEventScenario.testStage == TestStage.MOUSE_PRESSED) { + System.out.println("mousePressed"); + mouseActionPerformed = true; + MouseEventScenario.latch.countDown(); + } + } + + @Override + public void mouseReleased(MouseEvent e) { + if(MouseEventScenario.testStage == TestStage.MOUSE_RELEASED) { + System.out.println("mouseReleased"); + mouseActionPerformed = true; + MouseEventScenario.latch.countDown(); + } + } + + @Override + public void mouseEntered(MouseEvent e) { + if(MouseEventScenario.testStage == TestStage.MOUSE_ENTERED) { + System.out.println("mouseEntered"); + mouseActionPerformed = true; + MouseEventScenario.latch.countDown(); + } + } + + @Override + public void mouseExited(MouseEvent e) { + if(MouseEventScenario.testStage == TestStage.MOUSE_EXITED) { + System.out.println("mouseExited"); + mouseActionPerformed = true; + MouseEventScenario.latch.countDown(); + } + } + }; + + public CefBrowserFrame(int width, int height) { + this.width = width; + this.height = height; + } + + public void initUI() { + browser.getComponent().addMouseMotionListener(mouseAdapter); + browser.getComponent().addMouseListener(mouseAdapter); + browser.getComponent().addMouseWheelListener(mouseAdapter); + setResizable(false); + getContentPane().add(browser.getCefBrowser().getUIComponent()); + setSize(width, height); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosed(WindowEvent e) { + browser.dispose(); + } + }); + setVisible(true); + } + + public void hideAndShowBrowser() { + Container parent = browser.getComponent().getParent(); + parent.remove(browser.getComponent()); + SwingUtilities.invokeLater(() -> { + parent.add(browser.getComponent()); + MouseEventScenario.latch.countDown(); + }); + } + + public JBCefBrowser getBrowser() { + return browser; + } + + public void resetMouseActionPerformedFlag() { + mouseActionPerformed = false; + } + + public boolean isMouseActionPerformed() { + return mouseActionPerformed; + } + + public Point getFrameCenter() { + return new Point(getLocationOnScreen().x + getWidth() / 2, + getLocationOnScreen().y + getHeight() / 2); + } +} \ No newline at end of file diff --git a/test/jdk/jb/java/jcef/MouseEventTest.java b/test/jdk/jb/java/jcef/MouseEventTest.java index fdea04f5366a..e2f0a2032d20 100644 --- a/test/jdk/jb/java/jcef/MouseEventTest.java +++ b/test/jdk/jb/java/jcef/MouseEventTest.java @@ -1,158 +1,32 @@ // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -import javax.swing.*; import java.awt.*; -import java.awt.event.*; import java.lang.reflect.InvocationTargetException; /** * @test * @key headful - * @summary Regression test for JBR-2412. The test checks that mouse actions are handled on jcef browser after hide and show it. + * @summary Regression test for JBR-2639. The test checks that mouse actions are handled on jcef browser. * @run main/othervm MouseEventTest */ + public class MouseEventTest { - private static Robot robot; - private static final int WIDTH = 400; - private static final int HEIGHT = 400; - private static CefBrowserFrame browserFrame = new CefBrowserFrame(WIDTH, HEIGHT); public static void main(String[] args) throws InvocationTargetException, InterruptedException { + MouseEventScenario scenario = new MouseEventScenario(); try { - robot = new Robot(); - SwingUtilities.invokeAndWait(browserFrame::initUI); - robot.waitForIdle(); + scenario.initUI(); - doMouseActions(); - if (!browserFrame.isMouseActionPerformed()) { - throw new RuntimeException("Test FAILED. Some of mouse actions were not handled."); + //mouseEntered and mouseExited events work unstable. These actions are not tested. + scenario.doMouseActions(); - } - - browserFrame.resetMouseActionsPerformedFlag(); - browserFrame.hideAndShowBrowser(); - robot.delay(100); - - doMouseActions(); - if (!browserFrame.isMouseActionPerformed()) { - throw new RuntimeException("Test FAILED. Some of mouse actions were not handled."); - } System.out.println("Test PASSED"); } catch (AWTException e) { e.printStackTrace(); } finally { - browserFrame.getBrowser().dispose(); - JBCefApp.getInstance().getCefApp().dispose(); - SwingUtilities.invokeAndWait(browserFrame::dispose); + scenario.disposeBrowserFrame(); } } - - private static void doMouseActions() { - Point frameCenter = new Point(browserFrame.getLocationOnScreen().x + WIDTH /2, - browserFrame.getLocationOnScreen().y + HEIGHT /2); - robot.mouseMove(frameCenter.x, frameCenter.y); - robot.delay(100); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - robot.delay(100); - robot.mouseMove(frameCenter.x + 1, frameCenter.y); - robot.delay(100); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - robot.delay(100); - robot.mouseWheel(1); - robot.delay(100); - - } -} - - -class CefBrowserFrame extends JFrame { - private final JBCefBrowser browser = new JBCefBrowser(); - private final int width, height; - private volatile boolean mouseActionPerformed = false; - - private MouseAdapter mouseAdapter = new MouseAdapter() { - @Override - public void mouseDragged(MouseEvent e) { - mouseActionPerformed = true; - System.out.println("mouseDragged"); - } - - @Override - public void mouseMoved(MouseEvent e) { - mouseActionPerformed = true; - System.out.println("mouseMoved"); - } - - @Override - public void mouseWheelMoved(MouseWheelEvent e) { - mouseActionPerformed = true; - System.out.println("mouseWheelMoved"); - } - - @Override - public void mouseClicked(MouseEvent e) { - mouseActionPerformed = true; - System.out.println("mouseClicked"); - } - - @Override - public void mousePressed(MouseEvent e) { - mouseActionPerformed = true; - System.out.println("mousePressed"); - } - - @Override - public void mouseReleased(MouseEvent e) { - mouseActionPerformed = true; - System.out.println("mouseReleased"); - } - - @Override - public void mouseEntered(MouseEvent e) { - mouseActionPerformed = true; - System.out.println("mouseEntered"); - } - }; - - public CefBrowserFrame(int width, int height) { - this.width = width; - this.height = height; - } - - public void initUI() { - browser.getComponent().addMouseMotionListener(mouseAdapter); - browser.getComponent().addMouseListener(mouseAdapter); - browser.getComponent().addMouseWheelListener(mouseAdapter); - - getContentPane().add(browser.getCefBrowser().getUIComponent()); - setSize(width, height); - setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - addWindowListener(new WindowAdapter() { - @Override - public void windowClosed(WindowEvent e) { - browser.dispose(); - } - }); - setVisible(true); - } - - public void hideAndShowBrowser() { - Container parent = browser.getComponent().getParent(); - parent.remove(browser.getComponent()); - SwingUtilities.invokeLater(() -> parent.add(browser.getComponent())); - } - - public JBCefBrowser getBrowser() { - return browser; - } - - public void resetMouseActionsPerformedFlag() { - mouseActionPerformed = false; - } - - public boolean isMouseActionPerformed() { - return mouseActionPerformed; - } } \ No newline at end of file diff --git a/test/jdk/jb/java/jcef/MouseWheelEventTest.java b/test/jdk/jb/java/jcef/MouseWheelEventTest.java deleted file mode 100644 index 069a0c3fb29d..000000000000 --- a/test/jdk/jb/java/jcef/MouseWheelEventTest.java +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - -import javax.swing.*; -import java.awt.*; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseWheelEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.lang.reflect.InvocationTargetException; - -/** - * @test - * @key headful - * @summary Regression test for JBR-2639. The test checks that mouse wheel action is handled on jcef browser. - * @run main/othervm MouseWheelEventTest - */ - -public class MouseWheelEventTest { - - public static void main(String[] args) throws InvocationTargetException, InterruptedException { - final int width = 400; - final int height = 400; - CefBrowserFrame browserFrame = new CefBrowserFrame(width, height); - - try { - Robot r = new Robot(); - SwingUtilities.invokeAndWait(browserFrame::initUI); - r.waitForIdle(); - - r.mouseMove(browserFrame.getLocationOnScreen().x + width/2, - browserFrame.getLocationOnScreen().y + height/2); - r.delay(100); - r.mouseWheel(1); - r.delay(100); - - if (browserFrame.isMouseWheelMoved()) { - System.out.println("Test PASSED"); - } else { - throw new RuntimeException("Test FAILED. Mouse wheel action was not handled."); - } - } catch (AWTException e) { - e.printStackTrace(); - } finally { - browserFrame.getBrowser().dispose(); - JBCefApp.getInstance().getCefApp().dispose(); - SwingUtilities.invokeAndWait(browserFrame::dispose); - } - } -} - - -class CefBrowserFrame extends JFrame { - - private volatile boolean mouseWheelMoved = false; - private final JBCefBrowser browser = new JBCefBrowser(); - private int width, height; - - public CefBrowserFrame(int width, int height) { - this.width = width; - this.height = height; - } - - public void initUI() { - browser.getComponent().addMouseWheelListener(new MouseAdapter() { - @Override - public void mouseWheelMoved(MouseWheelEvent e) { - mouseWheelMoved = true; - System.out.println("mouseWheelMoved"); - } - }); - getContentPane().add(browser.getCefBrowser().getUIComponent()); - setSize(width, height); - setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - addWindowListener(new WindowAdapter() { - @Override - public void windowClosed(WindowEvent e) { - browser.dispose(); - } - }); - setVisible(true); - } - - public JBCefBrowser getBrowser() { - return browser; - } - - public boolean isMouseWheelMoved() { - return mouseWheelMoved; - } -} \ No newline at end of file