]> source.dussan.org Git - vaadin-framework.git/commitdiff
Check eventRouter value against null in Page class (#14803).
authorDenis Anisimov <denis@vaadin.com>
Sat, 4 Oct 2014 08:32:32 +0000 (11:32 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 7 Oct 2014 13:01:15 +0000 (13:01 +0000)
Change-Id: Ia7dd54c4cf10a2c56a5ef36864a6bb94f1d9167f

server/src/com/vaadin/server/Page.java
server/tests/src/com/vaadin/server/PageTest.java [new file with mode: 0644]

index 3acea97c0f3eb4cf7b95c12a4af2647facdc7957..3ddf4862b27449dbf66b93dfcd6d64095f9c705e 100644 (file)
@@ -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 (file)
index 0000000..b782b1f
--- /dev/null
@@ -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;
+
+    }
+}