|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Commons Demo |
| 4 | + * %% |
| 5 | + * Copyright (C) 2020 - 2026 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.flowingcode.vaadin.addons.demo.it; |
| 21 | + |
| 22 | +import static org.hamcrest.CoreMatchers.not; |
| 23 | +import com.flowingcode.vaadin.addons.demo.it.TabbedDemoView.TabbedDemoViewConditionalFalse; |
| 24 | +import com.flowingcode.vaadin.addons.demo.it.TabbedDemoView.TabbedDemoViewConditionalTrue; |
| 25 | +import com.flowingcode.vaadin.addons.demo.it.TabbedDemoView.TabbedDemoViewMultiSource; |
| 26 | +import com.flowingcode.vaadin.addons.demo.it.TabbedDemoView.TabbedDemoViewNoSource; |
| 27 | +import com.flowingcode.vaadin.addons.demo.it.TabbedDemoView.TabbedDemoViewSingleSource; |
| 28 | +import com.vaadin.flow.component.Component; |
| 29 | +import com.vaadin.flow.router.Route; |
| 30 | +import org.hamcrest.Description; |
| 31 | +import org.hamcrest.Matcher; |
| 32 | +import org.hamcrest.TypeSafeMatcher; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Test; |
| 35 | +import org.openqa.selenium.By; |
| 36 | +import org.openqa.selenium.SearchContext; |
| 37 | + |
| 38 | +public class TabbedDemoViewIT extends AbstractViewTest { |
| 39 | + |
| 40 | + private SourceCodeViewerElement viewer; |
| 41 | + |
| 42 | + protected void open(Class<? extends Component> clazz) { |
| 43 | + if (viewer != null) { |
| 44 | + throw new IllegalStateException(); |
| 45 | + } |
| 46 | + getDriver().get(getURL(clazz.getAnnotation(Route.class).value())); |
| 47 | + getCommandExecutor().waitForVaadin(); |
| 48 | + getDriver().findElement(By.id("content")); |
| 49 | + } |
| 50 | + |
| 51 | + public static Matcher<SearchContext> hasElement(String cssSelector) { |
| 52 | + return new TypeSafeMatcher<>() { |
| 53 | + |
| 54 | + @Override |
| 55 | + protected boolean matchesSafely(SearchContext container) { |
| 56 | + return !container.findElements(By.cssSelector(cssSelector)).isEmpty(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void describeTo(Description description) { |
| 61 | + description.appendText("a page containing element: ").appendValue(cssSelector); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void describeMismatchSafely(SearchContext container, |
| 66 | + Description mismatchDescription) { |
| 67 | + mismatchDescription.appendText("no elements matched selector: ").appendValue(cssSelector); |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + private Matcher<SearchContext> hasPrimaryCodeTabs() { |
| 73 | + return hasElement("[slot='primary'] vaadin-menu-bar"); |
| 74 | + } |
| 75 | + |
| 76 | + private Matcher<SearchContext> hasPrimaryCodeViewer() { |
| 77 | + return hasElement("[slot='primary'] code-viewer"); |
| 78 | + } |
| 79 | + |
| 80 | + private Matcher<SearchContext> hasSecondaryCodeTabs() { |
| 81 | + return hasElement("[slot='secondary'] vaadin-menu-bar"); |
| 82 | + } |
| 83 | + |
| 84 | + private Matcher<SearchContext> hasSecondaryCodeViewer() { |
| 85 | + return hasElement("[slot='secondary'] code-viewer"); |
| 86 | + } |
| 87 | + |
| 88 | + private void assertThat(Matcher<SearchContext> matcher) { |
| 89 | + Assert.assertThat(getDriver(), matcher); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testSimpleNoSource() { |
| 94 | + open(TabbedDemoViewNoSource.class); |
| 95 | + assertThat(not(hasPrimaryCodeTabs())); |
| 96 | + assertThat(not(hasPrimaryCodeViewer())); |
| 97 | + assertThat(not(hasSecondaryCodeTabs())); |
| 98 | + assertThat(not(hasSecondaryCodeViewer())); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testSimpleSingleSource() { |
| 103 | + open(TabbedDemoViewSingleSource.class); |
| 104 | + assertThat(not(hasPrimaryCodeTabs())); |
| 105 | + assertThat(not(hasPrimaryCodeViewer())); |
| 106 | + assertThat(not(hasSecondaryCodeTabs())); |
| 107 | + assertThat(hasSecondaryCodeViewer()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testSimpleMultiSource() { |
| 112 | + open(TabbedDemoViewMultiSource.class); |
| 113 | + assertThat(not(hasPrimaryCodeTabs())); |
| 114 | + assertThat(not(hasPrimaryCodeViewer())); |
| 115 | + assertThat(hasSecondaryCodeTabs()); |
| 116 | + assertThat(hasSecondaryCodeViewer()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testSimpleConditionalTrue() { |
| 121 | + open(TabbedDemoViewConditionalTrue.class); |
| 122 | + assertThat(not(hasPrimaryCodeTabs())); |
| 123 | + assertThat(not(hasPrimaryCodeViewer())); |
| 124 | + assertThat(not(hasSecondaryCodeTabs())); |
| 125 | + assertThat(hasSecondaryCodeViewer()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testSimpleConditionalFalse() { |
| 130 | + open(TabbedDemoViewConditionalFalse.class); |
| 131 | + assertThat(not(hasPrimaryCodeTabs())); |
| 132 | + assertThat(not(hasPrimaryCodeViewer())); |
| 133 | + assertThat(not(hasSecondaryCodeTabs())); |
| 134 | + assertThat(not(hasSecondaryCodeViewer())); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments