-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RUM-5750 Use NO_EXPORT_FLAG for BroadcastReceiver on API above 26 #2170
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,16 @@ internal abstract class ThreadSafeReceiver : BroadcastReceiver() { | |
|
||
val isRegistered = AtomicBoolean(false) | ||
|
||
@SuppressLint("UnspecifiedRegisterReceiverFlag") | ||
// We suppress the warning here as this method is not available on all Android versions | ||
@SuppressLint("WrongConstant", "UnspecifiedRegisterReceiverFlag") | ||
fun registerReceiver( | ||
context: Context, | ||
filter: IntentFilter | ||
): Intent? { | ||
val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
context.registerReceiver(this, filter, Context.RECEIVER_NOT_EXPORTED) | ||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normally we should use ContextCompat here: |
||
context.registerReceiver(this, filter, RECEIVER_NOT_EXPORTED_COMPAT) | ||
} else { | ||
context.registerReceiver(this, filter) | ||
} | ||
|
@@ -37,4 +40,8 @@ internal abstract class ThreadSafeReceiver : BroadcastReceiver() { | |
context.unregisterReceiver(this) | ||
} | ||
} | ||
|
||
companion object { | ||
internal const val RECEIVER_NOT_EXPORTED_COMPAT: Int = 0x4 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,133 @@ | ||||||
/* | ||||||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||||||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||||||
* Copyright 2016-Present Datadog, Inc. | ||||||
*/ | ||||||
|
||||||
package com.datadog.android.core.internal.receiver | ||||||
|
||||||
import android.annotation.SuppressLint | ||||||
import android.content.Context | ||||||
import android.content.Intent | ||||||
import android.content.IntentFilter | ||||||
import android.os.Build | ||||||
import com.datadog.android.utils.forge.Configurator | ||||||
import com.datadog.tools.unit.annotations.TestTargetApi | ||||||
import com.datadog.tools.unit.extensions.ApiLevelExtension | ||||||
import fr.xgouchet.elmyr.junit5.ForgeConfiguration | ||||||
import fr.xgouchet.elmyr.junit5.ForgeExtension | ||||||
import org.assertj.core.api.Assertions.assertThat | ||||||
import org.junit.jupiter.api.BeforeEach | ||||||
import org.junit.jupiter.api.Test | ||||||
import org.junit.jupiter.api.extension.ExtendWith | ||||||
import org.junit.jupiter.api.extension.Extensions | ||||||
import org.mockito.Mock | ||||||
import org.mockito.junit.jupiter.MockitoExtension | ||||||
import org.mockito.junit.jupiter.MockitoSettings | ||||||
import org.mockito.kotlin.verify | ||||||
import org.mockito.kotlin.verifyNoInteractions | ||||||
import org.mockito.kotlin.verifyNoMoreInteractions | ||||||
import org.mockito.quality.Strictness | ||||||
|
||||||
@Extensions( | ||||||
ExtendWith(MockitoExtension::class), | ||||||
ExtendWith(ForgeExtension::class), | ||||||
ExtendWith(ApiLevelExtension::class) | ||||||
) | ||||||
@MockitoSettings(strictness = Strictness.LENIENT) | ||||||
@ForgeConfiguration(Configurator::class) | ||||||
internal class ThreadSafeReceiverTest { | ||||||
private lateinit var testedReceiver: ThreadSafeReceiver | ||||||
|
||||||
@Mock | ||||||
lateinit var mockContext: Context | ||||||
|
||||||
@Mock | ||||||
lateinit var mockIntentFilter: IntentFilter | ||||||
|
||||||
@BeforeEach | ||||||
fun `set up`() { | ||||||
testedReceiver = TestableThreadSafeReceiver() | ||||||
} | ||||||
|
||||||
// region registerReceiver | ||||||
|
||||||
@TestTargetApi(Build.VERSION_CODES.O) | ||||||
@Test | ||||||
fun `M use the no export flag W registerReceiver { API version above 26}`() { | ||||||
// When | ||||||
testedReceiver.registerReceiver(mockContext, mockIntentFilter) | ||||||
|
||||||
// Then | ||||||
verify(mockContext).registerReceiver( | ||||||
testedReceiver, | ||||||
mockIntentFilter, | ||||||
ThreadSafeReceiver.RECEIVER_NOT_EXPORTED_COMPAT | ||||||
) | ||||||
assertThat(this.testedReceiver.isRegistered.get()).isTrue() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor:
Suggested change
|
||||||
} | ||||||
|
||||||
@TestTargetApi(Build.VERSION_CODES.TIRAMISU) | ||||||
@Test | ||||||
fun `M use the no export flag W registerReceiver { API version above 33}`() { | ||||||
// When | ||||||
testedReceiver.registerReceiver(mockContext, mockIntentFilter) | ||||||
|
||||||
// Then | ||||||
verify(mockContext).registerReceiver( | ||||||
testedReceiver, | ||||||
mockIntentFilter, | ||||||
Context.RECEIVER_NOT_EXPORTED | ||||||
) | ||||||
assertThat(this.testedReceiver.isRegistered.get()).isTrue() | ||||||
} | ||||||
|
||||||
@SuppressLint("UnspecifiedRegisterReceiverFlag") | ||||||
@TestTargetApi(Build.VERSION_CODES.N) | ||||||
@Test | ||||||
fun `M not use the no export flag W registerReceiver { API version below 26}`() { | ||||||
// When | ||||||
testedReceiver.registerReceiver(mockContext, mockIntentFilter) | ||||||
|
||||||
// Then | ||||||
verify(mockContext).registerReceiver(testedReceiver, mockIntentFilter) | ||||||
verifyNoMoreInteractions(mockContext) | ||||||
assertThat(this.testedReceiver.isRegistered.get()).isTrue() | ||||||
} | ||||||
|
||||||
// endregion | ||||||
|
||||||
// region unregisterReceiver | ||||||
|
||||||
@Test | ||||||
fun `M unregister the receiver W unregisterReceiver { registered }`() { | ||||||
// Given | ||||||
testedReceiver.isRegistered.set(true) | ||||||
|
||||||
// When | ||||||
testedReceiver.unregisterReceiver(mockContext) | ||||||
|
||||||
// Then | ||||||
verify(mockContext).unregisterReceiver(testedReceiver) | ||||||
assertThat(this.testedReceiver.isRegistered.get()).isFalse() | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun `M do nothing W unregisterReceiver { not registered }`() { | ||||||
// Given | ||||||
testedReceiver.isRegistered.set(false) | ||||||
|
||||||
// When | ||||||
testedReceiver.unregisterReceiver(mockContext) | ||||||
|
||||||
// Then | ||||||
verifyNoInteractions(mockContext) | ||||||
assertThat(this.testedReceiver.isRegistered.get()).isFalse() | ||||||
} | ||||||
|
||||||
// endregion | ||||||
} | ||||||
|
||||||
internal class TestableThreadSafeReceiver : ThreadSafeReceiver() { | ||||||
override fun onReceive(context: Context?, intent: Intent?) {} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following on from the dev chat, if we have to suppress perhaps we should document the reason in a comment