Home | History | Annotate | Download | only in deletionhelper
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.storagemanager.deletionhelper;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import android.support.v7.preference.Preference;
     22 import android.support.v7.preference.PreferenceScreen;
     23 import com.android.storagemanager.testing.TestingConstants;
     24 import java.util.ArrayList;
     25 import java.util.List;
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 import org.mockito.ArgumentCaptor;
     30 import org.mockito.Mock;
     31 import org.mockito.MockitoAnnotations;
     32 import org.robolectric.RobolectricTestRunner;
     33 import org.robolectric.RuntimeEnvironment;
     34 import org.robolectric.annotation.Config;
     35 
     36 import static org.mockito.Matchers.any;
     37 import static org.mockito.Mockito.never;
     38 import static org.mockito.Mockito.verify;
     39 import static org.mockito.Mockito.when;
     40 
     41 @RunWith(RobolectricTestRunner.class)
     42 @Config(manifest = TestingConstants.MANIFEST, sdk = TestingConstants.SDK_VERSION)
     43 public class AppDeletionPreferenceGroupTest {
     44     @Mock private AppsAsyncLoader.PackageInfo mPackage1;
     45     @Mock private AppDeletionType mBackend;
     46     @Mock private PreferenceScreen mScreen;
     47     private List<AppsAsyncLoader.PackageInfo> mApps;
     48     private AppDeletionPreferenceGroup mGroup;
     49 
     50     @Before
     51     public void setUp() {
     52         MockitoAnnotations.initMocks(this);
     53         mApps = new ArrayList<>();
     54         mApps.add(mPackage1);
     55         mGroup = new AppDeletionPreferenceGroup(RuntimeEnvironment.application);
     56         mGroup.setDeletionType(mBackend);
     57         mGroup.mScreen = mScreen;
     58     }
     59 
     60     @Test
     61     public void addsPreferenceToScreen_onNoThreshold() {
     62         when(mBackend.getDeletionThreshold()).thenReturn(0L);
     63         verify(mScreen, never()).addPreference(any());
     64         mGroup.onAppRebuild(mApps);
     65 
     66         // Verify that a preference was added to the screen for mPackage1
     67         verify(mScreen).addPreference(any());
     68     }
     69 
     70     @Test
     71     public void addsPreferenceToScreenWithHighOrder_onNoThreshold() {
     72         when(mBackend.getDeletionThreshold()).thenReturn(0L);
     73 
     74         mGroup.onAppRebuild(mApps);
     75         ArgumentCaptor<Preference> apps = ArgumentCaptor.forClass(Preference.class);
     76         verify(mScreen).addPreference(apps.capture());
     77 
     78         assertThat(apps.getValue().getOrder()).isGreaterThan(0);
     79     }
     80 }
     81