package com.vaadin.tests.components.window;

import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.TestForWindowOpen;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.JavaScript;

public class PageOpenTest extends AbstractTestUI {

    final String url = "http://www.google.com";

    @Override
    protected void setup(VaadinRequest request) {
        final Page page = getPage();

        addComponent(new TestForWindowOpen());

        addComponent(new Button("Page.open _blank always as popup",
                new Button.ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        page.open(url, "_blank", true);
                    }
                }));

        addComponent(new Button("Page.open _blank NOT always as popup",
                new Button.ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        page.open(url, "_blank", false);
                    }
                }));

        addComponent(new Button("Page.open _new always as popup",
                new Button.ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        page.open(url, "_new", true);
                    }
                }));

        addComponent(new Button("Page.open _new NOT always as popup",
                new Button.ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        page.open(url, "_new", false);
                    }
                }));
        addComponent(new Button(
                "Execute Javascript window.open(www.google.com, _blank)",
                new Button.ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        JavaScript
                                .getCurrent()
                                .execute(
                                        "window.open(\"http://www.google.com\", \"_blank\");");
                    }
                }));
        addComponent(new Button(
                "Execute Javascript window.open(www.google.com, _blank, resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes)",
                new Button.ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        JavaScript
                                .getCurrent()
                                .execute(
                                        "window.open(\"http://www.google.com\", \"_blank\", \"resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes\");");
                    }
                }));

    }

    @Override
    protected String getTestDescription() {
        return "Windows never opened to a new tab";
    }

    @Override
    protected Integer getTicketNumber() {
        return 7842;
    }

}
