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

Commit 00d2c252 authored by Chalard Jean's avatar Chalard Jean Committed by android-build-merger
Browse files

Merge "Add a common test library." am: 9690384221

am: 4343cf32aa

Change-Id: I6104a1a677cb2e4978f11be32dfc604ee431d009
parents 212b5b59 0d0cc8cc
Loading
Loading
Loading
Loading

tests/lib/Android.bp

0 → 100644
+26 −0
Original line number Original line Diff line number Diff line
//
// Copyright (C) 2019 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.
//

java_library {
    name: "net-tests-utils",
    srcs: [
        "src/**/*.java",
        "src/**/*.kt",
    ],
    static_libs: [
        "kotlin-test",
    ],
}
+50 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2019 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.testutils

import android.os.ConditionVariable
import android.os.Handler
import android.os.HandlerThread
import java.util.concurrent.Executor
import kotlin.test.fail

/**
 * Block until the specified Handler or HandlerThread becomes idle, or until timeoutMs has passed.
 */
fun Handler.waitForIdle(timeoutMs: Long) = waitForIdleHandler(this, timeoutMs)
fun HandlerThread.waitForIdle(timeoutMs: Long) = waitForIdleHandler(this.threadHandler, timeoutMs)
fun waitForIdleHandler(handler: HandlerThread, timeoutMs: Long) {
    waitForIdleHandler(handler.threadHandler, timeoutMs)
}
fun waitForIdleHandler(handler: Handler, timeoutMs: Long) {
    val cv = ConditionVariable(false)
    handler.post(cv::open)
    if (!cv.block(timeoutMs)) {
        fail("Handler did not become idle after ${timeoutMs}ms")
    }
}

/**
 * Block until the given Serial Executor becomes idle, or until timeoutMs has passed.
 */
fun waitForIdleSerialExecutor(executor: Executor, timeoutMs: Long) {
    val cv = ConditionVariable()
    executor.execute(cv::open)
    if (!cv.block(timeoutMs)) {
        fail("Executor did not become idle after ${timeoutMs}ms")
    }
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -23,6 +23,7 @@ android_test {
    static_libs: [
    static_libs: [
        "androidx.test.rules",
        "androidx.test.rules",
        "mockito-target-extended-minus-junit4",
        "mockito-target-extended-minus-junit4",
        "net-tests-utils",
        "NetworkStackBase",
        "NetworkStackBase",
        "testables",
        "testables",
    ],
    ],
+4 −11
Original line number Original line Diff line number Diff line
@@ -92,6 +92,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.networkstack.R;
import com.android.networkstack.R;
import com.android.networkstack.metrics.DataStallDetectionStats;
import com.android.networkstack.metrics.DataStallDetectionStats;
import com.android.networkstack.metrics.DataStallStatsUtils;
import com.android.networkstack.metrics.DataStallStatsUtils;
import com.android.testutils.HandlerUtilsKt;


import org.junit.After;
import org.junit.After;
import org.junit.Before;
import org.junit.Before;
@@ -420,7 +421,7 @@ public class NetworkMonitorTest {
        final WrappedNetworkMonitor nm = new WrappedNetworkMonitor();
        final WrappedNetworkMonitor nm = new WrappedNetworkMonitor();
        nm.start();
        nm.start();
        setNetworkCapabilities(nm, nc);
        setNetworkCapabilities(nm, nc);
        waitForIdle(nm.getHandler());
        HandlerUtilsKt.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS);
        mCreatedNetworkMonitors.add(nm);
        mCreatedNetworkMonitors.add(nm);
        return nm;
        return nm;
    }
    }
@@ -437,15 +438,7 @@ public class NetworkMonitorTest {


    private void setNetworkCapabilities(NetworkMonitor nm, NetworkCapabilities nc) {
    private void setNetworkCapabilities(NetworkMonitor nm, NetworkCapabilities nc) {
        nm.notifyNetworkCapabilitiesChanged(nc);
        nm.notifyNetworkCapabilitiesChanged(nc);
        waitForIdle(nm.getHandler());
        HandlerUtilsKt.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS);
    }

    private void waitForIdle(Handler handler) {
        final ConditionVariable cv = new ConditionVariable(false);
        handler.post(cv::open);
        if (!cv.block(HANDLER_TIMEOUT_MS)) {
            fail("Timed out waiting for handler");
        }
    }
    }


    @Test
    @Test
@@ -1125,7 +1118,7 @@ public class NetworkMonitorTest {
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            fail("Unexpected exception: " + e);
            fail("Unexpected exception: " + e);
        }
        }
        waitForIdle(monitor.getHandler());
        HandlerUtilsKt.waitForIdle(monitor.getHandler(), HANDLER_TIMEOUT_MS);


        return monitor;
        return monitor;
    }
    }