Home | History | Annotate | Download | only in applications
      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.applications;
     18 
     19 import static org.mockito.ArgumentMatchers.nullable;
     20 import static org.mockito.Matchers.any;
     21 import static org.mockito.Mockito.doNothing;
     22 import static org.mockito.Mockito.mock;
     23 import static org.mockito.Mockito.never;
     24 import static org.mockito.Mockito.spy;
     25 import static org.mockito.Mockito.verify;
     26 import static org.mockito.Mockito.when;
     27 
     28 import android.content.Context;
     29 import android.view.View;
     30 import android.widget.Button;
     31 
     32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     33 import com.android.settings.widget.ActionButtonPreference;
     34 import com.android.settings.widget.ActionButtonPreferenceTest;
     35 import com.android.settingslib.applications.StorageStatsSource.AppStorageStats;
     36 
     37 import org.junit.Before;
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 import org.mockito.Mock;
     41 import org.mockito.MockitoAnnotations;
     42 import org.robolectric.RuntimeEnvironment;
     43 
     44 @RunWith(SettingsRobolectricTestRunner.class)
     45 public class AppStorageSettingsTest {
     46 
     47     @Mock
     48     private AppStorageSizesController mSizesController;
     49     private ActionButtonPreference mButtonsPref;
     50     private AppStorageSettings mSettings;
     51     private Button mLeftButton;
     52     private Button mRightButton;
     53 
     54     @Before
     55     public void setUp() {
     56         MockitoAnnotations.initMocks(this);
     57         mLeftButton = new Button(RuntimeEnvironment.application);
     58         mRightButton = new Button(RuntimeEnvironment.application);
     59         mSettings = spy(new AppStorageSettings());
     60         mSettings.mSizeController = mSizesController;
     61         mButtonsPref = ActionButtonPreferenceTest.createMock();
     62         mSettings.mButtonsPref = mButtonsPref;
     63 
     64         when(mButtonsPref.setButton1OnClickListener(any(View.OnClickListener.class)))
     65                 .thenAnswer(invocation -> {
     66                     final Object[] args = invocation.getArguments();
     67                     mLeftButton.setOnClickListener((View.OnClickListener) args[0]);
     68                     return mButtonsPref;
     69                 });
     70         when(mButtonsPref.setButton2OnClickListener(any(View.OnClickListener.class)))
     71                 .thenAnswer(invocation -> {
     72                     final Object[] args = invocation.getArguments();
     73                     mRightButton.setOnClickListener((View.OnClickListener) args[0]);
     74                     return mButtonsPref;
     75                 });
     76     }
     77 
     78     @Test
     79     public void updateUiWithSize_noAppStats_shouldDisableClearButtons() {
     80         mSettings.updateUiWithSize(null);
     81 
     82         verify(mSizesController).updateUi(nullable(Context.class));
     83         verify(mButtonsPref).setButton1Enabled(false);
     84         verify(mButtonsPref).setButton2Enabled(false);
     85     }
     86 
     87     @Test
     88     public void updateUiWithSize_hasDataAndCache_shouldEnableClearButtons() {
     89         final AppStorageStats stats = mock(AppStorageStats.class);
     90         when(stats.getCacheBytes()).thenReturn(5000L);
     91         when(stats.getDataBytes()).thenReturn(10000L);
     92         doNothing().when(mSettings).handleClearCacheClick();
     93         doNothing().when(mSettings).handleClearDataClick();
     94 
     95         mSettings.updateUiWithSize(stats);
     96         verify(mButtonsPref).setButton1Enabled(true);
     97         verify(mButtonsPref).setButton2Enabled(true);
     98         mLeftButton.performClick();
     99         verify(mSettings).handleClearDataClick();
    100         verify(mSettings, never()).handleClearCacheClick();
    101 
    102         mRightButton.performClick();
    103         verify(mSettings).handleClearDataClick();
    104         verify(mSettings).handleClearCacheClick();
    105     }
    106 }
    107 
    108