Skip to content

Commit 7306c12

Browse files
committed
setSubscription bug fix + Robolectric tests
* Fixed caching issue with setSubscription - Added Robolectric tests for the fix.
1 parent 4fcc464 commit 7306c12

File tree

15 files changed

+498
-71
lines changed

15 files changed

+498
-71
lines changed

OneSignalSDK.jar

428 Bytes
Binary file not shown.

OneSignalSDK/app/app.iml

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
<facet type="android" name="Android">
1010
<configuration>
1111
<option name="SELECTED_BUILD_VARIANT" value="debug" />
12-
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
12+
<option name="SELECTED_TEST_ARTIFACT" value="_unit_test_" />
1313
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
1414
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
1515
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
16-
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
17-
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugAndroidTestSources" />
18-
<option name="TEST_SOURCE_GEN_TASK_NAME" value="generateDebugAndroidTestSources" />
16+
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugUnitTest" />
17+
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugUnitTestSources" />
1918
<option name="ALLOW_USER_CONFIGURATION" value="false" />
2019
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
2120
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
@@ -26,7 +25,7 @@
2625
</component>
2726
<component name="NewModuleRootManager" inherit-compiler-output="false">
2827
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
29-
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/androidTest/debug" />
28+
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
3029
<exclude-output />
3130
<content url="file://$MODULE_DIR$">
3231
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
@@ -35,33 +34,34 @@
3534
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/debug" isTestSource="false" generated="true" />
3635
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/debug" type="java-resource" />
3736
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/generated/debug" type="java-resource" />
38-
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/androidTest/debug" isTestSource="true" generated="true" />
39-
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/androidTest/debug" isTestSource="true" generated="true" />
40-
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/debug" isTestSource="true" generated="true" />
41-
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/androidTest/debug" isTestSource="true" generated="true" />
42-
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/androidTest/debug" type="java-test-resource" />
43-
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/generated/androidTest/debug" type="java-test-resource" />
4437
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" type="java-resource" />
4538
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" type="java-resource" />
4639
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" type="java-resource" />
4740
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
4841
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
4942
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
5043
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
44+
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
45+
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
46+
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
47+
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
48+
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
49+
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/jni" isTestSource="true" />
50+
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
5151
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
5252
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
5353
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
5454
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
5555
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
5656
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
5757
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
58-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
59-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
60-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
61-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
62-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
63-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
64-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
58+
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
59+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
60+
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
61+
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
62+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
63+
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
64+
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
6565
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
6666
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
6767
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
@@ -84,13 +84,36 @@
8484
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
8585
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
8686
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
87+
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
88+
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
8789
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
8890
</content>
8991
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
9092
<orderEntry type="sourceFolder" forTests="false" />
93+
<orderEntry type="library" exported="" scope="TEST" name="robolectric-resources-3.0-rc3" level="project" />
94+
<orderEntry type="library" exported="" scope="TEST" name="asm-5.0.1" level="project" />
95+
<orderEntry type="library" exported="" scope="TEST" name="asm-commons-5.0.1" level="project" />
96+
<orderEntry type="library" exported="" scope="TEST" name="vtd-xml-2.11" level="project" />
97+
<orderEntry type="library" exported="" scope="TEST" name="hamcrest-core-1.3" level="project" />
98+
<orderEntry type="library" exported="" name="support-annotations-22.1.1" level="project" />
99+
<orderEntry type="library" exported="" scope="TEST" name="asm-tree-5.0.1" level="project" />
100+
<orderEntry type="library" exported="" scope="TEST" name="robolectric-annotations-3.0-rc3" level="project" />
101+
<orderEntry type="library" exported="" scope="TEST" name="junit-4.12" level="project" />
102+
<orderEntry type="library" exported="" scope="TEST" name="asm-analysis-5.0.1" level="project" />
91103
<orderEntry type="library" exported="" name="appcompat-v7-22.1.1" level="project" />
104+
<orderEntry type="library" exported="" scope="TEST" name="robolectric-utils-3.0-rc3" level="project" />
105+
<orderEntry type="library" exported="" scope="TEST" name="icu4j-53.1" level="project" />
106+
<orderEntry type="library" exported="" scope="TEST" name="ant-1.8.0" level="project" />
107+
<orderEntry type="library" exported="" scope="TEST" name="robolectric-3.0-rc3" level="project" />
108+
<orderEntry type="library" exported="" scope="TEST" name="ant-launcher-1.8.0" level="project" />
109+
<orderEntry type="library" exported="" scope="TEST" name="accessibility-test-framework-1.0" level="project" />
110+
<orderEntry type="library" exported="" scope="TEST" name="bcprov-jdk16-1.46" level="project" />
111+
<orderEntry type="library" exported="" scope="TEST" name="asm-util-5.0.1" level="project" />
112+
<orderEntry type="library" exported="" scope="TEST" name="sqlite4java-0.282" level="project" />
113+
<orderEntry type="library" exported="" scope="TEST" name="maven-ant-tasks-2.1.3" level="project" />
92114
<orderEntry type="library" exported="" name="support-v4-22.1.1" level="project" />
93-
<orderEntry type="library" exported="" name="support-annotations-22.1.1" level="project" />
115+
<orderEntry type="library" exported="" scope="TEST" name="mockable-android-22" level="project" />
116+
<orderEntry type="library" exported="" scope="TEST" name="shadows-core-3.0-rc3" level="project" />
94117
<orderEntry type="module" module-name="onesignal" exported="" />
95118
</component>
96119
</module>

