Skip to content

Commit c746185

Browse files
authored
fix: UI serialization with menu bar (vaadin#4159)
1 parent 8506b90 commit c746185

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2000-2022 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.menubar.tests;
17+
18+
import com.vaadin.flow.component.UI;
19+
import com.vaadin.flow.component.html.Div;
20+
import com.vaadin.flow.component.html.NativeButton;
21+
import com.vaadin.flow.component.html.Span;
22+
import com.vaadin.flow.component.menubar.MenuBar;
23+
import com.vaadin.flow.router.Route;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.io.ObjectInputStream;
29+
import java.io.ObjectOutputStream;
30+
31+
@Route("vaadin-menu-bar/serialization")
32+
public class MenuBarSerializationPage extends Div {
33+
34+
private MenuBar menuBar;
35+
private NativeButton serializeAndDeserializeUiButton;
36+
private Span exceptionMessageSpan;
37+
38+
public MenuBarSerializationPage() {
39+
menuBar = new MenuBar();
40+
menuBar.addItem("Menu item");
41+
42+
serializeAndDeserializeUiButton = new NativeButton(
43+
"Serialize&Deserialize UI");
44+
serializeAndDeserializeUiButton
45+
.addClickListener(event -> serializeAndDeserialize());
46+
serializeAndDeserializeUiButton.setId("serialize-and-deserialize-ui");
47+
48+
exceptionMessageSpan = new Span();
49+
exceptionMessageSpan.setId("exception-message-span");
50+
51+
add(serializeAndDeserializeUiButton, menuBar, exceptionMessageSpan);
52+
}
53+
54+
private void serializeAndDeserialize() {
55+
ByteArrayOutputStream bs = new ByteArrayOutputStream();
56+
try (ObjectOutputStream out = new ObjectOutputStream(bs)) {
57+
out.writeObject(UI.getCurrent());
58+
} catch (IOException ex) {
59+
exceptionMessageSpan.setText(ex.getMessage());
60+
}
61+
try (ObjectInputStream in = new ObjectInputStream(
62+
new ByteArrayInputStream(bs.toByteArray()))) {
63+
in.readObject();
64+
} catch (IOException | ClassNotFoundException | ClassCastException ex) {
65+
exceptionMessageSpan.setText(ex.getMessage());
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2022 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.menubar.tests;
17+
18+
import com.vaadin.flow.component.menubar.testbench.MenuBarElement;
19+
import com.vaadin.flow.testutil.TestPath;
20+
import com.vaadin.tests.AbstractComponentIT;
21+
import org.junit.Assert;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
@TestPath("vaadin-menu-bar/serialization")
26+
public class MenuBarSerializationIT extends AbstractComponentIT {
27+
private MenuBarElement menuBar;
28+
29+
@Before
30+
public void init() {
31+
open();
32+
menuBar = $(MenuBarElement.class).waitForFirst();
33+
}
34+
35+
@Test
36+
public void serializeAndDeserializeUi_noExceptionIsThrown() {
37+
$("button").id("serialize-and-deserialize-ui").click();
38+
Assert.assertEquals("",
39+
$("span").id("exception-message-span").getText());
40+
}
41+
}

vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/main/java/com/vaadin/flow/component/menubar/MenuBar.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.vaadin.flow.component.dependency.NpmPackage;
4141
import com.vaadin.flow.dom.Element;
4242
import com.vaadin.flow.function.SerializableConsumer;
43+
import com.vaadin.flow.function.SerializableRunnable;
4344
import com.vaadin.flow.internal.JsonSerializer;
4445

4546
import elemental.json.JsonObject;
@@ -76,7 +77,14 @@ public class MenuBar extends Component
7677
*/
7778
public MenuBar() {
7879
menuItemsArrayGenerator = new MenuItemsArrayGenerator<>(this);
79-
menuManager = new MenuManager<>(this, this::resetContent,
80+
// Not a lambda because of UI serialization purposes
81+
SerializableRunnable resetContent = new SerializableRunnable() {
82+
@Override
83+
public void run() {
84+
resetContent();
85+
}
86+
};
87+
menuManager = new MenuManager<>(this, resetContent,
8088
(menu, contentReset) -> new MenuBarRootItem(this, contentReset),
8189
MenuItem.class, null);
8290
addAttachListener(event -> {

0 commit comments

Comments
 (0)