Skip to content

Commit 99568ae

Browse files
committed
Move serialization v0 to a tests-only java file
1 parent 1d14eb1 commit 99568ae

File tree

4 files changed

+171
-49
lines changed

4 files changed

+171
-49
lines changed

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationUtilities.java

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -577,53 +577,4 @@ private static Notification.Builder recoverBuilderPreNougat(Context context, Not
577577

578578
return builder;
579579
}
580-
581-
// for testing purposes: copy-paste of what serialization was in version 0
582-
// except for hardcoded version 0
583-
private static boolean serializeNotificationCustom_v0(Notification notification, DataOutputStream out) {
584-
try {
585-
out.write(UNITY_MAGIC_NUMBER);
586-
out.writeInt(0); // NOTIFICATION_SERIALIZATION_VERSION
587-
588-
// serialize extras
589-
boolean showWhen = notification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN, false);
590-
byte[] extras = serializeParcelable(notification.extras);
591-
out.writeInt(extras == null ? 0 : extras.length);
592-
if (extras != null && extras.length > 0)
593-
out.write(extras);
594-
else {
595-
// parcelling may fail in case it contains binder object, when that happens serialize manually what we care about
596-
out.writeInt(notification.extras.getInt(KEY_ID));
597-
serializeString(out, notification.extras.getString(Notification.EXTRA_TITLE));
598-
serializeString(out, notification.extras.getString(Notification.EXTRA_TEXT));
599-
serializeString(out, notification.extras.getString(KEY_SMALL_ICON));
600-
serializeString(out, notification.extras.getString(KEY_LARGE_ICON));
601-
out.writeLong(notification.extras.getLong(KEY_FIRE_TIME, -1));
602-
out.writeLong(notification.extras.getLong(KEY_REPEAT_INTERVAL, -1));
603-
serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? null : notification.extras.getString(Notification.EXTRA_BIG_TEXT));
604-
out.writeBoolean(notification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER, false));
605-
out.writeBoolean(showWhen);
606-
serializeString(out, notification.extras.getString(KEY_INTENT_DATA));
607-
}
608-
609-
serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? null : notification.getChannelId());
610-
Integer color = UnityNotificationManager.getNotificationColor(notification);
611-
out.writeBoolean(color != null);
612-
if (color != null)
613-
out.writeInt(color);
614-
out.writeInt(notification.number);
615-
out.writeBoolean(0 != (notification.flags & Notification.FLAG_AUTO_CANCEL));
616-
serializeString(out, notification.getGroup());
617-
out.writeBoolean(0 != (notification.flags & Notification.FLAG_GROUP_SUMMARY));
618-
out.writeInt(UnityNotificationManager.getNotificationGroupAlertBehavior(notification));
619-
serializeString(out, notification.getSortKey());
620-
if (showWhen)
621-
out.writeLong(notification.when);
622-
623-
return true;
624-
} catch (Exception e) {
625-
Log.e(TAG_UNITY, "Failed to serialize notification", e);
626-
return false;
627-
}
628-
}
629580
}

com.unity.mobile.notifications/Tests/Runtime/Android/AndroidNotificationSimpleTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,43 @@
33
using UnityEngine;
44
using UnityEngine.TestTools;
55
using Unity.Notifications.Android;
6+
#if UNITY_EDITOR
7+
using UnityEditor;
8+
#endif
69

