Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 036ab4d1 authored by Hugo Benichi's avatar Hugo Benichi Committed by android-build-merger
Browse files

Merge "ConnectivityServiceTest: some fixes in CallbackInfo"

am: 37727596

Change-Id: I636b7b0ef210aaa3e130e1c1a162a35cdc099295
parents 051b1410 37727596
Loading
Loading
Loading
Loading
+38 −23
Original line number Diff line number Diff line
@@ -1080,32 +1080,40 @@ public class ConnectivityServiceTest extends AndroidTestCase {
        LOST
    }

    /**
     * Utility NetworkCallback for testing. The caller must explicitly test for all the callbacks
     * this class receives, by calling expectCallback() exactly once each time a callback is
     * received. assertNoCallback may be called at any time.
     */
    private class TestNetworkCallback extends NetworkCallback {
        // Chosen to be much less than the linger timeout. This ensures that we can distinguish
        // between a LOST callback that arrives immediately and a LOST callback that arrives after
        // the linger timeout.
        private final static int TIMEOUT_MS = 50;

        private class CallbackInfo {
    private static class CallbackInfo {
        public final CallbackState state;
        public final Network network;
            public Object arg;
        public final Object arg;
        public CallbackInfo(CallbackState s, Network n, Object o) {
            state = s; network = n; arg = o;
        }
            public String toString() { return String.format("%s (%s)", state, network); }
        public String toString() {
            return String.format("%s (%s)", state, network);
        }
        @Override
        public boolean equals(Object o) {
            if (!(o instanceof CallbackInfo)) return false;
            // Ignore timeMs, since it's unpredictable.
            CallbackInfo other = (CallbackInfo) o;
                return state == other.state && Objects.equals(network, other.network);
            return (state == other.state) && Objects.equals(network, other.network);
        }
        @Override
        public int hashCode() {
            return Objects.hash(state, network);
        }
    }

    /**
     * Utility NetworkCallback for testing. The caller must explicitly test for all the callbacks
     * this class receives, by calling expectCallback() exactly once each time a callback is
     * received. assertNoCallback may be called at any time.
     */
    private class TestNetworkCallback extends NetworkCallback {
        // Chosen to be much less than the linger timeout. This ensures that we can distinguish
        // between a LOST callback that arrives immediately and a LOST callback that arrives after
        // the linger timeout.
        private final static int TIMEOUT_MS = 50;

        private final LinkedBlockingQueue<CallbackInfo> mCallbacks = new LinkedBlockingQueue<>();

        protected void setLastCallback(CallbackState state, Network network, Object o) {
@@ -1127,17 +1135,24 @@ public class ConnectivityServiceTest extends AndroidTestCase {
            setLastCallback(CallbackState.LOST, network, null);
        }

        CallbackInfo nextCallback(int timeoutMs) {
            CallbackInfo cb = null;
            try {
                cb = mCallbacks.poll(timeoutMs, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
            }
            if (cb == null) {
                // LinkedBlockingQueue.poll() returns null if it timeouts.
                fail("Did not receive callback after " + timeoutMs + "ms");
            }
            return cb;
        }

        void expectCallback(CallbackState state, MockNetworkAgent mockAgent, int timeoutMs) {
            CallbackInfo expected = new CallbackInfo(
                    state, (mockAgent != null) ? mockAgent.getNetwork() : null, 0);
            CallbackInfo actual;
            try {
                actual = mCallbacks.poll(timeoutMs, TimeUnit.MILLISECONDS);
            CallbackInfo actual = nextCallback(timeoutMs);
            assertEquals("Unexpected callback:", expected, actual);
            } catch (InterruptedException e) {
                fail("Did not receive expected " + expected + " after " + TIMEOUT_MS + "ms");
                actual = null;  // Or the compiler can't tell it's never used uninitialized.
            }
            if (state == CallbackState.LOSING) {
                String msg = String.format(
                        "Invalid linger time value %d, must be between %d and %d",