From 0b9f00b4f04b73a2631ef4dccb03bb8607d7aca2 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Sat, 4 Oct 2014 11:32:32 +0300 Subject: [PATCH] Check eventRouter value against null in Page class (#14803). Change-Id: Ia7dd54c4cf10a2c56a5ef36864a6bb94f1d9167f --- server/src/com/vaadin/server/Page.java | 13 ++- .../tests/src/com/vaadin/server/PageTest.java | 92 +++++++++++++++++++ 2 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 server/tests/src/com/vaadin/server/PageTest.java diff --git a/server/src/com/vaadin/server/Page.java b/server/src/com/vaadin/server/Page.java index 3acea97c0f..3ddf4862b2 100644 --- a/server/src/com/vaadin/server/Page.java +++ b/server/src/com/vaadin/server/Page.java @@ -485,14 +485,14 @@ public class Page implements Serializable { } private void addListener(Class eventType, Object target, Method method) { - if (eventRouter == null) { + if (!hasEventRouter()) { eventRouter = new EventRouter(); } eventRouter.addListener(eventType, target, method); } private void removeListener(Class eventType, Object target, Method method) { - if (eventRouter != null) { + if (hasEventRouter()) { eventRouter.removeListener(eventType, target, method); } } @@ -599,7 +599,7 @@ public class Page implements Serializable { } private void fireEvent(EventObject event) { - if (eventRouter != null) { + if (hasEventRouter()) { eventRouter.fireEvent(event); } } @@ -776,8 +776,8 @@ public class Page implements Serializable { BrowserWindowResizeListener resizeListener) { removeListener(BrowserWindowResizeEvent.class, resizeListener, BROWSER_RESIZE_METHOD); - getState(true).hasResizeListeners = eventRouter - .hasListeners(BrowserWindowResizeEvent.class); + getState(true).hasResizeListeners = hasEventRouter() + && eventRouter.hasListeners(BrowserWindowResizeEvent.class); } /** @@ -1242,4 +1242,7 @@ public class Page implements Serializable { return state; } + private boolean hasEventRouter() { + return eventRouter != null; + } } diff --git a/server/tests/src/com/vaadin/server/PageTest.java b/server/tests/src/com/vaadin/server/PageTest.java new file mode 100644 index 0000000000..b782b1f67d --- /dev/null +++ b/server/tests/src/com/vaadin/server/PageTest.java @@ -0,0 +1,92 @@ +/* + * 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.server; + +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.server.Page.BrowserWindowResizeEvent; +import com.vaadin.server.Page.BrowserWindowResizeListener; +import com.vaadin.shared.ui.ui.PageState; +import com.vaadin.ui.UI; + +/** + * + * Tests for {@link Page} + * + * @author Vaadin Ltd + */ +public class PageTest { + + @Test + public void removeBrowserWindowResizeListener_listenerIsAttached_listenerRemoved() { + Page page = new Page(EasyMock.createMock(UI.class), + EasyMock.createMock(PageState.class)); + + TestBrowserWindowResizeListener listener = new TestBrowserWindowResizeListener(); + page.addBrowserWindowResizeListener(listener); + page.removeBrowserWindowResizeListener(listener); + + page.updateBrowserWindowSize(0, 0, true); + + Assert.assertFalse("Listener is called after removal", + listener.isCalled()); + } + + @Test + public void removeBrowserWindowResizeListener_listenerIsNotAttached_stateIsUpdated() { + TestPage page = new TestPage(EasyMock.createMock(UI.class), + EasyMock.createMock(PageState.class)); + + BrowserWindowResizeListener listener = EasyMock + .createMock(BrowserWindowResizeListener.class); + page.removeBrowserWindowResizeListener(listener); + + Assert.assertFalse( + "Page state 'hasResizeListeners' property has wrong value", + page.getState(false).hasResizeListeners); + } + + private static class TestPage extends Page { + + public TestPage(UI uI, PageState state) { + super(uI, state); + } + + @Override + protected PageState getState(boolean markAsDirty) { + return super.getState(markAsDirty); + } + + } + + private static class TestBrowserWindowResizeListener implements + BrowserWindowResizeListener { + + @Override + public void browserWindowResized(BrowserWindowResizeEvent event) { + isCalled = true; + } + + public boolean isCalled() { + return isCalled; + } + + private boolean isCalled; + + } +} -- 2.39.5