blob: 2a447715fb6bd2b217ab0eb6ff9c4e5e5895fceb [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;
16
17import static com.android.systemui.tuner.TunablePadding.FLAG_END;
18import static com.android.systemui.tuner.TunablePadding.FLAG_START;
19
20import static org.mockito.ArgumentMatchers.any;
21import static org.mockito.ArgumentMatchers.anyInt;
22import static org.mockito.ArgumentMatchers.anyString;
23import static org.mockito.ArgumentMatchers.eq;
Jason Monkd4afe152017-05-01 15:37:43 -040024import static org.mockito.Mockito.mock;
25import static org.mockito.Mockito.never;
26import static org.mockito.Mockito.spy;
27import static org.mockito.Mockito.times;
28import static org.mockito.Mockito.verify;
29import static org.mockito.Mockito.when;
30
31import android.app.Fragment;
Jason Monkfba8faf2017-05-23 10:42:59 -040032import android.support.test.filters.SmallTest;
Jason Monkd4afe152017-05-01 15:37:43 -040033import android.testing.AndroidTestingRunner;
34import android.view.Display;
35import android.view.View;
36import android.view.WindowManager;
37
38import com.android.systemui.R.dimen;
39import com.android.systemui.RoundedCorners.TunablePaddingTagListener;
40import com.android.systemui.fragments.FragmentHostManager;
41import com.android.systemui.fragments.FragmentService;
42import com.android.systemui.statusbar.phone.StatusBar;
43import com.android.systemui.statusbar.phone.StatusBarWindowView;
44import com.android.systemui.tuner.TunablePadding;
45import com.android.systemui.tuner.TunablePadding.TunablePaddingService;
46import com.android.systemui.tuner.TunerService;
47
48import org.junit.Before;
49import org.junit.Test;
50import org.junit.runner.RunWith;
51
52@RunWith(AndroidTestingRunner.class)
Jason Monkfba8faf2017-05-23 10:42:59 -040053@SmallTest
Jason Monkd4afe152017-05-01 15:37:43 -040054public class RoundedCornersTest extends SysuiTestCase {
55
56 private RoundedCorners mRoundedCorners;
57 private StatusBar mStatusBar;
58 private WindowManager mWindowManager;
59 private FragmentService mFragmentService;
60 private FragmentHostManager mFragmentHostManager;
61 private TunerService mTunerService;
62 private StatusBarWindowView mView;
63 private TunablePaddingService mTunablePaddingService;
64
65 @Before
66 public void setup() {
67 mStatusBar = mock(StatusBar.class);
68 mWindowManager = mock(WindowManager.class);
69 mView = spy(new StatusBarWindowView(mContext, null));
70 when(mStatusBar.getStatusBarWindow()).thenReturn(mView);
Jason Monkc429f692017-06-27 13:13:49 -040071 when(mStatusBar.getNavigationBarWindow()).thenReturn(mView);
Jason Monkd4afe152017-05-01 15:37:43 -040072 mContext.putComponent(StatusBar.class, mStatusBar);
73
74 Display display = mContext.getSystemService(WindowManager.class).getDefaultDisplay();
75 when(mWindowManager.getDefaultDisplay()).thenReturn(display);
76 mContext.addMockSystemService(WindowManager.class, mWindowManager);
77
78 mFragmentService = mDependency.injectMockDependency(FragmentService.class);
79 mFragmentHostManager = mock(FragmentHostManager.class);
80 when(mFragmentService.getFragmentHostManager(any())).thenReturn(mFragmentHostManager);
81
82 mTunerService = mDependency.injectMockDependency(TunerService.class);
83
84 mRoundedCorners = new RoundedCorners();
85 mRoundedCorners.mContext = mContext;
86 mRoundedCorners.mComponents = mContext.getComponents();
87
88 mTunablePaddingService = mDependency.injectMockDependency(TunablePaddingService.class);
89 }
90
91 @Test
92 public void testNoRounding() {
93 mContext.getOrCreateTestableResources().addOverride(dimen.rounded_corner_radius, 0);
Jason Monkc429f692017-06-27 13:13:49 -040094 mContext.getOrCreateTestableResources()
95 .addOverride(dimen.rounded_corner_content_padding, 0);
Jason Monkd4afe152017-05-01 15:37:43 -040096
97 mRoundedCorners.start();
98 // No views added.
99 verify(mWindowManager, never()).addView(any(), any());
100 // No Fragments watched.
101 verify(mFragmentHostManager, never()).addTagListener(any(), any());
102 // No Tuners tuned.
103 verify(mTunerService, never()).addTunable(any(), any());
104 }
105
106 @Test
107 public void testRounding() {
108 mContext.getOrCreateTestableResources().addOverride(dimen.rounded_corner_radius, 20);
Jason Monkc429f692017-06-27 13:13:49 -0400109 mContext.getOrCreateTestableResources()
110 .addOverride(dimen.rounded_corner_content_padding, 20);
Jason Monkd4afe152017-05-01 15:37:43 -0400111
112 mRoundedCorners.start();
113 // Add 2 windows for rounded corners (top and bottom).
114 verify(mWindowManager, times(2)).addView(any(), any());
115
Jason Monk089981b2017-06-30 09:10:01 -0400116 // Add 2 tag listeners for each of the fragments that are needed.
117 verify(mFragmentHostManager, times(2)).addTagListener(any(), any());
Jason Monkd4afe152017-05-01 15:37:43 -0400118 // One tunable.
119 verify(mTunerService, times(1)).addTunable(any(), any());
120 // One TunablePadding.
121 verify(mTunablePaddingService, times(1)).add(any(), anyString(), anyInt(), anyInt());
122 }
123
124 @Test
125 public void testPaddingTagListener() {
126 TunablePaddingTagListener tagListener = new TunablePaddingTagListener(14, 5);
127 View v = mock(View.class);
128 View child = mock(View.class);
129 Fragment f = mock(Fragment.class);
130 TunablePadding padding = mock(TunablePadding.class);
131
132 when(mTunablePaddingService.add(any(), anyString(), anyInt(), anyInt()))
133 .thenReturn(padding);
134 when(f.getView()).thenReturn(v);
135 when(v.findViewById(5)).thenReturn(child);
136
137 // Trigger callback and verify we get a TunablePadding created.
138 tagListener.onFragmentViewCreated(null, f);
139 verify(mTunablePaddingService).add(eq(child), eq(RoundedCorners.PADDING), eq(14),
140 eq(FLAG_START | FLAG_END));
141
142 // Call again and verify destroy is called.
143 tagListener.onFragmentViewCreated(null, f);
144 verify(padding).destroy();
145 }
146
Geoffrey Pitsch0053e992017-05-22 15:32:29 -0400147}