710
class AndroidNotificationSimpleTests
11+
: IPrebuildSetup, IPostBuildCleanup
812
{
913
const string kChannelId = "SerializeDeserializeNotificationChannel";
1014

15+
#if UNITY_EDITOR
16+
PluginImporter GetTestUtils()
17+
{
18+
var assets = AssetDatabase.FindAssets("UnityNotificationTestUtils");
19+
if (assets.Length != 1)
20+
throw new Exception("Something is wrong");
21+
return (PluginImporter)AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(assets[0]));
22+
}
23+
#endif
24+
25+
public void Setup()
26+
{
27+
#if UNITY_EDITOR
28+
var testUtils = GetTestUtils();
29+
testUtils.SetCompatibleWithPlatform(BuildTarget.Android, true);
30+
testUtils.SaveAndReimport();
31+
#endif
32+
}
33+
34+
public void Cleanup()
35+
{
36+
#if UNITY_EDITOR
37+
var testUtils = GetTestUtils();
38+
testUtils.SetCompatibleWithPlatform(BuildTarget.Android, false);
39+
testUtils.SaveAndReimport();
40+
#endif
41+
}
42+
1143
[OneTimeSetUp]
1244
public void BeforeAllTests()
1345
{
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.unity.androidnotifications;
2+
3+
import java.io.DataOutputStream;
4+
import android.app.Notification;
5+
import android.os.Build;
6+
import android.util.Log;
7+
import static com.unity.androidnotifications.UnityNotificationManager.KEY_ID;
8+
import static com.unity.androidnotifications.UnityNotificationManager.KEY_FIRE_TIME;
9+
import static com.unity.androidnotifications.UnityNotificationManager.KEY_REPEAT_INTERVAL;
10+
import static com.unity.androidnotifications.UnityNotificationManager.KEY_INTENT_DATA;
11+
import static com.unity.androidnotifications.UnityNotificationManager.KEY_SMALL_ICON;
12+
import static com.unity.androidnotifications.UnityNotificationManager.KEY_LARGE_ICON;
13+
import static com.unity.androidnotifications.UnityNotificationManager.TAG_UNITY;
14+
import static com.unity.androidnotifications.UnityNotificationUtilities.UNITY_MAGIC_NUMBER;
15+
import static com.unity.androidnotifications.UnityNotificationUtilities.serializeParcelable;
16+
import static com.unity.androidnotifications.UnityNotificationUtilities.serializeString;
17+
18+
// Java class for testing purposes, not included in regular build
19+
public class UnityNotificationTestUtils {
20+
// copy-paste of what serialization was in version 0
21+
// except for hardcoded version 0
22+
private static boolean serializeNotificationCustom_v0(Notification notification, DataOutputStream out) {
23+
try {
24+
out.write(UNITY_MAGIC_NUMBER);
25+
out.writeInt(0); // NOTIFICATION_SERIALIZATION_VERSION
26+
27+
// serialize extras
28+
boolean showWhen = notification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN, false);
29+
byte[] extras = serializeParcelable(notification.extras);
30+
out.writeInt(extras == null ? 0 : extras.length);
31+
if (extras != null && extras.length > 0)
32+
out.write(extras);
33+
else {
34+
// parcelling may fail in case it contains binder object, when that happens serialize manually what we care about
35+
out.writeInt(notification.extras.getInt(KEY_ID));
36+
serializeString(out, notification.extras.getString(Notification.EXTRA_TITLE));
37+
serializeString(out, notification.extras.getString(Notification.EXTRA_TEXT));
38+
serializeString(out, notification.extras.getString(KEY_SMALL_ICON));
39+
serializeString(out, notification.extras.getString(KEY_LARGE_ICON));
40+
out.writeLong(notification.extras.getLong(KEY_FIRE_TIME, -1));
41+
out.writeLong(notification.extras.getLong(KEY_REPEAT_INTERVAL, -1));
42+
serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? null : notification.extras.getString(Notification.EXTRA_BIG_TEXT));
43+
out.writeBoolean(notification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER, false));
44+
out.writeBoolean(showWhen);
45+
serializeString(out, notification.extras.getString(KEY_INTENT_DATA));
46+
}
47+
48+
serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? null : notification.getChannelId());
49+
Integer color = UnityNotificationManager.getNotificationColor(notification);
50+
out.writeBoolean(color != null);
51+
if (color != null)
52+
out.writeInt(color);
53+
out.writeInt(notification.number);
54+
out.writeBoolean(0 != (notification.flags & Notification.FLAG_AUTO_CANCEL));
55+
serializeString(out, notification.getGroup());
56+
out.writeBoolean(0 != (notification.flags & Notification.FLAG_GROUP_SUMMARY));
57+
out.writeInt(UnityNotificationManager.getNotificationGroupAlertBehavior(notification));
58+
serializeString(out, notification.getSortKey());
59+
if (showWhen)
60+
out.writeLong(notification.when);
61+
62+
return true;
63+
} catch (Exception e) {
64+
Log.e(TAG_UNITY, "Failed to serialize notification", e);
65+
return false;
66+
}
67+
}
68+
}

com.unity.mobile.notifications/Tests/Runtime/Android/UnityNotificationTestUtils.java.meta

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)