Loading src/com/android/settings/panel/InternetConnectivityPanel.java +47 −15 Original line number Diff line number Diff line Loading @@ -84,12 +84,13 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve } if (TextUtils.equals(intent.getAction(), WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { showProgressBar(); updateProgressBar(); updatePanelTitle(); return; } if (TextUtils.equals(intent.getAction(), WifiManager.NETWORK_STATE_CHANGED_ACTION)) { updateProgressBar(); updatePanelTitle(); } } Loading @@ -110,13 +111,40 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve private int mDefaultDataSubid = SubscriptionManager.INVALID_SUBSCRIPTION_ID; // Wi-Fi scanning progress bar protected HandlerInjector mHandlerInjector; protected boolean mIsProgressBarVisible; protected final Runnable mHideProgressBarRunnable = () -> { protected boolean mIsScanningSubTitleShownOnce; protected Runnable mHideProgressBarRunnable = () -> { setProgressBarVisible(false); }; protected Runnable mHideScanningSubTitleRunnable = () -> { mIsScanningSubTitleShownOnce = true; updatePanelTitle(); }; /** * Wrapper for testing compatibility. */ @VisibleForTesting static class HandlerInjector { protected final Handler mHandler; HandlerInjector(Context context) { mHandler = context.getMainThreadHandler(); } public void postDelay(Runnable runnable) { mHandler.postDelayed(runnable, 2000 /* delay millis */); } public void removeCallbacks(Runnable runnable) { mHandler.removeCallbacks(runnable); } } private InternetConnectivityPanel(Context context) { mContext = context.getApplicationContext(); mHandlerInjector = new HandlerInjector(context); mIsProviderModelEnabled = Utils.isProviderModelEnabled(mContext); mInternetUpdater = new InternetUpdater(context, null /* Lifecycle */, this); Loading Loading @@ -150,7 +178,7 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve mTelephonyManager.registerTelephonyCallback( new HandlerExecutor(new Handler(Looper.getMainLooper())), mTelephonyCallback); mContext.registerReceiver(mWifiStateReceiver, mWifiStateFilter); showProgressBar(); updateProgressBar(); updatePanelTitle(); } Loading @@ -165,7 +193,8 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve mConnectivityListener.stop(); mTelephonyManager.unregisterTelephonyCallback(mTelephonyCallback); mContext.unregisterReceiver(mWifiStateReceiver); mContext.getMainThreadHandler().removeCallbacks(mHideProgressBarRunnable); mHandlerInjector.removeCallbacks(mHideProgressBarRunnable); mHandlerInjector.removeCallbacks(mHideScanningSubTitleRunnable); } /** Loading Loading @@ -246,6 +275,7 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve */ @Override public void onAirplaneModeChanged(boolean isAirplaneModeOn) { log("onAirplaneModeChanged: isAirplaneModeOn:" + isAirplaneModeOn); updatePanelTitle(); } Loading @@ -254,6 +284,7 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve */ @Override public void onWifiEnabledChanged(boolean enabled) { log("onWifiEnabledChanged: enabled:" + enabled); updatePanelTitle(); } Loading Loading @@ -305,13 +336,6 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve return; } if (mIsProgressBarVisible) { // When the Wi-Fi scan result callback is received // Sub-Title: Searching for networks... mSubtitle = SUBTITLE_TEXT_SEARCHING_FOR_NETWORKS; return; } if (mInternetUpdater.isAirplaneModeOn()) { return; } Loading @@ -319,11 +343,18 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve final List<ScanResult> wifiList = mWifiManager.getScanResults(); if (wifiList != null && wifiList.size() != 0) { // When the Wi-Fi scan result is not empty // Sub-Title: Select the network you want to use for data // Sub-Title: Tap a network to connect mSubtitle = SUBTITLE_TEXT_TAP_A_NETWORK_TO_CONNECT; return; } if (!mIsScanningSubTitleShownOnce && mIsProgressBarVisible) { // When the Wi-Fi scan result callback is received // Sub-Title: Searching for networks... mSubtitle = SUBTITLE_TEXT_SEARCHING_FOR_NETWORKS; return; } // Sub-Title: // show non_carrier_network_unavailable // - while Wi-Fi on + no Wi-Fi item Loading Loading @@ -353,7 +384,7 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve mSubtitle = SUBTITLE_TEXT_NON_CARRIER_NETWORK_UNAVAILABLE; } protected void showProgressBar() { protected void updateProgressBar() { if (mWifiManager == null || !mInternetUpdater.isWifiEnabled()) { setProgressBarVisible(false); return; Loading @@ -362,8 +393,9 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve setProgressBarVisible(true); List<ScanResult> wifiScanResults = mWifiManager.getScanResults(); if (wifiScanResults != null && wifiScanResults.size() > 0) { mContext.getMainThreadHandler().postDelayed(mHideProgressBarRunnable, 2000 /* delay millis */); mHandlerInjector.postDelay(mHideProgressBarRunnable); } else if (!mIsScanningSubTitleShownOnce) { mHandlerInjector.postDelay(mHideScanningSubTitleRunnable); } } Loading tests/unit/src/com/android/settings/panel/InternetConnectivityPanelTest.java +40 −12 Original line number Diff line number Diff line Loading @@ -18,8 +18,6 @@ package com.android.settings.panel; import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.clearInvocations; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; Loading Loading @@ -95,11 +93,31 @@ public class InternetConnectivityPanelTest { private FragmentActivity mPanelActivity; private Context mContext; private FakeHandlerInjector mFakeHandlerInjector; private InternetConnectivityPanel mPanel; private class FakeHandlerInjector extends InternetConnectivityPanel.HandlerInjector { private Runnable mRunnable; FakeHandlerInjector(Context context) { super(context); } @Override public void postDelay(Runnable runnable) { mRunnable = runnable; } public Runnable getRunnable() { return mRunnable; } } @Before public void setUp() { mContext = spy(ApplicationProvider.getApplicationContext()); mFakeHandlerInjector = new FakeHandlerInjector(mContext); when(mContext.getApplicationContext()).thenReturn(mContext); when(mContext.getMainThreadHandler()).thenReturn(mMainThreadHandler); when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager); Loading @@ -109,6 +127,7 @@ public class InternetConnectivityPanelTest { mPanel.mIsProviderModelEnabled = true; mPanel.mInternetUpdater = mInternetUpdater; mPanel.mProviderModelSliceHelper = mProviderModelSliceHelper; mPanel.mHandlerInjector = mFakeHandlerInjector; } @Test Loading Loading @@ -295,36 +314,41 @@ public class InternetConnectivityPanelTest { } @Test public void showProgressBar_wifiDisabled_hideProgress() { public void updateProgressBar_wifiDisabled_hideProgress() { mPanel.mIsProgressBarVisible = true; doReturn(false).when(mInternetUpdater).isWifiEnabled(); clearInvocations(mPanelContentCallback); mPanel.showProgressBar(); mPanel.updateProgressBar(); assertThat(mPanel.isProgressBarVisible()).isFalse(); verify(mPanelContentCallback).onProgressBarVisibleChanged(); } @Test public void showProgressBar_noWifiScanResults_showProgressForever() { public void updateProgressBar_noWifiScanResults_showProgressForever() { mPanel.mIsScanningSubTitleShownOnce = false; mPanel.mIsProgressBarVisible = false; doReturn(true).when(mInternetUpdater).isWifiEnabled(); List<ScanResult> noWifiScanResults = new ArrayList<>(); doReturn(noWifiScanResults).when(mWifiManager).getScanResults(); clearInvocations(mPanelContentCallback); mPanel.showProgressBar(); mPanel.updateProgressBar(); assertThat(mPanel.isProgressBarVisible()).isTrue(); assertThat(mPanel.mIsProgressBarVisible).isTrue(); verify(mPanelContentCallback).onProgressBarVisibleChanged(); verify(mPanelContentCallback).onHeaderChanged(); verify(mMainThreadHandler, never()) .postDelayed(any() /* mHideProgressBarRunnable */, anyLong()); assertThat(mFakeHandlerInjector.getRunnable()) .isEqualTo(mPanel.mHideScanningSubTitleRunnable); mFakeHandlerInjector.getRunnable().run(); assertThat(mPanel.mIsScanningSubTitleShownOnce).isTrue(); assertThat(mPanel.mIsProgressBarVisible).isTrue(); } @Test public void showProgressBar_hasWifiScanResults_showProgressDelayedHide() { public void updateProgressBar_hasWifiScanResults_showProgressDelayedHide() { mPanel.mIsProgressBarVisible = false; doReturn(true).when(mInternetUpdater).isWifiEnabled(); List<ScanResult> hasWifiScanResults = mock(ArrayList.class); Loading @@ -332,11 +356,15 @@ public class InternetConnectivityPanelTest { doReturn(hasWifiScanResults).when(mWifiManager).getScanResults(); clearInvocations(mPanelContentCallback); mPanel.showProgressBar(); mPanel.updateProgressBar(); assertThat(mPanel.isProgressBarVisible()).isTrue(); verify(mPanelContentCallback).onProgressBarVisibleChanged(); verify(mMainThreadHandler).postDelayed(any() /* mHideProgressBarRunnable */, anyLong()); assertThat(mFakeHandlerInjector.getRunnable()) .isEqualTo(mPanel.mHideProgressBarRunnable); mFakeHandlerInjector.getRunnable().run(); assertThat(mPanel.mIsProgressBarVisible).isFalse(); } @Test Loading Loading
src/com/android/settings/panel/InternetConnectivityPanel.java +47 −15 Original line number Diff line number Diff line Loading @@ -84,12 +84,13 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve } if (TextUtils.equals(intent.getAction(), WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { showProgressBar(); updateProgressBar(); updatePanelTitle(); return; } if (TextUtils.equals(intent.getAction(), WifiManager.NETWORK_STATE_CHANGED_ACTION)) { updateProgressBar(); updatePanelTitle(); } } Loading @@ -110,13 +111,40 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve private int mDefaultDataSubid = SubscriptionManager.INVALID_SUBSCRIPTION_ID; // Wi-Fi scanning progress bar protected HandlerInjector mHandlerInjector; protected boolean mIsProgressBarVisible; protected final Runnable mHideProgressBarRunnable = () -> { protected boolean mIsScanningSubTitleShownOnce; protected Runnable mHideProgressBarRunnable = () -> { setProgressBarVisible(false); }; protected Runnable mHideScanningSubTitleRunnable = () -> { mIsScanningSubTitleShownOnce = true; updatePanelTitle(); }; /** * Wrapper for testing compatibility. */ @VisibleForTesting static class HandlerInjector { protected final Handler mHandler; HandlerInjector(Context context) { mHandler = context.getMainThreadHandler(); } public void postDelay(Runnable runnable) { mHandler.postDelayed(runnable, 2000 /* delay millis */); } public void removeCallbacks(Runnable runnable) { mHandler.removeCallbacks(runnable); } } private InternetConnectivityPanel(Context context) { mContext = context.getApplicationContext(); mHandlerInjector = new HandlerInjector(context); mIsProviderModelEnabled = Utils.isProviderModelEnabled(mContext); mInternetUpdater = new InternetUpdater(context, null /* Lifecycle */, this); Loading Loading @@ -150,7 +178,7 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve mTelephonyManager.registerTelephonyCallback( new HandlerExecutor(new Handler(Looper.getMainLooper())), mTelephonyCallback); mContext.registerReceiver(mWifiStateReceiver, mWifiStateFilter); showProgressBar(); updateProgressBar(); updatePanelTitle(); } Loading @@ -165,7 +193,8 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve mConnectivityListener.stop(); mTelephonyManager.unregisterTelephonyCallback(mTelephonyCallback); mContext.unregisterReceiver(mWifiStateReceiver); mContext.getMainThreadHandler().removeCallbacks(mHideProgressBarRunnable); mHandlerInjector.removeCallbacks(mHideProgressBarRunnable); mHandlerInjector.removeCallbacks(mHideScanningSubTitleRunnable); } /** Loading Loading @@ -246,6 +275,7 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve */ @Override public void onAirplaneModeChanged(boolean isAirplaneModeOn) { log("onAirplaneModeChanged: isAirplaneModeOn:" + isAirplaneModeOn); updatePanelTitle(); } Loading @@ -254,6 +284,7 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve */ @Override public void onWifiEnabledChanged(boolean enabled) { log("onWifiEnabledChanged: enabled:" + enabled); updatePanelTitle(); } Loading Loading @@ -305,13 +336,6 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve return; } if (mIsProgressBarVisible) { // When the Wi-Fi scan result callback is received // Sub-Title: Searching for networks... mSubtitle = SUBTITLE_TEXT_SEARCHING_FOR_NETWORKS; return; } if (mInternetUpdater.isAirplaneModeOn()) { return; } Loading @@ -319,11 +343,18 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve final List<ScanResult> wifiList = mWifiManager.getScanResults(); if (wifiList != null && wifiList.size() != 0) { // When the Wi-Fi scan result is not empty // Sub-Title: Select the network you want to use for data // Sub-Title: Tap a network to connect mSubtitle = SUBTITLE_TEXT_TAP_A_NETWORK_TO_CONNECT; return; } if (!mIsScanningSubTitleShownOnce && mIsProgressBarVisible) { // When the Wi-Fi scan result callback is received // Sub-Title: Searching for networks... mSubtitle = SUBTITLE_TEXT_SEARCHING_FOR_NETWORKS; return; } // Sub-Title: // show non_carrier_network_unavailable // - while Wi-Fi on + no Wi-Fi item Loading Loading @@ -353,7 +384,7 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve mSubtitle = SUBTITLE_TEXT_NON_CARRIER_NETWORK_UNAVAILABLE; } protected void showProgressBar() { protected void updateProgressBar() { if (mWifiManager == null || !mInternetUpdater.isWifiEnabled()) { setProgressBarVisible(false); return; Loading @@ -362,8 +393,9 @@ public class InternetConnectivityPanel implements PanelContent, LifecycleObserve setProgressBarVisible(true); List<ScanResult> wifiScanResults = mWifiManager.getScanResults(); if (wifiScanResults != null && wifiScanResults.size() > 0) { mContext.getMainThreadHandler().postDelayed(mHideProgressBarRunnable, 2000 /* delay millis */); mHandlerInjector.postDelay(mHideProgressBarRunnable); } else if (!mIsScanningSubTitleShownOnce) { mHandlerInjector.postDelay(mHideScanningSubTitleRunnable); } } Loading
tests/unit/src/com/android/settings/panel/InternetConnectivityPanelTest.java +40 −12 Original line number Diff line number Diff line Loading @@ -18,8 +18,6 @@ package com.android.settings.panel; import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.clearInvocations; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; Loading Loading @@ -95,11 +93,31 @@ public class InternetConnectivityPanelTest { private FragmentActivity mPanelActivity; private Context mContext; private FakeHandlerInjector mFakeHandlerInjector; private InternetConnectivityPanel mPanel; private class FakeHandlerInjector extends InternetConnectivityPanel.HandlerInjector { private Runnable mRunnable; FakeHandlerInjector(Context context) { super(context); } @Override public void postDelay(Runnable runnable) { mRunnable = runnable; } public Runnable getRunnable() { return mRunnable; } } @Before public void setUp() { mContext = spy(ApplicationProvider.getApplicationContext()); mFakeHandlerInjector = new FakeHandlerInjector(mContext); when(mContext.getApplicationContext()).thenReturn(mContext); when(mContext.getMainThreadHandler()).thenReturn(mMainThreadHandler); when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager); Loading @@ -109,6 +127,7 @@ public class InternetConnectivityPanelTest { mPanel.mIsProviderModelEnabled = true; mPanel.mInternetUpdater = mInternetUpdater; mPanel.mProviderModelSliceHelper = mProviderModelSliceHelper; mPanel.mHandlerInjector = mFakeHandlerInjector; } @Test Loading Loading @@ -295,36 +314,41 @@ public class InternetConnectivityPanelTest { } @Test public void showProgressBar_wifiDisabled_hideProgress() { public void updateProgressBar_wifiDisabled_hideProgress() { mPanel.mIsProgressBarVisible = true; doReturn(false).when(mInternetUpdater).isWifiEnabled(); clearInvocations(mPanelContentCallback); mPanel.showProgressBar(); mPanel.updateProgressBar(); assertThat(mPanel.isProgressBarVisible()).isFalse(); verify(mPanelContentCallback).onProgressBarVisibleChanged(); } @Test public void showProgressBar_noWifiScanResults_showProgressForever() { public void updateProgressBar_noWifiScanResults_showProgressForever() { mPanel.mIsScanningSubTitleShownOnce = false; mPanel.mIsProgressBarVisible = false; doReturn(true).when(mInternetUpdater).isWifiEnabled(); List<ScanResult> noWifiScanResults = new ArrayList<>(); doReturn(noWifiScanResults).when(mWifiManager).getScanResults(); clearInvocations(mPanelContentCallback); mPanel.showProgressBar(); mPanel.updateProgressBar(); assertThat(mPanel.isProgressBarVisible()).isTrue(); assertThat(mPanel.mIsProgressBarVisible).isTrue(); verify(mPanelContentCallback).onProgressBarVisibleChanged(); verify(mPanelContentCallback).onHeaderChanged(); verify(mMainThreadHandler, never()) .postDelayed(any() /* mHideProgressBarRunnable */, anyLong()); assertThat(mFakeHandlerInjector.getRunnable()) .isEqualTo(mPanel.mHideScanningSubTitleRunnable); mFakeHandlerInjector.getRunnable().run(); assertThat(mPanel.mIsScanningSubTitleShownOnce).isTrue(); assertThat(mPanel.mIsProgressBarVisible).isTrue(); } @Test public void showProgressBar_hasWifiScanResults_showProgressDelayedHide() { public void updateProgressBar_hasWifiScanResults_showProgressDelayedHide() { mPanel.mIsProgressBarVisible = false; doReturn(true).when(mInternetUpdater).isWifiEnabled(); List<ScanResult> hasWifiScanResults = mock(ArrayList.class); Loading @@ -332,11 +356,15 @@ public class InternetConnectivityPanelTest { doReturn(hasWifiScanResults).when(mWifiManager).getScanResults(); clearInvocations(mPanelContentCallback); mPanel.showProgressBar(); mPanel.updateProgressBar(); assertThat(mPanel.isProgressBarVisible()).isTrue(); verify(mPanelContentCallback).onProgressBarVisibleChanged(); verify(mMainThreadHandler).postDelayed(any() /* mHideProgressBarRunnable */, anyLong()); assertThat(mFakeHandlerInjector.getRunnable()) .isEqualTo(mPanel.mHideProgressBarRunnable); mFakeHandlerInjector.getRunnable().run(); assertThat(mPanel.mIsProgressBarVisible).isFalse(); } @Test Loading