summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2014-12-30 12:18:24 +0200
committerTeemu Suo-Anttila <teemusa@vaadin.com>2015-04-27 11:49:24 +0300
commitd7888976df86533b01440f302244fb2dc5dfce2e (patch)
tree130c7e1b7e6b6bee488374f9b57d22b2f73f6c73
parent55caa3017ae4a75b12ef6e484f40c02b4dff0400 (diff)
downloadvaadin-framework-d7888976df86533b01440f302244fb2dc5dfce2e.tar.gz
vaadin-framework-d7888976df86533b01440f302244fb2dc5dfce2e.zip
Update favicon when changing themes (#15481)
Change-Id: I3622d211ac5e668f7dce999605118f8f16ffae90
-rw-r--r--client/src/com/vaadin/client/ui/ui/UIConnector.java22
-rw-r--r--uitest/src/com/vaadin/tests/themes/ThemeChangeFavicon.java44
-rw-r--r--uitest/src/com/vaadin/tests/themes/ThemeChangeFaviconTest.java87
3 files changed, 153 insertions, 0 deletions
diff --git a/client/src/com/vaadin/client/ui/ui/UIConnector.java b/client/src/com/vaadin/client/ui/ui/UIConnector.java
index 804ad45f92..e7c252f26a 100644
--- a/client/src/com/vaadin/client/ui/ui/UIConnector.java
+++ b/client/src/com/vaadin/client/ui/ui/UIConnector.java
@@ -887,6 +887,26 @@ public class UIConnector extends AbstractSingleComponentContainerConnector
}
+ private void updateVaadinFavicon(String newTheme) {
+ NodeList<Element> iconElements = querySelectorAll("link[rel~=\"icon\"]");
+ for (int i = 0; i < iconElements.getLength(); i++) {
+ Element iconElement = iconElements.getItem(i);
+
+ String href = iconElement.getAttribute("href");
+ if (href != null && href.contains("VAADIN/themes")
+ && href.endsWith("/favicon.ico")) {
+ href = href.replaceFirst("VAADIN/themes/.+?/favicon.ico",
+ "VAADIN/themes/" + newTheme + "/favicon.ico");
+ iconElement.setAttribute("href", href);
+ }
+ }
+ }
+
+ private static native NodeList<Element> querySelectorAll(String selector)
+ /*-{
+ return $doc.querySelectorAll(selector);
+ }-*/;
+
/**
* Finds a link tag for a style sheet with the given URL
*
@@ -979,6 +999,8 @@ public class UIConnector extends AbstractSingleComponentContainerConnector
getWidget().getParent().addStyleName(newTheme);
VOverlay.getOverlayContainer(getConnection()).addClassName(
activeTheme);
+
+ updateVaadinFavicon(newTheme);
}
forceStateChangeRecursively(UIConnector.this);
diff --git a/uitest/src/com/vaadin/tests/themes/ThemeChangeFavicon.java b/uitest/src/com/vaadin/tests/themes/ThemeChangeFavicon.java
new file mode 100644
index 0000000000..8995583975
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/themes/ThemeChangeFavicon.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.themes;
+
+import java.util.Arrays;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+
+public class ThemeChangeFavicon extends AbstractTestUI {
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ for (final String theme : Arrays.asList("valo", "reindeer")) {
+ addComponent(new Button(theme, new Button.ClickListener() {
+ @Override
+ public void buttonClick(ClickEvent event) {
+ setTheme(theme);
+ }
+ }));
+ }
+ }
+
+ @Override
+ public String getDescription() {
+ return "UI for testing that the favicon changes when changing themes";
+ }
+
+}
diff --git a/uitest/src/com/vaadin/tests/themes/ThemeChangeFaviconTest.java b/uitest/src/com/vaadin/tests/themes/ThemeChangeFaviconTest.java
new file mode 100644
index 0000000000..e4788f93f5
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/themes/ThemeChangeFaviconTest.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.themes;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.remote.DesiredCapabilities;
+import org.openqa.selenium.support.ui.ExpectedCondition;
+
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.parallel.Browser;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class ThemeChangeFaviconTest extends SingleBrowserTest {
+
+ @Override
+ public List<DesiredCapabilities> getBrowsersToTest() {
+ // Seems like stylesheet onload is not fired on PhantomJS
+ // https://github.com/ariya/phantomjs/issues/12332
+ return Collections.singletonList(Browser.FIREFOX
+ .getDesiredCapabilities());
+ }
+
+ @Test
+ public void changeFavicon() throws InterruptedException {
+ setDebug(true);
+ openTestURL();
+ assertFavicon("reindeer");
+
+ changeTheme("valo");
+ assertFavicon("valo");
+
+ changeTheme("reindeer");
+ assertFavicon("reindeer");
+ }
+
+ private void changeTheme(final String theme) {
+ $(ButtonElement.class).caption(theme).first().click();
+
+ final WebElement rootDiv = findElement(By
+ .xpath("//div[contains(@class,'v-app')]"));
+ waitUntil(new ExpectedCondition<Boolean>() {
+
+ @Override
+ public Boolean apply(WebDriver input) {
+ String rootClass = rootDiv.getAttribute("class").trim();
+
+ return rootClass.contains(theme);
+ }
+ }, 30);
+ }
+
+ private void assertFavicon(String theme) {
+ String faviconUrl = "/VAADIN/themes/" + theme + "/favicon.ico";
+
+ List<WebElement> elements = findElements(By
+ .cssSelector("link[rel~=\"icon\"]"));
+
+ Assert.assertEquals(2, elements.size());
+
+ for (WebElement element : elements) {
+ Assert.assertTrue(element.getAttribute("href")
+ + " does not end with " + faviconUrl,
+ element.getAttribute("href").endsWith(faviconUrl));
+ }
+ }
+
+}