Skip to content

Commit

Permalink
Merge pull request #2628 from kylecorry31/flashlight-ui-test
Browse files Browse the repository at this point in the history
Add flashlight tool tests
  • Loading branch information
kylecorry31 authored Nov 4, 2024
2 parents 7f18b97 + da95113 commit 4b1c77d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject2
import com.kylecorry.andromeda.permissions.Permissions
import com.kylecorry.andromeda.permissions.SpecialPermission
import com.kylecorry.andromeda.torch.TorchStateChangedTopic
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.main.MainActivity
import com.kylecorry.trail_sense.main.NotificationChannels
Expand Down Expand Up @@ -46,6 +47,9 @@ object TestUtils {
var inUseCameraIds: List<String> = emptyList()
private set

var isTorchOn: Boolean = false
private set

private var cameraAvailabilityCallback = object : CameraManager.AvailabilityCallback() {
override fun onCameraAvailable(cameraId: String) {
inUseCameraIds = inUseCameraIds.filter { it != cameraId }
Expand All @@ -56,6 +60,8 @@ object TestUtils {
}
}

private var torchTopic: TorchStateChangedTopic? = null

val context: Context
get() = InstrumentationRegistry.getInstrumentation().targetContext

Expand Down Expand Up @@ -111,6 +117,16 @@ object TestUtils {
manager?.unregisterAvailabilityCallback(cameraAvailabilityCallback)
}

fun listenForTorchUsage() {
torchTopic = TorchStateChangedTopic(context)
torchTopic?.subscribe(this::onTorchStateChanged)
}

fun stopListeningForTorchUsage() {
torchTopic?.unsubscribe(this::onTorchStateChanged)
torchTopic = null
}

fun not(action: () -> Unit) {
try {
action()
Expand Down Expand Up @@ -429,4 +445,9 @@ object TestUtils {
}
}
}

private fun onTorchStateChanged(isOn: Boolean): Boolean {
isTorchOn = isOn
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.kylecorry.trail_sense.test_utils
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.navigation.NavController
import androidx.test.core.app.ActivityScenario
import com.kylecorry.andromeda.core.tryOrNothing
import com.kylecorry.andromeda.torch.Torch
import com.kylecorry.trail_sense.main.MainActivity
import com.kylecorry.trail_sense.shared.extensions.findNavController
import com.kylecorry.trail_sense.test_utils.TestUtils.context
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.After
Expand Down Expand Up @@ -37,6 +40,7 @@ open class ToolTestBase(private val toolId: Long) {
TestUtils.setWaitForIdleTimeout()
TestUtils.setupApplication()
TestUtils.listenForCameraUsage()
TestUtils.listenForTorchUsage()
volume = TestUtils.mute()
scenario = TestUtils.startWithTool(toolId) {
navController = it.findNavController()
Expand All @@ -47,5 +51,11 @@ open class ToolTestBase(private val toolId: Long) {
fun tearDown() {
TestUtils.unmute(volume)
TestUtils.stopListeningForCameraUsage()
TestUtils.stopListeningForTorchUsage()
tryOrNothing {
if (TestUtils.isTorchOn) {
Torch(context).off()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.kylecorry.trail_sense.tools.flashlight

import com.kylecorry.andromeda.torch.Torch
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.click
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.doesNotHaveNotification
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.hasNotification
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.isFalse
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.isNotVisible
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.isTrue
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.isVisible
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.string
import com.kylecorry.trail_sense.test_utils.TestUtils
import com.kylecorry.trail_sense.test_utils.TestUtils.context
import com.kylecorry.trail_sense.test_utils.TestUtils.openQuickActions
import com.kylecorry.trail_sense.test_utils.ToolTestBase
import com.kylecorry.trail_sense.test_utils.views.quickAction
import com.kylecorry.trail_sense.tools.flashlight.infrastructure.FlashlightService
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Test

@HiltAndroidTest
class ToolFlashlightTest : ToolTestBase(Tools.FLASHLIGHT) {

@Test
fun verifyBasicFunctionality() {
canToggleFlashlight()
canToggleScreenFlashlight()
verifyQuickAction()
}

private fun canToggleFlashlight() {
if (!Torch.isAvailable(context)) {
return
}

click(R.id.flashlight_on_btn)
hasNotification(
FlashlightService.NOTIFICATION_ID,
title = string(R.string.flashlight_title)
)
isTrue { TestUtils.isTorchOn }

click(R.id.flashlight_on_btn)
doesNotHaveNotification(FlashlightService.NOTIFICATION_ID)
isFalse { TestUtils.isTorchOn }
}

private fun canToggleScreenFlashlight() {
click(R.id.screen_flashlight_btn)
isNotVisible(R.id.screen_flashlight_btn)

click(R.id.red_white_switcher)
click(R.id.red_white_switcher)

isVisible(R.id.brightness_seek)

click(R.id.off_btn)

isVisible(R.id.screen_flashlight_btn)
}

private fun verifyQuickAction() {
openQuickActions()

if (Torch.isAvailable(context)) {
click(quickAction(Tools.QUICK_ACTION_FLASHLIGHT))
hasNotification(
FlashlightService.NOTIFICATION_ID,
title = string(R.string.flashlight_title)
)
isTrue { TestUtils.isTorchOn }

click(quickAction(Tools.QUICK_ACTION_FLASHLIGHT))
doesNotHaveNotification(FlashlightService.NOTIFICATION_ID)
isFalse { TestUtils.isTorchOn }
}

click(quickAction(Tools.QUICK_ACTION_SCREEN_FLASHLIGHT))
isVisible(R.id.screen_flashlight)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.kylecorry.trail_sense.test_utils.AutomationLibrary.hasText
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.isChecked
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.isNotChecked
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.isVisible
import com.kylecorry.trail_sense.test_utils.TestUtils.context
import com.kylecorry.trail_sense.test_utils.TestUtils.mute
import com.kylecorry.trail_sense.test_utils.TestUtils.unmute
import com.kylecorry.trail_sense.test_utils.ToolTestBase
Expand All @@ -20,6 +21,10 @@ class ToolMetalDetectorTest : ToolTestBase(Tools.METAL_DETECTOR) {

@Test
fun verifyBasicFunctionality() {
if (!Tools.isToolAvailable(context, Tools.METAL_DETECTOR)) {
return
}

// Verify the title
hasText(R.id.metal_detector_title, Regex("\\d+ uT"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.kylecorry.trail_sense.test_utils.AutomationLibrary.not
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.optional
import com.kylecorry.trail_sense.test_utils.AutomationLibrary.string
import com.kylecorry.trail_sense.test_utils.TestUtils
import com.kylecorry.trail_sense.test_utils.TestUtils.context
import com.kylecorry.trail_sense.test_utils.ToolTestBase
import com.kylecorry.trail_sense.test_utils.views.quickAction
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools
Expand All @@ -23,6 +24,10 @@ class ToolWeatherTest : ToolTestBase(Tools.WEATHER) {

@Test
fun verifyBasicFunctionality() {
if (!Tools.isToolAvailable(context, Tools.WEATHER)) {
return
}

// Historic temperature disclaimer
clickOk()

Expand Down
2 changes: 1 addition & 1 deletion scripts/parse_test_screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def load_screenshot_base64():
with open(log_file, 'r') as f:
text = f.readlines()

lines = [line[line.index('Screenshot: ') + len('Screenshot: '):].strip() for line in text if line.strip()]
lines = [line[line.index('Screenshot: ') + len('Screenshot: '):].strip() for line in text if 'Screenshot: ' in line]
return ''.join(lines)

def convert_base64_to_image(base64_string):
Expand Down

0 comments on commit 4b1c77d

Please sign in to comment.