Create DvrApiTest to load libdvr.so at runtime and get Dvr API.

Bug: 69267804
Test: mma, manually push and run dvr_display-test and
dvr_buffer_queue-test in a rooted 8.1.0 phone
Visually check the screen display red color.

Change-Id: I00934d18e4c9fa8edef0a421c03124a65c98e260
diff --git a/libs/vr/libdvr/tests/dvr_api_test.h b/libs/vr/libdvr/tests/dvr_api_test.h
new file mode 100644
index 0000000..648af75
--- /dev/null
+++ b/libs/vr/libdvr/tests/dvr_api_test.h
@@ -0,0 +1,38 @@
+#include <dlfcn.h>
+#include <dvr/dvr_api.h>
+
+#include <gtest/gtest.h>
+
+#define ASSERT_NOT_NULL(x) ASSERT_TRUE((x) != nullptr)
+
+/** DvrTestBase loads the libdvr.so at runtime and get the Dvr API version 1. */
+class DvrApiTest : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    int flags = RTLD_NOW | RTLD_LOCAL;
+
+    // Here we need to ensure that libdvr is loaded with RTLD_NODELETE flag set
+    // (so that calls to `dlclose` don't actually unload the library). This is a
+    // workaround for an Android NDK bug. See more detail:
+    // https://github.com/android-ndk/ndk/issues/360
+    flags |= RTLD_NODELETE;
+    platform_handle_ = dlopen("libdvr.so", flags);
+    ASSERT_NOT_NULL(platform_handle_) << "Dvr shared library missing.";
+
+    auto dvr_get_api = reinterpret_cast<decltype(&dvrGetApi)>(
+        dlsym(platform_handle_, "dvrGetApi"));
+    ASSERT_NOT_NULL(dvr_get_api) << "Platform library missing dvrGetApi.";
+
+    ASSERT_EQ(dvr_get_api(&api_, sizeof(api_), /*version=*/1), 0)
+        << "Unable to find compatible Dvr API.";
+  }
+
+  void TearDown() override {
+    if (platform_handle_ != nullptr) {
+      dlclose(platform_handle_);
+    }
+  }
+
+  void* platform_handle_ = nullptr;
+  DvrApi_v1 api_;
+};
diff --git a/libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp b/libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp
index ea37935..301458a 100644
--- a/libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp
+++ b/libs/vr/libdvr/tests/dvr_buffer_queue-test.cpp
@@ -1,6 +1,5 @@
 #include <android/log.h>
 #include <android/native_window.h>
-#include <dlfcn.h>
 #include <dvr/dvr_api.h>
 #include <dvr/dvr_buffer_queue.h>
 
@@ -9,14 +8,14 @@
 #include <array>
 #include <unordered_map>
 
+#include "dvr_api_test.h"
+
 #define LOG_TAG "dvr_buffer_queue-test"
 
 #ifndef ALOGD
 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
 #endif
 
-#define ASSERT_NOT_NULL(x) ASSERT_TRUE((x) != nullptr)
-
 #ifndef ALOGD_IF
 #define ALOGD_IF(cond, ...) \
   ((__predict_false(cond)) ? ((void)ALOGD(__VA_ARGS__)) : (void)0)
@@ -31,7 +30,7 @@
 static constexpr uint64_t kBufferUsage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN;
 static constexpr size_t kQueueCapacity = 3;
 
