• 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 
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.app.ActionBar;
27 import android.app.Activity;
28 import android.support.v7.widget.RecyclerView;
29 import android.view.View;
30 
31 import com.android.settings.TestConfig;
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
34 import com.android.settingslib.core.lifecycle.LifecycleObserver;
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 import org.robolectric.annotation.Config;
43 import org.robolectric.util.ReflectionHelpers;
44 
45 import java.util.List;
46 
47 @RunWith(SettingsRobolectricTestRunner.class)
48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
49 public class ActionBarShadowControllerTest {
50 
51     @Mock
52     private RecyclerView mRecyclerView;
53     @Mock
54     private Activity mActivity;
55     @Mock
56     private ActionBar mActionBar;
57     private Lifecycle mLifecycle;
58     private View mView;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         when(mActivity.getActionBar()).thenReturn(mActionBar);
64         mView = new View(RuntimeEnvironment.application);
65         mLifecycle = new Lifecycle();
66     }
67 
68     @Test
attachToRecyclerView_shouldAddScrollWatcherAndUpdateActionBar()69     public void attachToRecyclerView_shouldAddScrollWatcherAndUpdateActionBar() {
70         when(mRecyclerView.canScrollVertically(-1)).thenReturn(false);
71 
72         ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView);
73 
74         verify(mActionBar).setElevation(ActionBarShadowController.ELEVATION_LOW);
75     }
76 
77     @Test
attachToRecyclerView_customViewAsActionBar_shouldUpdateElevationOnScroll()78     public void attachToRecyclerView_customViewAsActionBar_shouldUpdateElevationOnScroll() {
79         // Setup
80         mView.setElevation(50);
81         when(mRecyclerView.canScrollVertically(-1)).thenReturn(false);
82         final ActionBarShadowController controller =
83                 ActionBarShadowController.attachToRecyclerView(mView, mLifecycle, mRecyclerView);
84         assertThat(mView.getElevation()).isEqualTo(ActionBarShadowController.ELEVATION_LOW);
85 
86         // Scroll
87         when(mRecyclerView.canScrollVertically(-1)).thenReturn(true);
88         controller.mScrollChangeWatcher.onScrolled(mRecyclerView, 10 /* dx */, 10 /* dy */);
89         assertThat(mView.getElevation()).isEqualTo(ActionBarShadowController.ELEVATION_HIGH);
90     }
91 
92     @Test
attachToRecyclerView_lifecycleChange_shouldAttachDetach()93     public void attachToRecyclerView_lifecycleChange_shouldAttachDetach() {
94         ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView);
95 
96         List<LifecycleObserver> observers = ReflectionHelpers.getField(mLifecycle, "mObservers");
97         assertThat(observers).hasSize(1);
98         verify(mRecyclerView).addOnScrollListener(any());
99 
100         mLifecycle.onStop();
101         verify(mRecyclerView).removeOnScrollListener(any());
102 
103         mLifecycle.onStart();
104         verify(mRecyclerView, times(2)).addOnScrollListener(any());
105     }
106 
107     @Test
onScrolled_nullAnchorViewAndActivity_shouldNotCrash()108     public void onScrolled_nullAnchorViewAndActivity_shouldNotCrash() {
109         final Activity activity = null;
110         final ActionBarShadowController controller =
111                 ActionBarShadowController.attachToRecyclerView(activity, mLifecycle, mRecyclerView);
112 
113         // Scroll
114         controller.mScrollChangeWatcher.onScrolled(mRecyclerView, 10 /* dx */, 10 /* dy */);
115         // no crash
116     }
117 
118 }
119