-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add theme variants demo and test helpers. #4412
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,23 @@ | |
*/ | ||
package com.vaadin.flow.demo; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import com.vaadin.flow.testutil.ChromeBrowserTest; | ||
|
||
import static com.vaadin.flow.demo.DemoView.COMPONENT_WITH_VARIANTS_ID; | ||
import static com.vaadin.flow.demo.DemoView.VARIANT_TOGGLE_BUTTONS_DIV_ID; | ||
|
||
/** | ||
* Base class for the integration tests of component demos. | ||
* | ||
|
@@ -43,4 +54,85 @@ public void openDemoPageAndCheckForErrors() { | |
layout = findElement(By.className("demo-view")); | ||
checkLogsForErrors(); | ||
} | ||
|
||
/** | ||
* Verifies variants functionality for the current layout. | ||
* | ||
* The test will fail if a specific variant demo is not added first with | ||
* {@link DemoView#addVariantsDemo(Supplier, BiConsumer, BiConsumer, Function, Enum[])} | ||
* method. | ||
*/ | ||
protected void verifyThemeVariantsBeingToggled() { | ||
List<WebElement> toggleThemeButtons = layout | ||
.findElement(By.id(VARIANT_TOGGLE_BUTTONS_DIV_ID)) | ||
.findElements(By.tagName("button")); | ||
Assert.assertFalse( | ||
"Expected at least one toggle theme button in 'buttonDiv', but got none", | ||
toggleThemeButtons.isEmpty()); | ||
toggleThemeButtons.forEach(button -> toggleVariantAndCheck( | ||
layout.findElement(By.id(COMPONENT_WITH_VARIANTS_ID)), button)); | ||
} | ||
|
||
private void toggleVariantAndCheck(WebElement component, | ||
WebElement button) { | ||
List<String> initialButtonThemes = getComponentThemes(component); | ||
String initialButtonText = button.getText(); | ||
|
||
button.click(); | ||
verifyThemeIsToggled(getComponentThemes(component), button.getText(), | ||
initialButtonThemes, initialButtonText); | ||
|
||
button.click(); | ||
Assert.assertEquals( | ||
"After two toggle variants button clicks, button text should be the same as before testing", | ||
button.getText(), initialButtonText); | ||
|
||
List<String> currentThemes = getComponentThemes(component); | ||
String assertionMessage = "After two toggle variants button clicks, component 'theme' attribute should contain the same value as before testing"; | ||
Assert.assertEquals(assertionMessage, currentThemes.size(), | ||
initialButtonThemes.size()); | ||
currentThemes.forEach(currentTheme -> Assert.assertTrue( | ||
assertionMessage + String.format( | ||
" but theme variant '%s' is missing", currentTheme), | ||
initialButtonThemes.contains(currentTheme))); | ||
|
||
} | ||
|
||
private void verifyThemeIsToggled(List<String> updatedThemes, | ||
String updatedButtonText, List<String> previousThemes, | ||
String previousButtonText) { | ||
Assert.assertNotEquals("Button should change its text after toggling", | ||
previousButtonText, updatedButtonText); | ||
|
||
boolean shouldAddTheme = previousButtonText.startsWith("Add"); | ||
if (shouldAddTheme) { | ||
Assert.assertTrue( | ||
"When a theme variant got added, toggle button text should start with 'Remove' word", | ||
updatedButtonText.startsWith("Remove")); | ||
Assert.assertEquals( | ||
"When a theme variant got added, component 'theme' attribute should contain one more variant that before", | ||
previousThemes.size() + 1, updatedThemes.size()); | ||
Assert.assertTrue( | ||
"When a theme variant got added, component 'theme' attribute should contain all previous theme variants", | ||
updatedThemes.containsAll(previousThemes)); | ||
} else { | ||
Assert.assertTrue( | ||
"When a theme variant got removed, toggle button text should start with 'Add' word", | ||
updatedButtonText.startsWith("Add")); | ||
Assert.assertEquals( | ||
"When a theme variant got removed, component 'theme' attribute should contain one less variant than before", | ||
previousThemes.size() - 1, updatedThemes.size()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Assert.assertTrue( | ||
"When a theme variant got removed, previous theme variants should contain all theme variants from component 'theme' attribute", | ||
previousThemes.containsAll(updatedThemes)); | ||
} | ||
} | ||
|
||
private List<String> getComponentThemes(WebElement component) { | ||
String themeAttributeValue = component.getAttribute("theme"); | ||
if (themeAttributeValue == null || themeAttributeValue.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
return Arrays.asList(themeAttributeValue.split(" ")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cast one of the operands of this addition operation to a "long".