• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.launcher3.util
18 
19 import android.view.View
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import androidx.test.platform.app.InstrumentationRegistry
23 import com.android.launcher3.R
24 import com.android.launcher3.util.ViewCache.CacheEntry
25 import com.google.common.truth.Truth.assertThat
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 
30 @SmallTest
31 @RunWith(AndroidJUnit4::class)
32 class ViewCacheTest {
33 
34     private lateinit var underTest: ViewCache
35 
36     private val context = InstrumentationRegistry.getInstrumentation().context
37     private val layoutId =
<lambda>null38         context.run { resources.getIdentifier("test_layout_appwidget_blue", "layout", packageName) }
39 
40     @Before
setUpnull41     fun setUp() {
42         underTest = ViewCache()
43         underTest.setCacheSize(layoutId, 5)
44     }
45 
46     @Test
get_view_from_empty_cachenull47     fun get_view_from_empty_cache() {
48         val view: View = underTest.getView(layoutId, context, null)
49 
50         val cacheEntry = view.getTag(R.id.cache_entry_tag_id) as ViewCache.CacheEntry
51         assertThat(cacheEntry).isNotNull()
52         assertThat(cacheEntry.mMaxSize).isEqualTo(5)
53         assertThat(cacheEntry.mCurrentSize).isEqualTo(0)
54         assertThat(cacheEntry.mViews[0]).isNull()
55     }
56 
57     @Test
recyclerViewnull58     fun recyclerView() {
59         val view: View = underTest.getView(layoutId, context, null)
60         val cacheEntry = view.getTag(R.id.cache_entry_tag_id) as ViewCache.CacheEntry
61 
62         underTest.recycleView(layoutId, view)
63 
64         assertThat(cacheEntry.mMaxSize).isEqualTo(5)
65         assertThat(cacheEntry.mCurrentSize).isEqualTo(1)
66         assertThat(cacheEntry.mViews[0]).isSameInstanceAs(view)
67     }
68 
69     @Test
get_view_from_cachenull70     fun get_view_from_cache() {
71         val view: View = underTest.getView(layoutId, context, null)
72         underTest.recycleView(layoutId, view)
73 
74         val newView = underTest.getView<View>(layoutId, context, null)
75 
76         assertThat(view).isSameInstanceAs(newView)
77     }
78 
79     @Test
change_tag_id_recyclerView_noOpnull80     fun change_tag_id_recyclerView_noOp() {
81         val view: View = underTest.getView(layoutId, context, null)
82         val cacheEntry = view.getTag(R.id.cache_entry_tag_id) as ViewCache.CacheEntry
83 
84         view.setTag(R.id.cache_entry_tag_id, CacheEntry(3))
85         underTest.recycleView(layoutId, view)
86 
87         assertThat(cacheEntry.mMaxSize).isEqualTo(5)
88         assertThat(cacheEntry.mCurrentSize).isEqualTo(0)
89         assertThat(cacheEntry.mViews[0]).isNull()
90     }
91 }
92