1 /*
2  * Copyright (C) 2019 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.statusbar
18 
19 import com.google.common.truth.Truth.assertThat
20 
21 import android.graphics.Bitmap
22 import android.graphics.Canvas
23 import android.graphics.Color
24 import android.graphics.Point
25 import android.testing.AndroidTestingRunner
26 import androidx.test.filters.SmallTest
27 import com.android.systemui.SysuiTestCase
28 import org.junit.After
29 import org.junit.Before
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 private const val WIDTH = 200
34 private const val HEIGHT = 200
35 
36 @RunWith(AndroidTestingRunner::class)
37 @SmallTest
38 class MediaArtworkProcessorTest : SysuiTestCase() {
39 
40     private var screenWidth = 0
41     private var screenHeight = 0
42 
43     private lateinit var processor: MediaArtworkProcessor
44 
45     @Before
46     fun setUp() {
47         processor = MediaArtworkProcessor()
48 
49         val point = Point()
50         checkNotNull(context.display).getSize(point)
51         screenWidth = point.x
52         screenHeight = point.y
53     }
54 
55     @After
56     fun tearDown() {
57         processor.clearCache()
58     }
59 
60     @Test
61     fun testProcessArtwork() {
62         // GIVEN some "artwork", which is just a solid blue image
63         val artwork = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888)
64         Canvas(artwork).drawColor(Color.BLUE)
65         // WHEN the background is created from the artwork
66         val background = processor.processArtwork(context, artwork)!!
67         // THEN the background has the size of the screen that has been downsamples
68         assertThat(background.height).isLessThan(screenHeight)
69         assertThat(background.width).isLessThan(screenWidth)
70         assertThat(background.config).isEqualTo(Bitmap.Config.ARGB_8888)
71     }
72 
73     @Test
74     fun testCache() {
75         // GIVEN a solid blue image
76         val artwork = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888)
77         Canvas(artwork).drawColor(Color.BLUE)
78         // WHEN the background is processed twice
79         val background1 = processor.processArtwork(context, artwork)!!
80         val background2 = processor.processArtwork(context, artwork)!!
81         // THEN the two bitmaps are the same
82         // Note: This is currently broken and trying to use caching causes issues
83         assertThat(background1).isNotSameInstanceAs(background2)
84     }
85 
86     @Test
87     fun testConfig() {
88         // GIVEN some which is not ARGB_8888
89         val artwork = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ALPHA_8)
90         Canvas(artwork).drawColor(Color.BLUE)
91         // WHEN the background is created from the artwork
92         val background = processor.processArtwork(context, artwork)!!
93         // THEN the background has Config ARGB_8888
94         assertThat(background.config).isEqualTo(Bitmap.Config.ARGB_8888)
95     }
96 
97     @Test
98     fun testRecycledArtwork() {
99         // GIVEN some "artwork", which is just a solid blue image
100         val artwork = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888)
101         Canvas(artwork).drawColor(Color.BLUE)
102         // AND the artwork is recycled
103         artwork.recycle()
104         // WHEN the background is created from the artwork
105         val background = processor.processArtwork(context, artwork)
106         // THEN the processed bitmap is null
107         assertThat(background).isNull()
108     }
109 }
110