diff options
| author | Chalard Jean <jchalard@google.com> | 2020-04-14 04:54:33 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-04-14 04:54:33 +0000 |
| commit | b0bf3e94fadfd043678390061b931ca6f1369a68 (patch) | |
| tree | 860613aded4954e568d2be5a1a7b22341cc909f4 /core/java/android | |
| parent | 705ebb0946ca0629882ade11818fd979209b8377 (diff) | |
| parent | 0348de6615f6056dde6e9d42372afb9c55af5ed2 (diff) | |
Merge "Add instrumentation to test NetworkAgent." into rvc-dev
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/net/NetworkAgent.java | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/core/java/android/net/NetworkAgent.java b/core/java/android/net/NetworkAgent.java index 31d7d0820996..65e772cb5ebb 100644 --- a/core/java/android/net/NetworkAgent.java +++ b/core/java/android/net/NetworkAgent.java @@ -25,12 +25,14 @@ import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; import android.os.Build; import android.os.Bundle; +import android.os.ConditionVariable; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.Messenger; import android.util.Log; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.AsyncChannel; import com.android.internal.util.Protocol; @@ -572,6 +574,37 @@ public abstract class NetworkAgent { } /** + * Register this network agent with a testing harness. + * + * The returned Messenger sends messages to the Handler. This allows a test to send + * this object {@code CMD_*} messages as if they came from ConnectivityService, which + * is useful for testing the behavior. + * + * @hide + */ + public Messenger registerForTest(final Network network) { + log("Registering NetworkAgent for test"); + synchronized (mRegisterLock) { + mNetwork = network; + mInitialConfiguration = null; + } + return new Messenger(mHandler); + } + + /** + * Waits for the handler to be idle. + * This is useful for testing, and has smaller scope than an accessor to mHandler. + * TODO : move the implementation in common library with the tests + * @hide + */ + @VisibleForTesting + public boolean waitForIdle(final long timeoutMs) { + final ConditionVariable cv = new ConditionVariable(false); + mHandler.post(cv::open); + return cv.block(timeoutMs); + } + + /** * @return The Network associated with this agent, or null if it's not registered yet. */ @Nullable |
