package com.doublesymmetry.trackplayer import org.junit.Test import org.junit.Assert.* import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment import org.json.JSONObject @RunWith(RobolectricTestRunner::class) class SleepTimerStateTest { @Test fun `time mode persists correctly to SharedPreferences`() { val context = RuntimeEnvironment.getApplication() val prefs = context.getSharedPreferences("TrackPlayerPrefs", 0) val json = JSONObject().apply { put("type", "time") put("remainingSeconds", 120.0) put("fadeOutSeconds", 30.0) } prefs.edit().putString("sleep_timer_state", json.toString()).apply() val parsed = JSONObject(prefs.getString("sleep_timer_state", null)!!) assertEquals("time", parsed.getString("type")) assertEquals(120.0, parsed.getDouble("remainingSeconds"), 0.01) assertEquals(30.0, parsed.getDouble("fadeOutSeconds"), 0.01) } @Test fun `mediaItem mode persists correctly`() { val context = RuntimeEnvironment.getApplication() val prefs = context.getSharedPreferences("TrackPlayerPrefs", 0) val json = JSONObject().apply { put("type", "mediaItem") put("index", 5) } prefs.edit().putString("sleep_timer_state", json.toString()).apply() val parsed = JSONObject(prefs.getString("sleep_timer_state", null)!!) assertEquals("mediaItem", parsed.getString("type")) assertEquals(5, parsed.getInt("index")) } @Test fun `cancel removes state from SharedPreferences`() { val context = RuntimeEnvironment.getApplication() val prefs = context.getSharedPreferences("TrackPlayerPrefs", 0) prefs.edit().putString("sleep_timer_state", """{"type":"time"}""").apply() assertNotNull(prefs.getString("sleep_timer_state", null)) prefs.edit().remove("sleep_timer_state").apply() assertNull(prefs.getString("sleep_timer_state", null)) } }