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/Android.mk b/tests/camera2/Android.mk
index e9a8ebc..6b6a6c4 100644
--- a/tests/camera2/Android.mk
+++ b/tests/camera2/Android.mk
@@ -9,6 +9,8 @@
 	CameraModuleTests.cpp \
 	CameraStreamTests.cpp \
 	CameraFrameTests.cpp \
+	ForkedTests.cpp \
+	TestForkerEventListener.cpp \
 
 LOCAL_SHARED_LIBRARIES := \
 	libutils \
@@ -34,6 +36,8 @@
 	frameworks/av/services/camera/libcameraservice \
 	frameworks/native/include \
 
+LOCAL_CFLAGS += -Wall -Wextra
+
 LOCAL_MODULE:= camera2_test
 LOCAL_MODULE_TAGS := tests
 
diff --git a/tests/camera2/CameraFrameTests.cpp b/tests/camera2/CameraFrameTests.cpp
index 9e3482c..6f88312 100644
--- a/tests/camera2/CameraFrameTests.cpp
+++ b/tests/camera2/CameraFrameTests.cpp
@@ -32,6 +32,7 @@
 #include <unistd.h>
 
 #include "CameraStreamFixture.h"
+#include "TestExtensions.h"
 
 #define CAMERA_FRAME_TIMEOUT    1000000000 //nsecs (1 secs)
 #define CAMERA_HEAP_COUNT       2 //HALBUG: 1 means registerBuffers fails
@@ -56,12 +57,16 @@
 
 public:
     CameraFrameTest() : CameraStreamFixture(STREAM_PARAMETERS) {
+        TEST_EXTENSION_FORKING_CONSTRUCTOR;
+
         if (!HasFatalFailure()) {
             CreateStream();
         }
     }
 
     ~CameraFrameTest() {
+        TEST_EXTENSION_FORKING_DESTRUCTOR;
+
         if (mDevice.get()) {
             mDevice->waitUntilDrained();
         }
@@ -69,8 +74,10 @@
     }
 
     virtual void SetUp() {
+        TEST_EXTENSION_FORKING_SET_UP;
     }
     virtual void TearDown() {
+        TEST_EXTENSION_FORKING_TEAR_DOWN;
     }
 
 protected:
