Skip to content

Commit

Permalink
Implement open and setLocation method to Page. (vaadin#4869)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahalsaad committed Mar 4, 2019
1 parent 7913878 commit ef82107
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 1 deletion.
102 changes: 102 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/component/page/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.Component;
Expand Down Expand Up @@ -389,6 +393,104 @@ public Registration addBrowserWindowResizeListener(
return resizeReceiver.addListener(resizeListener);
}

/**
* Opens the given url in a window with the given name.
* <p>
* The supplied {@code windowName} is used as the target name in a
* window.open call in the client. This means that special values such as
* "_blank", "_self", "_top", "_parent" have special meaning. An empty or
* <code>null</code> window name is also a special case.
* </p>
* <p>
* "", null and "_self" as {@code windowName} all causes the URL to be
* opened in the current window, replacing any old contents. For
* downloadable content you should avoid "_self" as "_self" causes the
* client to skip rendering of any other changes as it considers them
* irrelevant (the page will be replaced by the response from the URL). This
* can speed up the opening of a URL, but it might also put the client side
* into an inconsistent state if the window content is not completely
* replaced e.g., if the URL is downloaded instead of displayed in the
* browser.
* </p>
* <p>
* "_blank" as {@code windowName} causes the URL to always be opened in a
* new window or tab (depends on the browser and browser settings).
* </p>
* <p>
* "_top" and "_parent" as {@code windowName} works as specified by the HTML
* standard.
* </p>
* <p>
* Any other {@code windowName} will open the URL in a window with that
* name, either by opening a new window/tab in the browser or by replacing
* the contents of an existing window with that name.
* </p>
*
* @param url
* the URL to open.
* @param windowName
* the name of the window.
*/
public void open(String url, String windowName) {
open(url, windowName, -1, -1);
}

/**
* Opens the given URL in a window with the given size, border and name. For
* more information on the meaning of {@code windowName}, see
* {@link #open(String, String)}.
*
* @param url
* the URL to open.
* @param windowName
* the name of the window.
* @param width
* the width of the window in pixels
* @param height
* the height of the window in pixels
*/
public void open(String url, String windowName, int width, int height) {
Map<String, Object> openOption = new HashMap<>();

if (width >= 0) {
openOption.put("width", width);
}

if (height >= 0) {
openOption.put("height", height);
}

String windowFeatures = openOption
.entrySet()
.stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining(","));

executeJavaScript("window.open($0, $1, $2)", url, windowName, windowFeatures);
}

/**
* Navigates this page to the given URI. The contents of this page in the
* browser is replaced with whatever is returned for the given URI.
*
* @param uri
* the URI to show
*/
public void setLocation(String uri) {
open(uri, "_self");
}

/**
* Navigates this page to the given URI. The contents of this page in the
* browser is replaced with whatever is returned for the given URI.
*
* @param uri
* the URI to show
*/
public void setLocation(URI uri) {
setLocation(uri.toString());
}

private static class LazyJsLoader implements Serializable {

private static final String JS_FILE_NAME = "windowResizeListener.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,27 @@ public void beforeEnter(BeforeEnterEvent event) {
getPage().reload();
});

add(input, updateButton, overrideButton, reloadButton);
String url = "https://www.google.com";

Div setLocationButton = new Div();
setLocationButton.setId("setLocation");
setLocationButton.setText("Set page location");
setLocationButton.addClickListener(e -> getPage().setLocation(url));

Div openButton = new Div();
openButton.setId("open");
openButton.setText("Open url");
openButton.addClickListener(e -> getPage().open(url, "secondwindow"));

Div openWithSizeButton = new Div();
openWithSizeButton.setId("openWithSize");
openWithSizeButton.setText("Open url in a window with height and width");
openWithSizeButton.addClickListener(e -> {
getPage().open(url, "sizewindow", 200, 200);
});

add(input, updateButton, overrideButton, reloadButton,
setLocationButton, openButton, openWithSizeButton);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.vaadin.flow.uitest.ui;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;

import com.vaadin.flow.component.html.testbench.InputTextElement;
import com.vaadin.flow.testutil.ChromeBrowserTest;
import org.openqa.selenium.WebDriver;

public class PageIT extends ChromeBrowserTest {

Expand Down Expand Up @@ -66,4 +68,37 @@ public void testReload() {
input = $(InputTextElement.class).id("input");
Assert.assertEquals("", input.getValue());
}

@Test
public void testSetLocation() {
open();

findElement(By.id("setLocation")).click();
Assert.assertThat(getDriver().getCurrentUrl(),
Matchers.startsWith("https://www.google.com"));
}

@Test
public void testOpenUrl() {
open();

findElement(By.id("open")).click();
Assert.assertThat(
getDriver().switchTo().window("secondwindow").getCurrentUrl(),
Matchers.startsWith("https://www.google.com")
);
}

@Test
public void testOpenUrlWithSizedWindow() {
open();

findElement(By.id("openWithSize")).click();
WebDriver newWindow = getDriver().switchTo().window("sizewindow");
Assert.assertThat(newWindow.getCurrentUrl(),
Matchers.startsWith("https://www.google.com"));

Assert.assertEquals(newWindow.manage().window().getSize().getWidth(), 200);
Assert.assertEquals(newWindow.manage().window().getSize().getHeight(), 200);
}
}

0 comments on commit ef82107

Please sign in to comment.