1 /*
2  * Copyright (C) 2021 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.settings.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.graphics.drawable.Drawable;
23 
24 import androidx.test.core.app.ApplicationProvider;
25 
26 import com.android.settings.R;
27 import com.android.settings.testutils.ImageTestUtils;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.RobolectricTestRunner;
32 
33 /** Tests for {@link AccessibilityLayerDrawable}. */
34 @RunWith(RobolectricTestRunner.class)
35 public class AccessibilityLayerDrawableTest {
36 
37     private static final int TEST_RES_ID =
38             com.android.internal.R.drawable.ic_accessibility_magnification;
39     private static final int TEST_RES_ID_2 =
40             com.android.internal.R.drawable.ic_accessibility_color_inversion;
41     private final Context mContext = ApplicationProvider.getApplicationContext();
42 
43     @Test
createLayerDrawable_configCorrect()44     public void createLayerDrawable_configCorrect() {
45         final Drawable expected1stDrawable = mContext.getDrawable(
46                 R.drawable.accessibility_button_preview_base);
47         final Drawable expected2ndDrawable = mContext.getDrawable(TEST_RES_ID);
48 
49         final AccessibilityLayerDrawable actualDrawable =
50                 AccessibilityLayerDrawable.createLayerDrawable(mContext, TEST_RES_ID,
51                         /* opacity= */ 27);
52 
53         final Drawable actual1stDrawable = actualDrawable.getDrawable(0);
54         final Drawable actual2ndDrawable = actualDrawable.getDrawable(1);
55         assertThat(ImageTestUtils.drawableToBitmap(actual1stDrawable).sameAs(
56                 ImageTestUtils.drawableToBitmap(expected1stDrawable))).isTrue();
57         assertThat(ImageTestUtils.drawableToBitmap(actual2ndDrawable).sameAs(
58                 ImageTestUtils.drawableToBitmap(expected2ndDrawable))).isTrue();
59     }
60 
61     @Test
updateLayerDrawable_expectedFloatingMenuLayerDrawableState()62     public void updateLayerDrawable_expectedFloatingMenuLayerDrawableState() {
63         final AccessibilityLayerDrawable originalDrawable =
64                 AccessibilityLayerDrawable.createLayerDrawable(mContext, TEST_RES_ID, /* opacity= */
65                         72);
66 
67         originalDrawable.updateLayerDrawable(mContext, TEST_RES_ID_2, /* opacity= */ 27);
68 
69         assertThat(originalDrawable.getConstantState()).isEqualTo(
70                 new AccessibilityLayerDrawable.AccessibilityLayerDrawableState(mContext,
71                         TEST_RES_ID_2, /* opacity= */ 27));
72     }
73 }
74