@@ -79,9 +86,7 @@
 
 TEST_P(CameraFrameTest, GetFrame) {
 
-    if (HasFatalFailure()) {
-        return;
-    }
+    TEST_EXTENSION_FORKING_INIT;
 
     /* Submit a PREVIEW type request, then wait until we get the frame back */
     CameraMetadata previewRequest;
diff --git a/tests/camera2/CameraMetadataTests.cpp b/tests/camera2/CameraMetadataTests.cpp
index 739eba6..18710bc 100644
--- a/tests/camera2/CameraMetadataTests.cpp
+++ b/tests/camera2/CameraMetadataTests.cpp
@@ -32,7 +32,9 @@
 #include <gui/SurfaceTextureClient.h>
 
 #include <string>
+
 #include "CameraStreamFixture.h"
+#include "TestExtensions.h"
 
 namespace android {
 namespace camera2 {
@@ -45,16 +47,18 @@
     /*mHeapCount*/  2
 };
 
-class DISABLED_CameraMetadataTest
+class CameraMetadataTest
     : public ::testing::Test,
       public CameraStreamFixture {
 
 public:
-    DISABLED_CameraMetadataTest()
+    CameraMetadataTest()
     : CameraStreamFixture(METADATA_STREAM_PARAMETERS) {
+        TEST_EXTENSION_FORKING_CONSTRUCTOR;
     }
 
-    ~DISABLED_CameraMetadataTest() {
+    ~CameraMetadataTest() {
+        TEST_EXTENSION_FORKING_DESTRUCTOR;
     }
 
     int GetTypeFromTag(uint32_t tag) const {
@@ -67,22 +71,13 @@
         return entry.type;
     }
 
-    static void SetUpTestCase() {
-    }
-
-    static void TearDownTestCase()
-    {
-    }
-
 protected:
 
 };
 
-TEST_F(DISABLED_CameraMetadataTest, types) {
+TEST_F(CameraMetadataTest, types) {
 
-    if (HasFatalFailure()) {
-        return;
-    }
+    TEST_EXTENSION_FORKING_INIT;
 
     //FIXME: set this up in an external file of some sort (xml?)
     {
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
diff --git a/tests/camera2/CameraModuleTests.cpp b/tests/camera2/CameraModuleTests.cpp
index 2bfdb88..5e85698 100644
--- a/tests/camera2/CameraModuleTests.cpp
+++ b/tests/camera2/CameraModuleTests.cpp
@@ -16,7 +16,7 @@
 
 #include <gtest/gtest.h>
 
-#define LOG_TAG "DISABLED_CameraModuleTest"
+#define LOG_TAG "CameraModuleTest"
 #define LOG_NDEBUG 0
 #include <utils/Log.h>
 
@@ -31,23 +31,13 @@
 namespace camera2 {
 namespace tests {
 
-class DISABLED_CameraModuleTest : public ::testing::Test,
+class CameraModuleTest : public ::testing::Test,
                                   public CameraModuleFixture<> {
-
-    virtual void SetUp() {
-        //CameraModuleFixture::SetUp();
-    }
-
-    virtual void TearDown() {
-        //CameraModuleFixture::TearDown();
-    }
 };
 
-TEST_F(DISABLED_CameraModuleTest, LoadModule) {
+TEST_F(CameraModuleTest, LoadModule) {
 
-    if (HasFatalFailure()) {
-        return;
-    }
+    TEST_EXTENSION_FORKING_INIT;
 
     for (int i = 0; i < mNumberOfCameras; ++i) {
         mDevice = new Camera2Device(i);
@@ -58,11 +48,9 @@
 
 }
 
-TEST_F(DISABLED_CameraModuleTest, LoadModuleBadIndices) {
+TEST_F(CameraModuleTest, LoadModuleBadIndices) {
 
-    if (HasFatalFailure()) {
-        return;
-    }
+    TEST_EXTENSION_FORKING_INIT;
 
     int idx[] = { -1, mNumberOfCameras, mNumberOfCameras + 1 };
 
@@ -77,11 +65,9 @@
     }
 }
 
-TEST_F(DISABLED_CameraModuleTest, GetCameraInfo) {
+TEST_F(CameraModuleTest, GetCameraInfo) {
 
-    if (HasFatalFailure()) {
-        return;
-    }
+    TEST_EXTENSION_FORKING_INIT;
 
     for (int i = 0; i < mNumberOfCameras; ++i) {
         struct camera_info info;
@@ -90,11 +76,9 @@
 
 }
 
-TEST_F(DISABLED_CameraModuleTest, GetCameraInfoBadIndices) {
+TEST_F(CameraModuleTest, GetCameraInfoBadIndices) {
 
-    if (HasFatalFailure()) {
-        return;
-    }
+    TEST_EXTENSION_FORKING_INIT;
 
     int idx[] = { -1, mNumberOfCameras, mNumberOfCameras + 1 };
     for (unsigned i = 0; i < sizeof(idx)/sizeof(idx[0]); ++i) {
@@ -109,6 +93,8 @@
 /**
  * TODO: Additional test to add: open two cameras at once.
  *       (is allowed to fail, at least for now, but should not blow up)
+ *     - open same device multiple times
+ *     - close same device multiple times
  */
 
 
diff --git a/tests/camera2/CameraStreamFixture.h b/tests/camera2/CameraStreamFixture.h
index 4bd8ac1..d4ba764 100644
--- a/tests/camera2/CameraStreamFixture.h
+++ b/tests/camera2/CameraStreamFixture.h
@@ -14,12 +14,17 @@
  * limitations under the License.
  */
 
+#ifndef __ANDROID_HAL_CAMERA2_TESTS_STREAM_FIXTURE__
+#define __ANDROID_HAL_CAMERA2_TESTS_STREAM_FIXTURE__
+
 #include <gtest/gtest.h>
+#include <iostream>
 
 #include <gui/CpuConsumer.h>
 #include <gui/SurfaceTextureClient.h>
 
 #include "CameraModuleFixture.h"
+#include "TestExtensions.h"
 
 namespace android {
 namespace camera2 {
@@ -31,24 +36,38 @@
     int mHeapCount;
 };
 
+inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) {
+    *os <<  "{ ";
+    *os <<  "CameraID: "  << p.mCameraId << ", ";
+    *os <<  "Format: "    << p.mCameraId << ", ";
+    *os <<  "HeapCount: " << p.mCameraId;
+    *os << " }";
+}
+
 class CameraStreamFixture
     : public CameraModuleFixture</*InfoQuirk*/true> {
 
 public:
     CameraStreamFixture(CameraStreamParams p)
     : CameraModuleFixture(p.mCameraId) {
+        TEST_EXTENSION_FORKING_CONSTRUCTOR;
+
         mParam = p;
 
         SetUp();
     }
 
     ~CameraStreamFixture() {
+        TEST_EXTENSION_FORKING_DESTRUCTOR;
+
         TearDown();
     }
 
 private:
 
     void SetUp() {
+        TEST_EXTENSION_FORKING_SET_UP;
+
         CameraStreamParams p = mParam;
         sp<Camera2Device> device = mDevice;
 
@@ -69,6 +88,7 @@
         }
     }
     void TearDown() {
+        TEST_EXTENSION_FORKING_TEAR_DOWN;
     }
 
 protected:
@@ -95,13 +115,6 @@
         ASSERT_EQ(OK, mDevice->deleteStream(mStreamId));
     }
 
-    /* consider factoring out this common code into
-      a CameraStreamFixture<T>, e.g.
-      class CameraStreamTest : TestWithParam<CameraStreamParameters>,
-                               CameraStreamFixture<CameraStreamParameters>
-       to make it easier for other classes to not duplicate the params
-      */
-
     int mWidth;
     int mHeight;
 
@@ -117,3 +130,4 @@
 }
 }
 
+#endif
diff --git a/tests/camera2/CameraStreamTests.cpp b/tests/camera2/CameraStreamTests.cpp
index e06e4e9..26b2551 100644
--- a/tests/camera2/CameraStreamTests.cpp
+++ b/tests/camera2/CameraStreamTests.cpp
@@ -16,7 +16,7 @@
 
 #include <gtest/gtest.h>
 
-#define LOG_TAG "DISABLED_CameraStreamTest"
+#define LOG_TAG "CameraStreamTest"
 #define LOG_NDEBUG 0
 #include <utils/Log.h>
 
@@ -30,7 +30,7 @@
 #include <gui/SurfaceTextureClient.h>
 
 #include "CameraStreamFixture.h"
-
+#include "TestExtensions.h"
 
 using namespace android;
 using namespace android::camera2;
@@ -39,39 +39,36 @@
 namespace camera2 {
 namespace tests {
 
-class DISABLED_CameraStreamTest
+class CameraStreamTest
     : public ::testing::TestWithParam<CameraStreamParams>,
       public CameraStreamFixture {
 
 public:
-    DISABLED_CameraStreamTest() : CameraStreamFixture(GetParam()) {
+    CameraStreamTest() : CameraStreamFixture(GetParam()) {
+        TEST_EXTENSION_FORKING_CONSTRUCTOR;
     }
 
-    ~DISABLED_CameraStreamTest() {
+    ~CameraStreamTest() {
+        TEST_EXTENSION_FORKING_DESTRUCTOR;
     }
 
     virtual void SetUp() {
+        TEST_EXTENSION_FORKING_SET_UP;
     }
     virtual void TearDown() {
+        TEST_EXTENSION_FORKING_TEAR_DOWN;
     }
 
 protected:
 
 };
 
-TEST_P(DISABLED_CameraStreamTest, CreateStream) {
+TEST_P(CameraStreamTest, CreateStream) {
 
-    if (HasFatalFailure()) {
-        return;
-    }
+    TEST_EXTENSION_FORKING_INIT;
 
-    CreateStream();
-
-    if (HasFatalFailure()) {
-        return;
-    }
-
-    DeleteStream();
+    ASSERT_NO_FATAL_FAILURE(CreateStream());
+    ASSERT_NO_FATAL_FAILURE(DeleteStream());
 }
 
 //TODO: use a combinatoric generator
@@ -138,7 +135,7 @@
     },
 };
 
-INSTANTIATE_TEST_CASE_P(StreamParameterCombinations, DISABLED_CameraStreamTest,
+INSTANTIATE_TEST_CASE_P(StreamParameterCombinations, CameraStreamTest,
     testing::ValuesIn(TestParameters));
 
 
diff --git a/tests/camera2/ForkedTests.cpp b/tests/camera2/ForkedTests.cpp
new file mode 100644
index 0000000..315233e
--- /dev/null
+++ b/tests/camera2/ForkedTests.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#include <gtest/gtest.h>
+
+#include "TestExtensions.h"
+
+namespace android {
+namespace camera2 {
+namespace tests {
+
+// Intentionally disabled since 2 of these tests are supposed to fail
+class DISABLED_ForkedTest : public ::testing::Test {
+
+    virtual void SetUp() {
+        TEST_EXTENSION_FORKING_SET_UP;
+    }
+
+    virtual void TearDown() {
+        TEST_EXTENSION_FORKING_TEAR_DOWN;
+    }
+};
+
+// intentionally fail
+TEST_F(DISABLED_ForkedTest, FailCrash) {
+    TEST_EXTENSION_FORKING_INIT;
+
+    //intentionally crash
+    *(int*)0 = 0xDEADBEEF;
+}
+
+TEST_F(DISABLED_ForkedTest, SucceedNormal) {
+    TEST_EXTENSION_FORKING_INIT;
+
+    EXPECT_TRUE(true);
+}
+
+// intentionally fail
+TEST_F(DISABLED_ForkedTest, FailNormal) {
+    TEST_EXTENSION_FORKING_INIT;
+
+    EXPECT_TRUE(false);
+}
+
+}
+}
+}
+
diff --git a/tests/camera2/TestExtensions.h b/tests/camera2/TestExtensions.h
new file mode 100644
index 0000000..68a5e97
--- /dev/null
+++ b/tests/camera2/TestExtensions.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2012 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 __ANDROID_HAL_CAMERA2_TESTS_EXTENSIONS__
+#define __ANDROID_HAL_CAMERA2_TESTS_EXTENSIONS__
+
+#include "TestForkerEventListener.h"
+
+// Use at the beginning of each Test::SetUp() impl
+#define TEST_EXTENSION_FORKING_SET_UP                                       \
+    do {                                                                    \
+        if (TEST_EXTENSION_FORKING_ENABLED) {                               \
+            if (!TestForkerEventListener::mIsForked) {                      \
+                return;                                                     \
+            }                                                               \
+        }                                                                   \
+    } while (false)                                                         \
+
+// Use at the beginning of each Test::TearDown() impl
+#define TEST_EXTENSION_FORKING_TEAR_DOWN   TEST_EXTENSION_FORKING_SET_UP
+
+// Use at the beginning of each Test::Test constructor
+#define TEST_EXTENSION_FORKING_CONSTRUCTOR TEST_EXTENSION_FORKING_SET_UP
+
+// Use at the beginning of each Test::~Test destructor
+#define TEST_EXTENSION_FORKING_DESTRUCTOR  TEST_EXTENSION_FORKING_TEAR_DOWN
+
+// Use at the beginning of each test body, e.g. TEST(x,y), TEST_F(x,y), etc
+#define TEST_EXTENSION_FORKING_INIT                                         \
+    do {                                                                    \
+        TEST_EXTENSION_FORKING_SET_UP;                                      \
+        if (HasFatalFailure()) return;                                      \
+    } while(false)                                                          \
+
+// Are we running each test by forking it?
+#define TEST_EXTENSION_FORKING_ENABLED  (TestForkerEventListener::mUsingForking)
+
+
+
+#endif
+
diff --git a/tests/camera2/TestForkerEventListener.cpp b/tests/camera2/TestForkerEventListener.cpp
new file mode 100644
index 0000000..c9f1942
--- /dev/null
+++ b/tests/camera2/TestForkerEventListener.cpp
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <string.h>
+
+#include <gtest/gtest.h>
+
+#include "TestForkerEventListener.h"
+#include "TestExtensions.h"
+
+#define DEBUG_TEST_FORKER_EVENT_LISTENER 0
+
+#define RETURN_CODE_PASSED 0
+#define RETURN_CODE_FAILED 1
+
+namespace android {
+namespace camera2 {
+namespace tests {
+
+bool TestForkerEventListener::mUsingForking     = true;
+bool TestForkerEventListener::mIsForked         = false;
+
+TestForkerEventListener::TestForkerEventListener() {
+    mIsForked = false;
+    mHasSucceeded = true;
+    mTermSignal = 0;
+}
+
+void TestForkerEventListener::SetForking(bool enabled) {
+    mUsingForking = enabled;
+}
+
+// Called before a test starts.
+void TestForkerEventListener::OnTestStart(const ::testing::TestInfo&) {
+
+    if (!TEST_EXTENSION_FORKING_ENABLED) {
+        return;
+    }
+
+    pid_t childPid = fork();
+    if (childPid != 0) {
+        int status;
+        waitpid(childPid, &status, /*options*/0);
+
+        // terminated normally?
+        mHasSucceeded = WIFEXITED(status);
+        // terminate with return code 0 = test passed, 1 = test failed
+        if (mHasSucceeded) {
+          mHasSucceeded = WEXITSTATUS(status) == RETURN_CODE_PASSED;
+        } else if (WIFSIGNALED(status)) {
+          mTermSignal = WTERMSIG(status);
+        }
+
+        /* the test is then skipped by inserting the various
+        TEST_EXTENSION_ macros in TestExtensions.h */
+
+    } else {
+        mIsForked = true;
+    }
+}
+
+// Called after a failed assertion or a SUCCEED() invocation.
+void TestForkerEventListener::OnTestPartResult(
+    const ::testing::TestPartResult& test_part_result) {
+
+    if (DEBUG_TEST_FORKER_EVENT_LISTENER) {
+        printf("%s in %s:%d\n%s\n",
+             test_part_result.failed() ? "*** Failure" : "Success",
+             test_part_result.file_name(),
+             test_part_result.line_number(),
+             test_part_result.summary());
+    }
+}
+
+// Called after a test ends.
+void TestForkerEventListener::OnTestEnd(const ::testing::TestInfo& test_info) {
+
+    if (!TEST_EXTENSION_FORKING_ENABLED) {
+        return;
+    }
+
+    if (mIsForked) {
+        exit(test_info.result()->Passed()
+            ? RETURN_CODE_PASSED : RETURN_CODE_FAILED);
+    } else if (!mHasSucceeded && mTermSignal != 0) {
+
+      printf("*** Test %s.%s crashed with signal = %s\n",
+             test_info.test_case_name(), test_info.name(),
+             strsignal(mTermSignal));
+    }
+
+    //TODO: overload the default event listener to suppress this message
+    // dynamically (e.g. by skipping OnTestPartResult after OnTestEnd )
+
+    // trigger a test failure if the child has failed
+    if (!mHasSucceeded) {
+        ADD_FAILURE();
+    }
+    mTermSignal = 0;
+}
+
+
+}
+}
+}
+
diff --git a/tests/camera2/TestForkerEventListener.h b/tests/camera2/TestForkerEventListener.h
new file mode 100644
index 0000000..2383f17
--- /dev/null
+++ b/tests/camera2/TestForkerEventListener.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2012 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 __ANDROID_HAL_CAMERA2_TESTS_FORKER_EVENT_LISTENER__
+#define __ANDROID_HAL_CAMERA2_TESTS_FORKER_EVENT_LISTENER__
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace camera2 {
+namespace tests {
+
+// Fork before each test runs.
+class TestForkerEventListener : public ::testing::EmptyTestEventListener {
+
+public:
+
+    TestForkerEventListener();
+
+    // Should we fork before running each test?
+    static void SetForking(bool enabled);
+
+private:
+
+    // Called before a test starts.
+    virtual void OnTestStart(const ::testing::TestInfo& test_info);
+
+    // Called after a failed assertion or a SUCCEED() invocation.
+    virtual void OnTestPartResult(
+        const ::testing::TestPartResult& test_part_result);
+
+    // Called after a test ends.
+    virtual void OnTestEnd(const ::testing::TestInfo& test_info);
+
+    bool mHasSucceeded;
+    int mTermSignal;
+
+public:
+    // do not read directly. use TEST_EXTENSION macros instead
+    static bool mUsingForking;
+    static bool mIsForked;
+};
+
+}
+}
+}
+
+#endif
diff --git a/tests/camera2/camera2.cpp b/tests/camera2/camera2.cpp
index b3bbc95..fbb0503 100644
--- a/tests/camera2/camera2.cpp
+++ b/tests/camera2/camera2.cpp
@@ -15,7 +15,7 @@
  */
 
 #define LOG_TAG "Camera2_test"
-#define LOG_NDEBUG 0
+//#define LOG_NDEBUG 0
 
 #include <utils/Log.h>
 #include <gtest/gtest.h>
@@ -28,6 +28,7 @@
 #include <system/camera_metadata.h>
 
 #include "camera2_utils.h"
+#include "TestExtensions.h"
 
 namespace android {
 namespace camera2 {
@@ -35,7 +36,7 @@
 
 class Camera2Test: public testing::Test {
   public:
-    static void SetUpTestCase() {
+    void SetUpModule() {
         int res;
 
         hw_module_t *module = NULL;
@@ -105,7 +106,7 @@
         }
     }
 
-    static void TearDownTestCase() {
+    void TearDownModule() {
         hw_module_t *module = reinterpret_cast<hw_module_t*>(sCameraModule);
         ASSERT_EQ(0, HWModuleHelpers::closeModule(module));
     }
@@ -283,20 +284,30 @@
     }
 
     virtual void SetUp() {
+        TEST_EXTENSION_FORKING_SET_UP;
+
+        SetUpModule();
+
         const ::testing::TestInfo* const testInfo =
                 ::testing::UnitTest::GetInstance()->current_test_info();
+        (void)testInfo;
 
-        ALOGV("*** Starting test %s in test case %s", testInfo->name(), testInfo->test_case_name());
+        ALOGV("*** Starting test %s in test case %s", testInfo->name(),
+              testInfo->test_case_name());
         mDevice = NULL;
     }
 
     virtual void TearDown() {
+        TEST_EXTENSION_FORKING_TEAR_DOWN;
+
         for (unsigned int i = 0; i < mStreams.size(); i++) {
             delete mStreams[i];
         }
         if (mDevice != NULL) {
             closeCameraDevice(mDevice);
         }
+
+        TearDownModule();
     }
 
     camera2_device    *mDevice;
@@ -324,6 +335,9 @@
 
 
 TEST_F(Camera2Test, OpenClose) {
+
+    TEST_EXTENSION_FORKING_INIT;
+
     status_t res;
 
     for (int id = 0; id < getNumCameras(); id++) {
@@ -338,6 +352,9 @@
 }
 
 TEST_F(Camera2Test, Capture1Raw) {
+
+    TEST_EXTENSION_FORKING_INIT;
+
     status_t res;
 
     for (int id = 0; id < getNumCameras(); id++) {
@@ -452,6 +469,9 @@
 }
 
 TEST_F(Camera2Test, CaptureBurstRaw) {
+
+    TEST_EXTENSION_FORKING_INIT;
+
     status_t res;
 
     for (int id = 0; id < getNumCameras(); id++) {
@@ -584,6 +604,9 @@
 }
 
 TEST_F(Camera2Test, ConstructDefaultRequests) {
+
+    TEST_EXTENSION_FORKING_INIT;
+
     status_t res;
 
     for (int id = 0; id < getNumCameras(); id++) {
@@ -613,7 +636,7 @@
     }
 }
 
-TEST_F(Camera2Test, DISABLED_Capture1Jpeg) {
+TEST_F(Camera2Test, Capture1Jpeg) {
     status_t res;
 
     for (int id = 0; id < getNumCameras(); id++) {
diff --git a/tests/camera2/camera2_utils.h b/tests/camera2/camera2_utils.h
index c938ee7..757044b 100644
--- a/tests/camera2/camera2_utils.h
+++ b/tests/camera2/camera2_utils.h
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#ifndef __ANDROID_HAL_CAMERA2_TESTS_UTILS__
+#define __ANDROID_HAL_CAMERA2_TESTS_UTILS__
+
 // Utility classes for camera2 HAL testing
 
 #include <system/camera_metadata.h>
@@ -243,3 +246,5 @@
 }
 }
 }
+
+#endif
diff --git a/tests/camera2/main.cpp b/tests/camera2/main.cpp
index 40b9872..92c117a 100644
--- a/tests/camera2/main.cpp
+++ b/tests/camera2/main.cpp
@@ -14,12 +14,33 @@
  * limitations under the License.
  */
 
+#include <stdlib.h>
+
 #include <gtest/gtest.h>
+#include "TestForkerEventListener.h"
+
+using android::camera2::tests::TestForkerEventListener;
 
 int main(int argc, char **argv) {
 
     ::testing::InitGoogleTest(&argc, argv);
 
+    {
+        //TODO: have a command line flag as well
+        char *env = getenv("CAMERA2_TEST_FORKING_DISABLED");
+        if (env) {
+            int forking = atoi(env);
+
+            TestForkerEventListener::SetForking(!forking);
+        }
+    }
+
+    // Gets hold of the event listener list.
+    ::testing::TestEventListeners& listeners =
+        ::testing::UnitTest::GetInstance()->listeners();
+    // Adds a listener to the end.  Google Test takes the ownership.
+    listeners.Append(new TestForkerEventListener());
+
     int ret = RUN_ALL_TESTS();
 
     return ret;