• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.widget;
18 
19 import static android.arch.lifecycle.Lifecycle.Event.ON_START;
20 import static android.arch.lifecycle.Lifecycle.Event.ON_STOP;
21 import static com.google.common.truth.Truth.assertThat;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.ActionBar;
28 import android.app.Activity;
29 import android.arch.lifecycle.LifecycleOwner;
30 import android.support.v7.widget.RecyclerView;
31 import android.view.View;
32 
33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
34 import com.android.settingslib.core.lifecycle.Lifecycle;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RuntimeEnvironment;
42 
43 @RunWith(SettingsRobolectricTestRunner.class)
44 public class ActionBarShadowControllerTest {
45 
46     @Mock
47     private RecyclerView mRecyclerView;
48     @Mock
49     private Activity mActivity;
50     @Mock
51     private ActionBar mActionBar;
52     private Lifecycle mLifecycle;
53     private LifecycleOwner mLifecycleOwner;
54     private View mView;
55 
56     @Before
setUp()57     public void setUp() {
58         MockitoAnnotations.initMocks(this);
59         when(mActivity.getActionBar()).thenReturn(mActionBar);
60         mView = new View(RuntimeEnvironment.application);
61         mLifecycleOwner = () -> mLifecycle;
62         mLifecycle = new Lifecycle(mLifecycleOwner);
63     }
64 
65     @Test
attachToRecyclerView_shouldAddScrollWatcherAndUpdateActionBar()66     public void attachToRecyclerView_shouldAddScrollWatcherAndUpdateActionBar() {
67         when(mRecyclerView.canScrollVertically(-1)).thenReturn(false);
68 
69         ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView);
70 
71         verify(mActionBar).setElevation(ActionBarShadowController.ELEVATION_LOW);
72     }
73 
74     @Test
attachToRecyclerView_customViewAsActionBar_shouldUpdateElevationOnScroll()75     public void attachToRecyclerView_customViewAsActionBar_shouldUpdateElevationOnScroll() {
76         // Setup
77         mView.setElevation(50);
78         when(mRecyclerView.canScrollVertically(-1)).thenReturn(false);
79         final ActionBarShadowController controller =
80                 ActionBarShadowController.attachToRecyclerView(mView, mLifecycle, mRecyclerView);
81         assertThat(mView.getElevation()).isEqualTo(ActionBarShadowController.ELEVATION_LOW);
82 
83         // Scroll
84         when(mRecyclerView.canScrollVertically(-1)).thenReturn(true);
85         controller.mScrollChangeWatcher.onScrolled(mRecyclerView, 10 /* dx */, 10 /* dy */);
86         assertThat(mView.getElevation()).isEqualTo(ActionBarShadowController.ELEVATION_HIGH);
87     }
88 
89     @Test
attachToRecyclerView_lifecycleChange_shouldAttachDetach()90     public void attachToRecyclerView_lifecycleChange_shouldAttachDetach() {
91         ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView);
92 
93         verify(mRecyclerView).addOnScrollListener(any());
94 
95         mLifecycle.handleLifecycleEvent(ON_START);
96         mLifecycle.handleLifecycleEvent(ON_STOP);
97         verify(mRecyclerView).removeOnScrollListener(any());
98 
99         mLifecycle.handleLifecycleEvent(ON_START);
100         verify(mRecyclerView, times(2)).addOnScrollListener(any());
101     }
102 
103     @Test
onScrolled_nullAnchorViewAndActivity_shouldNotCrash()104     public void onScrolled_nullAnchorViewAndActivity_shouldNotCrash() {
105         final Activity activity = null;
106         final ActionBarShadowController controller =
107                 ActionBarShadowController.attachToRecyclerView(activity, mLifecycle, mRecyclerView);
108 
109         // Scroll
110         controller.mScrollChangeWatcher.onScrolled(mRecyclerView, 10 /* dx */, 10 /* dy */);
111         // no crash
112     }
113 }
114