package seedu.address.ui;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static seedu.address.ui.HelpWindow.USERGUIDE_FILE_PATH;

import java.net.URL;

import org.junit.Before;
import org.junit.Test;
import org.testfx.api.FxToolkit;

import guitests.guihandles.HelpWindowHandle;
import javafx.stage.Stage;

public class HelpWindowTest extends GuiUnitTest {

    private HelpWindow helpWindow;
    private HelpWindowHandle helpWindowHandle;

    @Before
    public void setUp() throws Exception {
        guiRobot.interact(() -> helpWindow = new HelpWindow());
        Stage helpWindowStage = FxToolkit.setupStage((stage) -> stage.setScene(helpWindow.getRoot().getScene()));
        FxToolkit.showStage();
        helpWindowHandle = new HelpWindowHandle(helpWindowStage);
    }

    @Test
    public void display() {
        URL expectedHelpPage = HelpWindow.class.getResource(USERGUIDE_FILE_PATH);
        assertEquals(expectedHelpPage, helpWindowHandle.getLoadedUrl());
    }

    @Test
    public void isShowing_helpWindowIsShowing_returnsTrue() {
        guiRobot.interact(helpWindow::show);
        assertTrue(helpWindow.isShowing());
    }

    @Test
    public void isShowing_helpWindowIsHiding_returnsFalse() {
        guiRobot.interact(helpWindow.getRoot()::hide);
        assertFalse(helpWindow.isShowing());
    }

    @Test
    public void focus_helpWindowNotFocused_focused() throws Exception {
        assumeFalse("Test skipped in headless mode: Window focus behavior is buggy.", guiRobot.isHeadlessMode());
        guiRobot.interact(helpWindow::show);

        // Focus on another stage to remove focus from the helpWindow
        FxToolkit.setupStage(Stage::requestFocus);
        assertFalse(helpWindow.getRoot().isFocused());

        guiRobot.interact(helpWindow::focus);
        assertTrue(helpWindow.getRoot().isFocused());
    }
}

@@ -702,77 +702,30 @@
     
-Stage helpWindowStage = FxToolkit.setupStage((stage) -%3E stage.setScen
+FxToolkit.registerStag
 e(he
@@ -736,61 +736,17 @@
 ndow
-.
+::
 getRoot
-().getScene()));%0A        FxToolkit.showStage(
 );%0A 
@@ -802,21 +802,26 @@
 lpWindow
-Stage
+.getRoot()
 );%0A    %7D
@@ -858,17 +858,65 @@
 splay() 
-%7B
+throws Exception %7B%0A        FxToolkit.showStage();
 %0A       
@@ -1314,63 +1314,8 @@
 ) %7B%0A
-        guiRobot.interact(helpWindow.getRoot()::hide);%0A
     
@@ -1436,32 +1436,567 @@
 ows Exception %7B%0A
+        // TODO: This test skip can be removed once this bug is fixed:%0A        // https://github.com/javafxports/openjdk-jfx/issues/50%0A        //%0A        // When there are two stages (stage1 and stage2) shown,%0A        // stage1 is in focus and stage2.requestFocus() is called,%0A        // we expect that stage1.isFocused() will return false while%0A        // stage2.isFocused() returns true. However, as reported in the bug report,%0A        // both stage1.isFocused() and stage2.isFocused() returns true,%0A        // which fails the test.%0A
         assumeFa

Fix stalling HelpWindowTest