1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package androidx.lifecycle; 18 19import static org.hamcrest.CoreMatchers.is; 20import static org.hamcrest.CoreMatchers.nullValue; 21import static org.hamcrest.MatcherAssert.assertThat; 22 23import org.junit.Test; 24import org.junit.runner.RunWith; 25import org.junit.runners.JUnit4; 26 27@RunWith(JUnit4.class) 28public class ViewModelStoreTest { 29 30 @Test 31 public void testClear() { 32 ViewModelStore store = new ViewModelStore(); 33 TestViewModel viewModel1 = new TestViewModel(); 34 TestViewModel viewModel2 = new TestViewModel(); 35 store.put("a", viewModel1); 36 store.put("b", viewModel2); 37 assertThat(viewModel1.mCleared, is(false)); 38 assertThat(viewModel2.mCleared, is(false)); 39 store.clear(); 40 assertThat(viewModel1.mCleared, is(true)); 41 assertThat(viewModel2.mCleared, is(true)); 42 assertThat(store.get("a"), nullValue()); 43 assertThat(store.get("b"), nullValue()); 44 } 45 46 static class TestViewModel extends ViewModel { 47 boolean mCleared = false; 48 49 @Override 50 protected void onCleared() { 51 mCleared = true; 52 } 53 } 54} 55