OneSignalSDK/app/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ android {
2323
dependencies {
2424
compile fileTree(dir: 'libs', include: ['*.jar'])
2525
compile 'com.android.support:appcompat-v7:22.1.1'
26+
2627
//compile 'com.onesignal:OneSignal:1.8.1+@aar'
2728
compile project(':onesignal')
29+
30+
testCompile 'junit:junit:4.12'
31+
// testCompile 'org.robolectric:shadows-support-v4:3.0'
32+
testCompile('org.robolectric:robolectric:3.0-rc3') {
33+
exclude group: 'commons-logging', module: 'commons-logging'
34+
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
35+
}
2836
}

OneSignalSDK/app/src/androidTest/java/com/onesignal/example/ApplicationTest.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.onesignal.example;
2+
3+
import android.app.Activity;
4+
5+
public class BlankActivity extends Activity {
6+
}

OneSignalSDK/app/src/main/java/com/onesignal/example/MainActivity.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.util.Log;
88
import android.view.Menu;
99
import android.view.MenuItem;
10+
import android.view.View;
1011

1112
import com.onesignal.OneSignal;
1213
import com.onesignal.OneSignal.NotificationOpenedHandler;
@@ -30,7 +31,7 @@ protected void onCreate(Bundle savedInstanceState) {
3031
currentActivity = this;
3132

3233
// Enable Logging below to debug issues. (LogCat level, Visual level);
33-
// OneSignal.setLogLevel(OneSignal.LOG_LEVEL.DEBUG, OneSignal.LOG_LEVEL.DEBUG);
34+
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.DEBUG, OneSignal.LOG_LEVEL.NONE);
3435

3536
// Pass in your app's Context, Google Project number, OneSignal App ID, and a NotificationOpenedHandler
3637
OneSignal.init(this, "703322744261", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba", new ExampleNotificationOpenedHandler());
@@ -54,6 +55,14 @@ public void idsAvailable(String userId, String registrationId) {
5455
});
5556
}
5657

