Skip to content

Commit

Permalink
test: fix BrowserWindowResize test (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollovati authored Jul 19, 2024
1 parent 081855a commit c16e414
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.vaadin.flow.uitest.ui;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.uitest.vertx.ViewTestLayout;

Expand All @@ -35,8 +36,17 @@ protected void onShow() {

windowSize.setId("size-info");

getPage().addBrowserWindowResizeListener(event -> windowSize.setText(String.valueOf(event.getWidth())));
getPage().addBrowserWindowResizeListener(event -> windowSize.setText(
"%sx%s".formatted(event.getWidth(), event.getHeight())));

add(windowSize);

var modalBtn = new NativeButton("Open modal (should keep working");
modalBtn.setId("modal");
modalBtn.addClickListener(e -> {
add(new Div("Now modal, but resize events should still flow in"));
getUI().get().addModal(new Div());
});
add(modalBtn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,53 @@
public class BrowserWindowResizeIT extends ChromeBrowserTest {

@Test
public void listenResizeEvent() {
public void listenResizeEvent() throws InterruptedException {
open();

Dimension currentSize = getDriver().manage().window().getSize();

int newWidth = currentSize.getWidth() - 10;
getDriver().manage().window().setSize(new Dimension(newWidth, currentSize.getHeight()));
int newHeight = currentSize.getHeight() - 10;
getDriver().manage().window()
.setSize(new Dimension(newWidth, newHeight));

// debounced by default with 300
Thread.sleep(500);

WebElement info = findElement(By.id("size-info"));

Assert.assertEquals(String.valueOf(newWidth), info.getText());
Assert.assertEquals(String.valueOf(newWidth),
info.getText().split("x")[0]);
// Selenium sets the window size, the tested API reports viewport
// size...
int actualHeight = Integer.parseInt(info.getText().split("x")[1]);

newWidth -= 30;
getDriver().manage().window().setSize(new Dimension(newWidth, currentSize.getHeight()));
newHeight -= 20;
actualHeight -= 20;
getDriver().manage().window()
.setSize(new Dimension(newWidth, newHeight));

Thread.sleep(500);

Assert.assertEquals(String.valueOf(newWidth),
info.getText().split("x")[0]);
Assert.assertEquals(String.valueOf(actualHeight),
info.getText().split("x")[1]);

// check the same comes in still if modal component set
findElement(By.id("modal")).click();
newWidth += 30;
newHeight += 20;
actualHeight += 20;
getDriver().manage().window()
.setSize(new Dimension(newWidth, newHeight));

Thread.sleep(500);

Assert.assertEquals(String.valueOf(newWidth), info.getText());
Assert.assertEquals(String.valueOf(newWidth),
info.getText().split("x")[0]);
Assert.assertEquals(String.valueOf(actualHeight),
info.getText().split("x")[1]);
}
}

0 comments on commit c16e414

Please sign in to comment.