ActionBarShadowControllerTest.java revision 9f1e911759dc6fedaac9fa65afb79f6a93022bf4
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 17package com.android.settings.widget; 18 19 20import android.app.ActionBar; 21import android.app.Activity; 22import android.support.v7.widget.RecyclerView; 23 24import com.android.settings.testutils.SettingsRobolectricTestRunner; 25import com.android.settings.TestConfig; 26import com.android.settingslib.core.lifecycle.Lifecycle; 27import com.android.settingslib.core.lifecycle.LifecycleObserver; 28 29import org.junit.Before; 30import org.junit.Test; 31import org.junit.runner.RunWith; 32import org.mockito.Mock; 33import org.mockito.MockitoAnnotations; 34import org.robolectric.annotation.Config; 35import org.robolectric.util.ReflectionHelpers; 36 37import java.util.List; 38 39import static com.google.common.truth.Truth.assertThat; 40import static org.mockito.Matchers.any; 41import static org.mockito.Mockito.times; 42import static org.mockito.Mockito.verify; 43import static org.mockito.Mockito.when; 44 45@RunWith(SettingsRobolectricTestRunner.class) 46@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47public class ActionBarShadowControllerTest { 48 49 @Mock 50 private RecyclerView mRecyclerView; 51 @Mock 52 private Activity mActivity; 53 @Mock 54 private ActionBar mActionBar; 55 private Lifecycle mLifecycle; 56 57 @Before 58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 when(mActivity.getActionBar()).thenReturn(mActionBar); 61 mLifecycle = new Lifecycle(); 62 } 63 64 @Test 65 public void attachToRecyclerView_shouldAddScrollWatcherAndUpdateActionBar() { 66 when(mRecyclerView.canScrollVertically(-1)).thenReturn(false); 67 68 ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView); 69 70 verify(mActionBar).setElevation(0); 71 } 72 73 74 @Test 75 public void attachToRecyclerView_lifecycleChange_shouldAttachDetach() { 76 ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView); 77 78 List<LifecycleObserver> observers = ReflectionHelpers.getField(mLifecycle, "mObservers"); 79 assertThat(observers).hasSize(1); 80 verify(mRecyclerView).addOnScrollListener(any()); 81 82 mLifecycle.onStop(); 83 verify(mRecyclerView).removeOnScrollListener(any()); 84 85 mLifecycle.onStart(); 86 verify(mRecyclerView, times(2)).addOnScrollListener(any()); 87 } 88 89} 90