-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from wordpress-mobile/issue/23-extract-callba…
…ck-handler Issue #23: Extract callback handler
- Loading branch information
Showing
9 changed files
with
471 additions
and
62 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
WordPressEditor/src/androidTest/java/org/wordpress/android/editor/JsCallbackHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.wordpress.android.editor; | ||
|
||
import android.util.Log; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.shadows.ShadowLog; | ||
import org.wordpress.android.util.AppLog; | ||
|
||
import java.util.List; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertFalse; | ||
import static org.mockito.Mockito.mock; | ||
import static org.robolectric.shadows.ShadowLog.LogItem; | ||
|
||
@Config(emulateSdk = 18) | ||
@RunWith(RobolectricTestRunner.class) | ||
public class JsCallbackHandlerTest { | ||
private final static String EDITOR_LOG_TAG = "WordPress-" + AppLog.T.EDITOR.toString(); | ||
|
||
private JsCallbackReceiver mJsCallbackReceiver; | ||
|
||
@Before | ||
public void setUp() { | ||
EditorFragment editorFragment = mock(EditorFragment.class); | ||
mJsCallbackReceiver = new JsCallbackReceiver(editorFragment); | ||
} | ||
|
||
@Test | ||
public void testCallbacksRecognized() { | ||
mJsCallbackReceiver.executeCallback("callback-dom-loaded", ""); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-new-field", "field-name"); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-input", "arguments"); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-selection-changed", "arguments"); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-selection-style", "arguments"); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-focus-in", ""); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-focus-out", ""); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-image-replaced", "arguments"); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-image-tap", "arguments"); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-link-tap", "arguments"); | ||
assertNotLogged("Unhandled callback"); | ||
|
||
mJsCallbackReceiver.executeCallback("callback-log", "arguments"); | ||
assertNotLogged("Unhandled callback"); | ||
} | ||
|
||
@Test | ||
public void testUnknownCallbackShouldBeLogged() { | ||
mJsCallbackReceiver.executeCallback("callback-does-not-exist", "content"); | ||
assertLogged(Log.DEBUG, EDITOR_LOG_TAG, "Unhandled callback: callback-does-not-exist:content", null); | ||
} | ||
|
||
@Test | ||
public void testCallbackLog() { | ||
mJsCallbackReceiver.executeCallback("callback-log", "msg=test-message"); | ||
assertLogged(Log.DEBUG, EDITOR_LOG_TAG, "callback-log: test-message", null); | ||
} | ||
|
||
private void assertLogged(int type, String tag, String msg, Throwable throwable) { | ||
LogItem lastLog = ShadowLog.getLogs().get(0); | ||
assertEquals(type, lastLog.type); | ||
assertEquals(msg, lastLog.msg); | ||
assertEquals(tag, lastLog.tag); | ||
assertEquals(throwable, lastLog.throwable); | ||
} | ||
|
||
private void assertNotLogged(String msg) { | ||
List<LogItem> logList = ShadowLog.getLogs(); | ||
if (!logList.isEmpty()) { | ||
assertFalse(logList.get(0).msg.contains(msg)); | ||
ShadowLog.reset(); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.wordpress.android.editor; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.wordpress.android.editor.Utils.getChangeMapFromSets; | ||
import static org.wordpress.android.editor.Utils.splitDelimitedString; | ||
|
||
@Config(emulateSdk = 18) | ||
@RunWith(RobolectricTestRunner.class) | ||
public class UtilsTest { | ||
|
||
@Test | ||
public void testSplitDelimitedString() { | ||
Set<String> splitString = new HashSet<>(); | ||
|
||
// Test normal usage | ||
splitString.add("p"); | ||
splitString.add("bold"); | ||
splitString.add("justifyLeft"); | ||
|
||
assertEquals(splitString, splitDelimitedString("p~bold~justifyLeft", "~")); | ||
|
||
// Test empty string | ||
assertEquals(Collections.emptySet(), splitDelimitedString("", "~")); | ||
} | ||
|
||
@Test | ||
public void testGetChangeMapFromSets() { | ||
Set<String> oldSet = new HashSet<>(); | ||
Set<String> newSet = new HashSet<>(); | ||
Map<String, Boolean> expectedMap = new HashMap<>(); | ||
|
||
// Test normal usage | ||
oldSet.add("p"); | ||
oldSet.add("bold"); | ||
oldSet.add("justifyLeft"); | ||
|
||
newSet.add("p"); | ||
newSet.add("justifyRight"); | ||
|
||
expectedMap.put("bold", false); | ||
expectedMap.put("justifyLeft", false); | ||
expectedMap.put("justifyRight", true); | ||
|
||
assertEquals(expectedMap, getChangeMapFromSets(oldSet, newSet)); | ||
|
||
// Test no changes | ||
oldSet.clear(); | ||
oldSet.add("p"); | ||
oldSet.add("bold"); | ||
|
||
newSet.clear(); | ||
newSet.add("p"); | ||
newSet.add("bold"); | ||
|
||
assertEquals(Collections.emptyMap(), getChangeMapFromSets(oldSet, newSet)); | ||
|
||
// Test empty sets | ||
assertEquals(Collections.emptyMap(), getChangeMapFromSets(Collections.emptySet(), Collections.emptySet())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.