Home | History | Annotate | Download | only in deletionhelper
      1 /*
      2  * Copyright (C) 2016 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 android.content.Context;
     20 import android.support.v7.preference.PreferenceViewHolder;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.LinearLayout;
     25 import com.android.storagemanager.testing.TestingConstants;
     26 import com.android.storagemanager.R;
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 import org.mockito.Mock;
     31 import org.mockito.MockitoAnnotations;
     32 import org.robolectric.Robolectric;
     33 import org.robolectric.RobolectricTestRunner;
     34 import org.robolectric.RuntimeEnvironment;
     35 import org.robolectric.annotation.Config;
     36 
     37 import static com.google.common.truth.Truth.assertThat;
     38 import static org.mockito.Mockito.when;
     39 
     40 @RunWith(RobolectricTestRunner.class)
     41 @Config(manifest=TestingConstants.MANIFEST, sdk=TestingConstants.SDK_VERSION)
     42 public class PhotosDeletionPreferenceTest {
     43     private Context mContext;
     44     private PreferenceViewHolder mHolder;
     45     private PhotosDeletionPreference mPreference;
     46     @Mock private DeletionType mDeletionType;
     47 
     48     @Before
     49     public void setUp() throws Exception {
     50         MockitoAnnotations.initMocks(this);
     51         mContext = RuntimeEnvironment.application;
     52         mPreference = new PhotosDeletionPreference(mContext, null);
     53         mPreference.registerDeletionService(mDeletionType);
     54 
     55         // Inflate the preference and the widget.
     56         LayoutInflater inflater = LayoutInflater.from(mContext);
     57         final View view = inflater.inflate(mPreference.getLayoutResource(),
     58                 new LinearLayout(mContext), false);
     59         inflater.inflate(mPreference.getWidgetLayoutResource(),
     60                 (ViewGroup) view.findViewById(android.R.id.widget_frame));
     61 
     62         mHolder = PreferenceViewHolder.createInstanceForTests(view);
     63     }
     64 
     65     @Test
     66     public void testConstructor() {
     67         assertThat(mPreference.getFreeableBytes(DeletionHelperSettings.COUNT_CHECKED_ONLY))
     68                 .isEqualTo(0);
     69     }
     70 
     71     @Test
     72     public void testItemVisibilityBeforeLoaded() {
     73         mPreference.onBindViewHolder(mHolder);
     74         assertThat(mHolder.findViewById(R.id.progress_bar).getVisibility()).isEqualTo(View.VISIBLE);
     75         assertThat(mHolder.findViewById(android.R.id.icon).getVisibility()).isEqualTo(View.GONE);
     76         assertThat(mHolder.findViewById(android.R.id.widget_frame).getVisibility())
     77                 .isEqualTo(View.GONE);
     78     }
     79 
     80     @Test
     81     public void testItemVisibilityAfterLoaded() {
     82         mPreference.onFreeableChanged(0, 0);
     83         Robolectric.flushBackgroundThreadScheduler();
     84         Robolectric.flushForegroundThreadScheduler();
     85         mPreference.onBindViewHolder(mHolder);
     86 
     87         // After onFreeableChanged is called, we're no longer loading.
     88         assertThat(mHolder.findViewById(R.id.progress_bar).getVisibility()).isEqualTo(View.GONE);
     89         assertThat(mHolder.findViewById(android.R.id.icon).getVisibility()).isEqualTo(View.GONE);
     90         assertThat(mHolder.findViewById(android.R.id.checkbox).getVisibility())
     91                 .isEqualTo(View.VISIBLE);
     92     }
     93 
     94     @Test
     95     public void testTitleAndSummaryAfterLoaded() {
     96         mPreference.onFreeableChanged(10, 1024L);
     97         Robolectric.flushBackgroundThreadScheduler();
     98         Robolectric.flushForegroundThreadScheduler();
     99         mPreference.onBindViewHolder(mHolder);
    100 
    101         assertThat(mPreference.getTitle()).isEqualTo("Backed up photos & videos");
    102         assertThat(mPreference.getSummary().toString()).isEqualTo("1.00KB");
    103     }
    104 
    105     @Test
    106     public void testDisabledIfNothingToClear() {
    107         when(mDeletionType.isEmpty()).thenReturn(true);
    108         mPreference.onFreeableChanged(0, 0);
    109         Robolectric.flushBackgroundThreadScheduler();
    110         Robolectric.flushForegroundThreadScheduler();
    111         mPreference.onBindViewHolder(mHolder);
    112 
    113         assertThat(mPreference.isEnabled()).isFalse();
    114     }
    115 
    116     @Test
    117     public void testGetFreeableBytes() {
    118         mPreference.onFreeableChanged(100, 1024L);
    119         Robolectric.flushBackgroundThreadScheduler();
    120         Robolectric.flushForegroundThreadScheduler();
    121 
    122         assertThat(mPreference.getFreeableBytes(DeletionHelperSettings.COUNT_CHECKED_ONLY))
    123                 .isEqualTo(0);
    124         assertThat(mPreference.getFreeableBytes(DeletionHelperSettings.COUNT_UNCHECKED))
    125                 .isEqualTo(1024L);
    126     }
    127 }
    128