@@ -36,6 +36,7 @@ import io.mockk.unmockkAll
3636import io.mockk.verify
3737import org.junit.After
3838import org.junit.Assert.assertEquals
39+ import org.junit.Assert.assertFalse
3940import org.junit.Assert.assertTrue
4041import org.junit.Before
4142import org.junit.Test
@@ -46,6 +47,7 @@ import java.io.File
4647@RunWith(AndroidJUnit4 ::class )
4748class ProfileManagerTest {
4849 private lateinit var context: Context
50+ private val appDataRoot: File by lazy { context.filesDir.parentFile!! }
4951
5052 private val prefs: SharedPreferences
5153 get() = context.getSharedPreferences(PROFILE_REGISTRY_FILENAME , Context .MODE_PRIVATE )
@@ -200,4 +202,104 @@ class ProfileManagerTest {
200202 assertEquals(1 , allProfiles.size)
201203 assertTrue(allProfiles.containsKey(ProfileId .DEFAULT ))
202204 }
205+
206+ @Test
207+ fun `deleteProfile removes profile from registry` () {
208+ val manager = ProfileManager .create(context)
209+ val profileId = manager.createNewProfile(" Temporary" )
210+
211+ manager.deleteProfile(profileId)
212+
213+ val allProfiles = manager.getAllProfiles()
214+ assertFalse(allProfiles.containsKey(profileId))
215+ }
216+
217+ @Test
218+ fun `deleteProfile does not remove other profiles` () {
219+ val manager = ProfileManager .create(context)
220+ val keep = manager.createNewProfile(" Keep" )
221+ val remove = manager.createNewProfile(" Remove" )
222+
223+ manager.deleteProfile(remove)
224+
225+ val allProfiles = manager.getAllProfiles()
226+ assertEquals(2 , allProfiles.size)
227+ assertTrue(allProfiles.containsKey(ProfileId .DEFAULT ))
228+ assertTrue(allProfiles.containsKey(keep))
229+ }
230+
231+ @Test
232+ fun `deleteProfile allows deleting default when not active` () {
233+ val manager = ProfileManager .create(context)
234+ val other = manager.createNewProfile(" Other" )
235+
236+ prefs.edit(commit = true ) {
237+ putString(KEY_LAST_ACTIVE_PROFILE_ID , other.value)
238+ }
239+
240+ val freshManager = ProfileManager .create(context)
241+ freshManager.deleteProfile(ProfileId .DEFAULT )
242+
243+ assertFalse(freshManager.getAllProfiles().containsKey(ProfileId .DEFAULT ))
244+ }
245+
246+ @Test(expected = IllegalArgumentException ::class )
247+ fun `deleteProfile throws when deleting active profile` () {
248+ val manager = ProfileManager .create(context)
249+ val profileId = manager.createNewProfile(" Active" )
250+
251+ prefs.edit(commit = true ) {
252+ putString(KEY_LAST_ACTIVE_PROFILE_ID , profileId.value)
253+ }
254+
255+ val freshManager = ProfileManager .create(context)
256+ freshManager.deleteProfile(profileId)
257+ }
258+
259+ @Test
260+ fun `deleteProfile for Default profile wipes legacy root folders but preserves Alice and Bob` () {
261+ val manager = ProfileManager .create(context)
262+
263+ val aliceId = manager.createNewProfile(" Alice" )
264+ val aliceDir = File (appDataRoot, aliceId.value).apply { mkdirs() }
265+ val alicePref =
266+ File (appDataRoot, " shared_prefs/profile_${aliceId.value} .xml" ).apply {
267+ parentFile?.mkdirs()
268+ createNewFile()
269+ }
270+
271+ val defaultFile =
272+ File (context.filesDir, " legacy_default.txt" ).apply {
273+ parentFile?.mkdirs()
274+ writeText(" Old data" )
275+ }
276+ val defaultWebview = File (appDataRoot, " app_webview" ).apply { mkdirs() }
277+ val defaultPref =
278+ File (appDataRoot, " shared_prefs/com.ichi2.anki_preferences.xml" ).apply {
279+ createNewFile()
280+ }
281+
282+ manager.switchActiveProfile(aliceId)
283+ val managerWithAlice = ProfileManager .create(context)
284+
285+ managerWithAlice.deleteProfile(ProfileId .DEFAULT )
286+
287+ // Default data should be gone
288+ assertFalse(" Default's filesDir content should be gone" , defaultFile.exists())
289+ assertFalse(" Default's webview folder should be gone" , defaultWebview.exists())
290+ assertFalse(" Default's specific SharedPreferences should be gone" , defaultPref.exists())
291+
292+ // Alice and Global Registry MUST remain
293+ assertTrue(" Alice's directory must not be touched" , aliceDir.exists())
294+ assertTrue(" Alice's SharedPreferences must not be touched" , alicePref.exists())
295+ assertTrue(
296+ " The Profile Registry file itself must not be deleted" ,
297+ File (appDataRoot, " shared_prefs/$PROFILE_REGISTRY_FILENAME .xml" ).exists(),
298+ )
299+
300+ assertFalse(
301+ " Registry should no longer contain Default" ,
302+ managerWithAlice.getAllProfiles().containsKey(ProfileId .DEFAULT ),
303+ )
304+ }
203305}
0 commit comments