blob: e51884429718d29912c1ccf2d4ab0087397f15ef [file] [log] [blame]
Christine Franksf3529b22019-01-03 13:20:17 -08001/*
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
17package com.android.server.display;
18
19import static com.android.server.display.AppSaturationController.TRANSLATION_VECTOR;
20
21import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.ArgumentMatchers.eq;
23import static org.mockito.Mockito.never;
24import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26
27import android.app.ActivityManager;
28import android.os.UserHandle;
29
30import androidx.test.runner.AndroidJUnit4;
31
32import com.android.server.display.ColorDisplayService.ColorTransformController;
33
34import org.junit.After;
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38import org.mockito.Mock;
39import org.mockito.MockitoAnnotations;
40
41import java.lang.ref.WeakReference;
42
43@RunWith(AndroidJUnit4.class)
44public class AppSaturationControllerTest {
45
46 private static final String TEST_PACKAGE_NAME = "com.android.test";
47
48 private int mUserId;
49 private AppSaturationController mAppSaturationController;
50 private float[] mMatrix;
51
52 @Mock
53 private ColorTransformController mColorTransformController;
54
55 @Before
56 public void setUp() {
57 MockitoAnnotations.initMocks(this);
58 mUserId = ActivityManager.getCurrentUser();
59 mAppSaturationController = new AppSaturationController();
60 mMatrix = new float[9];
61 }
62
63 @After
64 public void tearDown() {
65 mAppSaturationController = null;
66 mUserId = UserHandle.USER_NULL;
67 }
68
69 @Test
70 public void addColorTransformController_appliesExistingSaturation() {
71 final WeakReference<ColorTransformController> ref = new WeakReference<>(
72 mColorTransformController);
73 mAppSaturationController.setSaturationLevel(TEST_PACKAGE_NAME, mUserId, 30);
74 mAppSaturationController.addColorTransformController(TEST_PACKAGE_NAME, mUserId, ref);
75 AppSaturationController.computeGrayscaleTransformMatrix(.3f, mMatrix);
76 verify(mColorTransformController).applyAppSaturation(eq(mMatrix), eq(TRANSLATION_VECTOR));
77 }
78
79 @Test
80 public void setSaturationLevel_resetToDefault() {
81 final WeakReference<ColorTransformController> ref = new WeakReference<>(
82 mColorTransformController);
83 mAppSaturationController.addColorTransformController(TEST_PACKAGE_NAME, mUserId, ref);
84 verify(mColorTransformController, never())
85 .applyAppSaturation(any(), eq(TRANSLATION_VECTOR));
86 mAppSaturationController.setSaturationLevel(TEST_PACKAGE_NAME, mUserId, 30);
87 AppSaturationController.computeGrayscaleTransformMatrix(.3f, mMatrix);
88 verify(mColorTransformController, times(1))
89 .applyAppSaturation(eq(mMatrix), eq(TRANSLATION_VECTOR));
90 mAppSaturationController.setSaturationLevel(TEST_PACKAGE_NAME, mUserId, 100);
91 AppSaturationController.computeGrayscaleTransformMatrix(1.0f, mMatrix);
92 verify(mColorTransformController, times(2))
93 .applyAppSaturation(eq(mMatrix), eq(TRANSLATION_VECTOR));
94 }
95
96 @Test
97 public void setSaturationLevel_updateLevel() {
98 final WeakReference<ColorTransformController> ref = new WeakReference<>(
99 mColorTransformController);
100 mAppSaturationController.addColorTransformController(TEST_PACKAGE_NAME, mUserId, ref);
101 verify(mColorTransformController, never())
102 .applyAppSaturation(any(), eq(TRANSLATION_VECTOR));
103 mAppSaturationController.setSaturationLevel(TEST_PACKAGE_NAME, mUserId, 30);
104 AppSaturationController.computeGrayscaleTransformMatrix(.3f, mMatrix);
105 verify(mColorTransformController).applyAppSaturation(eq(mMatrix), eq(TRANSLATION_VECTOR));
106 mAppSaturationController.setSaturationLevel(TEST_PACKAGE_NAME, mUserId, 70);
107 AppSaturationController.computeGrayscaleTransformMatrix(.7f, mMatrix);
108 verify(mColorTransformController, times(2))
109 .applyAppSaturation(eq(mMatrix), eq(TRANSLATION_VECTOR));
110 mAppSaturationController.setSaturationLevel(TEST_PACKAGE_NAME, mUserId, 100);
111 AppSaturationController.computeGrayscaleTransformMatrix(1.0f, mMatrix);
112 verify(mColorTransformController, times(3))
113 .applyAppSaturation(eq(mMatrix), eq(TRANSLATION_VECTOR));
114 }
115}