blob: 1e27915c1bb2d128dfdcf6c0467f62c490f2dd5e [file] [log] [blame]
Jason Monkd4afe152017-05-01 15:37:43 -04001/*
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
15package com.android.systemui.tuner;
16
17import static org.mockito.ArgumentMatchers.any;
18import static org.mockito.ArgumentMatchers.eq;
19import static org.mockito.Mockito.doAnswer;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23import static org.mockito.Mockito.withSettings;
24
Jason Monkfba8faf2017-05-23 10:42:59 -040025import android.support.test.filters.SmallTest;
Jason Monkd4afe152017-05-01 15:37:43 -040026import android.testing.LeakCheck.Tracker;
27import android.util.DisplayMetrics;
28import android.view.View;
29import android.view.WindowManager;
30
31import com.android.systemui.utils.leaks.LeakCheckedTest;
32
33import org.junit.Before;
34import org.junit.Test;
35
Jason Monkfba8faf2017-05-23 10:42:59 -040036@SmallTest
Jason Monkd4afe152017-05-01 15:37:43 -040037public 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);
Jason Monk1fc931a2017-12-14 13:22:58 -050048 mView = mock(View.class);
49 when(mView.getContext()).thenReturn(mContext);
Jason Monkd4afe152017-05-01 15:37:43 -040050
Jason Monkb1a5f232018-12-21 11:47:34 -050051 mTunerService = mock(TunerService.class);
52 mDependency.injectTestDependency(TunablePadding.TunablePaddingService.class,
53 new TunablePadding.TunablePaddingService(mTunerService));
Jason Monkd4afe152017-05-01 15:37:43 -040054 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
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
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
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 }
Jason Monkb1a5f232018-12-21 11:47:34 -0500123}