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.deviceinfo;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.Activity;
26 import android.content.pm.PackageManager;
27 import android.os.storage.VolumeInfo;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 
32 import com.android.settings.R;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Answers;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.Robolectric;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.shadows.ShadowApplication;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class PrivateVolumeOptionMenuControllerTest {
46 
47     @Mock
48     private MenuItem mMigrateMenuItem;
49     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
50     private Menu mMenu;
51     @Mock
52     private MenuInflater mMenuInflater;
53     @Mock
54     private PackageManager mPm;
55     @Mock
56     private VolumeInfo mVolumeInfo;
57     @Mock
58     private VolumeInfo mPrimaryInfo;
59 
60     private PrivateVolumeOptionMenuController mController;
61 
62     @Before
setUp()63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65 
66         when(mVolumeInfo.getType()).thenReturn(VolumeInfo.TYPE_PRIVATE);
67         when(mVolumeInfo.isMountedWritable()).thenReturn(true);
68         when(mPrimaryInfo.getType()).thenReturn(VolumeInfo.TYPE_PRIVATE);
69         when(mMenu.findItem(anyInt())).thenReturn(mMigrateMenuItem);
70         when(mMigrateMenuItem.getItemId()).thenReturn(100);
71 
72         mController = new PrivateVolumeOptionMenuController(
73                 Robolectric.setupActivity(Activity.class), mPrimaryInfo, mPm);
74     }
75 
76     @Test
testMigrateDataMenuItemIsAdded()77     public void testMigrateDataMenuItemIsAdded() {
78         mController.onCreateOptionsMenu(mMenu, mMenuInflater);
79 
80         verify(mMenu).add(Menu.NONE, 100, Menu.NONE, R.string.storage_menu_migrate);
81     }
82 
83     @Test
testMigrateDataIsNotVisibleNormally()84     public void testMigrateDataIsNotVisibleNormally() {
85         when(mPm.getPrimaryStorageCurrentVolume()).thenReturn(mPrimaryInfo);
86         when(mPrimaryInfo.isMountedWritable()).thenReturn(true);
87 
88         mController.onCreateOptionsMenu(mMenu, mMenuInflater);
89         mController.onPrepareOptionsMenu(mMenu);
90 
91         verify(mMigrateMenuItem).setVisible(false);
92     }
93 
94     @Test
testMigrateDataIsVisibleWhenExternalVolumeIsPrimary()95     public void testMigrateDataIsVisibleWhenExternalVolumeIsPrimary() {
96         when(mPm.getPrimaryStorageCurrentVolume()).thenReturn(mVolumeInfo);
97 
98         mController.onCreateOptionsMenu(mMenu, mMenuInflater);
99         mController.onPrepareOptionsMenu(mMenu);
100 
101         verify(mMigrateMenuItem).setVisible(true);
102     }
103 
104     @Test
testMigrateDataIsNotVisibleWhenExternalVolumeIsNotMounted()105     public void testMigrateDataIsNotVisibleWhenExternalVolumeIsNotMounted() {
106         when(mPm.getPrimaryStorageCurrentVolume()).thenReturn(mVolumeInfo);
107         when(mVolumeInfo.isMountedWritable()).thenReturn(false);
108 
109         mController.onCreateOptionsMenu(mMenu, mMenuInflater);
110         mController.onPrepareOptionsMenu(mMenu);
111 
112         verify(mMigrateMenuItem).setVisible(false);
113     }
114 
115     @Test
testMigrateDataGoesToMigrateWizard()116     public void testMigrateDataGoesToMigrateWizard() {
117         when(mPm.getPrimaryStorageCurrentVolume()).thenReturn(mVolumeInfo);
118 
119         mController.onCreateOptionsMenu(mMenu, mMenuInflater);
120         mController.onPrepareOptionsMenu(mMenu);
121 
122         assertThat(mController.onOptionsItemSelected(mMigrateMenuItem)).isTrue();
123         ShadowApplication shadowApplication = ShadowApplication.getInstance();
124         assertThat(shadowApplication).isNotNull();
125         assertThat(shadowApplication.getNextStartedActivity().getComponent().getClassName())
126                 .isEqualTo(StorageWizardMigrateConfirm.class.getName());
127     }
128 }
129