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
17package com.android.settings.applications;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.mockito.Matchers.nullable;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import android.app.Fragment;
26import android.content.Context;
27import android.content.Intent;
28import android.os.UserHandle;
29import android.os.storage.VolumeInfo;
30import android.provider.DocumentsContract;
31import android.view.LayoutInflater;
32
33import com.android.settings.testutils.SettingsRobolectricTestRunner;
34import com.android.settings.TestConfig;
35import com.android.settingslib.applications.StorageStatsSource;
36import com.android.settingslib.deviceinfo.StorageVolumeProvider;
37
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.mockito.Answers;
42import org.mockito.ArgumentCaptor;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45import org.robolectric.RuntimeEnvironment;
46import org.robolectric.annotation.Config;
47
48
49@RunWith(SettingsRobolectricTestRunner.class)
50@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
51public class MusicViewHolderControllerTest {
52    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
53    private Fragment mFragment;
54    @Mock
55    private StorageVolumeProvider mSvp;
56    @Mock
57    private StorageStatsSource mSource;
58
59    private Context mContext;
60    private MusicViewHolderController mController;
61    private VolumeInfo mVolume;
62    private AppViewHolder mHolder;
63
64    @Before
65    public void setUp() throws Exception {
66        MockitoAnnotations.initMocks(this);
67        mContext = RuntimeEnvironment.application;
68        mVolume = new VolumeInfo("id", 0, null, "id");
69        mController = new MusicViewHolderController(mContext, mSource, mVolume.fsUuid,
70                new UserHandle(0));
71
72        LayoutInflater inflater = LayoutInflater.from(mContext);
73        mHolder = AppViewHolder.createOrRecycle(inflater, null);
74    }
75
76    @Test
77    public void storageShouldBeZeroBytesIfQueriedBeforeStorageQueryFinishes() {
78        mController.setupView(mHolder);
79
80        assertThat(mHolder.summary.getText().toString()).isEqualTo("0.00B");
81    }
82
83    @Test
84    public void storageShouldRepresentStorageStatsQuery() throws Exception {
85        when(mSource.getExternalStorageStats(nullable(String.class), nullable(UserHandle.class))).thenReturn(
86                new StorageStatsSource.ExternalStorageStats(1, 1, 0, 0, 0));
87
88        mController.queryStats();
89        mController.setupView(mHolder);
90
91        assertThat(mHolder.summary.getText().toString()).isEqualTo("1.00B");
92    }
93
94    @Test
95    public void clickingShouldIntentIntoFilesApp() {
96        mController.onClick(mFragment);
97
98        final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
99        verify(mFragment).startActivity(argumentCaptor.capture());
100        Intent intent = argumentCaptor.getValue();
101
102        assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
103        assertThat(intent.getData()).isEqualTo(DocumentsContract.buildRootUri(
104                "com.android.providers.media.documents",
105                "audio_root"));
106        assertThat(intent.getType()).isEqualTo(DocumentsContract.Root.MIME_TYPE_ITEM);
107    }
108}
109