Browse Source

Update favicon when changing themes (#15481)

Change-Id: I3622d211ac5e668f7dce999605118f8f16ffae90
tags/7.5.0.beta1
Leif Åstrand 9 years ago
parent
commit
6ce22d8ad5

+ 22
- 0
client/src/com/vaadin/client/ui/ui/UIConnector.java View File

@@ -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);

+ 44
- 0
uitest/src/com/vaadin/tests/themes/ThemeChangeFavicon.java View File

@@ -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";
}

}

+ 87
- 0
uitest/src/com/vaadin/tests/themes/ThemeChangeFaviconTest.java View File

@@ -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));
}
}

}

Loading…
Cancel
Save