Clean up libcutils/libutils tests.

Move tests in the same directory as the corresponding code, so it's
easier to see what is/isn't tested.

Fix naming of libcutils_tests (plural) to match the singular that's more
common (even though the plural makes more sense to me).

Add these two to system/core/'s TEST_MAPPING.

Remove obsolete AndroidTest.xml.

Fix a flaky (timing-dependent) libcutils test.

Test: ran tests
Change-Id: I7e0a31ff45c8a152562bf66fc97161594249366e
diff --git a/TEST_MAPPING b/TEST_MAPPING
index bc5685b..cc85408 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -19,6 +19,9 @@
       "name": "libprocinfo_test"
     },
     {
+      "name": "libutils_test"
+    },
+    {
       "name": "memunreachable_test"
     },
     {
diff --git a/libcutils/Android.bp b/libcutils/Android.bp
index 17c8a13..b4b8cd1 100644
--- a/libcutils/Android.bp
+++ b/libcutils/Android.bp
@@ -196,4 +196,71 @@
     ],
 }
 
-subdirs = ["tests"]
+cc_defaults {
+    name: "libcutils_test_default",
+    srcs: ["sockets_test.cpp"],
+
+    target: {
+        android: {
+            srcs: [
+                "android_get_control_file_test.cpp",
+                "android_get_control_socket_test.cpp",
+                "ashmem_test.cpp",
+                "fs_config_test.cpp",
+                "memset_test.cpp",
+                "multiuser_test.cpp",
+                "properties_test.cpp",
+                "sched_policy_test.cpp",
+                "str_parms_test.cpp",
+                "trace-dev_test.cpp",
+            ],
+        },
+
+        not_windows: {
+            srcs: [
+                "str_parms_test.cpp",
+            ],
+        },
+    },
+
+    cflags: [
+        "-Wall",
+        "-Wextra",
+        "-Werror",
+    ],
+}
+
+test_libraries = [
+    "libcutils",
+    "liblog",
+    "libbase",
+    "libjsoncpp",
+    "libprocessgroup",
+]
+
+cc_test {
+    name: "libcutils_test",
+    test_suites: ["device-tests"],
+    defaults: ["libcutils_test_default"],
+    host_supported: true,
+    shared_libs: test_libraries,
+}
+
+cc_test {
+    name: "libcutils_test_static",
+    test_suites: ["device-tests"],
+    defaults: ["libcutils_test_default"],
+    static_libs: ["libc"] + test_libraries,
+    stl: "libc++_static",
+
+    target: {
+        android: {
+            static_executable: true,
+        },
+        windows: {
+            host_ldlibs: ["-lws2_32"],
+
+            enabled: true,
+        },
+    },
+}
diff --git a/libcutils/tests/android_get_control_file_test.cpp b/libcutils/android_get_control_file_test.cpp
similarity index 100%
rename from libcutils/tests/android_get_control_file_test.cpp
rename to libcutils/android_get_control_file_test.cpp
diff --git a/libcutils/tests/android_get_control_socket_test.cpp b/libcutils/android_get_control_socket_test.cpp
similarity index 100%
rename from libcutils/tests/android_get_control_socket_test.cpp
rename to libcutils/android_get_control_socket_test.cpp
diff --git a/libcutils/tests/AshmemTest.cpp b/libcutils/ashmem_test.cpp
similarity index 100%
rename from libcutils/tests/AshmemTest.cpp
rename to libcutils/ashmem_test.cpp
diff --git a/libcutils/tests/fs_config.cpp b/libcutils/fs_config_test.cpp
similarity index 100%
rename from libcutils/tests/fs_config.cpp
rename to libcutils/fs_config_test.cpp
diff --git a/libcutils/tests/MemsetTest.cpp b/libcutils/memset_test.cpp
similarity index 100%
rename from libcutils/tests/MemsetTest.cpp
rename to libcutils/memset_test.cpp
diff --git a/libcutils/tests/multiuser_test.cpp b/libcutils/multiuser_test.cpp
similarity index 100%
rename from libcutils/tests/multiuser_test.cpp
rename to libcutils/multiuser_test.cpp
diff --git a/libcutils/tests/PropertiesTest.cpp b/libcutils/properties_test.cpp
similarity index 100%
rename from libcutils/tests/PropertiesTest.cpp
rename to libcutils/properties_test.cpp
diff --git a/libcutils/tests/sched_policy_test.cpp b/libcutils/sched_policy_test.cpp
similarity index 96%
rename from libcutils/tests/sched_policy_test.cpp
rename to libcutils/sched_policy_test.cpp
index 1f657e2..a321c90 100644
--- a/libcutils/tests/sched_policy_test.cpp
+++ b/libcutils/sched_policy_test.cpp
@@ -90,17 +90,18 @@
     // A measureable effect of scheduling policy is that the kernel has 800x
     // greater slack time in waking up a sleeping background thread.
     //
