packages/apps/Settings
Revisão | 020af77825326cb24acbdc4aaf276a6976f06444 (tree) |
---|---|
Hora | 2021-03-22 18:43:57 |
Autor | Weng Su <wengsu@goog...> |
Commiter | Weng Su |
Hide edit menu if network is uneditable
- Screenshot:
Bug: 161896161
Test: manual test
make RunSettingsRoboTests ROBOTEST_FILTER=WifiNetworkDetailsFragment2Test
Merged-In: I47ea2cf19e178eff48f23e56903d0daa4ef8fa28
Change-Id: I47ea2cf19e178eff48f23e56903d0daa4ef8fa28
(cherry picked from commit b7dbd3a911ecec24bb7779fa7a85f1a5d6681d07)
@@ -75,7 +75,8 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements | ||
75 | 75 | // Interval between initiating SavedNetworkTracker scans |
76 | 76 | private static final long SCAN_INTERVAL_MILLIS = 10_000; |
77 | 77 | |
78 | - private NetworkDetailsTracker mNetworkDetailsTracker; | |
78 | + @VisibleForTesting | |
79 | + NetworkDetailsTracker mNetworkDetailsTracker; | |
79 | 80 | private HandlerThread mWorkerThread; |
80 | 81 | private WifiDetailPreferenceController2 mWifiDetailPreferenceController2; |
81 | 82 | private List<WifiDialog2.WifiDialog2Listener> mWifiDialogListeners = new ArrayList<>(); |
@@ -125,9 +126,11 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements | ||
125 | 126 | |
126 | 127 | @Override |
127 | 128 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
128 | - MenuItem item = menu.add(0, Menu.FIRST, 0, R.string.wifi_modify); | |
129 | - item.setIcon(com.android.internal.R.drawable.ic_mode_edit); | |
130 | - item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); | |
129 | + if (isEditable()) { | |
130 | + MenuItem item = menu.add(0, Menu.FIRST, 0, R.string.wifi_modify); | |
131 | + item.setIcon(com.android.internal.R.drawable.ic_mode_edit); | |
132 | + item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); | |
133 | + } | |
131 | 134 | super.onCreateOptionsMenu(menu, inflater); |
132 | 135 | } |
133 | 136 |
@@ -253,6 +256,17 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements | ||
253 | 256 | getArguments().getString(KEY_CHOSEN_WIFIENTRY_KEY)); |
254 | 257 | } |
255 | 258 | |
259 | + private boolean isEditable() { | |
260 | + if (mNetworkDetailsTracker == null) { | |
261 | + return false; | |
262 | + } | |
263 | + final WifiEntry wifiEntry = mNetworkDetailsTracker.getWifiEntry(); | |
264 | + if (wifiEntry == null) { | |
265 | + return false; | |
266 | + } | |
267 | + return wifiEntry.isSaved(); | |
268 | + } | |
269 | + | |
256 | 270 | /** |
257 | 271 | * API call for refreshing the preferences in this fragment. |
258 | 272 | */ |
@@ -24,9 +24,9 @@ import static org.mockito.ArgumentMatchers.anyInt; | ||
24 | 24 | import static org.mockito.ArgumentMatchers.eq; |
25 | 25 | import static org.mockito.Mockito.doReturn; |
26 | 26 | import static org.mockito.Mockito.mock; |
27 | +import static org.mockito.Mockito.never; | |
27 | 28 | import static org.mockito.Mockito.spy; |
28 | 29 | import static org.mockito.Mockito.verify; |
29 | -import static org.mockito.Mockito.when; | |
30 | 30 | |
31 | 31 | import android.app.settings.SettingsEnums; |
32 | 32 | import android.view.Menu; |
@@ -36,12 +36,17 @@ import android.view.MenuItem; | ||
36 | 36 | import androidx.preference.Preference; |
37 | 37 | import androidx.preference.PreferenceScreen; |
38 | 38 | |
39 | +import com.android.settings.R; | |
39 | 40 | import com.android.settings.core.BasePreferenceController; |
40 | 41 | import com.android.settingslib.core.AbstractPreferenceController; |
42 | +import com.android.wifitrackerlib.NetworkDetailsTracker; | |
43 | +import com.android.wifitrackerlib.WifiEntry; | |
41 | 44 | |
42 | 45 | import org.junit.Before; |
43 | 46 | import org.junit.Test; |
44 | 47 | import org.junit.runner.RunWith; |
48 | +import org.mockito.Mock; | |
49 | +import org.mockito.MockitoAnnotations; | |
45 | 50 | import org.robolectric.RobolectricTestRunner; |
46 | 51 | import org.robolectric.RuntimeEnvironment; |
47 | 52 |
@@ -52,11 +57,22 @@ public class WifiNetworkDetailsFragment2Test { | ||
52 | 57 | |
53 | 58 | final String TEST_PREFERENCE_KEY = "TEST_PREFERENCE_KEY"; |
54 | 59 | |
60 | + @Mock | |
61 | + WifiEntry mWifiEntry; | |
62 | + @Mock | |
63 | + NetworkDetailsTracker mNetworkDetailsTracker; | |
64 | + @Mock | |
65 | + Menu mMenu; | |
55 | 66 | private WifiNetworkDetailsFragment2 mFragment; |
56 | 67 | |
57 | 68 | @Before |
58 | 69 | public void setUp() { |
70 | + MockitoAnnotations.initMocks(this); | |
71 | + doReturn(mWifiEntry).when(mNetworkDetailsTracker).getWifiEntry(); | |
72 | + doReturn(true).when(mWifiEntry).isSaved(); | |
73 | + | |
59 | 74 | mFragment = new WifiNetworkDetailsFragment2(); |
75 | + mFragment.mNetworkDetailsTracker = mNetworkDetailsTracker; | |
60 | 76 | } |
61 | 77 | |
62 | 78 | @Test |
@@ -77,16 +93,24 @@ public class WifiNetworkDetailsFragment2Test { | ||
77 | 93 | |
78 | 94 | @Test |
79 | 95 | public void onCreateOptionsMenu_shouldSetCorrectIcon() { |
80 | - final Menu menu = mock(Menu.class); | |
81 | 96 | final MenuItem menuItem = mock(MenuItem.class); |
82 | - doReturn(menuItem).when(menu).add(anyInt(), eq(Menu.FIRST), anyInt(), anyInt()); | |
97 | + doReturn(menuItem).when(mMenu).add(anyInt(), eq(Menu.FIRST), anyInt(), anyInt()); | |
83 | 98 | |
84 | - mFragment.onCreateOptionsMenu(menu, mock(MenuInflater.class)); | |
99 | + mFragment.onCreateOptionsMenu(mMenu, mock(MenuInflater.class)); | |
85 | 100 | |
86 | 101 | verify(menuItem).setIcon(com.android.internal.R.drawable.ic_mode_edit); |
87 | 102 | } |
88 | 103 | |
89 | 104 | @Test |
105 | + public void onCreateOptionsMenu_isNotSavedNetwork_shouldNotAddEditMenu() { | |
106 | + doReturn(false).when(mWifiEntry).isSaved(); | |
107 | + | |
108 | + mFragment.onCreateOptionsMenu(mMenu, mock(MenuInflater.class)); | |
109 | + | |
110 | + verify(mMenu, never()).add(anyInt(), anyInt(), anyInt(), eq(R.string.wifi_modify)); | |
111 | + } | |
112 | + | |
113 | + @Test | |
90 | 114 | public void refreshPreferences_controllerShouldUpdateStateAndDisplayPreference() { |
91 | 115 | final FakeFragment fragment = spy(new FakeFragment()); |
92 | 116 | final PreferenceScreen screen = mock(PreferenceScreen.class); |