Skip to content
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

Fix for running tests #65

Merged
merged 20 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions sentinel/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
<uses-sdk tools:overrideLibrary="androidx.security"/>

<application
android:name=".ui.SentinelTestApplication"
android:name="com.infinum.sentinel.ui.SentinelTestApplication"
android:label="@string/sentinel_name">
<activity
android:name=".ui.main.SentinelActivity"
android:name="com.infinum.sentinel.ui.main.SentinelActivity"
android:label="@string/sentinel_name"
android:theme="@style/Sentinel.Theme" />

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">

<meta-data
android:name="androidx.work.WorkManagerInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?
We removed need for this in last release

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember if there were any conflicts, but it is possible it is from that. I will re-check this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see now. This is from initial commit to this branch by Neven when we were figuring out what is wrong with the tests. I will check if this can be removed.

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import androidx.test.espresso.intent.Intents.times
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.work.Configuration
import androidx.work.WorkManager
import com.infinum.sentinel.ui.SentinelTestApplication
import com.infinum.sentinel.ui.main.SentinelActivity
import com.infinum.sentinel.ui.tools.DummyTool
Expand All @@ -32,6 +34,10 @@ internal class SentinelTests {
@JvmStatic
fun setupBeforeClass() {
context = ApplicationProvider.getApplicationContext<SentinelTestApplication>()
WorkManager.initialize(
context,
Configuration.Builder().build()
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only for Tests. WorkManager is missing and SentinelTests couldn't be run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing meta-data for WorkManager this can also be removed.

}
}

Expand Down Expand Up @@ -79,17 +85,16 @@ internal class SentinelTests {
fun sentinel_show() {
Sentinel.show()

intended(hasComponent(ComponentName(context, SentinelActivity::class.java)), times(2))
intended(hasComponent(ComponentName(context, SentinelActivity::class.java)), times(1))
}

// @Test
// @SmallTest
// @RequiresDevice
// fun sentinel_showDevice() {
// val instance = Sentinel.watch(context)
//
// instance.show()
//
// intended(hasComponent(ComponentName(context, SentinelActivity::class.java)), times(1))
// }
@Test
@SmallTest
fun sentinel_showDevice() {
val instance = Sentinel.watch()

instance.show()

intended(hasComponent(ComponentName(context, SentinelActivity::class.java)), times(1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith

@Ignore("This test is ignored because it's failing on CI")
@RunWith(AndroidJUnit4::class)
internal class ApplicationCollectorTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import com.infinum.sentinel.ui.SentinelTestApplication
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.util.ReflectionHelpers

@Ignore("This test is ignored because it's failing on CI")
@RunWith(AndroidJUnit4::class)
internal class DeviceCollectorDeviceTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import com.infinum.sentinel.ui.SentinelTestApplication
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.util.ReflectionHelpers

@Ignore("This test is ignored because it's failing on CI")
@RunWith(AndroidJUnit4::class)
internal class DeviceCollectorEmulatorTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@ import com.infinum.sentinel.ui.SentinelTestApplication
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith

@Ignore("This test is ignored because it's failing on CI")
@RunWith(AndroidJUnit4::class)
internal class PermissionsCollectorTests {

companion object {

private val APPENDED_PERMISSIONS = mapOf(Manifest.permission.REORDER_TASKS to true)
private val APPENDED_PERMISSIONS = mapOf(
Manifest.permission.REORDER_TASKS to true,
Manifest.permission.POST_NOTIFICATIONS to false,
Manifest.permission.POST_NOTIFICATIONS to false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POST_NOTIFICATIONS twice

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still twice...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed, finally πŸ˜„

Manifest.permission.WAKE_LOCK to true,
Manifest.permission.ACCESS_NETWORK_STATE to true,
Manifest.permission.RECEIVE_BOOT_COMPLETED to true,
Manifest.permission.FOREGROUND_SERVICE to true,
"com.infinum.sentinel.test.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION" to true
)

lateinit var actualPermissions: Map<String, Boolean>

Expand All @@ -40,13 +47,15 @@ internal class PermissionsCollectorTests {
fun permissions_notDeclared() {
assertTrue(actualPermissions.isNotEmpty())
assertEquals(APPENDED_PERMISSIONS.size, actualPermissions.size)
assertEquals(
APPENDED_PERMISSIONS.containsKey(Manifest.permission.REORDER_TASKS),
actualPermissions.containsKey(Manifest.permission.REORDER_TASKS)
)
assertEquals(
APPENDED_PERMISSIONS[Manifest.permission.REORDER_TASKS],
actualPermissions[Manifest.permission.REORDER_TASKS]
)
APPENDED_PERMISSIONS.forEach { (permission, expectedStatus) ->
assertEquals(
APPENDED_PERMISSIONS.containsKey(permission),
actualPermissions.containsKey(permission)
)
assertEquals(
expectedStatus,
actualPermissions[permission]
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config

@Ignore("This test is ignored because it's failing on CI")
@RunWith(AndroidJUnit4::class)
internal class PreferencesCollectorTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package com.infinum.sentinel.data.sources.raw.collectors

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.infinum.sentinel.ui.tools.AppInfoTool
import com.infinum.sentinel.Sentinel
import com.infinum.sentinel.ui.tools.DummyTool
import com.infinum.sentinel.ui.tools.NoNameTool
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith

@Ignore("This test is ignored because it's failing on CI")
@RunWith(AndroidJUnit4::class)
internal class ToolsCollectorTests {

Expand All @@ -33,20 +31,20 @@ internal class ToolsCollectorTests {
@SmallTest
fun tools_AreEmpty() {
val collector = ToolsCollector(setOf())
val expectedTools = setOf(AppInfoTool())
val expectedTools = setOf<Sentinel.Tool>()

val actualTools = collector()

assertEquals(expectedTools.size, actualTools.size)
assertTrue(actualTools.isNotEmpty())
assertTrue(actualTools.isEmpty())
assertEquals(expectedTools, actualTools)
}

@Test
@SmallTest
fun tools_AreUnique() {
val collector = ToolsCollector(setOf(dummyTool, dummyTool))
val expectedTools = setOf(dummyTool, AppInfoTool())
val expectedTools = setOf(dummyTool)

val actualTools = collector()

Expand All @@ -59,7 +57,7 @@ internal class ToolsCollectorTests {
@SmallTest
fun tools_AreValid() {
val collector = ToolsCollector(setOf(dummyTool, noNameTool))
val expectedTools = setOf(dummyTool, AppInfoTool())
val expectedTools = setOf(dummyTool)

val actualTools = collector()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.util.ReflectionHelpers

@Ignore("This test is ignored because it's failing on CI")
@RunWith(AndroidJUnit4::class)
internal class HtmlStringBuilderTests {

Expand Down Expand Up @@ -123,6 +121,33 @@ internal class HtmlStringBuilderTests {
?.use { it.readText() }
.orEmpty()

private fun checkDeviceSpecificFields(html: String): String {
val fields = listOf(
"screen_width",
"screen_height",
"screen_size",
"screen_density",
"font_scale"
)

val fieldPatterns = fields.map { field ->
field to Regex("""<div>$field:\s*[^<]*</div>""")
}

var updatedHtml = html

fieldPatterns.forEach { (field, pattern) ->
val matchResult = pattern.find(updatedHtml)
if (matchResult == null) {
throw AssertionError("Field $field is missing in the device object")
} else {
updatedHtml = updatedHtml.replace(matchResult.value, """<div>$field: </div>""")
}
}

return updatedHtml
}

Comment on lines +124 to +150
Copy link
Contributor Author

@AsimRibo AsimRibo Aug 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it comes to StringBuilder tests I didn't find an elegant way to mock these fields like it is the case with ReflectionHelpers. So I went with this approach because I think it is only important to assert that field is there.

When I get more familiar with Roboelectric, I will return to this, unless you have suggestions.

@Before
fun preferences_deleteDir() {
val prefsDirectory =
Expand All @@ -144,7 +169,8 @@ internal class HtmlStringBuilderTests {

assertNotNull(actualData)
assertTrue(actualData.isNotBlank())
assertEquals(EXPECTED_DATA_NO_PREFERENCES, actualData)
val cleanedUpData = checkDeviceSpecificFields(actualData)
assertEquals(EXPECTED_DATA_NO_PREFERENCES, cleanedUpData)
}

@Test
Expand All @@ -167,6 +193,7 @@ internal class HtmlStringBuilderTests {

assertNotNull(actualData)
assertTrue(actualData.isNotBlank())
assertEquals(EXPECTED_DATA, actualData)
val cleanedUpData = checkDeviceSpecificFields(actualData)
assertEquals(EXPECTED_DATA, cleanedUpData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ import com.infinum.sentinel.data.sources.raw.collectors.PreferencesCollector
import com.infinum.sentinel.domain.collectors.Collectors
import com.infinum.sentinel.ui.SentinelTestApplication
import java.io.File
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.util.ReflectionHelpers

@Ignore("This test is ignored because it's failing on CI")
@RunWith(AndroidJUnit4::class)
internal class JsonStringBuilderTests {

Expand Down Expand Up @@ -136,6 +135,27 @@ internal class JsonStringBuilderTests {
assertTrue(success)
}

private fun checkDeviceSpecificFields(json: JSONObject): JSONObject {
val device = json.optJSONObject("device") ?: throw AssertionError("Device object is missing")

val fields = listOf(
"screen_width",
"screen_height",
"screen_size",
"screen_density",
"font_scale"
)

fields.forEach { field ->
if (!device.has(field)) {
throw AssertionError("Field $field is missing in the device object")
}
device.put(field, "")
}

return json
}

@Test
@SmallTest
fun formatter_hasDataWithoutPreferences() {
Expand All @@ -147,7 +167,8 @@ internal class JsonStringBuilderTests {

assertNotNull(actualData)
assertTrue(actualData.isNotBlank())
assertEquals(EXPECTED_DATA_NO_PREFERENCES, actualData)
val cleanedUpData = checkDeviceSpecificFields(JSONObject(actualData))
assertEquals(EXPECTED_DATA_NO_PREFERENCES, cleanedUpData.toString())
}

@Test
Expand All @@ -171,6 +192,8 @@ internal class JsonStringBuilderTests {

assertNotNull(actualData)
assertTrue(actualData.isNotBlank())
assertEquals(EXPECTED_DATA, actualData)
assertTrue(actualData.isNotBlank())
val cleanedUpData = checkDeviceSpecificFields(JSONObject(actualData))
assertEquals(EXPECTED_DATA, cleanedUpData.toString())
}
}
Loading
Loading