58+
public void onSubscribeClicked(View v) {
59+
OneSignal.setSubscription(true);
60+
}
61+
62+
public void onUnsubscribeClicked(View v) {
63+
OneSignal.setSubscription(false);
64+
}
65+
5766
@Override
5867
protected void onPause() {
5968
super.onPause();

OneSignalSDK/app/src/main/res/layout/activity_main.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@
66
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
77

88
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
9-
android:layout_height="wrap_content" />
9+
android:layout_height="wrap_content"
10+
android:id="@+id/textView"/>
11+
12+
<Button
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:text="Subscribe"
16+
android:id="@+id/subscribe"
17+
android:layout_below="@+id/textView"
18+
android:layout_alignParentLeft="true"
19+
android:layout_alignParentStart="true"
20+
android:layout_marginTop="34dp"
21+
android:onClick="onSubscribeClicked"/>
22+
23+
<Button
24+
android:layout_width="wrap_content"
25+
android:layout_height="wrap_content"
26+
android:text="UnSubscribe"
27+
android:id="@+id/unsubscribe"
28+
android:layout_below="@+id/subscribe"
29+
android:layout_alignParentLeft="true"
30+
android:layout_alignParentStart="true"
31+
android:onClick="onUnsubscribeClicked"
32+
android:nestedScrollingEnabled="true"/>
1033

1134
</RelativeLayout>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.onesignal;
2+
3+
import android.content.Context;
4+
import android.util.Log;
5+
6+
import com.loopj.android.http.JsonHttpResponseHandler;
7+
import com.loopj.android.http.ResponseHandlerInterface;
8+
9+
import org.apache.http.entity.StringEntity;
10+
import org.json.JSONException;
11+
import org.json.JSONObject;
12+
import org.robolectric.annotation.Implementation;
13+
import org.robolectric.annotation.Implements;
14+
import org.robolectric.annotation.internal.Instrument;
15+
16+
import java.io.UnsupportedEncodingException;
17+
18+
@Implements(OneSignalRestClient.class)
19+
public class ShadowOneSignalRestClient {
20+
21+
public static JSONObject lastPost;
22+
public static Thread testThread;
23+
public static boolean failNext;
24+
25+
public static final String testUserId = "a2f7f967-e8cc-11e4-bed1-118f05be4511";
26+
27+
static void postSync(Context context, String url, JSONObject jsonBody, ResponseHandlerInterface responseHandler) throws UnsupportedEncodingException {
28+
Log.i("SHADOW_postSync", "url: " + url);
29+
lastPost = jsonBody;
30+
31+
if (failNext) {
32+
((JsonHttpResponseHandler)responseHandler).onFailure(400, null, new Exception(),new JSONObject());
33+
testThread.interrupt();
34+
return;
35+
}
36+
37+
String retJson = null;
38+
if (url.contains("on_session"))
39+
retJson = "{}";
40+
else
41+
retJson = "{\"id\": \"" + testUserId + "\"}";
42+
43+
try {
44+
((JsonHttpResponseHandler)responseHandler).onSuccess(200, null, new JSONObject(retJson));
45+
} catch (JSONException e) {
46+
e.printStackTrace();
47+
}
48+
49+
testThread.interrupt();
50+
}
51+
52+
static void putSync(Context context, String url, JSONObject jsonBody, ResponseHandlerInterface responseHandler) throws UnsupportedEncodingException {
53+
Log.i("SHADOW_putSync", "url: " + url);
54+
lastPost = jsonBody;
55+
56+
try {
57+
((JsonHttpResponseHandler)responseHandler).onSuccess(200, null, new JSONObject("{\"id\": \"" + testUserId + "\"}"));
58+
} catch (JSONException e) {
59+
e.printStackTrace();
60+
}
61+
62+
testThread.interrupt();
63+
}
64+
65+
static void put(final Context context, final String url, JSONObject jsonBody, final ResponseHandlerInterface responseHandler) throws UnsupportedEncodingException {
66+
Log.i("SHADOW_put", "url: " + url);
67+
68+
lastPost = jsonBody;
69+
70+
try {
71+
((JsonHttpResponseHandler)responseHandler).onSuccess(200, null, new JSONObject("{}"));
72+
} catch (JSONException e) {
73+
e.printStackTrace();
74+
}
75+
}
76+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.onesignal;
2+
3+
import android.content.Context;
4+
5+
import org.robolectric.annotation.Implements;
6+
7+
@Implements(PushRegistratorGPS.class)
8+
public class ShadowPushRegistratorGPS {
9+
10+
public static final String regId = "aspdfoh0fhj02hr-2h";
11+
12+
public void registerForPush(Context context, String googleProjectNumber, PushRegistrator.RegisteredHandler callback) {
13+
callback.complete(regId);
14+
}
15+
}

0 commit comments

Comments
 (0)