[Wi-Fi] Enhance savedaccesspoints2 unit test cases
Add new unit test cases in 1. SavedAccessPointsPreferenceController2Test.java 2. SavedAccessPointsWifiSettings2Test.java 3. SubscribedAccessPointsPreferenceController2Test.java The coverage rate of com.android.settings.wifi.savedaccesspoints2 will raise from 32% to 85% Bug: 151696220 Test: Run following test commands 1. make RunSettingsRoboTests ROBOTEST_FILTER=SavedAccessPointsPreferenceController2Test 2. make RunSettingsRoboTests ROBOTEST_FILTER=SavedAccessPointsWifiSettings2Test 3. make RunSettingsRoboTests ROBOTEST_FILTER=SubscribedAccessPointsPreferenceController2Test Change-Id: I43e43c95019659d507ae0d8b167c5d65cd036b95
This commit is contained in:
@@ -32,6 +32,7 @@ import android.text.TextUtils;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -50,15 +51,15 @@ import java.time.ZoneOffset;
|
|||||||
public class SavedAccessPointsWifiSettings2 extends DashboardFragment
|
public class SavedAccessPointsWifiSettings2 extends DashboardFragment
|
||||||
implements SavedNetworkTracker.SavedNetworkTrackerCallback {
|
implements SavedNetworkTracker.SavedNetworkTrackerCallback {
|
||||||
|
|
||||||
private static final String TAG = "SavedAccessPoints2";
|
@VisibleForTesting static final String TAG = "SavedAccessPoints2";
|
||||||
|
|
||||||
// Max age of tracked WifiEntries
|
// Max age of tracked WifiEntries
|
||||||
private static final long MAX_SCAN_AGE_MILLIS = 15_000;
|
private static final long MAX_SCAN_AGE_MILLIS = 15_000;
|
||||||
// Interval between initiating SavedNetworkTracker scans
|
// Interval between initiating SavedNetworkTracker scans
|
||||||
private static final long SCAN_INTERVAL_MILLIS = 10_000;
|
private static final long SCAN_INTERVAL_MILLIS = 10_000;
|
||||||
|
|
||||||
private SavedNetworkTracker mSavedNetworkTracker;
|
@VisibleForTesting SavedNetworkTracker mSavedNetworkTracker;
|
||||||
private HandlerThread mWorkerThread;
|
@VisibleForTesting HandlerThread mWorkerThread;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getMetricsCategory() {
|
public int getMetricsCategory() {
|
||||||
|
@@ -21,8 +21,12 @@ import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_U
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -119,4 +123,23 @@ public class SavedAccessPointsPreferenceController2Test {
|
|||||||
|
|
||||||
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(0);
|
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onPreferenceClick_shouldCallShowWifiPage() {
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
doNothing().when(mContext).startActivity(any());
|
||||||
|
doReturn(mContext).when(mSettings).getContext();
|
||||||
|
|
||||||
|
final String title = "ssid_title";
|
||||||
|
final String key = "key";
|
||||||
|
final WifiEntry mockWifiEntry = mock(WifiEntry.class);
|
||||||
|
when(mockWifiEntry.getTitle()).thenReturn(title);
|
||||||
|
when(mockWifiEntry.getKey()).thenReturn(key);
|
||||||
|
final WifiEntryPreference preference = new WifiEntryPreference(mContext, mockWifiEntry);
|
||||||
|
preference.setKey(key);
|
||||||
|
|
||||||
|
mController.onPreferenceClick(preference);
|
||||||
|
|
||||||
|
verify(mSettings, times(1)).showWifiPage(key, title);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,8 +18,18 @@ package com.android.settings.wifi.savedaccesspoints2;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
import androidx.fragment.app.FragmentManager;
|
||||||
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -30,7 +40,9 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class SavedAccessPointsWifiSettings2Test {
|
public class SavedAccessPointsWifiSettings2Test {
|
||||||
@@ -41,11 +53,15 @@ public class SavedAccessPointsWifiSettings2Test {
|
|||||||
private SavedAccessPointsPreferenceController2 mSavedApController;
|
private SavedAccessPointsPreferenceController2 mSavedApController;
|
||||||
|
|
||||||
private TestFragment mSettings;
|
private TestFragment mSettings;
|
||||||
|
private Context mContext;
|
||||||
|
private FragmentActivity mActivity;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mSettings = spy(new TestFragment());
|
mSettings = spy(new TestFragment());
|
||||||
|
mActivity = Robolectric.setupActivity(FragmentActivity.class);
|
||||||
|
|
||||||
doReturn(mSubscribedApController).when(mSettings)
|
doReturn(mSubscribedApController).when(mSettings)
|
||||||
.use(SubscribedAccessPointsPreferenceController2.class);
|
.use(SubscribedAccessPointsPreferenceController2.class);
|
||||||
@@ -60,6 +76,54 @@ public class SavedAccessPointsWifiSettings2Test {
|
|||||||
.isEqualTo(R.xml.wifi_display_saved_access_points2);
|
.isEqualTo(R.xml.wifi_display_saved_access_points2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTag_shouldReturnRightTag() {
|
||||||
|
assertThat(mSettings.getLogTag()).isEqualTo(SavedAccessPointsWifiSettings2.TAG);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onAttach_shouldCallSavedControllerSetHost() {
|
||||||
|
mSettings.onAttach(mContext);
|
||||||
|
|
||||||
|
verify(mSavedApController, times(1)).setHost(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onAttach_shouldCallSubscriptionControllerSetHost() {
|
||||||
|
mSettings.onAttach(mContext);
|
||||||
|
|
||||||
|
verify(mSubscribedApController, times(1)).setHost(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onCreate_shouldNewSavedNetworkTracker() {
|
||||||
|
mSettings = new TestFragment();
|
||||||
|
final FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
|
||||||
|
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||||
|
fragmentTransaction.add(mSettings, null /* tag */);
|
||||||
|
fragmentTransaction.commit();
|
||||||
|
final Bundle bundle = new Bundle();
|
||||||
|
|
||||||
|
mSettings.onCreate(bundle);
|
||||||
|
|
||||||
|
assertThat(mSettings.mSavedNetworkTracker).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onDestroy_shouldTerminateWorkerThread() {
|
||||||
|
mSettings = new TestFragment();
|
||||||
|
final FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
|
||||||
|
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||||
|
fragmentTransaction.add(mSettings, null /* tag */);
|
||||||
|
fragmentTransaction.commit();
|
||||||
|
final Bundle bundle = new Bundle();
|
||||||
|
mSettings.onCreate(bundle);
|
||||||
|
|
||||||
|
mSettings.onDestroy();
|
||||||
|
|
||||||
|
assertThat(mSettings.mWorkerThread.getState()).isEqualTo(Thread.State.TERMINATED);
|
||||||
|
}
|
||||||
|
|
||||||
public static class TestFragment extends SavedAccessPointsWifiSettings2 {
|
public static class TestFragment extends SavedAccessPointsWifiSettings2 {
|
||||||
|
|
||||||
public <T extends AbstractPreferenceController> T use(Class<T> clazz) {
|
public <T extends AbstractPreferenceController> T use(Class<T> clazz) {
|
||||||
|
@@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2020 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.wifi.savedaccesspoints2;
|
||||||
|
|
||||||
|
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import androidx.preference.PreferenceCategory;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
public class SubscribedAccessPointsPreferenceController2Test {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PreferenceScreen mPreferenceScreen;
|
||||||
|
@Mock
|
||||||
|
private PreferenceCategory mPreferenceCategory;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private SavedAccessPointsWifiSettings2 mSettings;
|
||||||
|
private SubscribedAccessPointsPreferenceController2 mController;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mSettings = spy(new SavedAccessPointsWifiSettings2());
|
||||||
|
mController = spy(new SubscribedAccessPointsPreferenceController2(mContext, "test_key"));
|
||||||
|
mController.setHost(mSettings);
|
||||||
|
|
||||||
|
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
|
||||||
|
.thenReturn(mPreferenceCategory);
|
||||||
|
when(mPreferenceCategory.getContext()).thenReturn(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAvailability_noSubscripbedAccessPoint_shouldNotAvailable() {
|
||||||
|
mController.mWifiEntries = new ArrayList<>();
|
||||||
|
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user