summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDenis Anisimov <denis@vaadin.com>2014-08-31 16:46:28 +0300
committerVaadin Code Review <review@vaadin.com>2014-10-07 12:44:18 +0000
commit3eede8ca8ac1db34400288f534a1088d301a7cee (patch)
treee94289c1e84f8e47fb716ed4198d895bdf8c1595 /server
parent0cd9540483262d0cded56fcc8c286c64a0b75631 (diff)
downloadvaadin-framework-3eede8ca8ac1db34400288f534a1088d301a7cee.tar.gz
vaadin-framework-3eede8ca8ac1db34400288f534a1088d301a7cee.zip
Provide API to change resource/url for BrowserWindowOpener (#12733).
Change-Id: I48d3bc36ea5283225524a75bcbc941534e4a419a
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/server/BrowserWindowOpener.java49
-rw-r--r--server/tests/src/com/vaadin/server/BrowserWindowOpenerTest.java79
2 files changed, 128 insertions, 0 deletions
diff --git a/server/src/com/vaadin/server/BrowserWindowOpener.java b/server/src/com/vaadin/server/BrowserWindowOpener.java
index 44679fbfbb..8cc1faa728 100644
--- a/server/src/com/vaadin/server/BrowserWindowOpener.java
+++ b/server/src/com/vaadin/server/BrowserWindowOpener.java
@@ -126,6 +126,55 @@ public class BrowserWindowOpener extends AbstractExtension {
}
/**
+ * Sets the provided URL {@code url} for this instance. The {@code url} will
+ * be opened in a new browser window/tab when the extended component is
+ * clicked.
+ *
+ * @param url
+ * URL to open
+ */
+ public void setUrl(String url) {
+ setResource(new ExternalResource(url));
+ }
+
+ /**
+ * Sets the provided {@code resource} for this instance. The
+ * {@code resource} will be opened in a new browser window/tab when the
+ * extended component is clicked.
+ *
+ * @param resource
+ * resource to open
+ */
+ public void setResource(Resource resource) {
+ setResource(BrowserWindowOpenerState.locationResource, resource);
+ }
+
+ /**
+ * Returns the resource for this instance.
+ *
+ * @return resource to open browser window
+ */
+ public Resource getResource() {
+ return getResource(BrowserWindowOpenerState.locationResource);
+ }
+
+ /**
+ * Returns the URL for this BrowserWindowOpener instance. Returns
+ * {@code null} if this instance is not URL resource based (a non URL based
+ * resource has been set for it).
+ *
+ * @return URL to open in the new browser window/tab when the extended
+ * component is clicked
+ */
+ public String getUrl() {
+ Resource resource = getResource();
+ if (resource instanceof ExternalResource) {
+ return ((ExternalResource) resource).getURL();
+ }
+ return null;
+ }
+
+ /**
* Sets the target window name that will be used. If a window has already
* been opened with the same name, the contents of that window will be
* replaced instead of opening a new window. If the name is
diff --git a/server/tests/src/com/vaadin/server/BrowserWindowOpenerTest.java b/server/tests/src/com/vaadin/server/BrowserWindowOpenerTest.java
new file mode 100644
index 0000000000..7c76f7d421
--- /dev/null
+++ b/server/tests/src/com/vaadin/server/BrowserWindowOpenerTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+
+import org.easymock.EasyMock;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.shared.communication.URLReference;
+import com.vaadin.shared.ui.BrowserWindowOpenerState;
+
+/**
+ *
+ * @author Vaadin Ltd
+ */
+public class BrowserWindowOpenerTest {
+
+ @Test
+ public void setResource_urlBasedOpener_resourceIsSetAndUrlIsNull() {
+ BrowserWindowOpener opener = new BrowserWindowOpener("url");
+
+ StreamResource resource = EasyMock.createMock(StreamResource.class);
+ opener.setResource(resource);
+
+ assertEquals("Unexpected resource is got on getResource() method",
+ resource, opener.getResource());
+ Assert.assertNull("Unexpected resource is got on getUrl() method",
+ opener.getUrl());
+
+ URLReference ref = opener.getState(false).resources
+ .get(BrowserWindowOpenerState.locationResource);
+ Assert.assertTrue(
+ "Url reference in the state is not ResourceReference",
+ ref instanceof ResourceReference);
+ Assert.assertEquals("Unexpected resource saved in state", resource,
+ ((ResourceReference) ref).getResource());
+ }
+
+ @Test
+ public void setUrl_urlBasedOpener_urlIsSet() {
+ BrowserWindowOpener opener = new BrowserWindowOpener("url");
+
+ String url = "newUrl";
+ opener.setUrl(url);
+
+ assertEquals("Unexpected URL is got on getURL() method", url,
+ opener.getUrl());
+ Assert.assertNotNull(
+ "Unexpected resource is got on getResource() method",
+ opener.getResource());
+
+ URLReference ref = opener.getState(false).resources
+ .get(BrowserWindowOpenerState.locationResource);
+ Assert.assertTrue(
+ "Url reference in the state is not ResourceReference",
+ ref instanceof ResourceReference);
+ Resource resource = ((ResourceReference) ref).getResource();
+ Assert.assertTrue("Resource reference is not ExternalResource",
+ resource instanceof ExternalResource);
+ Assert.assertEquals("Unexpected URL in resource saved in state", url,
+ ((ExternalResource) resource).getURL());
+ }
+
+}