ConfigDraftViewModelTest.kt (1996B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2026 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.merchantpos.config 18 19 import androidx.lifecycle.ViewModelProvider 20 import androidx.lifecycle.ViewModelStore 21 import androidx.lifecycle.ViewModelStoreOwner 22 23 import org.junit.Assert.assertEquals 24 import org.junit.Assert.assertNull 25 import org.junit.Assert.assertSame 26 import org.junit.Test 27 import org.junit.runner.RunWith 28 import org.robolectric.RobolectricTestRunner 29 import org.robolectric.annotation.Config 30 31 @RunWith(RobolectricTestRunner::class) 32 @Config(sdk = [28]) 33 class ConfigDraftViewModelTest 34 { 35 @Test 36 fun `activity scoped provider can retain credential draft in memory and clear it`() 37 { 38 val owner = 39 object : ViewModelStoreOwner 40 { 41 override val viewModelStore = ViewModelStore() 42 } 43 val firstProvider = ViewModelProvider(owner) 44 val draft = firstProvider[ConfigDraftViewModel::class.java] 45 draft.token = "in-memory-only" 46 draft.username = "sandbox" 47 48 assertSame(draft, ViewModelProvider(owner)[ConfigDraftViewModel::class.java]) 49 assertEquals("in-memory-only", draft.token) 50 assertEquals("sandbox", draft.username) 51 52 draft.clear() 53 assertNull(draft.token) 54 assertNull(draft.username) 55 owner.viewModelStore.clear() 56 } 57 }