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.Matchers.anyInt;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.os.storage.VolumeInfo;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 
30 import com.android.settings.R;
31 import com.android.settings.SettingsRobolectricTestRunner;
32 import com.android.settings.TestConfig;
33 import com.android.settings.applications.PackageManagerWrapper;
34 import com.android.settings.testutils.FakeFeatureFactory;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Answers;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.annotation.Config;
44 import org.robolectric.shadows.ShadowApplication;
45 
46 @RunWith(SettingsRobolectricTestRunner.class)
47 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
48 public class PrivateVolumeOptionMenuControllerTest {
49     @Mock
50     private MenuItem mMigrateMenuItem;
51     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
52     private Menu mMenu;
53     @Mock
54     private MenuInflater mMenuInflater;
55     @Mock
56     private PackageManagerWrapper mPm;
57     @Mock
58     private VolumeInfo mVolumeInfo;
59     @Mock
60     private VolumeInfo mPrimaryInfo;
61 
62     private PrivateVolumeOptionMenuController mController;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67 
68         when(mVolumeInfo.getType()).thenReturn(VolumeInfo.TYPE_PRIVATE);
69         when(mPrimaryInfo.getType()).thenReturn(VolumeInfo.TYPE_PRIVATE);
70         when(mMenu.findItem(anyInt())).thenReturn(mMigrateMenuItem);
71         when(mMigrateMenuItem.getItemId()).thenReturn(100);
72 
73         mController = new PrivateVolumeOptionMenuController(
74                 RuntimeEnvironment.application, mPrimaryInfo, mPm);
75     }
76 
77     @Test
testMigrateDataMenuItemIsAdded()78     public void testMigrateDataMenuItemIsAdded() {
79         mController.onCreateOptionsMenu(mMenu, mMenuInflater);
80 
81         verify(mMenu).add(Menu.NONE, 100, Menu.NONE, R.string.storage_menu_migrate);
82     }
83 
84     @Test
testMigrateDataIsNotVisibleNormally()85     public void testMigrateDataIsNotVisibleNormally() {
86         when(mPm.getPrimaryStorageCurrentVolume()).thenReturn(mPrimaryInfo);
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
testMigrateDataGoesToMigrateWizard()105     public void testMigrateDataGoesToMigrateWizard() {
106         when(mPm.getPrimaryStorageCurrentVolume()).thenReturn(mVolumeInfo);
107 
108         mController.onCreateOptionsMenu(mMenu, mMenuInflater);
109         mController.onPrepareOptionsMenu(mMenu);
110 
111         assertThat(mController.onOptionsItemSelected(mMigrateMenuItem)).isTrue();
112         assertThat(ShadowApplication.getInstance()
113                 .getNextStartedActivity().getComponent().getClassName())
114                 .isEqualTo(StorageWizardMigrateConfirm.class.getName());
115     }
116 }
117