1 /*
2  * Copyright (C) 2023 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 android.graphics.drawable
18 
19 import android.graphics.Canvas
20 import android.graphics.ColorFilter
21 import android.graphics.PixelFormat
22 
23 /**
24  * Stub drawable that does nothing. It's to be used in tests as a mock drawable and checked for the
25  * same instance
26  */
27 class TestStubDrawable(private val name: String? = null) : Drawable() {
28 
drawnull29     override fun draw(canvas: Canvas) = Unit
30 
31     override fun setAlpha(alpha: Int) = Unit
32 
33     override fun setColorFilter(colorFilter: ColorFilter?) = Unit
34 
35     override fun getOpacity(): Int = PixelFormat.UNKNOWN
36 
37     override fun toString(): String {
38         return name ?: super.toString()
39     }
40 
getConstantStatenull41     override fun getConstantState(): ConstantState =
42         TestStubConstantState(this, changingConfigurations)
43 
44     override fun equals(other: Any?): Boolean {
45         return (other as? TestStubDrawable ?: return false).name == name
46     }
47 
48     private class TestStubConstantState(
49         private val drawable: Drawable,
50         private val changingConfigurations: Int,
51     ) : ConstantState() {
52 
newDrawablenull53         override fun newDrawable(): Drawable = drawable
54 
55         override fun getChangingConfigurations(): Int = changingConfigurations
56     }
57 }
58