hidl_test: move duplicated code to shared lib

Test: hidl_test

Change-Id: Ia68b78d44d68a6b254c686afe64faf7cc89510d7
diff --git a/Android.bp b/Android.bp
index 84e11e5..f582790 100644
--- a/Android.bp
+++ b/Android.bp
@@ -13,11 +13,13 @@
     "tests/expression/1.0",
     "tests/foo/1.0",
     "tests/foo/1.0/default",
+    "tests/foo/1.0/default/lib",
     "tests/libhwbinder/1.0",
     "tests/libhwbinder/1.0/default",
     "tests/msgq/1.0",
     "tests/pointer/1.0",
     "tests/pointer/1.0/default",
+    "tests/pointer/1.0/default/lib",
     "wifi/1.0",
     "wifi/supplicant/1.0",
 ]
diff --git a/tests/foo/1.0/default/Android.bp b/tests/foo/1.0/default/Android.bp
index 90295a9..185e5ea 100644
--- a/tests/foo/1.0/default/Android.bp
+++ b/tests/foo/1.0/default/Android.bp
@@ -11,6 +11,7 @@
     shared_libs: [
         "libbase",
         "libhidl",
+        "libfootest",
         "libhwbinder",
         "liblog",
         "libutils",
diff --git a/tests/foo/1.0/default/Foo.cpp b/tests/foo/1.0/default/Foo.cpp
index cadb151..235a12d 100644
--- a/tests/foo/1.0/default/Foo.cpp
+++ b/tests/foo/1.0/default/Foo.cpp
@@ -4,6 +4,7 @@
 #include "Foo.h"
 #include "FooCallback.h"
 #include <android-base/logging.h>
+#include <hidl-test/FooHelper.h>
 #include <inttypes.h>
 #include <utils/Timers.h>
 
@@ -108,7 +109,7 @@
 
         ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYouIsntIt, " \
               "should block for %" PRId64 " seconds", cb.get(),
-              FooCallback::DELAY_S);
+              DELAY_S);
         c[1] = systemTime();
         bool answer = cb->heyItsYouIsntIt(cb);
         c[1] = systemTime() - c[1];
@@ -201,74 +202,6 @@
     return Void();
 }
 
-// NOTE: duplicated code in hidl_test
-using std::to_string;
-
-static std::string to_string(const IFoo::StringMatrix5x3 &M);
-static std::string to_string(const IFoo::StringMatrix3x5 &M);
-static std::string to_string(const hidl_string &s);
-
-template<typename T>
-static std::string to_string(const T *elems, size_t n) {
-    std::string out;
-    out = "[";
-    for (size_t i = 0; i < n; ++i) {
-        if (i > 0) {
-            out += ", ";
-        }
-        out += to_string(elems[i]);
-    }
-    out += "]";
-
-    return out;
-}
-
-template<typename T, size_t SIZE>
-static std::string to_string(const hidl_array<T, SIZE> &array) {
-    return to_string(&array[0], SIZE);
-}
-
-template<typename T, size_t SIZE1, size_t SIZE2>
-static std::string to_string(const hidl_array<T, SIZE1, SIZE2> &array) {
-    std::string out;
-    out = "[";
-    for (size_t i = 0; i < SIZE1; ++i) {
-        if (i > 0) {
-            out += ", ";
-        }
-
-        out += "[";
-        for (size_t j = 0; j < SIZE2; ++j) {
-            if (j > 0) {
-                out += ", ";
-            }
-
-            out += to_string(array[i][j]);
-        }
-        out += "]";
-    }
-    out += "]";
-
-    return out;
-}
-
-template<typename T>
-static std::string to_string(const hidl_vec<T> &vec) {
-    return to_string(&vec[0], vec.size());
-}
-
-static std::string to_string(const IFoo::StringMatrix5x3 &M) {
-    return to_string(M.s);
-}
-
-static std::string to_string(const IFoo::StringMatrix3x5 &M) {
-    return to_string(M.s);
-}
-
-static std::string to_string(const hidl_string &s) {
-    return std::string("'") + s.c_str() + "'";
-}
-
 Return<void> Foo::transposeMe(
         const hidl_array<float, 3, 5> &in, transposeMe_cb _cb) {
     ALOGI("SERVER(Foo) transposeMe(%s)", to_string(in).c_str());
@@ -286,48 +219,6 @@
 
     return Void();
 }
