Home | History | Annotate | Download | only in development
      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.development;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Mockito.verify;
     21 import static org.mockito.Mockito.when;
     22 
     23 import android.content.Context;
     24 import android.content.pm.IShortcutService;
     25 import android.os.RemoteException;
     26 import android.support.v14.preference.SwitchPreference;
     27 import android.support.v7.preference.PreferenceScreen;
     28 
     29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.mockito.Mock;
     35 import org.mockito.MockitoAnnotations;
     36 import org.robolectric.RuntimeEnvironment;
     37 import org.robolectric.util.ReflectionHelpers;
     38 
     39 @RunWith(SettingsRobolectricTestRunner.class)
     40 public class ShortcutManagerThrottlingPreferenceControllerTest {
     41 
     42     @Mock
     43     private SwitchPreference mPreference;
     44     @Mock
     45     private PreferenceScreen mPreferenceScreen;
     46     @Mock
     47     private IShortcutService mShortcutService;
     48 
     49     private Context mContext;
     50     private ShortcutManagerThrottlingPreferenceController mController;
     51 
     52     @Before
     53     public void setup() {
     54         MockitoAnnotations.initMocks(this);
     55         mContext = RuntimeEnvironment.application;
     56         mController = new ShortcutManagerThrottlingPreferenceController(mContext);
     57         ReflectionHelpers.setField(mController, "mShortcutService", mShortcutService);
     58         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
     59             .thenReturn(mPreference);
     60         mController.displayPreference(mPreferenceScreen);
     61     }
     62 
     63     @Test
     64     public void handlePreferenceTreeClick_differentPreferenceKey_shouldReturnFalse() {
     65         when(mPreference.getKey()).thenReturn("SomeRandomKey");
     66 
     67         assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse();
     68     }
     69 
     70     @Test
     71     public void handlePreferenceTreeClick_correctPreferenceKey_shouldResetThrottling()
     72             throws RemoteException {
     73         when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
     74 
     75         final boolean handled = mController.handlePreferenceTreeClick(mPreference);
     76 
     77         assertThat(handled).isTrue();
     78         verify(mShortcutService).resetThrottling();
     79     }
     80 }
     81