1 /*
2  * Copyright (C) 2018 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.deviceinfo;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.os.Bundle;
22 import android.os.storage.VolumeInfo;
23 import android.widget.Button;
24 
25 import com.android.settings.R;
26 import com.android.settings.testutils.shadow.ShadowStorageManager;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.robolectric.RobolectricTestRunner;
33 import org.robolectric.android.util.concurrent.PausedExecutorService;
34 import org.robolectric.annotation.Config;
35 import org.robolectric.shadows.ShadowLooper;
36 import org.robolectric.shadows.ShadowPausedAsyncTask;
37 import org.robolectric.shadows.androidx.fragment.FragmentController;
38 
39 @RunWith(RobolectricTestRunner.class)
40 @Config(shadows = ShadowStorageManager.class)
41 public class PrivateVolumeUnmountTest {
42 
43     private PrivateVolumeUnmount mFragment;
44     private PausedExecutorService mExecutorService;
45 
46     @Before
setUp()47     public void setUp() {
48         mExecutorService = new PausedExecutorService();
49         ShadowPausedAsyncTask.overrideExecutor(mExecutorService);
50         Bundle bundle = new Bundle();
51         bundle.putString(VolumeInfo.EXTRA_VOLUME_ID, "id");
52         mFragment = FragmentController.of(new PrivateVolumeUnmount(), bundle)
53                 .create()
54                 .start()
55                 .resume()
56                 .visible()
57                 .get();
58     }
59 
60     @After
tearDown()61     public void tearDown() {
62         ShadowStorageManager.reset();
63     }
64 
65     @Test
OnClickListener_shouldCallUnmount()66     public void OnClickListener_shouldCallUnmount() {
67         assertThat(ShadowStorageManager.isUnmountCalled()).isFalse();
68 
69         final Button confirm = mFragment.getView().findViewById(R.id.confirm);
70 
71         confirm.performClick();
72         mExecutorService.runAll();
73         ShadowLooper.idleMainLooper();
74 
75         assertThat(ShadowStorageManager.isUnmountCalled()).isTrue();
76     }
77 }