1 package com.android.settings.applications; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import static org.mockito.Matchers.nullable; 6 import static org.mockito.Mockito.verify; 7 import static org.mockito.Mockito.when; 8 9 import android.app.Fragment; 10 import android.content.Context; 11 import android.content.Intent; 12 import android.os.UserHandle; 13 import android.os.storage.VolumeInfo; 14 import android.view.LayoutInflater; 15 16 import com.android.settings.TestConfig; 17 import com.android.settings.testutils.SettingsRobolectricTestRunner; 18 import com.android.settingslib.applications.StorageStatsSource; 19 import com.android.settingslib.deviceinfo.StorageVolumeProvider; 20 21 import org.junit.Before; 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.mockito.Answers; 25 import org.mockito.ArgumentCaptor; 26 import org.mockito.Mock; 27 import org.mockito.MockitoAnnotations; 28 import org.robolectric.RuntimeEnvironment; 29 import org.robolectric.annotation.Config; 30 31 @RunWith(SettingsRobolectricTestRunner.class) 32 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 33 public class PhotosViewHolderControllerTest { 34 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 35 private Fragment mFragment; 36 37 @Mock private StorageVolumeProvider mSvp; 38 @Mock private StorageStatsSource mSource; 39 40 private Context mContext; 41 private PhotosViewHolderController mController; 42 private VolumeInfo mVolume; 43 private AppViewHolder mHolder; 44 45 @Before setUp()46 public void setUp() throws Exception { 47 MockitoAnnotations.initMocks(this); 48 mContext = RuntimeEnvironment.application; 49 mVolume = new VolumeInfo("id", 0, null, "id"); 50 mController = 51 new PhotosViewHolderController( 52 mContext, mSource, mVolume.fsUuid, new UserHandle(0)); 53 54 LayoutInflater inflater = LayoutInflater.from(mContext); 55 mHolder = AppViewHolder.createOrRecycle(inflater, null); 56 } 57 58 @Test storageShouldBeZeroBytesIfQueriedBeforeStorageQueryFinishes()59 public void storageShouldBeZeroBytesIfQueriedBeforeStorageQueryFinishes() { 60 mController.setupView(mHolder); 61 62 assertThat(mHolder.summary.getText().toString()).isEqualTo("0.00B"); 63 } 64 65 @Test storageShouldRepresentStorageStatsQuery()66 public void storageShouldRepresentStorageStatsQuery() throws Exception { 67 when(mSource.getExternalStorageStats(nullable(String.class), nullable(UserHandle.class))) 68 .thenReturn(new StorageStatsSource.ExternalStorageStats(1, 0, 1, 10, 0)); 69 70 mController.queryStats(); 71 mController.setupView(mHolder); 72 73 assertThat(mHolder.summary.getText().toString()).isEqualTo("11.00B"); 74 } 75 76 @Test clickingShouldIntentIntoFilesApp()77 public void clickingShouldIntentIntoFilesApp() { 78 mController.onClick(mFragment); 79 80 final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class); 81 verify(mFragment).startActivity(argumentCaptor.capture()); 82 Intent intent = argumentCaptor.getValue(); 83 84 assertThat(intent.getType()).isEqualTo("image/*"); 85 assertThat(intent.getAction()).isEqualTo(android.content.Intent.ACTION_VIEW); 86 assertThat(intent.getBooleanExtra(Intent.EXTRA_FROM_STORAGE, false)).isTrue(); 87 } 88 } 89