Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 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.server.display; |
| 18 | |
| 19 | import static com.android.server.display.AppSaturationController.TRANSLATION_VECTOR; |
| 20 | |
| 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.eq; |
| 23 | import static org.mockito.Mockito.never; |
| 24 | import static org.mockito.Mockito.times; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | |
| 27 | import android.app.ActivityManager; |
| 28 | import android.os.UserHandle; |
| 29 | |
| 30 | import androidx.test.runner.AndroidJUnit4; |
| 31 | |
| 32 | import com.android.server.display.ColorDisplayService.ColorTransformController; |
| 33 | |
| 34 | import org.junit.After; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.mockito.Mock; |
| 39 | import org.mockito.MockitoAnnotations; |
| 40 | |
| 41 | import java.lang.ref.WeakReference; |
| 42 | |
| 43 | @RunWith(AndroidJUnit4.class) |
| 44 | public 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 | } |