feat(steam): sync QAM brightness and volume with Android

This commit is contained in:
Kurt Himebauch committed 2026-10-26 12:32:36 -04:00
1 parent da127d1b4c
commit 6a5dfe1486
11 files changed
+575 -7

No files matched your search

@@ -0,0 +1,120 @@
package com.droiddeck.launcher.runtime
import android.app.Application
import android.content.Context
import android.media.AudioManager
import android.os.Looper
import android.provider.Settings
import java.io.File
import java.time.Duration
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.Implements
import org.robolectric.annotation.Implementation
import org.robolectric.shadows.ShadowSettings
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE, application = Application::class, shadows = [WriteSettingsShadow::class])
class DeviceControlsComponentTest {
@get:Rule val tmp = TemporaryFolder()
private val context get() = RuntimeEnvironment.getApplication()
private val audio get() = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
private lateinit var component: DeviceControlsComponent
private lateinit var directory: File
@Before fun start() {
WriteSettingsShadow.writable = true
Settings.System.putInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS, 128)
val root = tmp.newFolder()
directory = DeviceControlsComponent.directory(root)
component = DeviceControlsComponent(context, root)
component.publish()
component.start()
shadowOf(Looper.getMainLooper()).idle()
}
@After fun stop() {
component.stop()
shadowOf(Looper.getMainLooper()).idle()
}
private fun tick() = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(300))
private fun request(name: String, text: String) {
File(directory, "$name.request").writeText(text)
tick()
}
@Test fun androidChangesArePublishedAndRequestsUseDeviceVolumeSteps() {
request("volume", "999")
assertEquals(audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC), audio.getStreamVolume(AudioManager.STREAM_MUSIC))
audio.setStreamVolume(AudioManager.STREAM_MUSIC, 3, 0)
tick()
assertEquals("3", File(directory, "volume.state").readText().split(" ").first())
request("volume", "invalid")
assertEquals(3, audio.getStreamVolume(AudioManager.STREAM_MUSIC))
}
@Test fun deniedBrightnessIsReadBackAndAppliedOnlyAfterPermission() {
WriteSettingsShadow.writable = false
request("brightness", "0.75")
assertEquals(128, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
assertEquals(Settings.ACTION_MANAGE_WRITE_SETTINGS, shadowOf(context).nextStartedActivity.action)
assertFalse(JSONObject(File(directory, "state.json").readText()).getBoolean("brightnessWritable"))
WriteSettingsShadow.writable = true
tick()
assertEquals(192, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
}
@Test fun brightnessIsBoundedAndExternalChangesAreReadBack() {
for (invalid in listOf("NaN", "Infinity", "-0.1", "1.1")) request("brightness", invalid)
assertEquals(128, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
request("brightness", "0")
assertEquals(1, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
Settings.System.putInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS, 255)
tick()
assertEquals(1.0, JSONObject(File(directory, "state.json").readText()).getDouble("brightness"), 0.0001)
}
@Test fun stoppedSessionsDoNotConsumeNewCommands() {
component.stop()
shadowOf(Looper.getMainLooper()).idle()
request("brightness", "0.2")
assertEquals(128, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
}
@Test fun rejectedPermissionCanBeRequestedAgainWithoutOpeningRepeatedScreens() {
WriteSettingsShadow.writable = false
request("brightness", "0.75")
assertNotNull(shadowOf(context).nextStartedActivity)
request("brightness", "0.8")
assertNull(shadowOf(context).nextStartedActivity)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(5))
request("brightness", "0.8")
assertNotNull(shadowOf(context).nextStartedActivity)
}
@Test fun oversizedRequestsAreDiscardedAndDoNotBlockLaterCommands() {
request("brightness", "0".repeat(1000))
assertFalse(File(directory, "brightness.request.processing").exists())
request("brightness", "0.75")
assertEquals(192, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
}
}
@Implements(Settings.System::class)
class WriteSettingsShadow : ShadowSettings.ShadowSystem() {
companion object {
@JvmField var writable = true
@JvmStatic @Implementation fun canWrite(context: Context) = writable
}
}