-class DvrBufferQueueTest : public ::testing::Test {
+class DvrBufferQueueTest : public DvrApiTest {
  public:
   static void BufferAvailableCallback(void* context) {
     DvrBufferQueueTest* thiz = static_cast<DvrBufferQueueTest*>(context);
@@ -44,33 +43,12 @@
   }
 
  protected:
-  void SetUp() override {
-    int flags = RTLD_NOW | RTLD_LOCAL;
-
-    // Here we need to ensure that libdvr is loaded with RTLD_NODELETE flag set
-    // (so that calls to `dlclose` don't actually unload the library). This is a
-    // workaround for an Android NDK bug. See more detail:
-    // https://github.com/android-ndk/ndk/issues/360
-    flags |= RTLD_NODELETE;
-    platform_handle_ = dlopen("libdvr.so", flags);
-    ASSERT_NOT_NULL(platform_handle_) << "Dvr shared library missing.";
-
-    auto dvr_get_api = reinterpret_cast<decltype(&dvrGetApi)>(
-        dlsym(platform_handle_, "dvrGetApi"));
-    ASSERT_NOT_NULL(dvr_get_api) << "Platform library missing dvrGetApi.";
-
-    ASSERT_EQ(dvr_get_api(&api_, sizeof(api_), /*version=*/1), 0)
-        << "Unable to find compatible Dvr API.";
-  }
-
   void TearDown() override {
     if (write_queue_ != nullptr) {
       api_.WriteBufferQueueDestroy(write_queue_);
       write_queue_ = nullptr;
     }
-    if (platform_handle_ != nullptr) {
-      dlclose(platform_handle_);
-    }
+    DvrApiTest::TearDown();
   }
 
   void HandleBufferAvailable() {
@@ -87,8 +65,6 @@
   DvrWriteBufferQueue* write_queue_{nullptr};
   int buffer_available_count_{0};
   int buffer_removed_count_{0};
-  void* platform_handle_{nullptr};
-  DvrApi_v1 api_{};
 };
 
 TEST_F(DvrBufferQueueTest, WriteQueueCreateDestroy) {
diff --git a/libs/vr/libdvr/tests/dvr_display-test.cpp b/libs/vr/libdvr/tests/dvr_display-test.cpp
index c07b062..1165573 100644
--- a/libs/vr/libdvr/tests/dvr_display-test.cpp
+++ b/libs/vr/libdvr/tests/dvr_display-test.cpp
@@ -1,53 +1,29 @@
 #include <android/hardware_buffer.h>
 #include <android/log.h>
-#include <dlfcn.h>
 #include <dvr/dvr_api.h>
 #include <dvr/dvr_display_types.h>
 #include <dvr/dvr_surface.h>
 
 #include <gtest/gtest.h>
 
+#include "dvr_api_test.h"
+
 #define LOG_TAG "dvr_display-test"
 
 #ifndef ALOGD
 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
 #endif
 
-#define ASSERT_NOT_NULL(x) ASSERT_TRUE((x) != nullptr)
-
-class DvrDisplayTest : public ::testing::Test {
+class DvrDisplayTest : public DvrApiTest {
  protected:
-  void SetUp() override {
-    int flags = RTLD_NOW | RTLD_LOCAL;
-
-    // Here we need to ensure that libdvr is loaded with RTLD_NODELETE flag set
-    // (so that calls to `dlclose` don't actually unload the library). This is a
-    // workaround for an Android NDK bug. See more detail:
-    // https://github.com/android-ndk/ndk/issues/360
-    flags |= RTLD_NODELETE;
-    platform_handle_ = dlopen("libdvr.so", flags);
-    ASSERT_NOT_NULL(platform_handle_) << "Dvr shared library missing.";
-
-    auto dvr_get_api = reinterpret_cast<decltype(&dvrGetApi)>(
-        dlsym(platform_handle_, "dvrGetApi"));
-    ASSERT_NOT_NULL(dvr_get_api) << "Platform library missing dvrGetApi.";
-
-    ASSERT_EQ(dvr_get_api(&api_, sizeof(api_), /*version=*/1), 0)
-        << "Unable to find compatible Dvr API.";
-  }
-
   void TearDown() override {
     if (write_queue_ != nullptr) {
       api_.WriteBufferQueueDestroy(write_queue_);
       write_queue_ = nullptr;
     }
-    if (platform_handle_ != nullptr) {
-      dlclose(platform_handle_);
-    }
+    DvrApiTest::TearDown();
   }
 
-  void* platform_handle_ = nullptr;
-  DvrApi_v1 api_;
   DvrWriteBufferQueue* write_queue_ = nullptr;
 };