diff --git a/gwt-storage-gwt2-tests/pom.xml b/gwt-storage-gwt2-tests/pom.xml index 8fb3a4d..e69ec2b 100644 --- a/gwt-storage-gwt2-tests/pom.xml +++ b/gwt-storage-gwt2-tests/pom.xml @@ -18,7 +18,6 @@ 1.0.0 - 1.0.0-RC1 2.9.0 4.13.1 diff --git a/gwt-storage-gwt2-tests/src/test/java/org/gwtproject/storage/client/StorageTest.java b/gwt-storage-gwt2-tests/src/test/java/org/gwtproject/storage/client/StorageTest.java index 3e10df6..c3cbb52 100644 --- a/gwt-storage-gwt2-tests/src/test/java/org/gwtproject/storage/client/StorageTest.java +++ b/gwt-storage-gwt2-tests/src/test/java/org/gwtproject/storage/client/StorageTest.java @@ -49,11 +49,11 @@ protected void gwtSetUp() throws Exception { // setup for tests by removing event handler if (handler != null) { - storage.removeStorageEventHandler(handler); + Storage.removeStorageEventHandler(handler); handler = null; } if (handler2 != null) { - storage.removeStorageEventHandler(handler2); + Storage.removeStorageEventHandler(handler2); handler2 = null; } @@ -62,18 +62,18 @@ protected void gwtSetUp() throws Exception { } @Override - protected void gwtTearDown() throws Exception { + protected void gwtTearDown() { if (storage == null) { return; // do not run if not supported } // clean up by removing event handler if (handler != null) { - storage.removeStorageEventHandler(handler); + Storage.removeStorageEventHandler(handler); handler = null; } if (handler2 != null) { - storage.removeStorageEventHandler(handler2); + Storage.removeStorageEventHandler(handler2); handler2 = null; } @@ -137,7 +137,7 @@ public void testSet() { return; // do not run if not supported } - assertEquals(null, storage.getItem("foo")); + assertNull(storage.getItem("foo")); assertEquals(0, storage.getLength()); storage.setItem("foo", "bar1"); assertEquals("bar1", storage.getItem("foo")); @@ -163,9 +163,9 @@ public void testKey() { } // key(n) where n >= storage.length() should return null - assertEquals(null, storage.key(0)); + assertNull(storage.key(0)); storage.setItem("a", "b"); - assertEquals(null, storage.key(1)); + assertNull(storage.key(1)); storage.clear(); storage.setItem("foo1", "bar"); @@ -196,10 +196,10 @@ public void testRemoveItem() { // removing a key should remove that key and value storage.removeItem("foo1"); - assertEquals(null, storage.getItem("foo1")); + assertNull(storage.getItem("foo1")); assertEquals("bar2", storage.getItem("foo2")); storage.removeItem("foo2"); - assertEquals(null, storage.getItem("foo2")); + assertNull(storage.getItem("foo2")); } public void testClearStorageEvent() { @@ -210,19 +210,16 @@ public void testClearStorageEvent() { delayTestFinish(2000); storage.setItem("tcseFoo", "tcseBar"); handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - assertNull(event.getKey()); - assertNull(event.getOldValue()); - assertNull(event.getNewValue()); - assertEquals(storage, event.getStorageArea()); - assertNotNull(event.getUrl()); + event -> { + assertNull(event.getKey()); + assertNull(event.getOldValue()); + assertNull(event.getNewValue()); + assertEquals(storage, event.getStorageArea()); + assertNotNull(event.getUrl()); - finishTest(); - } + finishTest(); }; - storage.addStorageEventHandler(handler); + Storage.addStorageEventHandler(handler); storage.clear(); } @@ -235,19 +232,16 @@ public void testSetItemStorageEvent() { storage.setItem("tsiseFoo", "tsiseBarOld"); handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - assertEquals("tsiseFoo", event.getKey()); - assertEquals("tsiseBarNew", event.getNewValue()); - assertEquals("tsiseBarOld", event.getOldValue()); - assertEquals(storage, event.getStorageArea()); - assertNotNull(event.getUrl()); + event -> { + assertEquals("tsiseFoo", event.getKey()); + assertEquals("tsiseBarNew", event.getNewValue()); + assertEquals("tsiseBarOld", event.getOldValue()); + assertEquals(storage, event.getStorageArea()); + assertNotNull(event.getUrl()); - finishTest(); - } + finishTest(); }; - storage.addStorageEventHandler(handler); + Storage.addStorageEventHandler(handler); storage.setItem("tsiseFoo", "tsiseBarNew"); } @@ -260,14 +254,11 @@ public void testRemoveItemStorageEvent() { storage.setItem("triseFoo", "triseBarOld"); handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - assertEquals("triseFoo", event.getKey()); - finishTest(); - } + event -> { + assertEquals("triseFoo", event.getKey()); + finishTest(); }; - storage.addStorageEventHandler(handler); + Storage.addStorageEventHandler(handler); storage.removeItem("triseFoo"); } @@ -277,20 +268,16 @@ public void testHandlerRegistration() { } final boolean[] eventFired = new boolean[1]; - eventFired[0] = false; delayTestFinish(3000); handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - fail("Storage change should not have fired."); - eventFired[0] = true; - finishTest(); - } + event -> { + fail("Storage change should not have fired."); + eventFired[0] = true; + finishTest(); }; - HandlerRegistration registration = storage.addStorageEventHandler(handler); + HandlerRegistration registration = Storage.addStorageEventHandler(handler); registration.removeHandler(); // these should fire events, but they should not be caught by handler @@ -317,22 +304,19 @@ public void testEventInEvent() { storage.setItem("teieFoo", "teieBar"); handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - if ("teieFoo".equals(event.getKey())) { - storage.clear(); - storage.setItem("teieFoo2", "teieBar2"); - // firing events from within a handler should not corrupt the values. - assertEquals("teieFoo", event.getKey()); - storage.setItem("teieFooEndTest", "thanks"); - } - if ("teieFooEndTest".equals(event.getKey())) { - finishTest(); - } + event -> { + if ("teieFoo".equals(event.getKey())) { + storage.clear(); + storage.setItem("teieFoo2", "teieBar2"); + // firing events from within a handler should not corrupt the values. + assertEquals("teieFoo", event.getKey()); + storage.setItem("teieFooEndTest", "thanks"); + } + if ("teieFooEndTest".equals(event.getKey())) { + finishTest(); } }; - storage.addStorageEventHandler(handler); + Storage.addStorageEventHandler(handler); storage.removeItem("teieFoo"); } @@ -348,32 +332,26 @@ public void testMultipleEventHandlers() { storage.setItem("tmehFoo", "tmehBar"); handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - if ("tmehFoo".equals(event.getKey())) { - eventHandledCount[0]++; - if (eventHandledCount[0] == 2) { - finishTest(); - } + event -> { + if ("tmehFoo".equals(event.getKey())) { + eventHandledCount[0]++; + if (eventHandledCount[0] == 2) { + finishTest(); } } }; - storage.addStorageEventHandler(handler); + Storage.addStorageEventHandler(handler); handler2 = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - if ("tmehFoo".equals(event.getKey())) { - eventHandledCount[0]++; - if (eventHandledCount[0] == 2) { - finishTest(); - } + event -> { + if ("tmehFoo".equals(event.getKey())) { + eventHandledCount[0]++; + if (eventHandledCount[0] == 2) { + finishTest(); } } }; - storage.addStorageEventHandler(handler2); + Storage.addStorageEventHandler(handler2); storage.removeItem("tmehFoo"); } @@ -385,20 +363,17 @@ public void testEventStorageArea() { delayTestFinish(2000); storage.setItem("tesaFoo", "tesaBar"); handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - Storage eventStorage = event.getStorageArea(); - assertEquals(storage, eventStorage); - boolean equalsLocal = Storage.getLocalStorageIfSupported().equals(eventStorage); - boolean equalsSession = Storage.getSessionStorageIfSupported().equals(eventStorage); - // assert that storage is either local or session, but not both. - assertFalse(equalsLocal == equalsSession); + event -> { + Storage eventStorage = event.getStorageArea(); + assertEquals(storage, eventStorage); + boolean equalsLocal = Storage.getLocalStorageIfSupported().equals(eventStorage); + boolean equalsSession = Storage.getSessionStorageIfSupported().equals(eventStorage); + // assert that storage is either local or session, but not both. + assertFalse(equalsLocal == equalsSession); - finishTest(); - } + finishTest(); }; - storage.addStorageEventHandler(handler); + Storage.addStorageEventHandler(handler); storage.clear(); } diff --git a/gwt-storage-j2cl-tests/pom.xml b/gwt-storage-j2cl-tests/pom.xml index e11c14b..af1a39b 100644 --- a/gwt-storage-j2cl-tests/pom.xml +++ b/gwt-storage-j2cl-tests/pom.xml @@ -21,6 +21,7 @@ https://repo.vertispan.com/j2cl/ + 1.0.0-RC1 0.8-SNAPSHOT @@ -47,6 +48,13 @@ test + + org.gwtproject.timer + gwt-timer + ${gwt.timer.version} + test + + org.gwtproject.storage gwt-storage diff --git a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/LocalStorageMapTest.java b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/LocalStorageMapTest.java index 9c7084a..52b2f5f 100644 --- a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/LocalStorageMapTest.java +++ b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/LocalStorageMapTest.java @@ -18,10 +18,10 @@ import com.google.j2cl.junit.apt.J2clTestInput; /** Tests Local {@link StorageMap}. */ -@J2clTestInput(SessionStorageMapTest.class) +@J2clTestInput(LocalStorageMapTest.class) public class LocalStorageMapTest extends StorageMapTest { @Override - Storage getStorage() { + public Storage getStorage() { return Storage.getLocalStorageIfSupported(); } } diff --git a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/LocalStorageTest.java b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/LocalStorageTest.java index 334a751..85d387e 100644 --- a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/LocalStorageTest.java +++ b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/LocalStorageTest.java @@ -15,13 +15,10 @@ */ package org.gwtproject.storage.client; -import com.google.j2cl.junit.apt.J2clTestInput; - /** Tests Local {@link Storage}. */ -@J2clTestInput(LocalStorageTest.class) public class LocalStorageTest extends StorageTest { @Override - Storage getStorage() { + public Storage getStorage() { return Storage.getLocalStorageIfSupported(); } } diff --git a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/MapInterfaceTest.java b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/MapInterfaceTest.java index e51e30a..3bf1ff2 100644 --- a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/MapInterfaceTest.java +++ b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/MapInterfaceTest.java @@ -17,9 +17,8 @@ package org.gwtproject.storage.client; import static java.util.Collections.singleton; +import static org.junit.Assert.*; -import com.google.gwt.core.client.GWT; -import com.google.gwt.junit.client.GWTTestCase; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -28,6 +27,8 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import org.gwtproject.core.client.GWT; +import org.junit.Test; /** * Tests representing the contract of {@link Map}. Concrete subclasses of this base class test @@ -40,7 +41,7 @@ * @param the type of keys used by the maps under test * @param the type of mapped values used the maps under test */ -public abstract class MapInterfaceTest extends GWTTestCase { +public abstract class MapInterfaceTest { protected final boolean supportsPut; protected final boolean supportsRemove; @@ -249,6 +250,7 @@ protected final void assertInvariants(Map map) { */ protected void assertMoreInvariants(Map map) {} + @Test public void testClear() { final Map map; try { @@ -271,6 +273,7 @@ public void testClear() { assertInvariants(map); } + @Test public void testContainsKey() { final Map map; final K unmappedKey; @@ -294,6 +297,7 @@ public void testContainsKey() { assertInvariants(map); } + @Test public void testContainsValue() { final Map map; final V unmappedValue; @@ -317,6 +321,7 @@ public void testContainsValue() { assertInvariants(map); } + @Test public void testEntrySet() { final Map map; final Set> entrySet; @@ -342,6 +347,7 @@ public void testEntrySet() { } } + @Test public void testEntrySetForEmptyMap() { final Map map; try { @@ -352,6 +358,7 @@ public void testEntrySetForEmptyMap() { assertInvariants(map); } + @Test public void testEntrySetContainsEntryNullKeyPresent() { if (!allowsNullKeys || !supportsPut) { return; @@ -379,6 +386,7 @@ public void testEntrySetContainsEntryNullKeyPresent() { assertFalse(entrySet.contains(mapEntry(null, null))); } + @Test public void testEntrySetContainsEntryNullKeyMissing() { final Map map; final Set> entrySet; @@ -401,6 +409,7 @@ public void testEntrySetContainsEntryNullKeyMissing() { assertFalse(entrySet.contains(mapEntry(null, null))); } + @Test public void testEntrySetIteratorRemove() { final Map map; try { @@ -436,6 +445,7 @@ public void testEntrySetIteratorRemove() { assertInvariants(map); } + @Test public void testEntrySetRemove() { final Map map; try { @@ -461,6 +471,7 @@ public void testEntrySetRemove() { assertInvariants(map); } + @Test public void testEntrySetRemoveMissingKey() { final Map map; final K key; @@ -489,6 +500,7 @@ public void testEntrySetRemoveMissingKey() { assertInvariants(map); } + @Test public void testEntrySetRemoveDifferentValue() { final Map map; try { @@ -516,6 +528,7 @@ public void testEntrySetRemoveDifferentValue() { assertInvariants(map); } + @Test public void testEntrySetRemoveNullKeyPresent() { if (!allowsNullKeys || !supportsPut || !supportsRemove) { return; @@ -546,6 +559,7 @@ public void testEntrySetRemoveNullKeyPresent() { assertFalse(map.containsKey(null)); } + @Test public void testEntrySetRemoveNullKeyMissing() { final Map map; try { @@ -571,6 +585,7 @@ public void testEntrySetRemoveNullKeyMissing() { assertInvariants(map); } + @Test public void testEntrySetRemoveAll() { final Map map; try { @@ -600,6 +615,7 @@ public void testEntrySetRemoveAll() { assertInvariants(map); } + @Test public void testEntrySetRemoveAllNullFromEmpty() { final Map map; try { @@ -625,6 +641,7 @@ public void testEntrySetRemoveAllNullFromEmpty() { assertInvariants(map); } + @Test public void testEntrySetRetainAll() { final Map map; try { @@ -654,6 +671,7 @@ public void testEntrySetRetainAll() { assertInvariants(map); } + @Test public void testEntrySetRetainAllNullFromEmpty() { final Map map; try { @@ -679,6 +697,7 @@ public void testEntrySetRetainAllNullFromEmpty() { assertInvariants(map); } + @Test public void testEntrySetClear() { final Map map; try { @@ -701,6 +720,7 @@ public void testEntrySetClear() { assertInvariants(map); } + @Test public void testEntrySetAddAndAddAll() { final Map map; try { @@ -726,6 +746,7 @@ public void testEntrySetAddAndAddAll() { assertInvariants(map); } + @Test public void testEntrySetSetValue() { // TODO: Investigate the extent to which, in practice, maps that support // put() also support Entry.setValue(). @@ -752,6 +773,7 @@ public void testEntrySetSetValue() { assertInvariants(map); } + @Test public void testEntrySetSetValueSameValue() { // TODO: Investigate the extent to which, in practice, maps that support // put() also support Entry.setValue(). @@ -776,6 +798,7 @@ public void testEntrySetSetValueSameValue() { assertInvariants(map); } + @Test public void testEqualsForEqualMap() { final Map map; try { @@ -795,6 +818,7 @@ public void testEqualsForEqualMap() { * equals does not apply to Storage because there's only one instance so two * maps will always be equal. */ + @Test public void disabled_testEqualsForLargerMap() { if (!supportsPut) { return; @@ -817,6 +841,7 @@ public void disabled_testEqualsForLargerMap() { * equals does not apply to Storage because there's only one instance so two * maps will always be equal. */ + @Test public void disabled_testEqualsForSmallerMap() { if (!supportsRemove) { return; @@ -851,6 +876,7 @@ public void testEqualsForEmptyMap() { assertFalse(map.equals(null)); } + @Test public void testGet() { final Map map; try { @@ -872,6 +898,7 @@ public void testGet() { assertNull(map.get(unmappedKey)); } + @Test public void testGetForEmptyMap() { final Map map; K unmappedKey = null; @@ -884,6 +911,7 @@ public void testGetForEmptyMap() { assertNull(map.get(unmappedKey)); } + @Test public void testGetNull() { final Map map; try { @@ -907,6 +935,7 @@ public void testGetNull() { assertInvariants(map); } + @Test public void testHashCode() { final Map map; try { @@ -917,6 +946,7 @@ public void testHashCode() { assertInvariants(map); } + @Test public void testHashCodeForEmptyMap() { final Map map; try { @@ -927,6 +957,7 @@ public void testHashCodeForEmptyMap() { assertInvariants(map); } + @Test public void testPutNewKey() { final Map map; try { @@ -962,6 +993,7 @@ public void testPutNewKey() { assertInvariants(map); } + @Test public void testPutExistingKey() { final Map map; final K keyToPut; @@ -991,6 +1023,7 @@ public void testPutExistingKey() { assertInvariants(map); } + @Test public void testPutNullKey() { if (!supportsPut) { return; @@ -1025,6 +1058,7 @@ public void testPutNullKey() { assertInvariants(map); } + @Test public void testPutNullValue() { if (!supportsPut) { return; @@ -1060,6 +1094,7 @@ public void testPutNullValue() { assertInvariants(map); } + @Test public void testPutNullValueForExistingKey() { if (!supportsPut) { return; @@ -1091,6 +1126,7 @@ public void testPutNullValueForExistingKey() { assertInvariants(map); } + @Test public void testPutAllNewKey() { final Map map; try { @@ -1124,6 +1160,7 @@ public void testPutAllNewKey() { assertInvariants(map); } + @Test public void testPutAllExistingKey() { final Map map; final K keyToPut; @@ -1153,6 +1190,7 @@ public void testPutAllExistingKey() { assertInvariants(map); } + @Test public void testRemove() { final Map map; final K keyToRemove; @@ -1180,6 +1218,7 @@ public void testRemove() { assertInvariants(map); } + @Test public void testRemoveMissingKey() { final Map map; final K keyToRemove; @@ -1204,6 +1243,7 @@ public void testRemoveMissingKey() { assertInvariants(map); } + @Test public void testSize() { final Map map; try { @@ -1214,6 +1254,7 @@ public void testSize() { assertInvariants(map); } + @Test public void testKeySetClear() { final Map map; try { @@ -1236,6 +1277,7 @@ public void testKeySetClear() { assertInvariants(map); } + @Test public void testKeySetRemoveAllNullFromEmpty() { final Map map; try { @@ -1261,6 +1303,7 @@ public void testKeySetRemoveAllNullFromEmpty() { assertInvariants(map); } + @Test public void testKeySetRetainAllNullFromEmpty() { final Map map; try { @@ -1286,6 +1329,7 @@ public void testKeySetRetainAllNullFromEmpty() { assertInvariants(map); } + @Test public void testValues() { final Map map; final Collection valueCollection; @@ -1308,6 +1352,7 @@ public void testValues() { } } + @Test public void testValuesIteratorRemove() { final Map map; try { @@ -1345,6 +1390,7 @@ public void testValuesIteratorRemove() { assertInvariants(map); } + @Test public void testValuesRemove() { final Map map; try { @@ -1372,6 +1418,7 @@ public void testValuesRemove() { assertInvariants(map); } + @Test public void testValuesRemoveMissing() { final Map map; final V valueToRemove; @@ -1397,6 +1444,7 @@ public void testValuesRemoveMissing() { assertInvariants(map); } + @Test public void testValuesRemoveAll() { final Map map; try { @@ -1425,6 +1473,7 @@ public void testValuesRemoveAll() { assertInvariants(map); } + @Test public void testValuesRemoveAllNullFromEmpty() { final Map map; try { @@ -1450,6 +1499,7 @@ public void testValuesRemoveAllNullFromEmpty() { assertInvariants(map); } + @Test public void testValuesRetainAll() { final Map map; try { @@ -1479,6 +1529,7 @@ public void testValuesRetainAll() { assertInvariants(map); } + @Test public void testValuesRetainAllNullFromEmpty() { final Map map; try { @@ -1504,6 +1555,7 @@ public void testValuesRetainAllNullFromEmpty() { assertInvariants(map); } + @Test public void testValuesClear() { final Map map; try { @@ -1528,8 +1580,7 @@ public void testValuesClear() { } private void failForMissingNPE(Map map) { - if (map.isEmpty() && !GWT.isScript() && TestUtils.getJdkVersion() < 8) { - // JDK < 8 does not conform to the specification if the map is empty. + if (map.isEmpty() && !GWT.isScript()) { return; } fail("Should have thrown NullPointerException"); diff --git a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/SessionStorageMapTest.java b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/SessionStorageMapTest.java index 6e945ca..b4ab178 100644 --- a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/SessionStorageMapTest.java +++ b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/SessionStorageMapTest.java @@ -28,7 +28,7 @@ @J2clTestInput(SessionStorageMapTest.class) public class SessionStorageMapTest extends StorageMapTest { @Override - Storage getStorage() { + public Storage getStorage() { return Storage.getLocalStorageIfSupported(); } } diff --git a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/SessionStorageTest.java b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/SessionStorageTest.java index a2e4b8e..b645b9f 100644 --- a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/SessionStorageTest.java +++ b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/SessionStorageTest.java @@ -21,7 +21,7 @@ @J2clTestInput(SessionStorageTest.class) public class SessionStorageTest extends StorageTest { @Override - Storage getStorage() { + public Storage getStorage() { return Storage.getSessionStorageIfSupported(); } } diff --git a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/StorageMapTest.java b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/StorageMapTest.java index f9d96d4..f483c1e 100644 --- a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/StorageMapTest.java +++ b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/StorageMapTest.java @@ -20,28 +20,12 @@ /** Tests {@link StorageMap}. */ public abstract class StorageMapTest extends MapInterfaceTest { - protected Storage storage; + public Storage storage; public StorageMapTest() { super(false, false, true, true, true); } - @Override - public String getModuleName() { - return "org.gwtproject.storage.StorageTest"; - } - - @Override - protected void gwtSetUp() throws Exception { - storage = getStorage(); - if (storage == null) { - return; // do not run if not supported - } - - // setup for tests by emptying storage - storage.clear(); - } - /** * Returns a {@link Storage} object. * @@ -52,17 +36,17 @@ protected void gwtSetUp() throws Exception { abstract Storage getStorage(); @Override - protected String getKeyNotInPopulatedMap() throws UnsupportedOperationException { + public String getKeyNotInPopulatedMap() throws UnsupportedOperationException { return "nonExistingKey"; } @Override - protected String getValueNotInPopulatedMap() throws UnsupportedOperationException { + public String getValueNotInPopulatedMap() throws UnsupportedOperationException { return "nonExistingValue"; } @Override - protected Map makeEmptyMap() throws UnsupportedOperationException { + public Map makeEmptyMap() throws UnsupportedOperationException { if (storage == null) { throw new UnsupportedOperationException( "StorageMap not supported because Storage is not supported."); @@ -74,7 +58,7 @@ protected Map makeEmptyMap() throws UnsupportedOperationExceptio } @Override - protected Map makePopulatedMap() throws UnsupportedOperationException { + public Map makePopulatedMap() throws UnsupportedOperationException { if (storage == null) { throw new UnsupportedOperationException( "StorageMap not supported because Storage is not supported."); diff --git a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/StorageTest.java b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/StorageTest.java index b996ee8..e8d53b5 100644 --- a/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/StorageTest.java +++ b/gwt-storage-j2cl-tests/src/test/java/org/gwtproject/storage/client/StorageTest.java @@ -15,30 +15,24 @@ */ package org.gwtproject.storage.client; +import static org.junit.Assert.*; + +import elemental2.promise.Promise; +import org.gwtproject.core.client.GWT; import org.gwtproject.event.shared.HandlerRegistration; +import org.gwtproject.timer.client.Timer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; /** Tests {@link Storage}. */ public abstract class StorageTest { - protected Storage storage; - protected StorageEvent.Handler handler; - protected StorageEvent.Handler handler2; - - private native boolean isFirefox35OrLater() /*-{ - var geckoVersion = @com.google.gwt.dom.client.DOMImplMozilla::getGeckoVersion()(); - return (geckoVersion != -1) && (geckoVersion >= 1009001); - }-*/; - - private native boolean isSafari3OrBefore() /*-{ - return @com.google.gwt.dom.client.DOMImplWebkit::isWebkit525OrBefore()(); - }-*/; - - @Override - public String getModuleName() { - return "org.gwtproject.storage.Storage"; - } + public Storage storage; + public StorageEvent.Handler handler; + public StorageEvent.Handler handler2; - @Override - protected void gwtSetUp() throws Exception { + @Before + public void setUp() { storage = getStorage(); if (storage == null) { return; // do not run if not supported @@ -46,11 +40,11 @@ protected void gwtSetUp() throws Exception { // setup for tests by removing event handler if (handler != null) { - storage.removeStorageEventHandler(handler); + Storage.removeStorageEventHandler(handler); handler = null; } if (handler2 != null) { - storage.removeStorageEventHandler(handler2); + Storage.removeStorageEventHandler(handler2); handler2 = null; } @@ -58,19 +52,19 @@ protected void gwtSetUp() throws Exception { storage.clear(); } - @Override - protected void gwtTearDown() throws Exception { + @After + public void tearDown() { if (storage == null) { return; // do not run if not supported } // clean up by removing event handler if (handler != null) { - storage.removeStorageEventHandler(handler); + Storage.removeStorageEventHandler(handler); handler = null; } if (handler2 != null) { - storage.removeStorageEventHandler(handler2); + Storage.removeStorageEventHandler(handler2); handler2 = null; } @@ -87,6 +81,7 @@ protected void gwtTearDown() throws Exception { */ abstract Storage getStorage(); + @Test public void testClear() { if (storage == null) { return; // do not run if not supported @@ -98,6 +93,7 @@ public void testClear() { assertEquals(0, storage.getLength()); } + @Test public void testGet() { if (storage == null) { return; // do not run if not supported @@ -114,6 +110,7 @@ public void testGet() { storage.getItem("notset")); } + @Test public void testLength() { if (storage == null) { return; // do not run if not supported @@ -129,12 +126,13 @@ public void testLength() { assertEquals(0, storage.getLength()); } + @Test public void testSet() { if (storage == null) { return; // do not run if not supported } - assertEquals(null, storage.getItem("foo")); + assertNull(storage.getItem("foo")); assertEquals(0, storage.getLength()); storage.setItem("foo", "bar1"); assertEquals("bar1", storage.getItem("foo")); @@ -154,15 +152,16 @@ public void testSet() { } } + @Test public void testKey() { if (storage == null) { return; // do not run if not supported } // key(n) where n >= storage.length() should return null - assertEquals(null, storage.key(0)); + assertNull(storage.key(0)); storage.setItem("a", "b"); - assertEquals(null, storage.key(1)); + assertNull(storage.key(1)); storage.clear(); storage.setItem("foo1", "bar"); @@ -176,6 +175,7 @@ public void testKey() { assertTrue(storage.key(0).equals("foo2") || storage.key(1).equals("foo2")); } + @Test public void testRemoveItem() { if (storage == null) { return; // do not run if not supported @@ -193,225 +193,175 @@ public void testRemoveItem() { // removing a key should remove that key and value storage.removeItem("foo1"); - assertEquals(null, storage.getItem("foo1")); + assertNull(storage.getItem("foo1")); assertEquals("bar2", storage.getItem("foo2")); storage.removeItem("foo2"); - assertEquals(null, storage.getItem("foo2")); - } - - public void testClearStorageEvent() { - if (storage == null) { - return; // do not run if not supported - } - - delayTestFinish(2000); - storage.setItem("tcseFoo", "tcseBar"); - handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - assertNull(event.getKey()); - assertNull(event.getOldValue()); - assertNull(event.getNewValue()); - assertEquals(storage, event.getStorageArea()); - assertNotNull(event.getUrl()); - - finishTest(); - } - }; - storage.addStorageEventHandler(handler); - storage.clear(); - } - - public void testSetItemStorageEvent() { - if (storage == null) { - return; // do not run if not supported - } - - delayTestFinish(2000); - storage.setItem("tsiseFoo", "tsiseBarOld"); - - handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - assertEquals("tsiseFoo", event.getKey()); - assertEquals("tsiseBarNew", event.getNewValue()); - assertEquals("tsiseBarOld", event.getOldValue()); - assertEquals(storage, event.getStorageArea()); - assertNotNull(event.getUrl()); - - finishTest(); - } - }; - storage.addStorageEventHandler(handler); - storage.setItem("tsiseFoo", "tsiseBarNew"); + assertNull(storage.getItem("foo2")); } - public void testRemoveItemStorageEvent() { - if (storage == null) { - return; // do not run if not supported - } - - delayTestFinish(2000); - storage.setItem("triseFoo", "triseBarOld"); - - handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - assertEquals("triseFoo", event.getKey()); - finishTest(); - } - }; - storage.addStorageEventHandler(handler); - storage.removeItem("triseFoo"); + @Test(timeout = 2000) + public Promise testClearStorageEvent() { + return new Promise<>( + (resolve, reject) -> { + storage.setItem("tcseFoo", "tcseBar"); + handler = + event -> { + assertNull(event.getKey()); + assertNull(event.getOldValue()); + assertNull(event.getNewValue()); + assertEquals(storage, event.getStorageArea()); + assertNotNull(event.getUrl()); + + resolve.onInvoke((Void) null); + }; + Storage.addStorageEventHandler(handler); + storage.clear(); + }); } - public void testHandlerRegistration() { - if (storage == null) { - return; // do not run if not supported - } - - final boolean[] eventFired = new boolean[1]; - eventFired[0] = false; - - delayTestFinish(3000); - - handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - fail("Storage change should not have fired."); - eventFired[0] = true; - finishTest(); - } - }; - HandlerRegistration registration = storage.addStorageEventHandler(handler); - registration.removeHandler(); - - // these should fire events, but they should not be caught by handler - storage.setItem("thrFoo", "thrBar"); - storage.clear(); - - // schedule timer to make sure event didn't fire async - new Timer() { - @Override - public void run() { - if (!eventFired[0]) { - finishTest(); - } - } - }.schedule(1000); + @Test(timeout = 2000) + public Promise testSetItemStorageEvent() { + return new Promise<>( + (resolve, reject) -> { + storage.setItem("tsiseFoo", "tsiseBarOld"); + + handler = + event -> { + assertEquals("tsiseFoo", event.getKey()); + assertEquals("tsiseBarNew", event.getNewValue()); + assertEquals("tsiseBarOld", event.getOldValue()); + assertEquals(storage, event.getStorageArea()); + assertNotNull(event.getUrl()); + + resolve.onInvoke((Void) null); + }; + Storage.addStorageEventHandler(handler); + storage.setItem("tsiseFoo", "tsiseBarNew"); + }); } - public void testEventInEvent() { - if (storage == null) { - return; // do not run if not supported - } - - delayTestFinish(3000); - storage.setItem("teieFoo", "teieBar"); - - handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - if ("teieFoo".equals(event.getKey())) { - storage.clear(); - storage.setItem("teieFoo2", "teieBar2"); - // firing events from within a handler should not corrupt the values. - assertEquals("teieFoo", event.getKey()); - storage.setItem("teieFooEndTest", "thanks"); - } - if ("teieFooEndTest".equals(event.getKey())) { - finishTest(); - } - } - }; - storage.addStorageEventHandler(handler); - storage.removeItem("teieFoo"); + @Test(timeout = 2000) + public Promise testRemoveItemStorageEvent() { + return new Promise<>( + (resolve, reject) -> { + storage.setItem("triseFoo", "triseBarOld"); + + handler = + event -> { + assertEquals("triseFoo", event.getKey()); + resolve.onInvoke((Void) null); + }; + Storage.addStorageEventHandler(handler); + storage.removeItem("triseFoo"); + }); } - public void testMultipleEventHandlers() { - if (storage == null) { - return; // do not run if not supported - } - - delayTestFinish(3000); - - final int[] eventHandledCount = new int[] {0}; - - storage.setItem("tmehFoo", "tmehBar"); - - handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - if ("tmehFoo".equals(event.getKey())) { - eventHandledCount[0]++; - if (eventHandledCount[0] == 2) { - finishTest(); + @Test(timeout = 3000) + public Promise testHandlerRegistration() { + return new Promise<>( + (resolve, reject) -> { + final boolean[] eventFired = new boolean[1]; + + handler = + event -> { + fail("Storage change should not have fired."); + eventFired[0] = true; + resolve.onInvoke((Void) null); + }; + HandlerRegistration registration = Storage.addStorageEventHandler(handler); + registration.removeHandler(); + + // these should fire events, but they should not be caught by handler + storage.setItem("thrFoo", "thrBar"); + storage.clear(); + + // schedule timer to make sure event didn't fire async + new Timer() { + @Override + public void run() { + if (!eventFired[0]) { + resolve.onInvoke((Void) null); } } - } - }; - storage.addStorageEventHandler(handler); - - handler2 = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - if ("tmehFoo".equals(event.getKey())) { - eventHandledCount[0]++; - if (eventHandledCount[0] == 2) { - finishTest(); - } - } - } - }; - storage.addStorageEventHandler(handler2); - storage.removeItem("tmehFoo"); + }.schedule(1000); + }); } - public void testEventStorageArea() { - if (storage == null) { - return; // do not run if not supported - } + @Test(timeout = 3000) + public Promise testEventInEvent() { + return new Promise<>( + (resolve, reject) -> { + storage.setItem("teieFoo", "teieBar"); + + handler = + event -> { + if ("teieFoo".equals(event.getKey())) { + storage.clear(); + storage.setItem("teieFoo2", "teieBar2"); + // firing events from within a handler should not corrupt the values. + assertEquals("teieFoo", event.getKey()); + storage.setItem("teieFooEndTest", "thanks"); + } + if ("teieFooEndTest".equals(event.getKey())) { + resolve.onInvoke((Void) null); + } + }; + Storage.addStorageEventHandler(handler); + storage.removeItem("teieFoo"); + }); + } - delayTestFinish(2000); - storage.setItem("tesaFoo", "tesaBar"); - handler = - new StorageEvent.Handler() { - @Override - public void onStorageChange(StorageEvent event) { - Storage eventStorage = event.getStorageArea(); - assertEquals(storage, eventStorage); - boolean equalsLocal = Storage.getLocalStorageIfSupported().equals(eventStorage); - boolean equalsSession = Storage.getSessionStorageIfSupported().equals(eventStorage); - // assert that storage is either local or session, but not both. - assertFalse(equalsLocal == equalsSession); - - finishTest(); - } - }; - storage.addStorageEventHandler(handler); - storage.clear(); + @Test(timeout = 3000) + public Promise testMultipleEventHandlers() { + return new Promise<>( + (resolve, reject) -> { + final int[] eventHandledCount = new int[] {0}; + + storage.setItem("tmehFoo", "tmehBar"); + + handler = + event -> { + if ("tmehFoo".equals(event.getKey())) { + eventHandledCount[0]++; + if (eventHandledCount[0] == 2) { + resolve.onInvoke((Void) null); + } + } + }; + Storage.addStorageEventHandler(handler); + + handler2 = + event -> { + if ("tmehFoo".equals(event.getKey())) { + eventHandledCount[0]++; + if (eventHandledCount[0] == 2) { + resolve.onInvoke((Void) null); + } + } + }; + Storage.addStorageEventHandler(handler2); + storage.removeItem("tmehFoo"); + }); } - public void testSupported() { - // test the isxxxSupported() call - if (isFirefox35OrLater()) { - assertNotNull(storage); - assertTrue(Storage.isLocalStorageSupported()); - assertTrue(Storage.isSessionStorageSupported()); - assertTrue(Storage.isSupported()); - } - if (isSafari3OrBefore()) { - assertNull(storage); - assertFalse(Storage.isLocalStorageSupported()); - assertFalse(Storage.isSessionStorageSupported()); - assertFalse(Storage.isSupported()); - } + @Test(timeout = 2000) + public Promise testEventStorageArea() { + return new Promise<>( + (resolve, reject) -> { + storage.setItem("tesaFoo", "tesaBar"); + handler = + event -> { + Storage eventStorage = event.getStorageArea(); + assertEquals(storage, eventStorage); + boolean equalsLocal = Storage.getLocalStorageIfSupported().equals(eventStorage); + boolean equalsSession = Storage.getSessionStorageIfSupported().equals(eventStorage); + // assert that storage is either local or session, but not both. + assertNotEquals(equalsLocal, equalsSession); + + resolve.onInvoke((Void) null); + }; + Storage.addStorageEventHandler(handler); + storage.clear(); + }); } } diff --git a/gwt-storage/src/main/java/org/gwtproject/storage/client/StorageEvent.java b/gwt-storage/src/main/java/org/gwtproject/storage/client/StorageEvent.java index d424031..7096e1e 100644 --- a/gwt-storage/src/main/java/org/gwtproject/storage/client/StorageEvent.java +++ b/gwt-storage/src/main/java/org/gwtproject/storage/client/StorageEvent.java @@ -16,6 +16,7 @@ package org.gwtproject.storage.client; +import elemental2.webstorage.StorageEventInit; import jsinterop.annotations.JsConstructor; import jsinterop.annotations.JsOverlay; import jsinterop.annotations.JsPackage; @@ -64,7 +65,7 @@ public interface Handler { } @JsConstructor - private StorageEvent(String type, Object init) {} + private StorageEvent(String type, StorageEventInit init) {} /** * Returns a newly created and correctly initialized event. @@ -77,7 +78,7 @@ private StorageEvent(String type, Object init) {} * @return the newly created event object */ @JsOverlay - static StorageEvent createEvent(Object init) { + static StorageEvent createEvent(StorageEventInit init) { final StorageEvent storageEvent = new StorageEvent("storage", init); final JsPropertyMap se = Js.cast(storageEvent); final JsPropertyMap initSe = Js.cast(init); diff --git a/gwt-storage/src/main/java/org/gwtproject/storage/client/StorageImplNonNativeEvents.java b/gwt-storage/src/main/java/org/gwtproject/storage/client/StorageImplNonNativeEvents.java index 21d9c4f..155597c 100644 --- a/gwt-storage/src/main/java/org/gwtproject/storage/client/StorageImplNonNativeEvents.java +++ b/gwt-storage/src/main/java/org/gwtproject/storage/client/StorageImplNonNativeEvents.java @@ -18,8 +18,8 @@ import static elemental2.dom.DomGlobal.window; +import elemental2.webstorage.StorageEventInit; import elemental2.webstorage.WebStorageWindow; -import jsinterop.base.JsPropertyMap; /** * Implementation of Storage with non-native events. @@ -31,13 +31,12 @@ class StorageImplNonNativeEvents extends StorageImpl { private static StorageEvent createStorageEvent( String key, String oldValue, String newValue, String storageName) { - JsPropertyMap init = JsPropertyMap.of(); - init.set("key", key); - init.set("oldValue", oldValue); - init.set("newValue", newValue); - init.set("url", window.location.href); - init.set( - "storageArea", + StorageEventInit init = StorageEventInit.create(); + init.setKey(key); + init.setOldValue(oldValue); + init.setNewValue(newValue); + init.setUrl(window.location.href); + init.setStorageArea( "localStorage".equals(storageName) ? WebStorageWindow.of(window).localStorage : WebStorageWindow.of(window).sessionStorage);