summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Monk <jmonk@google.com>2015-05-13 11:07:53 -0400
committerJason Monk <jmonk@google.com>2015-05-13 11:08:53 -0400
commit2b51cc30d523cce3609a498c466364caf3ce504e (patch)
tree614e5d8c5ad0eba3dd7a267fba82ad5597a2c722
parentcf304fb35c2086601178858e307d3dda36dbbff7 (diff)
downloadframeworks_base-2b51cc30d523cce3609a498c466364caf3ce504e.zip
frameworks_base-2b51cc30d523cce3609a498c466364caf3ce504e.tar.gz
frameworks_base-2b51cc30d523cce3609a498c466364caf3ce504e.tar.bz2
Fix crash from WPS
Also fix the tests to handle the new threadedness while here. Bug: 20067687 Change-Id: I116bf14c71886b251c2e8bcaeb2c9b0e5f9ec817
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java3
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java14
-rw-r--r--packages/SettingsLib/tests/src/com/android/settingslib/wifi/WifiTrackerTest.java64
3 files changed, 63 insertions, 18 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
index 7eaa728..1ad7a51 100644
--- a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
+++ b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
@@ -826,6 +826,9 @@ public class AccessPoint implements Comparable<AccessPoint> {
}
static String removeDoubleQuotes(String string) {
+ if (TextUtils.isEmpty(string)) {
+ return "";
+ }
int length = string.length();
if ((length > 1) && (string.charAt(0) == '"')
&& (string.charAt(length - 1) == '"')) {
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java
index c4e3bd6..7060c64 100644
--- a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java
+++ b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java
@@ -95,20 +95,20 @@ public class WifiTracker {
public WifiTracker(Context context, WifiListener wifiListener, Looper workerLooper,
boolean includeSaved, boolean includeScans, boolean includePasspoints) {
this(context, wifiListener, workerLooper, includeSaved, includeScans, includePasspoints,
- (WifiManager) context.getSystemService(Context.WIFI_SERVICE));
+ (WifiManager) context.getSystemService(Context.WIFI_SERVICE), Looper.myLooper());
}
@VisibleForTesting
WifiTracker(Context context, WifiListener wifiListener, Looper workerLooper,
boolean includeSaved, boolean includeScans, boolean includePasspoints,
- WifiManager wifiManager) {
+ WifiManager wifiManager, Looper currentLooper) {
if (!includeSaved && !includeScans) {
throw new IllegalArgumentException("Must include either saved or scans");
}
mContext = context;
- mMainHandler = new MainHandler();
+ mMainHandler = new MainHandler(currentLooper);
mWorkHandler = new WorkHandler(
- workerLooper != null ? workerLooper : Looper.myLooper());
+ workerLooper != null ? workerLooper : currentLooper);
mWifiManager = wifiManager;
mIncludeSaved = includeSaved;
mIncludeScans = includeScans;
@@ -439,6 +439,10 @@ public class WifiTracker {
private static final int MSG_WIFI_STATE_CHANGED = 1;
private static final int MSG_ACCESS_POINT_CHANGED = 2;
+ public MainHandler(Looper looper) {
+ super(looper);
+ }
+
@Override
public void handleMessage(Message msg) {
if (mListener == null) {
@@ -481,7 +485,7 @@ public class WifiTracker {
@VisibleForTesting
class Scanner extends Handler {
- private static final int MSG_SCAN = 0;
+ static final int MSG_SCAN = 0;
private int mRetry = 0;
diff --git a/packages/SettingsLib/tests/src/com/android/settingslib/wifi/WifiTrackerTest.java b/packages/SettingsLib/tests/src/com/android/settingslib/wifi/WifiTrackerTest.java
index 8eb1ca4..103cd3a 100644
--- a/packages/SettingsLib/tests/src/com/android/settingslib/wifi/WifiTrackerTest.java
+++ b/packages/SettingsLib/tests/src/com/android/settingslib/wifi/WifiTrackerTest.java
@@ -23,9 +23,12 @@ import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiSsid;
+import android.os.HandlerThread;
+import android.os.Looper;
import android.util.Log;
import com.android.settingslib.BaseTest;
+import com.android.settingslib.wifi.WifiTracker.Scanner;
import com.android.settingslib.wifi.WifiTracker.WifiListener;
import org.mockito.ArgumentCaptor;
@@ -54,13 +57,25 @@ public class WifiTrackerTest extends BaseTest {
private WifiTracker mWifiTracker;
+ private HandlerThread mWorkerThread;
+ private Looper mLooper;
+ private HandlerThread mMainThread;
+ private Looper mMainLooper;
+
@Override
protected void setUp() throws Exception {
super.setUp();
mWifiManager = Mockito.mock(WifiManager.class);
mWifiListener = Mockito.mock(WifiListener.class);
- mWifiTracker = new WifiTracker(mContext, mWifiListener, true, true, mWifiManager);
+ mWorkerThread = new HandlerThread("TestHandlerThread");
+ mWorkerThread.start();
+ mLooper = mWorkerThread.getLooper();
+ mMainThread = new HandlerThread("TestHandlerThread");
+ mMainThread.start();
+ mMainLooper = mMainThread.getLooper();
+ mWifiTracker = new WifiTracker(mContext, mWifiListener, mLooper, true, true, true,
+ mWifiManager, mMainLooper);
mWifiTracker.mScanner = mWifiTracker.new Scanner();
Mockito.when(mWifiManager.isWifiEnabled()).thenReturn(true);
}
@@ -76,13 +91,14 @@ public class WifiTrackerTest extends BaseTest {
}
public void testAccessPointsCallback() {
- sendScanResultsAvailable();
+ sendScanResultsAndProcess(false);
Mockito.verify(mWifiListener, Mockito.atLeastOnce()).onAccessPointsChanged();
}
public void testConnectedCallback() {
sendConnected();
+ waitForThreads();
Mockito.verify(mWifiListener, Mockito.atLeastOnce()).onConnectedChanged();
assertEquals(true, mWifiTracker.isConnected());
@@ -94,6 +110,7 @@ public class WifiTrackerTest extends BaseTest {
Intent i = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
i.putExtra(WifiManager.EXTRA_WIFI_STATE, TEST_WIFI_STATE);
mWifiTracker.mReceiver.onReceive(mContext, i);
+ waitForThreads();
ArgumentCaptor<Integer> wifiState = ArgumentCaptor.forClass(Integer.class);
Mockito.verify(mWifiListener, Mockito.atLeastOnce())
@@ -106,7 +123,7 @@ public class WifiTrackerTest extends BaseTest {
// Make scans be successful.
Mockito.when(mWifiManager.startScan()).thenReturn(true);
- mWifiTracker.mScanner.handleMessage(null);
+ mWifiTracker.mScanner.handleMessage(mWifiTracker.mScanner.obtainMessage(Scanner.MSG_SCAN));
Mockito.verify(mWifiManager, Mockito.atLeastOnce()).startScan();
}
@@ -121,7 +138,7 @@ public class WifiTrackerTest extends BaseTest {
// Send all of the configs and scan results to the tracker.
Mockito.when(mWifiManager.getConfiguredNetworks()).thenReturn(wifiConfigs);
Mockito.when(mWifiManager.getScanResults()).thenReturn(scanResults);
- sendScanResultsAvailable();
+ sendScanResultsAndProcess(false);
List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints();
assertEquals("Expected number of results", NUM_NETWORKS, accessPoints.size());
@@ -131,7 +148,8 @@ public class WifiTrackerTest extends BaseTest {
}
public void testSavedOnly() {
- mWifiTracker = new WifiTracker(mContext, mWifiListener, true, false, mWifiManager);
+ mWifiTracker = new WifiTracker(mContext, mWifiListener, mLooper, true, false, true,
+ mWifiManager, mMainLooper);
mWifiTracker.mScanner = mWifiTracker.new Scanner();
List<WifiConfiguration> wifiConfigs = new ArrayList<WifiConfiguration>();
@@ -144,7 +162,7 @@ public class WifiTrackerTest extends BaseTest {
// Send all of the configs and scan results to the tracker.
Mockito.when(mWifiManager.getConfiguredNetworks()).thenReturn(wifiConfigs);
Mockito.when(mWifiManager.getScanResults()).thenReturn(scanResults);
- sendScanResultsAvailable();
+ sendScanResultsAndProcess(false);
List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints();
// Only expect the first two to come back in the results.
@@ -154,7 +172,8 @@ public class WifiTrackerTest extends BaseTest {
}
public void testAvailableOnly() {
- mWifiTracker = new WifiTracker(mContext, mWifiListener, false, true, mWifiManager);
+ mWifiTracker = new WifiTracker(mContext, mWifiListener, mLooper, false, true, true,
+ mWifiManager, mMainLooper);
mWifiTracker.mScanner = mWifiTracker.new Scanner();
List<WifiConfiguration> wifiConfigs = new ArrayList<WifiConfiguration>();
@@ -167,7 +186,7 @@ public class WifiTrackerTest extends BaseTest {
// Send all of the configs and scan results to the tracker.
Mockito.when(mWifiManager.getConfiguredNetworks()).thenReturn(wifiConfigs);
Mockito.when(mWifiManager.getScanResults()).thenReturn(scanResults);
- sendScanResultsAvailable();
+ sendScanResultsAndProcess(false);
// Expect the last one (sorted order) to be left off since its only saved.
List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints();
@@ -178,12 +197,13 @@ public class WifiTrackerTest extends BaseTest {
}
public void testNonEphemeralConnected() {
- mWifiTracker = new WifiTracker(mContext, mWifiListener, false, true, mWifiManager);
+ mWifiTracker = new WifiTracker(mContext, mWifiListener, mLooper, false, true, true,
+ mWifiManager, mMainLooper);
mWifiTracker.mScanner = mWifiTracker.new Scanner();
List<WifiConfiguration> wifiConfigs = new ArrayList<WifiConfiguration>();
List<ScanResult> scanResults = new ArrayList<ScanResult>();
- String[] expectedSsids = generateTestNetworks(wifiConfigs, scanResults, false);
+ generateTestNetworks(wifiConfigs, scanResults, false);
// Tell WifiTracker we are connected now.
sendConnected();
@@ -191,9 +211,8 @@ public class WifiTrackerTest extends BaseTest {
// Send all of the configs and scan results to the tracker.
Mockito.when(mWifiManager.getConfiguredNetworks()).thenReturn(wifiConfigs);
Mockito.when(mWifiManager.getScanResults()).thenReturn(scanResults);
- sendScanResultsAvailable();
// Do this twice to catch a bug that was happening in the caching, making things ephemeral.
- sendScanResultsAvailable();
+ sendScanResultsAndProcess(true);
List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints();
assertEquals("Expected number of results", NUM_NETWORKS - 1, accessPoints.size());
@@ -299,9 +318,28 @@ public class WifiTrackerTest extends BaseTest {
mWifiTracker.mReceiver.onReceive(mContext, intent);
}
- private void sendScanResultsAvailable() {
+ private void sendScanResultsAndProcess(boolean sendTwice) {
Intent i = new Intent(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
mWifiTracker.mReceiver.onReceive(mContext, i);
+ if (sendTwice) {
+ mWifiTracker.mReceiver.onReceive(mContext, i);
+ }
+ waitForThreads();
+ }
+
+ private void waitForThreads() {
+ // Run all processing.
+ mWorkerThread.quitSafely();
+ try {
+ mWorkerThread.join();
+ } catch (InterruptedException e) {
+ }
+ // Send all callbacks.
+ mMainThread.quitSafely();
+ try {
+ mMainThread.join();
+ } catch (InterruptedException e) {
+ }
}
}