Home | History | Annotate | Download | only in systemui
      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;
     16 
     17 import static com.android.systemui.tuner.TunablePadding.FLAG_END;
     18 import static com.android.systemui.tuner.TunablePadding.FLAG_START;
     19 
     20 import static org.mockito.ArgumentMatchers.any;
     21 import static org.mockito.ArgumentMatchers.anyInt;
     22 import static org.mockito.ArgumentMatchers.anyString;
     23 import static org.mockito.ArgumentMatchers.eq;
     24 import static org.mockito.Mockito.mock;
     25 import static org.mockito.Mockito.never;
     26 import static org.mockito.Mockito.spy;
     27 import static org.mockito.Mockito.times;
     28 import static org.mockito.Mockito.verify;
     29 import static org.mockito.Mockito.when;
     30 
     31 import android.app.Fragment;
     32 import android.support.test.filters.SmallTest;
     33 import android.testing.AndroidTestingRunner;
     34 import android.view.Display;
     35 import android.view.View;
     36 import android.view.WindowManager;
     37 
     38 import com.android.systemui.R.dimen;
     39 import com.android.systemui.RoundedCorners.TunablePaddingTagListener;
     40 import com.android.systemui.fragments.FragmentHostManager;
     41 import com.android.systemui.fragments.FragmentService;
     42 import com.android.systemui.statusbar.phone.StatusBar;
     43 import com.android.systemui.statusbar.phone.StatusBarWindowView;
     44 import com.android.systemui.tuner.TunablePadding;
     45 import com.android.systemui.tuner.TunablePadding.TunablePaddingService;
     46 import com.android.systemui.tuner.TunerService;
     47 
     48 import org.junit.Before;
     49 import org.junit.Test;
     50 import org.junit.runner.RunWith;
     51 
     52 @RunWith(AndroidTestingRunner.class)
     53 @SmallTest
     54 public 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);
     71         when(mStatusBar.getNavigationBarWindow()).thenReturn(mView);
     72         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);
     94         mContext.getOrCreateTestableResources()
     95                 .addOverride(dimen.rounded_corner_content_padding, 0);
     96 
     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);
    109         mContext.getOrCreateTestableResources()
    110                 .addOverride(dimen.rounded_corner_content_padding, 20);
    111 
    112         mRoundedCorners.start();
    113         // Add 2 windows for rounded corners (top and bottom).
    114         verify(mWindowManager, times(2)).addView(any(), any());
    115 
    116         // Add 2 tag listeners for each of the fragments that are needed.
    117         verify(mFragmentHostManager, times(2)).addTagListener(any(), any());
    118         // 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 
    147 }
    148