1/*
2 * Copyright (C) 2016 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 */
16package com.android.settings.deviceinfo.storage;
17
18
19import static com.android.settings.applications.ManageApplications.EXTRA_WORK_ID;
20import static com.android.settings.applications.ManageApplications.EXTRA_WORK_ONLY;
21import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES;
22
23import static com.google.common.truth.Truth.assertThat;
24
25import static org.mockito.ArgumentMatchers.nullable;
26import static org.mockito.Matchers.eq;
27import static org.mockito.Mockito.mock;
28import static org.mockito.Mockito.spy;
29import static org.mockito.Mockito.times;
30import static org.mockito.Mockito.verify;
31import static org.mockito.Mockito.when;
32
33import android.app.Fragment;
34import android.content.Context;
35import android.content.Intent;
36import android.graphics.drawable.Drawable;
37import android.os.UserHandle;
38import android.os.storage.VolumeInfo;
39import android.support.v7.preference.PreferenceScreen;
40import android.util.SparseArray;
41import android.view.LayoutInflater;
42import android.view.View;
43import android.widget.LinearLayout;
44
45import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
46import com.android.settings.R;
47import com.android.settings.SettingsActivity;
48import com.android.settings.testutils.SettingsRobolectricTestRunner;
49import com.android.settings.SubSettings;
50import com.android.settings.TestConfig;
51import com.android.settings.applications.ManageApplications;
52import com.android.settings.core.instrumentation.MetricsFeatureProvider;
53import com.android.settings.deviceinfo.PrivateVolumeSettings;
54import com.android.settings.deviceinfo.StorageItemPreference;
55import com.android.settings.testutils.FakeFeatureFactory;
56import com.android.settings.testutils.shadow.SettingsShadowResources;
57import com.android.settingslib.applications.StorageStatsSource;
58import com.android.settingslib.deviceinfo.StorageVolumeProvider;
59
60import org.junit.After;
61import org.junit.Before;
62import org.junit.Test;
63import org.junit.runner.RunWith;
64import org.mockito.Answers;
65import org.mockito.ArgumentCaptor;
66import org.mockito.Mock;
67import org.mockito.MockitoAnnotations;
68import org.robolectric.RuntimeEnvironment;
69import org.robolectric.annotation.Config;
70
71@RunWith(SettingsRobolectricTestRunner.class)
72@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
73public class StorageItemPreferenceControllerTest {
74    private Context mContext;
75    private VolumeInfo mVolume;
76    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
77    private Fragment mFragment;
78    @Mock
79    private StorageVolumeProvider mSvp;
80    private StorageItemPreferenceController mController;
81    private StorageItemPreference mPreference;
82    private FakeFeatureFactory mFakeFeatureFactory;
83    private MetricsFeatureProvider mMetricsFeatureProvider;
84
85    @Before
86    public void setUp() throws Exception {
87        MockitoAnnotations.initMocks(this);
88        mContext = spy(RuntimeEnvironment.application.getApplicationContext());
89        FakeFeatureFactory.setupForTest(mContext);
90        mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
91        mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider();
92        mVolume = spy(new VolumeInfo("id", 0, null, "id"));
93        // Note: null is passed as the Lifecycle because we are handling it outside of the normal
94        //       Settings fragment lifecycle for test purposes.
95        mController = new StorageItemPreferenceController(mContext, mFragment, mVolume, mSvp);
96        mPreference = new StorageItemPreference(mContext);
97
98        // Inflate the preference and the widget.
99        LayoutInflater inflater = LayoutInflater.from(mContext);
100        final View view = inflater.inflate(
101                mPreference.getLayoutResource(), new LinearLayout(mContext), false);
102    }
103
104    @After
105    public void tearDown() {
106        SettingsShadowResources.reset();
107    }
108
109    @Test
110    public void testUpdateStateWithInitialState() {
111        assertThat(mPreference.getSummary().toString()).isEqualTo(
112                mContext.getString(R.string.memory_calculating_size));
113    }
114
115    @Test
116    public void testClickPhotos() {
117        mPreference.setKey("pref_photos_videos");
118        mController.handlePreferenceTreeClick(mPreference);
119
120        final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
121        verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(),
122                nullable(UserHandle.class));
123
124        Intent intent = argumentCaptor.getValue();
125        assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
126        assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
127        assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
128                .isEqualTo(ManageApplications.class.getName());
129        assertThat(intent.getIntExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, 0))
130                .isEqualTo(R.string.storage_photos_videos);
131    }
132
133    @Test
134    public void testClickAudio() {
135        mPreference.setKey("pref_music_audio");
136        mController.handlePreferenceTreeClick(mPreference);
137
138        final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
139        verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(),
140                nullable(UserHandle.class));
141        Intent intent = argumentCaptor.getValue();
142
143        assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
144        assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
145        assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
146                ManageApplications.class.getName());
147        assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS).getInt(
148                ManageApplications.EXTRA_STORAGE_TYPE, 0)).isEqualTo(
149                ManageApplications.STORAGE_TYPE_MUSIC);
150    }
151
152    @Test
153    public void handlePreferenceTreeClick_tappingAudioWhileUninitializedDoesntCrash() {
154        mController.setVolume(null);
155
156        mPreference.setKey("pref_music_audio");
157        mController.handlePreferenceTreeClick(mPreference);
158    }
159
160    @Test
161    public void testClickApps() {
162        mPreference.setKey("pref_other_apps");
163        mController.handlePreferenceTreeClick(mPreference);
164
165        final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
166        verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(),
167                nullable(UserHandle.class));
168
169        Intent intent = argumentCaptor.getValue();
170        assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
171        assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
172        assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
173                ManageApplications.class.getName());
174        assertThat(intent.getIntExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, 0))
175                .isEqualTo(R.string.apps_storage);
176    }
177
178    @Test
179    public void testClickAppsForWork() {
180        mController = new StorageItemPreferenceController(mContext, mFragment, mVolume, mSvp, true);
181        mPreference.setKey("pref_other_apps");
182        mController.handlePreferenceTreeClick(mPreference);
183
184        final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
185        verify(mFragment.getActivity())
186                .startActivityAsUser(argumentCaptor.capture(), nullable(UserHandle.class));
187
188        Intent intent = argumentCaptor.getValue();
189        assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
190        assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
191        assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
192                .isEqualTo(ManageApplications.class.getName());
193        assertThat(intent.getIntExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, 0))
194                .isEqualTo(R.string.apps_storage);
195        assertThat(
196                        intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS)
197                                .getBoolean(EXTRA_WORK_ONLY))
198                .isTrue();
199        assertThat(
200                        intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS)
201                                .getInt(EXTRA_WORK_ID))
202                .isEqualTo(0);
203    }
204
205    @Test
206    public void handlePreferenceTreeClick_tappingAppsWhileUninitializedDoesntCrash() {
207        mController.setVolume(null);
208
209        mPreference.setKey("pref_other_apps");
210        mController.handlePreferenceTreeClick(mPreference);
211    }
212
213    @Test
214    public void testClickFiles() {
215        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(mVolume);
216        mPreference.setKey("pref_files");
217        mController.handlePreferenceTreeClick(mPreference);
218
219        final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
220        verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(),
221                nullable(UserHandle.class));
222
223        Intent intent = argumentCaptor.getValue();
224        Intent browseIntent = mVolume.buildBrowseIntent();
225        assertThat(intent.getAction()).isEqualTo(browseIntent.getAction());
226        assertThat(intent.getData()).isEqualTo(browseIntent.getData());
227        verify(mMetricsFeatureProvider, times(1)).action(
228                nullable(Context.class), eq(MetricsEvent.STORAGE_FILES));
229    }
230
231    @Test
232    public void testClickGames() {
233        mPreference.setKey("pref_games");
234        mController.handlePreferenceTreeClick(mPreference);
235
236        final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
237        verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(),
238                nullable(UserHandle.class));
239
240        Intent intent = argumentCaptor.getValue();
241        assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
242        assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
243        assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
244                ManageApplications.class.getName());
245        assertThat(intent.getIntExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, 0))
246                .isEqualTo(R.string.game_storage_settings);
247    }
248
249    @Test
250    public void testClickMovies() {
251        mPreference.setKey("pref_movies");
252        mController.handlePreferenceTreeClick(mPreference);
253
254        final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
255        verify(mFragment.getActivity()).startActivityAsUser(argumentCaptor.capture(),
256                nullable(UserHandle.class));
257
258        Intent intent = argumentCaptor.getValue();
259        assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MAIN);
260        assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
261        assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
262                ManageApplications.class.getName());
263        assertThat(intent.getIntExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, 0))
264                .isEqualTo(R.string.storage_movies_tv);
265    }
266
267    @Test
268    public void testClickSystem() {
269        mPreference.setKey("pref_system");
270        assertThat(mController.handlePreferenceTreeClick(mPreference)).isTrue();
271
272        verify(mFragment.getFragmentManager().beginTransaction()).add(
273                nullable(PrivateVolumeSettings.SystemInfoFragment.class), nullable(String.class));
274    }
275
276    @Test
277    public void testMeasurementCompletedUpdatesPreferences() {
278        StorageItemPreference audio = new StorageItemPreference(mContext);
279        StorageItemPreference image = new StorageItemPreference(mContext);
280        StorageItemPreference games = new StorageItemPreference(mContext);
281        StorageItemPreference movies = new StorageItemPreference(mContext);
282        StorageItemPreference apps = new StorageItemPreference(mContext);
283        StorageItemPreference system = new StorageItemPreference(mContext);
284        StorageItemPreference files = new StorageItemPreference(mContext);
285        PreferenceScreen screen = mock(PreferenceScreen.class);
286        when(screen.findPreference(
287                eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio);
288        when(screen.findPreference(
289                eq(StorageItemPreferenceController.PHOTO_KEY))).thenReturn(image);
290        when(screen.findPreference(
291                eq(StorageItemPreferenceController.GAME_KEY))).thenReturn(games);
292        when(screen.findPreference(
293                eq(StorageItemPreferenceController.MOVIES_KEY))).thenReturn(movies);
294        when(screen.findPreference(
295                eq(StorageItemPreferenceController.OTHER_APPS_KEY))).thenReturn(apps);
296        when(screen.findPreference(
297                eq(StorageItemPreferenceController.SYSTEM_KEY))).thenReturn(system);
298        when(screen.findPreference(
299                eq(StorageItemPreferenceController.FILES_KEY))).thenReturn(files);
300        mController.displayPreference(screen);
301
302        mController.setUsedSize(MEGABYTE_IN_BYTES * 970); // There should 870MB attributed.
303        StorageAsyncLoader.AppsStorageResult result = new StorageAsyncLoader.AppsStorageResult();
304        result.gamesSize = MEGABYTE_IN_BYTES * 80;
305        result.videoAppsSize = MEGABYTE_IN_BYTES * 160;
306        result.musicAppsSize = MEGABYTE_IN_BYTES * 40;
307        result.otherAppsSize = MEGABYTE_IN_BYTES * 90;
308        result.externalStats =
309                new StorageStatsSource.ExternalStorageStats(
310                        MEGABYTE_IN_BYTES * 500, // total
311                        MEGABYTE_IN_BYTES * 100, // audio
312                        MEGABYTE_IN_BYTES * 150, // video
313                        MEGABYTE_IN_BYTES * 200, 0); // image
314
315        SparseArray<StorageAsyncLoader.AppsStorageResult> results = new SparseArray<>();
316        results.put(0, result);
317        mController.onLoadFinished(results, 0);
318
319        assertThat(audio.getSummary().toString()).isEqualTo("0.14 GB");
320        assertThat(image.getSummary().toString()).isEqualTo("0.35 GB");
321        assertThat(games.getSummary().toString()).isEqualTo("0.08 GB");
322        assertThat(movies.getSummary().toString()).isEqualTo("0.16 GB");
323        assertThat(apps.getSummary().toString()).isEqualTo("0.09 GB");
324        assertThat(files.getSummary().toString()).isEqualTo("0.05 GB");
325    }
326
327    @Test
328    public void settingUserIdAppliesNewIcons() {
329        StorageItemPreference audio = spy(new StorageItemPreference(mContext));
330        audio.setIcon(R.drawable.ic_media_stream);
331        StorageItemPreference video = spy(new StorageItemPreference(mContext));
332        video.setIcon(R.drawable.ic_local_movies);
333        StorageItemPreference image = spy(new StorageItemPreference(mContext));
334        image.setIcon(R.drawable.ic_photo_library);
335        StorageItemPreference games = spy(new StorageItemPreference(mContext));
336        games.setIcon(R.drawable.ic_videogame_vd_theme_24);
337        StorageItemPreference apps = spy(new StorageItemPreference(mContext));
338        apps.setIcon(R.drawable.ic_storage_apps);
339        StorageItemPreference system = spy(new StorageItemPreference(mContext));
340        system.setIcon(R.drawable.ic_system_update_vd_theme_24);
341        StorageItemPreference files = spy(new StorageItemPreference(mContext));
342        files.setIcon(R.drawable.ic_folder_vd_theme_24);
343        PreferenceScreen screen = mock(PreferenceScreen.class);
344        when(screen.findPreference(
345                eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio);
346        when(screen.findPreference(
347                eq(StorageItemPreferenceController.MOVIES_KEY))).thenReturn(video);
348        when(screen.findPreference(
349                eq(StorageItemPreferenceController.PHOTO_KEY))).thenReturn(image);
350        when(screen.findPreference(
351                eq(StorageItemPreferenceController.GAME_KEY))).thenReturn(games);
352        when(screen.findPreference(
353                eq(StorageItemPreferenceController.OTHER_APPS_KEY))).thenReturn(apps);
354        when(screen.findPreference(
355                eq(StorageItemPreferenceController.SYSTEM_KEY))).thenReturn(system);
356        when(screen.findPreference(
357                eq(StorageItemPreferenceController.FILES_KEY))).thenReturn(files);
358        mController.displayPreference(screen);
359
360        mController.setUserId(new UserHandle(10));
361
362        verify(audio, times(2)).setIcon(nullable(Drawable.class));
363        verify(video, times(2)).setIcon(nullable(Drawable.class));
364        verify(image, times(2)).setIcon(nullable(Drawable.class));
365        verify(games, times(2)).setIcon(nullable(Drawable.class));
366        verify(apps, times(2)).setIcon(nullable(Drawable.class));
367        verify(system, times(2)).setIcon(nullable(Drawable.class));
368        verify(files, times(2)).setIcon(nullable(Drawable.class));
369    }
370
371    @Test
372    public void displayPreference_dontHideFilePreferenceWhenEmulatedInternalStorageUsed() {
373        StorageItemPreference audio = new StorageItemPreference(mContext);
374        StorageItemPreference image = new StorageItemPreference(mContext);
375        StorageItemPreference games = new StorageItemPreference(mContext);
376        StorageItemPreference apps = new StorageItemPreference(mContext);
377        StorageItemPreference system = new StorageItemPreference(mContext);
378        StorageItemPreference files = new StorageItemPreference(mContext);
379        PreferenceScreen screen = mock(PreferenceScreen.class);
380        when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
381                .thenReturn(audio);
382        when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
383                .thenReturn(image);
384        when(screen.findPreference(eq(StorageItemPreferenceController.GAME_KEY))).thenReturn(games);
385        when(screen.findPreference(eq(StorageItemPreferenceController.OTHER_APPS_KEY)))
386                .thenReturn(apps);
387        when(screen.findPreference(eq(StorageItemPreferenceController.SYSTEM_KEY)))
388                .thenReturn(system);
389        when(screen.findPreference(eq(StorageItemPreferenceController.FILES_KEY)))
390                .thenReturn(files);
391
392        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(mVolume);
393        when(mVolume.isMountedReadable()).thenReturn(true);
394
395        mController.displayPreference(screen);
396
397        verify(screen, times(0)).removePreference(files);
398    }
399
400    @Test
401    public void displayPreference_hideFilePreferenceWhenEmulatedStorageUnreadable() {
402        StorageItemPreference audio = new StorageItemPreference(mContext);
403        StorageItemPreference image = new StorageItemPreference(mContext);
404        StorageItemPreference games = new StorageItemPreference(mContext);
405        StorageItemPreference apps = new StorageItemPreference(mContext);
406        StorageItemPreference system = new StorageItemPreference(mContext);
407        StorageItemPreference files = new StorageItemPreference(mContext);
408        PreferenceScreen screen = mock(PreferenceScreen.class);
409        when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
410                .thenReturn(audio);
411        when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
412                .thenReturn(image);
413        when(screen.findPreference(eq(StorageItemPreferenceController.GAME_KEY))).thenReturn(games);
414        when(screen.findPreference(eq(StorageItemPreferenceController.OTHER_APPS_KEY)))
415                .thenReturn(apps);
416        when(screen.findPreference(eq(StorageItemPreferenceController.SYSTEM_KEY)))
417                .thenReturn(system);
418        when(screen.findPreference(eq(StorageItemPreferenceController.FILES_KEY)))
419                .thenReturn(files);
420
421        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(mVolume);
422        when(mVolume.isMountedReadable()).thenReturn(false);
423
424        mController.displayPreference(screen);
425
426        verify(screen).removePreference(files);
427    }
428
429    @Test
430    public void displayPreference_hideFilePreferenceWhenNoEmulatedInternalStorage() {
431        StorageItemPreference audio = new StorageItemPreference(mContext);
432        StorageItemPreference image = new StorageItemPreference(mContext);
433        StorageItemPreference games = new StorageItemPreference(mContext);
434        StorageItemPreference apps = new StorageItemPreference(mContext);
435        StorageItemPreference system = new StorageItemPreference(mContext);
436        StorageItemPreference files = new StorageItemPreference(mContext);
437        PreferenceScreen screen = mock(PreferenceScreen.class);
438        when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
439                .thenReturn(audio);
440        when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
441                .thenReturn(image);
442        when(screen.findPreference(eq(StorageItemPreferenceController.GAME_KEY))).thenReturn(games);
443        when(screen.findPreference(eq(StorageItemPreferenceController.OTHER_APPS_KEY)))
444                .thenReturn(apps);
445        when(screen.findPreference(eq(StorageItemPreferenceController.SYSTEM_KEY)))
446                .thenReturn(system);
447        when(screen.findPreference(eq(StorageItemPreferenceController.FILES_KEY)))
448                .thenReturn(files);
449
450        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(null);
451
452        mController.displayPreference(screen);
453
454        verify(screen).removePreference(files);
455    }
456
457    @Test
458    public void displayPreference_updateFilePreferenceToHideAfterSettingVolume() {
459        StorageItemPreference audio = new StorageItemPreference(mContext);
460        StorageItemPreference image = new StorageItemPreference(mContext);
461        StorageItemPreference games = new StorageItemPreference(mContext);
462        StorageItemPreference apps = new StorageItemPreference(mContext);
463        StorageItemPreference system = new StorageItemPreference(mContext);
464        StorageItemPreference files = new StorageItemPreference(mContext);
465        PreferenceScreen screen = mock(PreferenceScreen.class);
466        when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
467                .thenReturn(audio);
468        when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
469                .thenReturn(image);
470        when(screen.findPreference(eq(StorageItemPreferenceController.GAME_KEY))).thenReturn(games);
471        when(screen.findPreference(eq(StorageItemPreferenceController.OTHER_APPS_KEY)))
472                .thenReturn(apps);
473        when(screen.findPreference(eq(StorageItemPreferenceController.SYSTEM_KEY)))
474                .thenReturn(system);
475        when(screen.findPreference(eq(StorageItemPreferenceController.FILES_KEY)))
476                .thenReturn(files);
477
478        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(mVolume);
479        when(mVolume.isMountedReadable()).thenReturn(true);
480
481        mController.displayPreference(screen);
482        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(null);
483        mController.setVolume(mVolume);
484
485        verify(screen).removePreference(files);
486    }
487
488
489    @Test
490    public void displayPreference_updateFilePreferenceToShowAfterSettingVolume() {
491        StorageItemPreference audio = new StorageItemPreference(mContext);
492        StorageItemPreference image = new StorageItemPreference(mContext);
493        StorageItemPreference games = new StorageItemPreference(mContext);
494        StorageItemPreference apps = new StorageItemPreference(mContext);
495        StorageItemPreference system = new StorageItemPreference(mContext);
496        StorageItemPreference files = new StorageItemPreference(mContext);
497        PreferenceScreen screen = mock(PreferenceScreen.class);
498        when(screen.findPreference(eq(StorageItemPreferenceController.AUDIO_KEY)))
499                .thenReturn(audio);
500        when(screen.findPreference(eq(StorageItemPreferenceController.PHOTO_KEY)))
501                .thenReturn(image);
502        when(screen.findPreference(eq(StorageItemPreferenceController.GAME_KEY))).thenReturn(games);
503        when(screen.findPreference(eq(StorageItemPreferenceController.OTHER_APPS_KEY)))
504                .thenReturn(apps);
505        when(screen.findPreference(eq(StorageItemPreferenceController.SYSTEM_KEY)))
506                .thenReturn(system);
507        when(screen.findPreference(eq(StorageItemPreferenceController.FILES_KEY)))
508                .thenReturn(files);
509
510        // This will hide it initially.
511        mController.displayPreference(screen);
512
513        when(mSvp.findEmulatedForPrivate(nullable(VolumeInfo.class))).thenReturn(mVolume);
514        when(mVolume.isMountedReadable()).thenReturn(true);
515
516        // And we bring it back.
517        mController.setVolume(mVolume);
518
519        verify(screen).addPreference(files);
520    }
521}
522