summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorelmot <elmot@vaadin.com>2016-09-01 10:48:26 +0300
committerVaadin Code Review <review@vaadin.com>2016-09-01 13:31:54 +0000
commit876b6383e6ec50a8bbe34126b7bfed5f6f616bea (patch)
treeeef189cc63fb1e030227cc390bf734ba99aa44ef
parentda8394e5adeda85047a5e167bdd0904dc3387a87 (diff)
downloadvaadin-framework-876b6383e6ec50a8bbe34126b7bfed5f6f616bea.tar.gz
vaadin-framework-876b6383e6ec50a8bbe34126b7bfed5f6f616bea.zip
UnitTest for SingleSelection
Change-Id: I40b3b9bae7c1eb96377c0a593faee59ef52109fb
-rw-r--r--server/src/main/java/com/vaadin/data/selection/SingleSelection.java3
-rw-r--r--server/src/test/java/com/vaadin/data/SingleSelectionTest.java203
-rw-r--r--server/src/test/java/com/vaadin/server/data/datasource/bov/Person.java5
3 files changed, 210 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/data/selection/SingleSelection.java b/server/src/main/java/com/vaadin/data/selection/SingleSelection.java
index 9ede5b104c..fb6d3b1cac 100644
--- a/server/src/main/java/com/vaadin/data/selection/SingleSelection.java
+++ b/server/src/main/java/com/vaadin/data/selection/SingleSelection.java
@@ -139,7 +139,8 @@ public class SingleSelection<T> extends AbstractSelectionModel<T>
@Override
public void deselect(T value) {
- this.selectedItem = null;
+ if(Objects.equals(selectedItem,value))
+ doSelect(null, false);
}
@Override
diff --git a/server/src/test/java/com/vaadin/data/SingleSelectionTest.java b/server/src/test/java/com/vaadin/data/SingleSelectionTest.java
new file mode 100644
index 0000000000..5afa15e454
--- /dev/null
+++ b/server/src/test/java/com/vaadin/data/SingleSelectionTest.java
@@ -0,0 +1,203 @@
+/*
+ * 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.data;
+
+import com.vaadin.data.selection.SingleSelection;
+import com.vaadin.server.ClientMethodInvocation;
+import com.vaadin.server.data.DataCommunicator;
+import com.vaadin.server.data.datasource.bov.Person;
+import com.vaadin.shared.data.DataCommunicatorClientRpc;
+import com.vaadin.shared.data.DataCommunicatorConstants;
+import com.vaadin.shared.data.selection.SelectionModel;
+import com.vaadin.ui.AbstractListing;
+import elemental.json.JsonArray;
+import elemental.json.JsonObject;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test for {@link com.vaadin.data.selection.SingleSelection}
+ *
+ * @author Vaadin Ltd
+ */
+public class SingleSelectionTest {
+
+
+ private SingleSelection<Person> selectionModel;
+ private List<Person> selectionChanges;
+
+ private static class PersonListing extends AbstractListing<Person, SelectionModel.Single<Person>> {
+ public PersonListing() {
+ SingleSelection<Person> singleSelection = new SingleSelection<>(this);
+ setSelectionModel(singleSelection);
+
+ }
+
+ public SingleSelection<Person> getSelectionModel() {
+ return (SingleSelection<Person>) super.getSelectionModel();
+ }
+ }
+
+
+ @Before
+ public void initListing() {
+ listing = new PersonListing();
+ listing.setItems(PERSON_A, PERSON_B, PERSON_C);
+ selectionModel = listing.getSelectionModel();
+ selectionChanges = new ArrayList<>();
+ selectionModel.addSelectionListener(event -> selectionChanges.add(event.getValue()) );
+ }
+
+ public static final Person PERSON_C = new Person("c", 3);
+ public static final Person PERSON_B = new Person("b", 2);
+ public static final Person PERSON_A = new Person("a", 1);
+ public static final String RPC_INTERFACE = DataCommunicatorClientRpc.class.getName();
+ private PersonListing listing;
+
+ @Test
+ public void communication() {
+
+ selectionModel.select(PERSON_C);
+
+ DataCommunicator<Person> dataCommunicator = listing.getDataCommunicator();
+ dataCommunicator.beforeClientResponse(true);
+ List<ClientMethodInvocation> invocations = dataCommunicator.retrievePendingRpcCalls();
+ assertEquals(2, invocations.size());
+
+ ClientMethodInvocation invocationZero = invocations.get(0);
+ assertEquals(RPC_INTERFACE, invocationZero.getInterfaceName());
+ assertEquals("reset", invocationZero.getMethodName());
+
+ ClientMethodInvocation invocationOne = invocations.get(1);
+ assertEquals(RPC_INTERFACE, invocationOne.getInterfaceName());
+ assertEquals("setData", invocationOne.getMethodName());
+ JsonObject object = ((JsonArray) invocationOne.getParameters()[1]).getObject(2);
+
+ assertTrue("Expected selected item", object.getBoolean(DataCommunicatorConstants.SELECTED));
+ }
+
+ @Test
+ public void select() {
+
+ selectionModel.select(PERSON_B);
+
+ assertTrue(selectionModel.getSelectedItem().isPresent());
+
+ assertEquals(PERSON_B, selectionModel.getSelectedItem().orElse(null));
+
+ assertFalse(selectionModel.isSelected(PERSON_A));
+ assertTrue(selectionModel.isSelected(PERSON_B));
+ assertFalse(selectionModel.isSelected(PERSON_C));
+
+ assertEquals(Collections.singleton(PERSON_B), selectionModel.getSelectedItems());
+
+ assertEquals(Arrays.asList(PERSON_B), selectionChanges);
+ }
+
+ @Test
+ public void selectDeselect() {
+
+ selectionModel.select(PERSON_B);
+ selectionModel.deselect(PERSON_B);
+
+ assertFalse(selectionModel.getSelectedItem().isPresent());
+
+ assertFalse(selectionModel.isSelected(PERSON_A));
+ assertFalse(selectionModel.isSelected(PERSON_B));
+ assertFalse(selectionModel.isSelected(PERSON_C));
+
+ assertTrue(selectionModel.getSelectedItems().isEmpty());
+
+ assertEquals(Arrays.asList(PERSON_B,null), selectionChanges);
+ }
+ @Test
+ public void reselect() {
+
+ selectionModel.select(PERSON_B);
+ selectionModel.select(PERSON_C);
+
+ assertEquals(PERSON_C, selectionModel.getSelectedItem().orElse(null));
+
+ assertFalse(selectionModel.isSelected(PERSON_A));
+ assertFalse(selectionModel.isSelected(PERSON_B));
+ assertTrue(selectionModel.isSelected(PERSON_C));
+
+ assertEquals(Collections.singleton(PERSON_C), selectionModel.getSelectedItems());
+
+ assertEquals(Arrays.asList(PERSON_B, PERSON_C), selectionChanges);
+ }
+
+ @Test
+ public void deselectNoOp() {
+
+ selectionModel.select(PERSON_C);
+ selectionModel.deselect(PERSON_B);
+
+ assertEquals(PERSON_C, selectionModel.getSelectedItem().orElse(null));
+
+ assertFalse(selectionModel.isSelected(PERSON_A));
+ assertFalse(selectionModel.isSelected(PERSON_B));
+ assertTrue(selectionModel.isSelected(PERSON_C));
+
+ assertEquals(Collections.singleton(PERSON_C), selectionModel.getSelectedItems());
+
+ assertEquals(Arrays.asList(PERSON_C), selectionChanges);
+ }
+ @Test
+ public void selectTwice() {
+
+ selectionModel.select(PERSON_C);
+ selectionModel.select(PERSON_C);
+
+ assertEquals(PERSON_C, selectionModel.getSelectedItem().orElse(null));
+
+ assertFalse(selectionModel.isSelected(PERSON_A));
+ assertFalse(selectionModel.isSelected(PERSON_B));
+ assertTrue(selectionModel.isSelected(PERSON_C));
+
+ assertEquals(Collections.singleton(PERSON_C), selectionModel.getSelectedItems());
+
+ assertEquals(Arrays.asList(PERSON_C), selectionChanges);
+ }
+
+ @Test
+ public void deselectTwice() {
+
+ selectionModel.select(PERSON_C);
+ selectionModel.deselect(PERSON_C);
+ selectionModel.deselect(PERSON_C);
+
+ assertFalse(selectionModel.getSelectedItem().isPresent());
+
+ assertFalse(selectionModel.isSelected(PERSON_A));
+ assertFalse(selectionModel.isSelected(PERSON_B));
+ assertFalse(selectionModel.isSelected(PERSON_C));
+
+ assertTrue(selectionModel.getSelectedItems().isEmpty());
+
+ assertEquals(Arrays.asList(PERSON_C,null), selectionChanges);
+ }
+
+}
diff --git a/server/src/test/java/com/vaadin/server/data/datasource/bov/Person.java b/server/src/test/java/com/vaadin/server/data/datasource/bov/Person.java
index e123e24df2..f551e5be98 100644
--- a/server/src/test/java/com/vaadin/server/data/datasource/bov/Person.java
+++ b/server/src/test/java/com/vaadin/server/data/datasource/bov/Person.java
@@ -38,4 +38,9 @@ public class Person implements Serializable {
public int getBorn() {
return born;
}
+
+ @Override
+ public String toString() {
+ return name + "(" + born + ")";
+ }
}