diff options
author | xinhe <xinhe@google.com> | 2016-02-12 11:31:20 -0800 |
---|---|---|
committer | xinhe <xinhe@google.com> | 2016-02-12 13:42:49 -0800 |
commit | ed514c84aae008d245679f05c9dbcd7e71f126f8 (patch) | |
tree | 3762ce279d766d50c75784a01dd0ca4192575dac | |
parent | b2f519476ee484d43de057ded668d05dccc5ab76 (diff) |
add Clock class for Wifi
Add clock interface to wrapper the System and SystemClock
APIs. In this way we can mock it in mockito test. Integrated
with Quality Network Selection first
BUG=27166726
Change-Id: I952944ccbad32e07ba316197d1785e551e03ea21
4 files changed, 74 insertions, 8 deletions
diff --git a/service/java/com/android/server/wifi/Clock.java b/service/java/com/android/server/wifi/Clock.java new file mode 100644 index 000000000..df0df6d6a --- /dev/null +++ b/service/java/com/android/server/wifi/Clock.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2016 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.Wifi + */ + +package com.android.server.wifi; + +import android.os.SystemClock; +/** + * Wrapper class for time operations. Allows replacement of clock operations for testing. + */ +public class Clock { + + /** + * Get the current time of the clock in milliseconds. + * + * @return Current time in milliseconds. + */ + public long currentTimeMillis() { + return System.currentTimeMillis(); + } + + /** + * Returns milliseconds since boot, including time spent in sleep. + * + * @return Current time since boot in milliseconds. + */ + public long elapsedRealtime() { + return SystemClock.elapsedRealtime(); + } + + /** + * Returns the current timestamp of the most precise timer available on the local system, in + * nanoseconds. + * + * @return Current time in nanoseconds. + */ + public long nanoTime() { + return System.nanoTime(); + } + + /** + * Returns nanoseconds since boot, including time spent in sleep. + * + * @return Current time since boot in nanoseconds. + */ + public long elapsedRealtimeNanos() { + return SystemClock.elapsedRealtimeNanos(); + } +} diff --git a/service/java/com/android/server/wifi/WifiQualifiedNetworkSelection.java b/service/java/com/android/server/wifi/WifiQualifiedNetworkSelection.java index a180b3614..e81622b7d 100644 --- a/service/java/com/android/server/wifi/WifiQualifiedNetworkSelection.java +++ b/service/java/com/android/server/wifi/WifiQualifiedNetworkSelection.java @@ -41,6 +41,7 @@ class WifiQualifiedNetworkSelector { private WifiInfo mWifiInfo; private NetworkScoreManager mScoreManager; private WifiNetworkScoreCache mNetworkScoreCache; + private Clock mClock; private static final String TAG = "WifiQualifiedNetworkSelector:"; private boolean mDbg = true; private WifiConfiguration mCurrentConnectedNetwork = null; @@ -117,9 +118,10 @@ class WifiQualifiedNetworkSelector { } WifiQualifiedNetworkSelector(WifiConfigStore configureStore, Context context, - WifiInfo wifiInfo) { + WifiInfo wifiInfo, Clock clock) { mWifiConfigStore = configureStore; mWifiInfo = wifiInfo; + mClock = clock; mScoreManager = (NetworkScoreManager) context.getSystemService(Context.NETWORK_SCORE_SERVICE); if (mScoreManager != null) { @@ -257,7 +259,7 @@ class WifiQualifiedNetworkSelector { //Do not select again if last selection is within //MINIMUM_QUALIFIED_NETWORK_SELECTION_INTERVAL if (mLastQualifiedNetworkSelectionTimeStamp != INVALID_TIME_STAMP) { - long gap = System.currentTimeMillis() - mLastQualifiedNetworkSelectionTimeStamp; + long gap = mClock.currentTimeMillis() - mLastQualifiedNetworkSelectionTimeStamp; if (gap < MINIMUM_QUALIFIED_NETWORK_SELECTION_INTERVAL) { qnsLog("Too short to last successful Qualified Network Selection Gap is:" + gap + " ms!"); @@ -339,7 +341,7 @@ class WifiQualifiedNetworkSelector { //last user selection award if (sameSelect) { - long timeDifference = System.currentTimeMillis() + long timeDifference = mClock.currentTimeMillis() - mWifiConfigStore.getLastSelectedTimeStamp(); if (timeDifference > 0) { @@ -464,7 +466,7 @@ class WifiQualifiedNetworkSelector { boolean change = false; String key = selected.configKey(); - long currentTime = System.currentTimeMillis(); + long currentTime = mClock.currentTimeMillis(); List<WifiConfiguration> savedNetworks = mWifiConfigStore.getConfiguredNetworks(); for (WifiConfiguration network : savedNetworks) { @@ -590,7 +592,7 @@ class WifiQualifiedNetworkSelector { mWifiConfigStore.getWifiConfiguration(lastUserSelectedNetWorkKey); if (lastUserSelectedNetwork != null) { qnsLog("Last selection is " + lastUserSelectedNetwork.SSID + " Time to now: " - + ((System.currentTimeMillis() - mWifiConfigStore.getLastSelectedTimeStamp()) + + ((mClock.currentTimeMillis() - mWifiConfigStore.getLastSelectedTimeStamp()) / 1000 / 60 + " minutes")); } @@ -805,7 +807,7 @@ class WifiQualifiedNetworkSelector { mCurrentBssid = scanResultCandidate.BSSID; mCurrentConnectedNetwork = networkCandidate; - mLastQualifiedNetworkSelectionTimeStamp = System.currentTimeMillis(); + mLastQualifiedNetworkSelectionTimeStamp = mClock.currentTimeMillis(); return networkCandidate; } diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java index c27ec2742..84c755201 100644 --- a/service/java/com/android/server/wifi/WifiStateMachine.java +++ b/service/java/com/android/server/wifi/WifiStateMachine.java @@ -202,6 +202,7 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno private boolean mTemporarilyDisconnectWifi = false; private final String mPrimaryDeviceType; private final UserManager mUserManager; + private final Clock mClock = new Clock(); /* Scan results handling */ private List<ScanDetail> mScanResults = new ArrayList<>(); @@ -1188,7 +1189,7 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno mWifiInfo = new WifiInfo(); mWifiQualifiedNetworkSelector = new WifiQualifiedNetworkSelector(mWifiConfigStore, mContext, - mWifiInfo); + mWifiInfo, mClock); mSupplicantStateTracker = mFacade.makeSupplicantStateTracker( context, this, mWifiConfigStore, getHandler()); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectionTest.java b/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectionTest.java index f6967aa74..98f5da374 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectionTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectionTest.java @@ -62,8 +62,9 @@ public class WifiQualifiedNetworkSelectionTest { mContext = getContext(); mWifiConfigStore = getWifiConfigStore(); mWifiInfo = getWifiInfo(); + mWifiQualifiedNetworkSelector = new WifiQualifiedNetworkSelector(mWifiConfigStore, mContext, - mWifiInfo); + mWifiInfo, mClock); mWifiQualifiedNetworkSelector.enableVerboseLogging(1); } @@ -78,6 +79,7 @@ public class WifiQualifiedNetworkSelectionTest { private Resources mResource; private NetworkScoreManager mScoreManager; private WifiInfo mWifiInfo; + private Clock mClock = mock(Clock.class); private static final String[] DEFAULT_SSIDS = {"\"test1\"", "\"test2\""}; private static final String[] DEFAULT_BSSIDS = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; private static final String TAG = "QNS Unit Test"; |