1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.tuner; 16 17 import static org.mockito.ArgumentMatchers.any; 18 import static org.mockito.ArgumentMatchers.eq; 19 import static org.mockito.Mockito.doAnswer; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.testing.LeakCheck.Tracker; 25 import android.util.DisplayMetrics; 26 import android.view.View; 27 import android.view.WindowManager; 28 29 import androidx.test.filters.SmallTest; 30 31 import com.android.systemui.utils.leaks.LeakCheckedTest; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 36 @SmallTest 37 public class TunablePaddingTest extends LeakCheckedTest { 38 39 private static final String KEY = "KEY"; 40 private static final int DEFAULT = 42; 41 private View mView; 42 private TunablePadding mTunablePadding; 43 private TunerService mTunerService; 44 45 @Before setup()46 public void setup() { 47 injectLeakCheckedDependencies(ALL_SUPPORTED_CLASSES); 48 mView = mock(View.class); 49 when(mView.getContext()).thenReturn(mContext); 50 51 mTunerService = mock(TunerService.class); 52 mDependency.injectTestDependency(TunablePadding.TunablePaddingService.class, 53 new TunablePadding.TunablePaddingService(mTunerService)); 54 Tracker tracker = mLeakCheck.getTracker("tuner"); 55 doAnswer(invocation -> { 56 tracker.getLeakInfo(invocation.getArguments()[0]).addAllocation(new Throwable()); 57 return null; 58 }).when(mTunerService).addTunable(any(), any()); 59 doAnswer(invocation -> { 60 tracker.getLeakInfo(invocation.getArguments()[0]).clearAllocations(); 61 return null; 62 }).when(mTunerService).removeTunable(any()); 63 } 64 65 @Test testFlags()66 public void testFlags() { 67 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 68 TunablePadding.FLAG_START); 69 mTunablePadding.onTuningChanged(null, null); 70 verify(mView).setPadding(eq(DEFAULT), eq(0), eq(0), eq(0)); 71 mTunablePadding.destroy(); 72 73 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 74 TunablePadding.FLAG_TOP); 75 mTunablePadding.onTuningChanged(null, null); 76 verify(mView).setPadding(eq(0), eq(DEFAULT), eq(0), eq(0)); 77 mTunablePadding.destroy(); 78 79 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 80 TunablePadding.FLAG_END); 81 mTunablePadding.onTuningChanged(null, null); 82 verify(mView).setPadding(eq(0), eq(0), eq(DEFAULT), eq(0)); 83 mTunablePadding.destroy(); 84 85 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 86 TunablePadding.FLAG_BOTTOM); 87 mTunablePadding.onTuningChanged(null, null); 88 verify(mView).setPadding(eq(0), eq(0), eq(0), eq(DEFAULT)); 89 mTunablePadding.destroy(); 90 } 91 92 @Test testRtl()93 public void testRtl() { 94 when(mView.isLayoutRtl()).thenReturn(true); 95 96 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 97 TunablePadding.FLAG_END); 98 mTunablePadding.onTuningChanged(null, null); 99 verify(mView).setPadding(eq(DEFAULT), eq(0), eq(0), eq(0)); 100 mTunablePadding.destroy(); 101 102 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 103 TunablePadding.FLAG_START); 104 mTunablePadding.onTuningChanged(null, null); 105 verify(mView).setPadding(eq(0), eq(0), eq(DEFAULT), eq(0)); 106 mTunablePadding.destroy(); 107 } 108 109 @Test testTuning()110 public void testTuning() { 111 int value = 3; 112 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 113 TunablePadding.FLAG_START); 114 mTunablePadding.onTuningChanged(KEY, String.valueOf(value)); 115 116 DisplayMetrics metrics = new DisplayMetrics(); 117 mContext.getSystemService(WindowManager.class).getDefaultDisplay().getMetrics(metrics); 118 int output = (int) (metrics.density * value); 119 verify(mView).setPadding(eq(output), eq(0), eq(0), eq(0)); 120 121 mTunablePadding.destroy(); 122 } 123 } 124