Home | History | Annotate | Download | only in manageapplications
      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.manageapplications;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Matchers.nullable;
     21 import static org.mockito.Mockito.verify;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.app.Fragment;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.os.UserHandle;
     28 import android.os.storage.VolumeInfo;
     29 import android.text.format.Formatter;
     30 import android.view.View;
     31 import android.widget.FrameLayout;
     32 
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 import com.android.settingslib.applications.StorageStatsSource;
     35 
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 import org.mockito.Answers;
     40 import org.mockito.ArgumentCaptor;
     41 import org.mockito.Mock;
     42 import org.mockito.MockitoAnnotations;
     43 import org.robolectric.RuntimeEnvironment;
     44 
     45 @RunWith(SettingsRobolectricTestRunner.class)
     46 public class PhotosViewHolderControllerTest {
     47     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     48     private Fragment mFragment;
     49     @Mock
     50     private StorageStatsSource mSource;
     51 
     52     private Context mContext;
     53     private PhotosViewHolderController mController;
     54     private ApplicationViewHolder mHolder;
     55 
     56     @Before
     57     public void setUp() throws Exception {
     58         MockitoAnnotations.initMocks(this);
     59         mContext = RuntimeEnvironment.application;
     60         final String fsUuid = new VolumeInfo("id", 0, null, "id").fsUuid;
     61         mController = new PhotosViewHolderController(mContext, mSource, fsUuid, new UserHandle(0));
     62 
     63         final View view = ApplicationViewHolder.newView(new FrameLayout(mContext));
     64         mHolder = new ApplicationViewHolder(view, false /* useStableHeight */);
     65     }
     66 
     67     @Test
     68     public void storageShouldBeZeroBytesIfQueriedBeforeStorageQueryFinishes() {
     69         mController.setupView(mHolder);
     70 
     71         assertThat(mHolder.mSummary.getText().toString())
     72             .isEqualTo(Formatter.formatFileSize(mContext, 0));
     73     }
     74 
     75     @Test
     76     public void storageShouldRepresentStorageStatsQuery() throws Exception {
     77         when(mSource.getExternalStorageStats(nullable(String.class), nullable(UserHandle.class)))
     78             .thenReturn(new StorageStatsSource.ExternalStorageStats(1, 0, 1, 10, 0));
     79 
     80         mController.queryStats();
     81         mController.setupView(mHolder);
     82 
     83         assertThat(mHolder.mSummary.getText().toString())
     84             .isEqualTo(Formatter.formatFileSize(mContext, 11));
     85     }
     86 
     87     @Test
     88     public void clickingShouldIntentIntoFilesApp() {
     89         mController.onClick(mFragment);
     90 
     91         final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
     92         verify(mFragment).startActivity(argumentCaptor.capture());
     93         Intent intent = argumentCaptor.getValue();
     94 
     95         assertThat(intent.getType()).isEqualTo("image/*");
     96         assertThat(intent.getAction()).isEqualTo(android.content.Intent.ACTION_VIEW);
     97         assertThat(intent.getBooleanExtra(Intent.EXTRA_FROM_STORAGE, false)).isTrue();
     98     }
     99 }
    100