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 import static org.mockito.Mockito.withSettings; 24 25 import android.support.test.filters.SmallTest; 26 import android.testing.LeakCheck.Tracker; 27 import android.util.DisplayMetrics; 28 import android.view.View; 29 import android.view.WindowManager; 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 46 public void setup() { 47 injectLeakCheckedDependencies(ALL_SUPPORTED_CLASSES); 48 mView = mock(View.class, withSettings().spiedInstance(new View(mContext))); 49 50 mTunerService = mDependency.injectMockDependency(TunerService.class); 51 Tracker tracker = mLeakCheck.getTracker("tuner"); 52 doAnswer(invocation -> { 53 tracker.getLeakInfo(invocation.getArguments()[0]).addAllocation(new Throwable()); 54 return null; 55 }).when(mTunerService).addTunable(any(), any()); 56 doAnswer(invocation -> { 57 tracker.getLeakInfo(invocation.getArguments()[0]).clearAllocations(); 58 return null; 59 }).when(mTunerService).removeTunable(any()); 60 } 61 62 @Test 63 public void testFlags() { 64 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 65 TunablePadding.FLAG_START); 66 mTunablePadding.onTuningChanged(null, null); 67 verify(mView).setPadding(eq(DEFAULT), eq(0), eq(0), eq(0)); 68 mTunablePadding.destroy(); 69 70 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 71 TunablePadding.FLAG_TOP); 72 mTunablePadding.onTuningChanged(null, null); 73 verify(mView).setPadding(eq(0), eq(DEFAULT), eq(0), eq(0)); 74 mTunablePadding.destroy(); 75 76 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 77 TunablePadding.FLAG_END); 78 mTunablePadding.onTuningChanged(null, null); 79 verify(mView).setPadding(eq(0), eq(0), eq(DEFAULT), eq(0)); 80 mTunablePadding.destroy(); 81 82 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 83 TunablePadding.FLAG_BOTTOM); 84 mTunablePadding.onTuningChanged(null, null); 85 verify(mView).setPadding(eq(0), eq(0), eq(0), eq(DEFAULT)); 86 mTunablePadding.destroy(); 87 } 88 89 @Test 90 public void testRtl() { 91 when(mView.isLayoutRtl()).thenReturn(true); 92 93 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 94 TunablePadding.FLAG_END); 95 mTunablePadding.onTuningChanged(null, null); 96 verify(mView).setPadding(eq(DEFAULT), eq(0), eq(0), eq(0)); 97 mTunablePadding.destroy(); 98 99 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 100 TunablePadding.FLAG_START); 101 mTunablePadding.onTuningChanged(null, null); 102 verify(mView).setPadding(eq(0), eq(0), eq(DEFAULT), eq(0)); 103 mTunablePadding.destroy(); 104 } 105 106 @Test 107 public void testTuning() { 108 int value = 3; 109 mTunablePadding = TunablePadding.addTunablePadding(mView, KEY, DEFAULT, 110 TunablePadding.FLAG_START); 111 mTunablePadding.onTuningChanged(KEY, String.valueOf(value)); 112 113 DisplayMetrics metrics = new DisplayMetrics(); 114 mContext.getSystemService(WindowManager.class).getDefaultDisplay().getMetrics(metrics); 115 int output = (int) (metrics.density * value); 116 verify(mView).setPadding(eq(output), eq(0), eq(0), eq(0)); 117 118 mTunablePadding.destroy(); 119 } 120 }