Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Feb 7, 2023
1 parent ba8c711 commit 4319ed5
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private void setThreads(final @NotNull SentryEvent event, final @NotNull Hint hi
}

// This should not be set by Hybrid SDKs since they have their own threading model
if (!isHybridSDK && thread.getMain() == null) {
if (!isHybridSDK && thread.isMain() == null) {
thread.setMain(isMainThread);
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ private void setAppExtras(final @NotNull App app, final @NotNull Hint hint) {

@SuppressWarnings({"ObsoleteSdkInt", "deprecation", "NewApi"})
private void setArchitectures(final @NotNull Device device) {
String[] supportedAbis;
final String[] supportedAbis;
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.LOLLIPOP) {
supportedAbis = Build.SUPPORTED_ABIS;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ final class LifecycleWatcher implements DefaultLifecycleObserver {
public void onStart(final @NotNull LifecycleOwner owner) {
startSession();
addAppBreadcrumb("foreground");
// TODO: check if this still makes sense, I found a few threads that the right way would be
// owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
// This would be the only state that is responding to the user

// Consider using owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
// in the future.
AppState.getInstance().setInBackground(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DefaultAndroidEventProcessorTest {
private class Fixture {
val buildInfo = mock<BuildInfoProvider>()
val options = SentryAndroidOptions().apply {
setDebug(true)
isDebug = true
setLogger(mock())
sdkVersion = SdkVersion("test", "1.2.3")
}
Expand All @@ -77,6 +77,7 @@ class DefaultAndroidEventProcessorTest {
@BeforeTest
fun `set up`() {
context = ApplicationProvider.getApplicationContext()
AppState.getInstance().resetInstance()
}

@Test
Expand Down Expand Up @@ -161,7 +162,7 @@ class DefaultAndroidEventProcessorTest {
}

@Test
fun `Current should be true if it comes from main thread`() {
fun `Current and Main should be true if it comes from main thread`() {
val sut = fixture.getSut(context)

val sentryThread = SentryThread().apply {
Expand All @@ -174,6 +175,7 @@ class DefaultAndroidEventProcessorTest {
assertNotNull(sut.process(event, Hint())) {
assertNotNull(it.threads) { threads ->
assertTrue(threads.first().isCurrent == true)
assertTrue(threads.first().isMain == true)
}
}
}
Expand All @@ -193,6 +195,7 @@ class DefaultAndroidEventProcessorTest {
assertNotNull(sut.process(event, Hint())) {
assertNotNull(it.threads) { threads ->
assertFalse(threads.first().isCurrent == true)
assertFalse(threads.first().isMain == true)
}
}
}
Expand Down Expand Up @@ -497,4 +500,28 @@ class DefaultAndroidEventProcessorTest {
assertEquals("en_US", device.locale)
}
}

@Test
fun `Event sets InForeground to true if not in the background`() {
val sut = fixture.getSut(context)

AppState.getInstance().setInBackground(false)

assertNotNull(sut.process(SentryEvent(), Hint())) {
val app = it.contexts.app!!
assertTrue(app.inForeground!!)
}
}

@Test
fun `Event sets InForeground to false if in the background`() {
val sut = fixture.getSut(context)

AppState.getInstance().setInBackground(true)

assertNotNull(sut.process(SentryEvent(), Hint())) {
val app = it.contexts.app!!
assertFalse(app.inForeground!!)
}
}
}
2 changes: 1 addition & 1 deletion sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -3339,7 +3339,6 @@ public final class io/sentry/protocol/SentryStackTrace$JsonKeys {
public final class io/sentry/protocol/SentryThread : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
public fun <init> ()V
public fun getId ()Ljava/lang/Long;
public fun getMain ()Ljava/lang/Boolean;
public fun getName ()Ljava/lang/String;
public fun getPriority ()Ljava/lang/Integer;
public fun getStacktrace ()Lio/sentry/protocol/SentryStackTrace;
Expand All @@ -3348,6 +3347,7 @@ public final class io/sentry/protocol/SentryThread : io/sentry/JsonSerializable,
public fun isCrashed ()Ljava/lang/Boolean;
public fun isCurrent ()Ljava/lang/Boolean;
public fun isDaemon ()Ljava/lang/Boolean;
public fun isMain ()Ljava/lang/Boolean;
public fun serialize (Lio/sentry/JsonObjectWriter;Lio/sentry/ILogger;)V
public fun setCrashed (Ljava/lang/Boolean;)V
public fun setCurrent (Ljava/lang/Boolean;)V
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/protocol/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public App() {}
this.buildType = app.buildType;
this.deviceAppHash = app.deviceAppHash;
this.permissions = CollectionUtils.newConcurrentHashMap(app.permissions);
this.unknown = CollectionUtils.newConcurrentHashMap(app.unknown);
this.inForeground = app.inForeground;
this.unknown = CollectionUtils.newConcurrentHashMap(app.unknown);
}

@SuppressWarnings("unused")
Expand Down
54 changes: 27 additions & 27 deletions sentry/src/main/java/io/sentry/protocol/SentryThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public final class SentryThread implements JsonUnknown, JsonSerializable {
private @Nullable Boolean crashed;
private @Nullable Boolean current;
private @Nullable Boolean daemon;
private @Nullable SentryStackTrace stacktrace;
private @Nullable Boolean main;
private @Nullable SentryStackTrace stacktrace;

@SuppressWarnings("unused")
private @Nullable Map<String, Object> unknown;
Expand Down Expand Up @@ -167,24 +167,6 @@ public void setDaemon(final @Nullable Boolean daemon) {
this.daemon = daemon;
}

/**
* Gets the state of the thread.
*
* @return the state of the thread.
*/
public @Nullable String getState() {
return state;
}

/**
* Sets the state of the thread.
*
* @param state the state of the thread.
*/
public void setState(final @Nullable String state) {
this.state = state;
}

/**
* If applicable, a flag indicating whether the thread was responsible for rendering the user
* interface. On mobile platforms this is oftentimes referred to as the "main thread" or "ui
Expand All @@ -193,7 +175,7 @@ public void setState(final @Nullable String state) {
* @return if its the main thread or not
*/
@Nullable
public Boolean getMain() {
public Boolean isMain() {
return main;
}

Expand All @@ -208,6 +190,24 @@ public void setMain(final @Nullable Boolean main) {
this.main = main;
}

/**
* Gets the state of the thread.
*
* @return the state of the thread.
*/
public @Nullable String getState() {
return state;
}

/**
* Sets the state of the thread.
*
* @param state the state of the thread.
*/
public void setState(final @Nullable String state) {
this.state = state;
}

// region json

@Nullable
Expand All @@ -229,8 +229,8 @@ public static final class JsonKeys {
public static final String CRASHED = "crashed";
public static final String CURRENT = "current";
public static final String DAEMON = "daemon";
public static final String STACKTRACE = "stacktrace";
public static final String MAIN = "main";
public static final String STACKTRACE = "stacktrace";
}

@Override
Expand Down Expand Up @@ -258,12 +258,12 @@ public void serialize(@NotNull JsonObjectWriter writer, @NotNull ILogger logger)
if (daemon != null) {
writer.name(JsonKeys.DAEMON).value(daemon);
}
if (stacktrace != null) {
writer.name(JsonKeys.STACKTRACE).value(logger, stacktrace);
}
if (main != null) {
writer.name(JsonKeys.MAIN).value(main);
}
if (stacktrace != null) {
writer.name(JsonKeys.STACKTRACE).value(logger, stacktrace);
}
if (unknown != null) {
for (String key : unknown.keySet()) {
Object value = unknown.get(key);
Expand Down Expand Up @@ -306,13 +306,13 @@ public static final class Deserializer implements JsonDeserializer<SentryThread>
case JsonKeys.DAEMON:
sentryThread.daemon = reader.nextBooleanOrNull();
break;
case JsonKeys.MAIN:
sentryThread.main = reader.nextBooleanOrNull();
break;
case JsonKeys.STACKTRACE:
sentryThread.stacktrace =
reader.nextOrNull(logger, new SentryStackTrace.Deserializer());
break;
case JsonKeys.MAIN:
sentryThread.main = reader.nextBooleanOrNull();
break;
default:
if (unknown == null) {
unknown = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AppSerializationTest {
"WRITE_EXTERNAL_STORAGE" to "not_granted",
"CAMERA" to "granted"
)
inForeground = true
}
}
private val fixture = Fixture()
Expand Down
7 changes: 5 additions & 2 deletions sentry/src/test/java/io/sentry/protocol/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class AppTest {
app.appVersion = "app version"
app.buildType = "build type"
app.deviceAppHash = "device app hash"
app.inForeground = true
val unknown = mapOf(Pair("unknown", "unknown"))
app.setUnknown(unknown)
app.unknown = unknown

val clone = App(app)

Expand All @@ -41,8 +42,9 @@ class AppTest {
app.appVersion = "app version"
app.buildType = "build type"
app.deviceAppHash = "device app hash"
app.inForeground = true
val unknown = mapOf(Pair("unknown", "unknown"))
app.setUnknown(unknown)
app.unknown = unknown

val clone = App(app)

Expand All @@ -55,6 +57,7 @@ class AppTest {
assertEquals("app version", clone.appVersion)
assertEquals("build type", clone.buildType)
assertEquals("device app hash", clone.deviceAppHash)
assertEquals(true, clone.inForeground)
assertNotNull(clone.unknown) {
assertEquals("unknown", it["unknown"])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SentryThreadSerializationTest {
isCrashed = false
isCurrent = false
isDaemon = true
isMain = true
stacktrace = SentryStackTrace().apply {
frames = listOf(
SentryStackFrame().apply {
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/test/resources/json/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
{
"WRITE_EXTERNAL_STORAGE": "not_granted",
"CAMERA": "granted"
}
},
"in_foreground": true
}
1 change: 1 addition & 0 deletions sentry/src/test/resources/json/sentry_thread.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"crashed": false,
"current": false,
"daemon": true,
"main": true,
"stacktrace":
{
"frames":
Expand Down

0 comments on commit 4319ed5

Please sign in to comment.