package org.intellij.xquery.runner.ui.datasources;

import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.fixture.FrameFixture;
import org.intellij.xquery.runner.rt.XQueryDataSourceType;
import org.intellij.xquery.runner.ui.PanelTestingFrame;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/**
 * User: ligasgr
 * Date: 24/10/13
 * Time: 13:42
 */
public class ConfigurationFilePanelTest {
    private FrameFixture window;
    private ConfigurationFilePanel configurationFilePanel;

    @Before
    public void setUp() throws Exception {
        PanelTestingFrame frame = GuiActionRunner.execute(new GuiQuery<PanelTestingFrame>() {
            protected PanelTestingFrame executeInEDT() {
                configurationFilePanel = new ConfigurationFilePanel();
                return new PanelTestingFrame(configurationFilePanel.getMainPanel());
            }
        });
        window = new FrameFixture(frame);
        window.show();
    }

    @Test
    public void shouldShowPanelWhenNeeded() throws Exception {
        configurationFilePanel.init(XQueryDataSourceType.SAXON, false, null);

        assertThat(configurationFilePanel.getMainPanel().isVisible(), is(true));
    }

    @Test
    public void shouldHidePanelWhenNeeded() throws Exception {
        configurationFilePanel.init(XQueryDataSourceType.MARKLOGIC, false, null);

        assertThat(configurationFilePanel.getMainPanel().isVisible(), is(false));
    }

    @Test
    public void shouldDisableConfigFileField() throws Exception {
        configurationFilePanel.init(XQueryDataSourceType.SAXON, false, null);

        window.checkBox("configurationEnabled").requireNotSelected();
        window.textBox("configFile").requireDisabled();

    }

    @Test
    public void shouldEnableConfigFileField() throws Exception {
        configurationFilePanel.init(XQueryDataSourceType.SAXON, true, null);

        window.checkBox("configurationEnabled").requireSelected();
        window.textBox("configFile").requireEnabled();
    }

    @Test
    public void shouldChangeValueOfConfigurationEnabledToTrue() throws Exception {
        configurationFilePanel.init(XQueryDataSourceType.SAXON, false, null);

        window.checkBox("configurationEnabled").check();
        window.checkBox("configurationEnabled").requireSelected();
        window.textBox("configFile").requireEnabled();
        assertThat(configurationFilePanel.isConfigurationEnabled(), is(true));
    }

    @Test
    public void shouldChangeValueOfConfigurationEnabledToFalse() throws Exception {
        configurationFilePanel.init(XQueryDataSourceType.SAXON, true, null);

        window.checkBox("configurationEnabled").uncheck();
        window.checkBox("configurationEnabled").requireNotSelected();
        window.textBox("configFile").requireDisabled();
        assertThat(configurationFilePanel.isConfigurationEnabled(), is(false));
    }

    @Test
    public void shouldChangeValueOfConfigFileWhenTextEntered() throws Exception {
        configurationFilePanel.init(XQueryDataSourceType.SAXON, true, null);

        window.textBox("configFile").enterText("/my/file");
        assertThat(configurationFilePanel.getConfigFile(), is("/my/file"));
    }

    @After
    public void tearDown() throws Exception {
        window.cleanUp();
    }
}

@@ -1,8 +1,675 @@
+/*%0A * Copyright 2013 Grzegorz Ligas %3Cligasgr@gmail.com%3E and other contributors (see the CONTRIBUTORS file).%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -3047,57 +3047,8 @@
 ck()
-;%0A        window.checkBox(%22configurationEnabled%22)
 .req
@@ -3055,32 +3055,33 @@
 uireSelected();%0A
+%0A
         window.t
@@ -3438,57 +3438,8 @@
 ck()
-;%0A        window.checkBox(%22configurationEnabled%22)
 .req
@@ -3449,32 +3449,33 @@
 eNotSelected();%0A
+%0A
         window.t
@@ -3763,32 +3763,32 @@
 , true, null);%0A%0A
-
         window.t
@@ -3831,16 +3831,17 @@
 file%22);%0A
+%0A
         

Fix broken build on jenkins.