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 all 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
6 changes: 3 additions & 3 deletions sentinel/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<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" />
android:theme="@style/Sentinel.Theme"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,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,23 @@ 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.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 +46,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())
}
}
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 MarkdownStringBuilderTests {

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

private fun checkDeviceSpecificFields(markdown: String): String {
val fields = listOf(
"_screen_width_",
"_screen_height_",
"_screen_size_",
"_screen_density_",
"_font_scale_"
)

val fieldPatterns = fields.map { field ->
field to Regex("""$field:\s*.*""")
}

var updatedMarkdown = markdown

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

return updatedMarkdown
}

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

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 MarkdownStringBuilderTests {

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