-// end duplicated code
-
-static std::string QuuxToString(const IFoo::Quux &val) {
-    std::string s;
-
-    s = "Quux(first='";
-    s += val.first.c_str();
-    s += "', last='";
-    s += val.last.c_str();
-    s += "')";
-
-    return s;
-}
-
-static std::string MultiDimensionalToString(const IFoo::MultiDimensional &val) {
-    std::string s;
-
-    s += "MultiDimensional(";
-
-    s += "quuxMatrix=[";
-
-    size_t k = 0;
-    for (size_t i = 0; i < 5; ++i) {
-        if (i > 0) {
-            s += ", ";
-        }
-
-        s += "[";
-        for (size_t j = 0; j < 3; ++j, ++k) {
-            if (j > 0) {
-                s += ", ";
-            }
-
-            s += QuuxToString(val.quuxMatrix[i][j]);
-        }
-    }
-    s += "]";
-
-    s += ")";
-
-    return s;
-}
 
 Return<void> Foo::callingDrWho(
         const MultiDimensional &in, callingDrWho_cb _hidl_cb) {
diff --git a/tests/foo/1.0/default/FooCallback.cpp b/tests/foo/1.0/default/FooCallback.cpp
index d3eef77..e4704f2 100644
--- a/tests/foo/1.0/default/FooCallback.cpp
+++ b/tests/foo/1.0/default/FooCallback.cpp
@@ -3,7 +3,9 @@
 
 #include "FooCallback.h"
 #include <android-base/logging.h>
+#include <hidl-test/FooHelper.h>
 #include <inttypes.h>
+#include <utils/Timers.h>
 
 namespace android {
 namespace hardware {
diff --git a/tests/foo/1.0/default/FooCallback.h b/tests/foo/1.0/default/FooCallback.h
index 5921972..00233b5 100644
--- a/tests/foo/1.0/default/FooCallback.h
+++ b/tests/foo/1.0/default/FooCallback.h
@@ -6,7 +6,6 @@
 #include <hidl/MQDescriptor.h>
 
 #include <utils/Condition.h>
-#include <utils/Timers.h>
 namespace android {
 namespace hardware {
 namespace tests {
@@ -30,11 +29,6 @@
     Return<void> reportResults(int64_t ns, reportResults_cb _hidl_cb)  override;
     Return<void> youBlockedMeFor(const hidl_array<int64_t, 3 /* 3 */>& callerBlockedInfo)  override;
 
-    static constexpr nsecs_t DELAY_S = 1;
-    static constexpr nsecs_t DELAY_NS = seconds_to_nanoseconds(DELAY_S);
-    static constexpr nsecs_t TOLERANCE_NS = milliseconds_to_nanoseconds(10);
-    static constexpr nsecs_t ONEWAY_TOLERANCE_NS = milliseconds_to_nanoseconds(1);
-
     hidl_array<InvokeInfo, 3> invokeInfo;
     Mutex mLock;
     Condition mCond;
diff --git a/tests/foo/1.0/default/lib/Android.bp b/tests/foo/1.0/default/lib/Android.bp
new file mode 100644
index 0000000..c7ad57e
--- /dev/null
+++ b/tests/foo/1.0/default/lib/Android.bp
@@ -0,0 +1,15 @@
+cc_library_shared {
+    name: "libfootest",
+    srcs: [
+        "FooHelper.cpp"
+    ],
+
+    shared_libs: [
+        "libhidl",
+        "libhwbinder",
+        "android.hardware.tests.foo@1.0",
+    ],
+    local_include_dirs: ["include/hidl-test"],
+    export_include_dirs: ["include"],
+
+}
diff --git a/tests/foo/1.0/default/lib/FooHelper.cpp b/tests/foo/1.0/default/lib/FooHelper.cpp
new file mode 100644
index 0000000..8c5ff94
--- /dev/null
+++ b/tests/foo/1.0/default/lib/FooHelper.cpp
@@ -0,0 +1,57 @@
+#include "FooHelper.h"
+
+namespace android {
+
+std::string to_string(const IFoo::StringMatrix5x3 &M) {
+    return to_string(M.s);
+}
+
+std::string to_string(const IFoo::StringMatrix3x5 &M) {
+    return to_string(M.s);
+}
+
+std::string to_string(const hidl_string &s) {
+    return std::string("'") + s.c_str() + "'";
+}
+
+std::string QuuxToString(const IFoo::Quux &val) {
+    std::string s;
+
+    s = "Quux(first='";
+    s += val.first.c_str();
+    s += "', last='";
+    s += val.last.c_str();
+    s += "')";
+
+    return s;
+}
+
+std::string MultiDimensionalToString(const IFoo::MultiDimensional &val) {
+    std::string s;
+
+    s += "MultiDimensional(";
+
+    s += "quuxMatrix=[";
+
+    size_t k = 0;
+    for (size_t i = 0; i < 5; ++i) {
+        if (i > 0) {
+            s += ", ";
+        }
+
+        s += "[";
+        for (size_t j = 0; j < 3; ++j, ++k) {
+            if (j > 0) {
+                s += ", ";
+            }
+
+            s += QuuxToString(val.quuxMatrix[i][j]);
+        }
+    }
+    s += "]";
+
+    s += ")";
+
+    return s;
+}
+} // namespace android
diff --git a/tests/foo/1.0/default/lib/include/hidl-test/FooHelper.h b/tests/foo/1.0/default/lib/include/hidl-test/FooHelper.h
new file mode 100644
index 0000000..09b0557
--- /dev/null
+++ b/tests/foo/1.0/default/lib/include/hidl-test/FooHelper.h
@@ -0,0 +1,79 @@
+#ifndef ANDROID_HIDL_TEST_FOO_HELPER_H
+#define ANDROID_HIDL_TEST_FOO_HELPER_H
+#include <string>
+#include <android/hardware/tests/foo/1.0/IFoo.h>
+#include <utils/Timers.h>
+
+namespace android {
+
+using std::to_string;
+using hardware::hidl_string;
+using hardware::hidl_vec;
+using hardware::hidl_array;
+using hardware::tests::foo::V1_0::IFoo;
+
+static constexpr nsecs_t DELAY_S = 1;
+static constexpr nsecs_t DELAY_NS = seconds_to_nanoseconds(DELAY_S);
+static constexpr nsecs_t TOLERANCE_NS = milliseconds_to_nanoseconds(10);
+static constexpr nsecs_t ONEWAY_TOLERANCE_NS = milliseconds_to_nanoseconds(1);
+
+std::string to_string(const IFoo::StringMatrix5x3 &M);
+std::string to_string(const IFoo::StringMatrix3x5 &M);
+// Add quotes around s. For testing purposes only.
+std::string to_string(const hidl_string &s);
+
+template<typename T>
+std::string to_string(const T *elems, size_t n) {
+    std::string out;
+    out = "[";
+    for (size_t i = 0; i < n; ++i) {
+        if (i > 0) {
+            out += ", ";
+        }
+        out += to_string(elems[i]);
+    }
+    out += "]";
+
+    return out;
+}
+
+template<typename T, size_t SIZE>
+std::string to_string(const hidl_array<T, SIZE> &array) {
+    return to_string(&array[0], SIZE);
+}
+
+template<typename T, size_t SIZE1, size_t SIZE2>
+std::string to_string(const hidl_array<T, SIZE1, SIZE2> &array) {
+    std::string out;
+    out = "[";
+    for (size_t i = 0; i < SIZE1; ++i) {
+        if (i > 0) {
+            out += ", ";
+        }
+
+        out += "[";
+        for (size_t j = 0; j < SIZE2; ++j) {
+            if (j > 0) {
+                out += ", ";
+            }
+
+            out += to_string(array[i][j]);
+        }
+        out += "]";
+    }
+    out += "]";
+
+    return out;
+}
+
+template<typename T>
+std::string to_string(const hidl_vec<T> &vec) {
+    return to_string(&vec[0], vec.size());
+}
+
+std::string QuuxToString(const IFoo::Quux &val);
+
+std::string MultiDimensionalToString(const IFoo::MultiDimensional &val);
+
+} // namespace android
+#endif // ANDROID_HIDL_TEST_TEST_HELPER_H
diff --git a/tests/pointer/1.0/default/Android.bp b/tests/pointer/1.0/default/Android.bp
index cee6c91..081b7aa 100644
--- a/tests/pointer/1.0/default/Android.bp
+++ b/tests/pointer/1.0/default/Android.bp
@@ -11,6 +11,7 @@
     shared_libs: [
         "libbase",
         "libhidl",
+        "libpointertest",
         "libhwbinder",
         "liblog",
         "libutils",
diff --git a/tests/pointer/1.0/default/Graph.cpp b/tests/pointer/1.0/default/Graph.cpp
index a43df81..5aa2243 100644
--- a/tests/pointer/1.0/default/Graph.cpp
+++ b/tests/pointer/1.0/default/Graph.cpp
@@ -1,5 +1,6 @@
 #include "Graph.h"
 #include <android-base/logging.h>
+#include <hidl-test/PointerHelper.h>
 
 #define PUSH_ERROR_IF(__cond__) if(__cond__) { errors.push_back(std::to_string(__LINE__) + ": " + #__cond__); }
 
@@ -10,38 +11,6 @@
 namespace V1_0 {
 namespace implementation {
 
-static void simpleGraph(IGraph::Graph& g) {
-    g.nodes.resize(2);
-    g.edges.resize(1);
-    g.nodes[0].data = 10;
-    g.nodes[1].data = 20;
-    g.edges[0].left = &g.nodes[0];
-    g.edges[0].right = &g.nodes[1];
-}
-
-static bool isSimpleGraph(const IGraph::Graph &g) {
-    if(g.nodes.size() != 2) return false;
-    if(g.edges.size() != 1) return false;
-    if(g.nodes[0].data != 10) return false;
-    if(g.nodes[1].data != 20) return false;
-    if(g.edges[0].left != &g.nodes[0]) return false;
-    if(g.edges[0].right != &g.nodes[1]) return false;
-    return true;
-}
-
-static void logSimpleGraph(const char *prefix, const IGraph::Graph& g) {
-    ALOGI("%s Graph %p, %d nodes, %d edges", prefix, &g, (int)g.nodes.size(), (int)g.edges.size());
-    std::ostringstream os;
-    for(size_t i = 0; i < g.nodes.size(); i++)
-      os << &g.nodes[i] << " = " << g.nodes[i].data << ", ";
-    ALOGI("%s Nodes: [%s]", prefix, os.str().c_str());
-    os.str("");
-    os.clear();
-    for(size_t i = 0; i < g.edges.size(); i++)
-      os << g.edges[i].left << " -> " << g.edges[i].right << ", ";
-    ALOGI("%s Edges: [%s]", prefix, os.str().c_str());
-}
-
 // Methods from ::android::hardware::tests::pointer::V1_0::IGraph follow.
 Return<void> Graph::passAGraph(const IGraph::Graph& g) {
     ALOGI("SERVER(Graph) passAGraph start.");
diff --git a/tests/pointer/1.0/default/lib/Android.bp b/tests/pointer/1.0/default/lib/Android.bp
new file mode 100644
index 0000000..efc61df
--- /dev/null
+++ b/tests/pointer/1.0/default/lib/Android.bp
@@ -0,0 +1,17 @@
+cc_library_shared {
+    name: "libpointertest",
+    srcs: [
+        "PointerHelper.cpp"
+    ],
+
+    shared_libs: [
+        "libbase",
+        "libhidl",
+        "libhwbinder",
+        "liblog",
+        "android.hardware.tests.pointer@1.0",
+    ],
+    local_include_dirs: ["include/hidl-test"],
+    export_include_dirs: ["include"],
+
+}
diff --git a/tests/pointer/1.0/default/lib/PointerHelper.cpp b/tests/pointer/1.0/default/lib/PointerHelper.cpp
new file mode 100644
index 0000000..ed7d49a
--- /dev/null
+++ b/tests/pointer/1.0/default/lib/PointerHelper.cpp
@@ -0,0 +1,37 @@
+#define LOG_TAG "hidl_test"
+#include <android-base/logging.h>
+#include "PointerHelper.h"
+namespace android {
+
+void simpleGraph(IGraph::Graph& g) {
+    g.nodes.resize(2);
+    g.edges.resize(1);
+    g.nodes[0].data = 10;
+    g.nodes[1].data = 20;
+    g.edges[0].left = &g.nodes[0];
+    g.edges[0].right = &g.nodes[1];
+}
+
+bool isSimpleGraph(const IGraph::Graph &g) {
+    if(g.nodes.size() != 2) return false;
+    if(g.edges.size() != 1) return false;
+    if(g.nodes[0].data != 10) return false;
+    if(g.nodes[1].data != 20) return false;
+    if(g.edges[0].left != &g.nodes[0]) return false;
+    if(g.edges[0].right != &g.nodes[1]) return false;
+    return true;
+}
+
+void logSimpleGraph(const char *prefix, const IGraph::Graph& g) {
+    ALOGI("%s Graph %p, %d nodes, %d edges", prefix, &g, (int)g.nodes.size(), (int)g.edges.size());
+    std::ostringstream os;
+    for(size_t i = 0; i < g.nodes.size(); i++)
+      os << &g.nodes[i] << " = " << g.nodes[i].data << ", ";
+    ALOGI("%s Nodes: [%s]", prefix, os.str().c_str());
+    os.str("");
+    os.clear();
+    for(size_t i = 0; i < g.edges.size(); i++)
+      os << g.edges[i].left << " -> " << g.edges[i].right << ", ";
+    ALOGI("%s Edges: [%s]", prefix, os.str().c_str());
+}
+} // namespace android
diff --git a/tests/pointer/1.0/default/lib/include/hidl-test/PointerHelper.h b/tests/pointer/1.0/default/lib/include/hidl-test/PointerHelper.h
new file mode 100644
index 0000000..cd2edcb
--- /dev/null
+++ b/tests/pointer/1.0/default/lib/include/hidl-test/PointerHelper.h
@@ -0,0 +1,15 @@
+#ifndef ANDROID_HIDL_TEST_POINTER_HELPER_H
+#define ANDROID_HIDL_TEST_POINTER_HELPER_H
+
+#include <android/hardware/tests/pointer/1.0/IGraph.h>
+
+using ::android::hardware::tests::pointer::V1_0::IGraph;
+
+namespace android {
+
+void simpleGraph(IGraph::Graph& g);
+bool isSimpleGraph(const IGraph::Graph &g);
+void logSimpleGraph(const char *prefix, const IGraph::Graph& g);
+
+} // namespace android
+#endif // ANDROID_HIDL_TEST_POINTER_HELPER_H