Skip to content

Commit

Permalink
Only do hookUpPolymerElement for Polymer elements (#4147)
Browse files Browse the repository at this point in the history
* Only do hookUpPolymerElement for Polymer elements
  • Loading branch information
Legioth authored and Denis committed May 22, 2018
1 parent e51488c commit 042a252
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ private native void bindPolymerModelProperties(StateNode node,
var self = this;
try {
$wnd.customElements.whenDefined(element.localName).then( function () {
self.@SimpleElementBindingStrategy::hookUpPolymerElement(*)(node, element);
if ( @com.vaadin.client.PolymerUtils::isPolymerElement(*)(element) ) {
self.@SimpleElementBindingStrategy::hookUpPolymerElement(*)(node, element);
}
});
}
catch (e) {
Expand Down Expand Up @@ -401,6 +403,8 @@ private void bindInitialModelProperties(StateNode stateNode,

private void bindModelProperties(StateNode stateNode, Element htmlNode,
String path) {
assert PolymerUtils.isPolymerElement(htmlNode);

Command command = () -> stateNode
.getMap(NodeFeatures.ELEMENT_PROPERTIES)
.forEachProperty((property, key) -> bindSubProperty(stateNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1685,11 +1685,14 @@ public void testReadyCallback_polymerElement_readyIsCalledAndNotified() {

public void testReadyCallback_deferredPolymerElementAndNoListeners_readyIsCalled() {
element = Browser.getDocument().createElement("x-my");
WidgetUtil.setJsProperty(element, "localName", "x-my");

assertDeferredPolymerElement_originalReadyIsCalled(element);
}

public void testReadyCallback_deferredPolymerElement_readyIsCalledAndNotified() {
element = Browser.getDocument().createElement("x-my");
WidgetUtil.setJsProperty(element, "localName", "x-my");

PolymerUtils.addReadyListener(element,
() -> WidgetUtil.setJsProperty(element, "baz", "foobar"));
Expand All @@ -1701,6 +1704,7 @@ public void testReadyCallback_deferredPolymerElement_readyIsCalledAndNotified()

private void assertDeferredPolymerElement_originalReadyIsCalled(
Element element) {
initPolymer(element);
mockWhenDefined(element);

NativeFunction function = NativeFunction.create("this['foo']='bar';");
Expand Down Expand Up @@ -1792,6 +1796,8 @@ private native String getToString(Object value)

private native void mockWhenDefined(Element element)
/*-{
$wnd.OldPolymer = $wnd.Polymer;
$wnd.Polymer = null;
$wnd.customElements = {
whenDefined: function() {
return {
Expand All @@ -1805,6 +1811,7 @@ private native void mockWhenDefined(Element element)

private native void runWhenDefined(Element element)
/*-{
$wnd.Polymer = $wnd.OldPolymer;
element.callback();
}-*/;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,13 @@ private native void addMockMethods(Element element)
element._propertiesChanged = function() {
element.propertiesChangedCallCount += 1;
};
element.callbackCallCount = 0;
$wnd.customElements = {
whenDefined: function() {
return {
then: function (callback) {
$wnd.Polymer = $wnd.OldPolymer;
element.callbackCallCount += 1;
callback();
}
Expand All @@ -443,6 +444,7 @@ private native void addMockMethods(Element element)

private native void emulatePolymerNotLoaded()
/*-{
$wnd.OldPolymer = $wnd.Polymer;
$wnd.Polymer = null;
}-*/;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2000-2018 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.flow.uitest.ui;

import com.vaadin.flow.component.dependency.JavaScript;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.uitest.servlet.ViewTestLayout;

@Route(value = "com.vaadin.flow.uitest.ui.CustomCustomElementView", layout = ViewTestLayout.class)
@JavaScript("frontend://com/vaadin/flow/uitest/ui/CustomCustomElement.js")
public class CustomCustomElementView extends AbstractDivView {

public CustomCustomElementView() {
Element element = new Element("custom-custom-element");
element.getStyle().set("display", "block");

element.setProperty("property", "initial");

getElement().appendChild(element);

add(new NativeButton("update property",
event -> element.setProperty("property", "updated")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// A custom element that isn't using Polymer
class CustomCustomElement extends HTMLElement {
static get is() { return 'custom-custom-element' }

constructor() {
super();
this.attachShadow({mode: "open"});
this.property = "constructor";
}

set property(value) {
this.shadowRoot.textContent = value;
}

get property() {
return this.shadowRoot.textContent;
}
}
customElements.define(CustomCustomElement.is, CustomCustomElement);
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2000-2017 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.flow.uitest.ui;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;

import com.vaadin.flow.testutil.ChromeBrowserTest;
import com.vaadin.testbench.TestBenchElement;

public class CustomCustomElementIT extends ChromeBrowserTest {

@Before
public void setUp() {
open();
}

@Test
public void clickOnButton_removeFromLayout() {
TestBenchElement customElement = $("custom-custom-element").first();

Assert.assertEquals("initial",
customElement.getPropertyString("shadowRoot", "textContent"));

findElement(By.tagName("button")).click();

Assert.assertEquals("updated",
customElement.getPropertyString("shadowRoot", "textContent"));
}

}

0 comments on commit 042a252

Please sign in to comment.