-    // Look for 100x difference in how long FB and BG threads actually sleep
+    // Look for 10x difference in how long FB and BG threads actually sleep
     // when trying to sleep for 1 ns.  This difference is large enough not
     // to happen by chance, but small enough (compared to 800x) to keep inherent
     // fuzziness in scheduler behavior from causing false negatives.
-    const unsigned int BG_FG_SLACK_FACTOR = 100;
+    const unsigned int BG_FG_SLACK_FACTOR = 10;
 
     ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
     auto bgSleepTime = medianSleepTime();
 
     ASSERT_EQ(0, set_sched_policy(0, SP_FOREGROUND));
     auto fgSleepTime = medianSleepTime();
+
     ASSERT_GT(bgSleepTime, fgSleepTime * BG_FG_SLACK_FACTOR);
 }
 
diff --git a/libcutils/tests/sockets_test.cpp b/libcutils/sockets_test.cpp
similarity index 100%
rename from libcutils/tests/sockets_test.cpp
rename to libcutils/sockets_test.cpp
diff --git a/libcutils/tests/test_str_parms.cpp b/libcutils/str_parms_test.cpp
similarity index 100%
rename from libcutils/tests/test_str_parms.cpp
rename to libcutils/str_parms_test.cpp
diff --git a/libcutils/tests/Android.bp b/libcutils/tests/Android.bp
deleted file mode 100644
index 3892ce0..0000000
--- a/libcutils/tests/Android.bp
+++ /dev/null
@@ -1,92 +0,0 @@
-// Copyright (C) 2014 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.
-
-cc_defaults {
-    name: "libcutils_test_default",
-    srcs: ["sockets_test.cpp"],
-
-    target: {
-        android: {
-            srcs: [
-                "AshmemTest.cpp",
-                "fs_config.cpp",
-                "MemsetTest.cpp",
-                "PropertiesTest.cpp",
-                "sched_policy_test.cpp",
-                "trace-dev_test.cpp",
-                "test_str_parms.cpp",
-                "android_get_control_socket_test.cpp",
-                "android_get_control_file_test.cpp",
-                "multiuser_test.cpp",
-            ],
-        },
-
-        not_windows: {
-            srcs: [
-                "test_str_parms.cpp",
-                "fs_config.cpp",
-            ],
-        },
-    },
-
-    multilib: {
-        lib32: {
-            suffix: "32",
-        },
-        lib64: {
-            suffix: "64",
-        },
-    },
-
-    cflags: [
-        "-Wall",
-        "-Wextra",
-        "-Werror",
-    ],
-}
-
-test_libraries = [
-    "libcutils",
-    "liblog",
-    "libbase",
-    "libjsoncpp",
-    "libprocessgroup",
-]
-
-cc_test {
-    name: "libcutils_test",
-    test_suites: ["device-tests"],
-    defaults: ["libcutils_test_default"],
-    host_supported: true,
-    shared_libs: test_libraries,
-}
-
-cc_test {
-    name: "libcutils_test_static",
-    test_suites: ["device-tests"],
-    defaults: ["libcutils_test_default"],
-    static_libs: ["libc"] + test_libraries,
-    stl: "libc++_static",
-
-    target: {
-        android: {
-            static_executable: true,
-        },
-        windows: {
-            host_ldlibs: ["-lws2_32"],
-
-            enabled: true,
-        },
-    },
-}
diff --git a/libcutils/tests/AndroidTest.xml b/libcutils/tests/AndroidTest.xml
deleted file mode 100644
index dd7aca2..0000000
--- a/libcutils/tests/AndroidTest.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2017 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.
--->
-<configuration description="Config for libcutils_test">
-    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
-        <option name="cleanup" value="true" />
-        <option name="push" value="libcutils_test->/data/local/tmp/libcutils_test" />
-    </target_preparer>
-    <option name="test-suite-tag" value="apct" />
-    <test class="com.android.tradefed.testtype.GTest" >
-        <option name="native-test-device-path" value="/data/local/tmp" />
-        <option name="module-name" value="libcutils_test" />
-    </test>
-</configuration>
\ No newline at end of file
diff --git a/libcutils/tests/trace-dev_test.cpp b/libcutils/trace-dev_test.cpp
similarity index 100%
rename from libcutils/tests/trace-dev_test.cpp
rename to libcutils/trace-dev_test.cpp
diff --git a/libutils/Android.bp b/libutils/Android.bp
index fb7ca32..93aa1e6 100644
--- a/libutils/Android.bp
+++ b/libutils/Android.bp
@@ -195,17 +195,83 @@
     },
 }
 
