1 /* 2 * Copyright (C) 2020 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 17 package com.android.systemui.controls.controller 18 19 import android.content.ComponentName 20 import android.testing.AndroidTestingRunner 21 import androidx.test.filters.SmallTest 22 import com.android.systemui.SysuiTestCase 23 import org.junit.Assert.assertEquals 24 import org.junit.Assert.assertTrue 25 import org.junit.Before 26 import org.junit.Test 27 import org.junit.runner.RunWith 28 import org.mockito.ArgumentMatchers 29 import org.mockito.Mock 30 import org.mockito.Mockito 31 import org.mockito.Mockito.`when` 32 import org.mockito.Mockito.inOrder 33 import org.mockito.Mockito.mock 34 import org.mockito.Mockito.never 35 import org.mockito.Mockito.verify 36 import org.mockito.MockitoAnnotations 37 import java.io.File 38 39 @SmallTest 40 @RunWith(AndroidTestingRunner::class) 41 class AuxiliaryPersistenceWrapperTest : SysuiTestCase() { 42 43 companion object { 44 fun <T> any(): T = Mockito.any() 45 private val TEST_COMPONENT = ComponentName.unflattenFromString("test_pkg/.test_cls")!! 46 private val TEST_COMPONENT_OTHER = 47 ComponentName.unflattenFromString("test_pkg/.test_other")!! 48 } 49 50 @Mock 51 private lateinit var persistenceWrapper: ControlsFavoritePersistenceWrapper 52 @Mock 53 private lateinit var structure1: StructureInfo 54 @Mock 55 private lateinit var structure2: StructureInfo 56 @Mock 57 private lateinit var structure3: StructureInfo 58 59 private lateinit var auxiliaryFileWrapper: AuxiliaryPersistenceWrapper 60 61 @Before 62 fun setUp() { 63 MockitoAnnotations.initMocks(this) 64 65 `when`(structure1.componentName).thenReturn(TEST_COMPONENT) 66 `when`(structure2.componentName).thenReturn(TEST_COMPONENT_OTHER) 67 `when`(structure3.componentName).thenReturn(TEST_COMPONENT) 68 69 `when`(persistenceWrapper.fileExists).thenReturn(true) 70 `when`(persistenceWrapper.readFavorites()).thenReturn( 71 listOf(structure1, structure2, structure3)) 72 73 auxiliaryFileWrapper = AuxiliaryPersistenceWrapper(persistenceWrapper) 74 } 75 76 @Test 77 fun testInitialStructures() { 78 val expected = listOf(structure1, structure2, structure3) 79 assertEquals(expected, auxiliaryFileWrapper.favorites) 80 } 81 82 @Test 83 fun testInitialize_fileDoesNotExist() { 84 `when`(persistenceWrapper.fileExists).thenReturn(false) 85 auxiliaryFileWrapper.initialize() 86 assertTrue(auxiliaryFileWrapper.favorites.isEmpty()) 87 } 88 89 @Test 90 fun testGetCachedValues_component() { 91 val cached = auxiliaryFileWrapper.getCachedFavoritesAndRemoveFor(TEST_COMPONENT) 92 val expected = listOf(structure1, structure3) 93 94 assertEquals(expected, cached) 95 } 96 97 @Test 98 fun testGetCachedValues_componentOther() { 99 val cached = auxiliaryFileWrapper.getCachedFavoritesAndRemoveFor(TEST_COMPONENT_OTHER) 100 val expected = listOf(structure2) 101 102 assertEquals(expected, cached) 103 } 104 105 @Test 106 fun testGetCachedValues_component_removed() { 107 auxiliaryFileWrapper.getCachedFavoritesAndRemoveFor(TEST_COMPONENT) 108 verify(persistenceWrapper).storeFavorites(listOf(structure2)) 109 } 110 111 @Test 112 fun testChangeFile() { 113 auxiliaryFileWrapper.changeFile(mock(File::class.java)) 114 val inOrder = inOrder(persistenceWrapper) 115 inOrder.verify(persistenceWrapper).changeFileAndBackupManager( 116 any(), ArgumentMatchers.isNull()) 117 inOrder.verify(persistenceWrapper).readFavorites() 118 } 119 120 @Test 121 fun testFileRemoved() { 122 `when`(persistenceWrapper.fileExists).thenReturn(false) 123 124 assertEquals(emptyList<StructureInfo>(), 125 auxiliaryFileWrapper.getCachedFavoritesAndRemoveFor(TEST_COMPONENT)) 126 assertEquals(emptyList<StructureInfo>(), 127 auxiliaryFileWrapper.getCachedFavoritesAndRemoveFor(TEST_COMPONENT_OTHER)) 128 129 verify(persistenceWrapper, never()).storeFavorites(ArgumentMatchers.anyList()) 130 } 131 }