Camera2: Tests -- Fork each test before executing it

Use CAMERA2_TEST_FORKING_DISABLED=1 environment variable to override this
behavior (e.g. when wanting to attach gdb to the test app).

Change-Id: Ib639885bdb827fc2415c878cbcb1b2d84dff687b
diff --git a/tests/camera2/CameraModuleFixture.h b/tests/camera2/CameraModuleFixture.h
index f000fdf..d604ff7 100644
--- a/tests/camera2/CameraModuleFixture.h
+++ b/tests/camera2/CameraModuleFixture.h
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#ifndef __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
+#define __ANDROID_HAL_CAMERA2_TESTS_MODULE_FIXTURE__
+
 #include <gtest/gtest.h>
 
 #include "hardware/hardware.h"
@@ -21,6 +24,7 @@
 
 #include "Camera2Device.h"
 #include "camera2_utils.h"
+#include "TestExtensions.h"
 
 namespace android {
 namespace camera2 {
@@ -30,18 +34,24 @@
 struct CameraModuleFixture {
 
     CameraModuleFixture(int CameraID = -1) {
+        TEST_EXTENSION_FORKING_CONSTRUCTOR;
+
         mCameraID = CameraID;
 
         SetUp();
     }
 
     ~CameraModuleFixture() {
+        TEST_EXTENSION_FORKING_DESTRUCTOR;
+
         TearDown();
     }
 
 private:
 
     void SetUp() {
+        TEST_EXTENSION_FORKING_SET_UP;
+
         ASSERT_LE(0, hw_get_module(CAMERA_HARDWARE_MODULE_ID,
             (const hw_module_t **)&mModule)) << "Could not load camera module";
         ASSERT_NE((void*)0, mModule);
@@ -58,14 +68,18 @@
     }
 
     void TearDown() {
+        TEST_EXTENSION_FORKING_TEAR_DOWN;
+
         TearDownMixin();
 
         /* important: device must be destructed before closing module,
            since it calls back into HAL */
         mDevice.clear();
 
-        ASSERT_EQ(0, HWModuleHelpers::closeModule(&mModule->common))
-            << "Failed to close camera HAL module";
+        if (!TEST_EXTENSION_FORKING_ENABLED) {
+            ASSERT_EQ(0, HWModuleHelpers::closeModule(&mModule->common))
+                << "Failed to close camera HAL module";
+        }
     }
 
     void SetUpMixin() {
@@ -105,3 +119,4 @@
 }
 }
 
+#endif