-// Include subdirectory makefiles
-// ============================================================
-
 cc_test {
-    name: "SharedBufferTest",
+    name: "libutils_test",
     host_supported: true,
-    static_libs: ["libutils"],
-    shared_libs: ["liblog"],
-    srcs: ["SharedBufferTest.cpp"],
+
+    srcs: [
+        "BitSet_test.cpp",
+        "FileMap_test.cpp",
+        "LruCache_test.cpp",
+        "Mutex_test.cpp",
+        "SharedBuffer_test.cpp",
+        "Singleton_test.cpp",
+        "String8_test.cpp",
+        "StrongPointer_test.cpp",
+        "Unicode_test.cpp",
+        "Vector_test.cpp",
+    ],
+
+    target: {
+        android: {
+            srcs: [
+                "SystemClock_test.cpp",
+            ],
+            shared_libs: [
+                "libz",
+                "liblog",
+                "libcutils",
+                "libutils",
+                "libbase",
+            ],
+        },
+        linux: {
+            srcs: [
+                "Looper_test.cpp",
+                "RefBase_test.cpp",
+            ],
+        },
+        host: {
+            static_libs: [
+                "libutils",
+                "liblog",
+                "libbase",
+            ],
+        },
+    },
+
+    required: [
+        "libutils_test_singleton1",
+        "libutils_test_singleton2",
+    ],
+
+    cflags: [
+        "-Wall",
+        "-Wextra",
+        "-Werror",
+        "-Wthread-safety",
+    ],
+}
+
+cc_test_library {
+    name: "libutils_test_singleton1",
+    host_supported: true,
+    relative_install_path: "libutils_test",
+    srcs: ["Singleton_test1.cpp"],
     cflags: [
         "-Wall",
         "-Werror",
     ],
 }
+
+cc_test_library {
+    name: "libutils_test_singleton2",
+    host_supported: true,
+    relative_install_path: "libutils_test",
+    srcs: ["Singleton_test2.cpp"],
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+    shared_libs: ["libutils_test_singleton1"],
+}
diff --git a/libutils/tests/BitSet_test.cpp b/libutils/BitSet_test.cpp
similarity index 100%
rename from libutils/tests/BitSet_test.cpp
rename to libutils/BitSet_test.cpp
diff --git a/libutils/tests/FileMap_test.cpp b/libutils/FileMap_test.cpp
similarity index 100%
rename from libutils/tests/FileMap_test.cpp
rename to libutils/FileMap_test.cpp
diff --git a/libutils/tests/Looper_test.cpp b/libutils/Looper_test.cpp
similarity index 96%
rename from libutils/tests/Looper_test.cpp
rename to libutils/Looper_test.cpp
index 2282ced..6fdc0ed 100644
--- a/libutils/tests/Looper_test.cpp
+++ b/libutils/Looper_test.cpp
@@ -9,7 +9,7 @@
 #include <unistd.h>
 #include <time.h>
 
-#include "TestHelpers.h"
+#include <utils/threads.h>
 
 // # of milliseconds to fudge stopwatch measurements
 #define TIMING_TOLERANCE_MS 25
@@ -23,6 +23,59 @@
     MSG_TEST4 = 4,
 };
 
+class Pipe {
+public:
+    int sendFd;
+    int receiveFd;
+
+    Pipe() {
+        int fds[2];
+        ::pipe(fds);
+
+        receiveFd = fds[0];
+        sendFd = fds[1];
+    }
+
+    ~Pipe() {
+        if (sendFd != -1) {
+            ::close(sendFd);
+        }
+
+        if (receiveFd != -1) {
+            ::close(receiveFd);
+        }
+    }
+
+    status_t writeSignal() {
+        ssize_t nWritten = ::write(sendFd, "*", 1);
+        return nWritten == 1 ? 0 : -errno;
+    }
+
+    status_t readSignal() {
+        char buf[1];
+        ssize_t nRead = ::read(receiveFd, buf, 1);
+        return nRead == 1 ? 0 : nRead == 0 ? -EPIPE : -errno;
+    }
+};
+
+class DelayedTask : public Thread {
+    int mDelayMillis;
+
+public:
+    explicit DelayedTask(int delayMillis) : mDelayMillis(delayMillis) { }
+
+protected:
+    virtual ~DelayedTask() { }
+
+    virtual void doTask() = 0;
+
+    virtual bool threadLoop() {
+        usleep(mDelayMillis * 1000);
+        doTask();
+        return false;
+    }
+};
+
 class DelayedWake : public DelayedTask {
     sp<Looper> mLooper;
 
diff --git a/libutils/tests/LruCache_test.cpp b/libutils/LruCache_test.cpp
similarity index 100%
rename from libutils/tests/LruCache_test.cpp
rename to libutils/LruCache_test.cpp
diff --git a/libutils/tests/Mutex_test.cpp b/libutils/Mutex_test.cpp
similarity index 100%
rename from libutils/tests/Mutex_test.cpp
rename to libutils/Mutex_test.cpp
diff --git a/libutils/tests/RefBase_test.cpp b/libutils/RefBase_test.cpp
similarity index 100%
rename from libutils/tests/RefBase_test.cpp
rename to libutils/RefBase_test.cpp
diff --git a/libutils/SharedBufferTest.cpp b/libutils/SharedBuffer_test.cpp
similarity index 100%
rename from libutils/SharedBufferTest.cpp
rename to libutils/SharedBuffer_test.cpp
diff --git a/libutils/tests/Singleton_test.cpp b/libutils/Singleton_test.cpp
similarity index 88%
rename from libutils/tests/Singleton_test.cpp
rename to libutils/Singleton_test.cpp
index 9acd3c3..61084b0 100644
--- a/libutils/tests/Singleton_test.cpp
+++ b/libutils/Singleton_test.cpp
@@ -30,15 +30,15 @@
 
 TEST(SingletonTest, bug35674422) {
     std::string path = android::base::GetExecutableDirectory();
-    // libutils_tests_singleton1.so contains the ANDROID_SINGLETON_STATIC_INSTANCE
+    // libutils_test_singleton1.so contains the ANDROID_SINGLETON_STATIC_INSTANCE
     // definition of SingletonTestData, load it first.
-    std::string lib = android::base::StringPrintf("%s/libutils_tests_singleton1.so", path.c_str());
+    std::string lib = android::base::StringPrintf("%s/libutils_test_singleton1.so", path.c_str());
     void* handle1 = dlopen(lib.c_str(), RTLD_NOW);
     ASSERT_TRUE(handle1 != nullptr) << dlerror();
 
-    // libutils_tests_singleton2.so references SingletonTestData but should not
+    // libutils_test_singleton2.so references SingletonTestData but should not
     // have a definition
-    lib = android::base::StringPrintf("%s/libutils_tests_singleton2.so", path.c_str());
+    lib = android::base::StringPrintf("%s/libutils_test_singleton2.so", path.c_str());
     void* handle2 = dlopen(lib.c_str(), RTLD_NOW);
     ASSERT_TRUE(handle2 != nullptr) << dlerror();
 
diff --git a/libutils/tests/Singleton_test.h b/libutils/Singleton_test.h
similarity index 100%
rename from libutils/tests/Singleton_test.h
rename to libutils/Singleton_test.h
diff --git a/libutils/tests/Singleton_test1.cpp b/libutils/Singleton_test1.cpp
similarity index 100%
rename from libutils/tests/Singleton_test1.cpp
rename to libutils/Singleton_test1.cpp
diff --git a/libutils/tests/Singleton_test2.cpp b/libutils/Singleton_test2.cpp
similarity index 100%
rename from libutils/tests/Singleton_test2.cpp
rename to libutils/Singleton_test2.cpp
diff --git a/libutils/tests/String8_test.cpp b/libutils/String8_test.cpp
similarity index 100%
rename from libutils/tests/String8_test.cpp
rename to libutils/String8_test.cpp
diff --git a/libutils/tests/StrongPointer_test.cpp b/libutils/StrongPointer_test.cpp
similarity index 100%
rename from libutils/tests/StrongPointer_test.cpp
rename to libutils/StrongPointer_test.cpp
diff --git a/libutils/tests/SystemClock_test.cpp b/libutils/SystemClock_test.cpp
similarity index 100%
rename from libutils/tests/SystemClock_test.cpp
rename to libutils/SystemClock_test.cpp
diff --git a/libutils/tests/Unicode_test.cpp b/libutils/Unicode_test.cpp
similarity index 100%
rename from libutils/tests/Unicode_test.cpp
rename to libutils/Unicode_test.cpp
diff --git a/libutils/tests/Vector_test.cpp b/libutils/Vector_test.cpp
similarity index 100%
rename from libutils/tests/Vector_test.cpp
rename to libutils/Vector_test.cpp
diff --git a/libutils/tests/Android.bp b/libutils/tests/Android.bp
deleted file mode 100644
index 62f5acb..0000000
--- a/libutils/tests/Android.bp
+++ /dev/null
@@ -1,97 +0,0 @@
-//
-// Copyright (C) 2014 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.
-//
-
-// Build the unit tests.
-
-cc_test {
-    name: "libutils_tests",
-    host_supported: true,
-
-    srcs: [
-        "BitSet_test.cpp",
-        "FileMap_test.cpp",
-        "LruCache_test.cpp",
-        "Mutex_test.cpp",
-        "Singleton_test.cpp",
-        "String8_test.cpp",
-        "StrongPointer_test.cpp",
-        "Unicode_test.cpp",
-        "Vector_test.cpp",
-    ],
-
-    target: {
-        android: {
-            srcs: [
-                "SystemClock_test.cpp",
-            ],
-            shared_libs: [
-                "libz",
-                "liblog",
-                "libcutils",
-                "libutils",
-                "libbase",
-            ],
-        },
-        linux: {
-            srcs: [
-                "Looper_test.cpp",
-                "RefBase_test.cpp",
-            ],
-        },
-        host: {
-            static_libs: [
-                "libutils",
-                "liblog",
-                "libbase",
-            ],
-        },
-    },
-
-    required: [
-        "libutils_tests_singleton1",
-        "libutils_tests_singleton2",
-    ],
-
-    cflags: [
-        "-Wall",
-        "-Wextra",
-        "-Werror",
-        "-Wthread-safety",
-    ],
-}
-
-cc_test_library {
-    name: "libutils_tests_singleton1",
-    host_supported: true,
-    relative_install_path: "libutils_tests",
-    srcs: ["Singleton_test1.cpp"],
-    cflags: [
-        "-Wall",
-        "-Werror",
-    ],
-}
-
-cc_test_library {
-    name: "libutils_tests_singleton2",
-    host_supported: true,
-    relative_install_path: "libutils_tests",
-    srcs: ["Singleton_test2.cpp"],
-    cflags: [
-        "-Wall",
-        "-Werror",
-    ],
-    shared_libs: ["libutils_tests_singleton1"],
-}
diff --git a/libutils/tests/README.txt b/libutils/tests/README.txt
deleted file mode 100644
index ad54e57..0000000
--- a/libutils/tests/README.txt
+++ /dev/null
@@ -1,8 +0,0 @@
-Run device tests:
-
-mma -j<whatever>
-(after adb root; adb disable-verity; adb reboot)
-adb root
-adb remount
-adb sync
-adb shell /data/nativetest/libutils_tests/libutils_tests
diff --git a/libutils/tests/TestHelpers.h b/libutils/tests/TestHelpers.h
deleted file mode 100644
index 6801cd7..0000000
--- a/libutils/tests/TestHelpers.h
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-
-#ifndef TESTHELPERS_H
-#define TESTHELPERS_H
-
-#include <utils/threads.h>
-
-namespace android {
-
-class Pipe {
-public:
-    int sendFd;
-    int receiveFd;
-
-    Pipe() {
-        int fds[2];
-        ::pipe(fds);
-
-        receiveFd = fds[0];
-        sendFd = fds[1];
-    }
-
-    ~Pipe() {
-        if (sendFd != -1) {
-            ::close(sendFd);
-        }
-
-        if (receiveFd != -1) {
-            ::close(receiveFd);
-        }
-    }
-
-    status_t writeSignal() {
-        ssize_t nWritten = ::write(sendFd, "*", 1);
-        return nWritten == 1 ? 0 : -errno;
-    }
-
-    status_t readSignal() {
-        char buf[1];
-        ssize_t nRead = ::read(receiveFd, buf, 1);
-        return nRead == 1 ? 0 : nRead == 0 ? -EPIPE : -errno;
-    }
-};
-
-class DelayedTask : public Thread {
-    int mDelayMillis;
-
-public:
-    explicit DelayedTask(int delayMillis) : mDelayMillis(delayMillis) { }
-
-protected:
-    virtual ~DelayedTask() { }
-
-    virtual void doTask() = 0;
-
-    virtual bool threadLoop() {
-        usleep(mDelayMillis * 1000);
-        doTask();
-        return false;
-    }
-};
-
-} // namespace android
-
-#endif // TESTHELPERS_H