Merge "Use strong pointers and scope"
diff --git a/tests/camera2/Android.mk b/tests/camera2/Android.mk
index 29a028b..b312328 100644
--- a/tests/camera2/Android.mk
+++ b/tests/camera2/Android.mk
@@ -12,6 +12,7 @@
 	CameraBurstTests.cpp \
 	ForkedTests.cpp \
 	TestForkerEventListener.cpp \
+	TestSettings.cpp \
 
 LOCAL_SHARED_LIBRARIES := \
 	libutils \
diff --git a/tests/camera2/CameraBurstTests.cpp b/tests/camera2/CameraBurstTests.cpp
index ea567f7..e39970c 100644
--- a/tests/camera2/CameraBurstTests.cpp
+++ b/tests/camera2/CameraBurstTests.cpp
@@ -51,7 +51,6 @@
 namespace tests {
 
 static CameraStreamParams STREAM_PARAMETERS = {
-    /*mCameraId*/   0,
     /*mFormat*/     CAMERA_EXPOSURE_FORMAT,
     /*mHeapCount*/  CAMERA_HEAP_COUNT
 };
@@ -123,16 +122,6 @@
 
         return acc;
     }
-
-protected:
-
-    camera_metadata_ro_entry GetStaticEntry(uint32_t tag) const {
-        const CameraMetadata& staticInfo = mDevice->info();
-        camera_metadata_ro_entry entry = staticInfo.find(tag);
-        return entry;
-    }
-
-
 };
 
 TEST_F(CameraBurstTest, ManualExposureControl) {
diff --git a/tests/camera2/CameraFrameTests.cpp b/tests/camera2/CameraFrameTests.cpp
index b24f4ac..13d1b17 100644
--- a/tests/camera2/CameraFrameTests.cpp
+++ b/tests/camera2/CameraFrameTests.cpp
@@ -46,7 +46,6 @@
 namespace tests {
 
 static CameraStreamParams STREAM_PARAMETERS = {
-    /*mCameraId*/   0,
     /*mFormat*/     HAL_PIXEL_FORMAT_YCrCb_420_SP,
     /*mHeapCount*/  CAMERA_HEAP_COUNT
 };
@@ -116,8 +115,18 @@
     for (int i = 0; i < GetParam(); ++i) {
         ALOGV("Reading capture request %d", i);
         ASSERT_EQ(OK, mDevice->waitForNextFrame(CAMERA_FRAME_TIMEOUT));
+
         CameraMetadata frameMetadata;
         ASSERT_EQ(OK, mDevice->getNextFrame(&frameMetadata));
+
+        // wait for buffer to be available
+        ASSERT_EQ(OK, mFrameListener->waitForFrame(CAMERA_FRAME_TIMEOUT));
+        ALOGV("We got the frame now");
+
+        // mark buffer consumed so producer can re-dequeue it
+        CpuConsumer::LockedBuffer imgBuffer;
+        ASSERT_EQ(OK, mCpuConsumer->lockNextBuffer(&imgBuffer));
+        ASSERT_EQ(OK, mCpuConsumer->unlockBuffer(imgBuffer));
     }
 
 }
diff --git a/tests/camera2/CameraMetadataTests.cpp b/tests/camera2/CameraMetadataTests.cpp
index 18710bc..d02d104 100644
--- a/tests/camera2/CameraMetadataTests.cpp
+++ b/tests/camera2/CameraMetadataTests.cpp
@@ -42,7 +42,6 @@
 
 //FIXME: dont hardcode
 static CameraStreamParams METADATA_STREAM_PARAMETERS = {
-    /*mCameraId*/   0,
     /*mFormat*/     HAL_PIXEL_FORMAT_YCrCb_420_SP,
     /*mHeapCount*/  2
 };
diff --git a/tests/camera2/CameraModuleFixture.h b/tests/camera2/CameraModuleFixture.h
index d604ff7..cd8ddc4 100644
--- a/tests/camera2/CameraModuleFixture.h
+++ b/tests/camera2/CameraModuleFixture.h
@@ -37,17 +37,17 @@
         TEST_EXTENSION_FORKING_CONSTRUCTOR;
 
         mCameraID = CameraID;
-
-        SetUp();
     }
 
     ~CameraModuleFixture() {
         TEST_EXTENSION_FORKING_DESTRUCTOR;
-
-        TearDown();
     }
 
-private:
+    camera_metadata_ro_entry GetStaticEntry(uint32_t tag) const {
+        const CameraMetadata& staticInfo = mDevice->info();
+        camera_metadata_ro_entry entry = staticInfo.find(tag);
+        return entry;
+    }
 
     void SetUp() {
         TEST_EXTENSION_FORKING_SET_UP;
@@ -82,6 +82,8 @@
         }
     }
 
+private:
+
     void SetUpMixin() {
         /* For using this fixture in other tests only */
         if (mCameraID != -1) {
diff --git a/tests/camera2/CameraModuleTests.cpp b/tests/camera2/CameraModuleTests.cpp
index 5e85698..fc6fd36 100644
--- a/tests/camera2/CameraModuleTests.cpp
+++ b/tests/camera2/CameraModuleTests.cpp
@@ -33,6 +33,38 @@
 
 class CameraModuleTest : public ::testing::Test,
                                   public CameraModuleFixture<> {
+
+public:
+    CameraModuleTest() {
+        CameraModuleFixture::SetUp();
+    }
+
+    ~CameraModuleTest() {
+        CameraModuleFixture::TearDown();
+    }
+
+    status_t initializeDevice(int cameraId) {
+
+        // ignore HAL1s. count as test pass
+        status_t stat;
+        if (isDeviceVersionHal2(cameraId, &stat) && stat == OK) {
+            stat = mDevice->initialize(mModule);
+        }
+
+        return stat;
+    }
+
+    int getDeviceVersion(int cameraId, status_t* status) {
+        camera_info info;
+        *status = mModule->get_camera_info(cameraId, &info);
+
+        return info.device_version;
+    }
+
+    bool isDeviceVersionHal2(int cameraId, status_t* status) {
+        return getDeviceVersion(cameraId, status)
+               >= CAMERA_DEVICE_API_VERSION_2_0;
+    }
 };
 
 TEST_F(CameraModuleTest, LoadModule) {
@@ -41,7 +73,8 @@
 
     for (int i = 0; i < mNumberOfCameras; ++i) {
         mDevice = new Camera2Device(i);
-        ASSERT_EQ(OK, mDevice->initialize(mModule))
+
+        ASSERT_EQ(OK, initializeDevice(i))
             << "Failed to initialize device " << i;
         mDevice.clear();
     }
@@ -56,7 +89,7 @@
 
     for (unsigned i = 0; i < sizeof(idx)/sizeof(idx[0]); ++i) {
         mDevice = new Camera2Device(idx[i]);
-        status_t deviceInitializeCode = mDevice->initialize(mModule);
+        status_t deviceInitializeCode = initializeDevice(idx[i]);
         EXPECT_NE(OK, deviceInitializeCode);
         EXPECT_EQ(-ENODEV, deviceInitializeCode)
             << "Incorrect error code when trying to initialize invalid index "
diff --git a/tests/camera2/CameraStreamFixture.h b/tests/camera2/CameraStreamFixture.h
index d93d26b..569b9d1 100644
--- a/tests/camera2/CameraStreamFixture.h
+++ b/tests/camera2/CameraStreamFixture.h
@@ -33,14 +33,12 @@
 namespace tests {
 
 struct CameraStreamParams {
-    int mCameraId;
     int mFormat;
     int mHeapCount;
 };
 
 inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) {
     *os <<  "{ ";
-    *os <<  "CameraID: "  << p.mCameraId  << ", ";
     *os <<  "Format: "    << p.mFormat    << ", ";
     *os <<  "HeapCount: " << p.mHeapCount;
     *os << " }";
@@ -51,7 +49,7 @@
 
 public:
     CameraStreamFixture(CameraStreamParams p)
-    : CameraModuleFixture(p.mCameraId) {
+    : CameraModuleFixture(TestSettings::DeviceId()) {
         TEST_EXTENSION_FORKING_CONSTRUCTOR;
 
         mParam = p;
@@ -70,6 +68,8 @@
     void SetUp() {
         TEST_EXTENSION_FORKING_SET_UP;
 
+        CameraModuleFixture::SetUp();
+
         CameraStreamParams p = mParam;
         sp<Camera2Device> device = mDevice;
 
@@ -91,6 +91,13 @@
     }
     void TearDown() {
         TEST_EXTENSION_FORKING_TEAR_DOWN;
+
+        // important: shut down HAL before releasing streams
+        CameraModuleFixture::TearDown();
+
+        mNativeWindow.clear();
+        mCpuConsumer.clear();
+        mFrameListener.clear();
     }
 
 protected:
diff --git a/tests/camera2/CameraStreamTests.cpp b/tests/camera2/CameraStreamTests.cpp
index 26b2551..b076296 100644
--- a/tests/camera2/CameraStreamTests.cpp
+++ b/tests/camera2/CameraStreamTests.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <iostream>
+#include <iomanip>
 #include <gtest/gtest.h>
 
 #define LOG_TAG "CameraStreamTest"
@@ -67,6 +69,36 @@
 
     TEST_EXTENSION_FORKING_INIT;
 
+    /** Make sure the format requested is supported. PASS this test if it's not
+      * not supported.
+      *
+      * TODO: would be nice of not running this test in the first place
+      *       somehow.
+      */
+    {
+        camera_metadata_ro_entry availableFormats =
+            GetStaticEntry(ANDROID_SCALER_AVAILABLE_FORMATS);
+
+        bool hasFormat = false;
+        for (size_t i = 0; i < availableFormats.count; ++i) {
+            if (availableFormats.data.i32[i] == GetParam().mFormat) {
+                hasFormat = true;
+                break;
+            }
+        }
+
+        if (!hasFormat) {
+            const ::testing::TestInfo* const test_info =
+                ::testing::UnitTest::GetInstance()->current_test_info();
+            std::cerr << "Skipping test "
+                      << test_info->test_case_name() << "."
+                      << test_info->name()
+                      << " because the format was not available: 0x"
+                      << std::hex << GetParam().mFormat << std::endl;
+            return;
+        }
+    }
+
     ASSERT_NO_FATAL_FAILURE(CreateStream());
     ASSERT_NO_FATAL_FAILURE(DeleteStream());
 }
@@ -74,62 +106,50 @@
 //TODO: use a combinatoric generator
 static CameraStreamParams TestParameters[] = {
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
         /*mHeapCount*/ 1
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
         /*mHeapCount*/ 2
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
         /*mHeapCount*/ 3
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_YCrCb_420_SP, // NV21
         /*mHeapCount*/ 1
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_YCrCb_420_SP,
         /*mHeapCount*/ 2
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_YCrCb_420_SP,
         /*mHeapCount*/ 3
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_YV12,
         /*mHeapCount*/ 1
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_YV12,
         /*mHeapCount*/ 2
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_YV12,
         /*mHeapCount*/ 3
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_RAW_SENSOR,
         /*mHeapCount*/ 1
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_RAW_SENSOR,
         /*mHeapCount*/ 2
     },
     {
-        /*cameraId*/   0,
         /*mFormat*/    HAL_PIXEL_FORMAT_RAW_SENSOR,
         /*mHeapCount*/ 3
     },
diff --git a/tests/camera2/TestExtensions.h b/tests/camera2/TestExtensions.h
index 68a5e97..2af587d 100644
--- a/tests/camera2/TestExtensions.h
+++ b/tests/camera2/TestExtensions.h
@@ -18,6 +18,7 @@
 #define __ANDROID_HAL_CAMERA2_TESTS_EXTENSIONS__
 
 #include "TestForkerEventListener.h"
+#include "TestSettings.h"
 
 // Use at the beginning of each Test::SetUp() impl
 #define TEST_EXTENSION_FORKING_SET_UP                                       \
@@ -46,7 +47,8 @@
     } while(false)                                                          \
 
 // Are we running each test by forking it?
-#define TEST_EXTENSION_FORKING_ENABLED  (TestForkerEventListener::mUsingForking)
+#define TEST_EXTENSION_FORKING_ENABLED                                      \
+    (android::camera2::tests::TestSettings::ForkingEnabled())
 
 
 
diff --git a/tests/camera2/TestForkerEventListener.cpp b/tests/camera2/TestForkerEventListener.cpp
index c9f1942..9416db2 100644
--- a/tests/camera2/TestForkerEventListener.cpp
+++ b/tests/camera2/TestForkerEventListener.cpp
@@ -33,7 +33,6 @@
 namespace camera2 {
 namespace tests {
 
-bool TestForkerEventListener::mUsingForking     = true;
 bool TestForkerEventListener::mIsForked         = false;
 
 TestForkerEventListener::TestForkerEventListener() {
@@ -42,10 +41,6 @@
     mTermSignal = 0;
 }
 
-void TestForkerEventListener::SetForking(bool enabled) {
-    mUsingForking = enabled;
-}
-
 // Called before a test starts.
 void TestForkerEventListener::OnTestStart(const ::testing::TestInfo&) {
 
diff --git a/tests/camera2/TestForkerEventListener.h b/tests/camera2/TestForkerEventListener.h
index 2383f17..347a06b 100644
--- a/tests/camera2/TestForkerEventListener.h
+++ b/tests/camera2/TestForkerEventListener.h
@@ -30,9 +30,6 @@
 
     TestForkerEventListener();
 
-    // Should we fork before running each test?
-    static void SetForking(bool enabled);
-
 private:
 
     // Called before a test starts.
@@ -50,7 +47,6 @@
 
 public:
     // do not read directly. use TEST_EXTENSION macros instead
-    static bool mUsingForking;
     static bool mIsForked;
 };
 
diff --git a/tests/camera2/TestSettings.cpp b/tests/camera2/TestSettings.cpp
new file mode 100644
index 0000000..f07adc8
--- /dev/null
+++ b/tests/camera2/TestSettings.cpp
@@ -0,0 +1,167 @@
+/*
+ * 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 <cstdlib>
+#include <getopt.h>
+#include <cstring>
+#include <iostream>
+
+#include "TestSettings.h"
+
+#include "TestForkerEventListener.h"
+
+namespace android {
+namespace camera2 {
+namespace tests {
+
+bool TestSettings::mForkingDisabled     = false;
+int  TestSettings::mDeviceId            = 0;
+char* const* TestSettings::mArgv;
+
+// --forking-disabled, false by default
+bool TestSettings::ForkingDisabled() {
+    return mForkingDisabled;
+}
+
+// reverse of --forking-disabled (not a flag), true by default
+bool TestSettings::ForkingEnabled() {
+    return !ForkingDisabled();
+}
+
+// --device-id, 0 by default
+int TestSettings::DeviceId() {
+    return mDeviceId;
+}
+
+// returns false if usage should be printed and we should exit early
+bool TestSettings::ParseArgs(int argc, char* const argv[])
+{
+    {
+        char *env = getenv("CAMERA2_TEST_FORKING_DISABLED");
+        if (env) {
+            mForkingDisabled = atoi(env);
+        }
+
+        env = getenv("CAMERA2_TEST_DEVICE_ID");
+        if (env) {
+            mDeviceId = atoi(env);
+        }
+    }
+
+    bool printHelp = false;
+    bool unknownArgs = false;
+
+    opterr = 0; // do not print errors for unknown arguments
+    while (true) {
+        int c;
+        int option_index = 0;
+
+        static struct option long_options[] = {
+            /* name              has_arg          flag val */
+            {"forking-disabled", optional_argument, 0,  0  },
+            {"device-id",        required_argument, 0,  0  },
+            {"help",             no_argument,       0, 'h' },
+            {0,                  0,                 0,  0  }
+        };
+
+        // Note: '+' in optstring means do not mutate argv
+        c = getopt_long(argc, argv, "+h", long_options, &option_index);
+
+        if (c == -1) { // All arguments exhausted
+            break;
+        }
+        if (c == '?') { // Argument not in option lists
+            const char *arg = argv[optind-1];
+            // Anything beginning with gtest_ will get handled by gtest
+            if (strstr(arg, "--gtest_") != arg) {
+                std::cerr << "Unknown argument: " << arg << std::endl;
+                unknownArgs = true;
+            }
+            continue;
+        }
+
+        switch (c) {
+        case 0: // long option
+            switch (option_index) {
+            case 0: {
+                const char *arg = optarg ?: "1";
+                mForkingDisabled = atoi(arg);
+                break;
+            }
+            case 1: {
+                mDeviceId = atoi(optarg);
+                break;
+            }
+            default:
+                std::cerr << "Unknown long option: " << option_index << std::endl;
+                break;
+            }
+            break; // case 0
+        case 'h': // help
+            printHelp = true;
+            break;
+        default: // case '?'
+            std::cerr << "Unknown option: " << optarg << std::endl;
+        }
+    }
+
+    if (unknownArgs) {
+        std::cerr << std::endl;
+    }
+
+    mArgv = argv;
+
+    if (printHelp || unknownArgs) {
+        return false;
+    }
+
+    std::cerr << "Forking Disabled: "
+              << (mForkingDisabled ? "yes" : "no") << std::endl;
+
+    std::cerr << "Device ID: " << mDeviceId << std::endl;
+
+    return true;
+}
+
+// print usage/help list of commands (non-gtest)
+void TestSettings::PrintUsage() {
+    std::cerr << "Usage: " << mArgv[0] << " [OPTIONS]" << std::endl;
+    std::cerr << std::endl;
+
+    std::cerr << "Main modes of operation:"
+              << std::endl;
+    std::cerr << "   --forking-disabled[=1]  don't fork process before "
+              << std::endl
+              << "                           running a new test."
+              << std::endl
+              << "                           (default enabled)"
+              << std::endl;
+    std::cerr << "   --device-id=ID          specify a different camera ID"
+              << std::endl
+              << "                           (default 0)"
+              << std::endl;
+
+    std::cerr << "   -h, --help              print this help listing"
+              << std::endl;
+
+
+    std::cerr << std::endl;
+}
+
+}
+}
+}
+
diff --git a/tests/camera2/TestSettings.h b/tests/camera2/TestSettings.h
new file mode 100644
index 0000000..6164de5
--- /dev/null
+++ b/tests/camera2/TestSettings.h
@@ -0,0 +1,56 @@
+/*
+:qa
+ * 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_SETTINGS__
+#define __ANDROID_HAL_CAMERA2_TESTS_SETTINGS__
+
+namespace android {
+namespace camera2 {
+namespace tests {
+
+class TestSettings {
+
+public:
+    // --forking-disabled, false by default
+    static bool ForkingDisabled();
+
+    // reverse of --forking-disabled (not a flag), true by default
+    static bool ForkingEnabled();
+
+    // --device-id, 0 by default
+    static int DeviceId();
+
+    // returns false if usage should be printed and we should exit early
+    static bool ParseArgs(int argc, char* const argv[]);
+
+    // print usage/help list of commands (non-gtest)
+    static void PrintUsage();
+
+private:
+    TestSettings();
+    ~TestSettings();
+
+    static bool mForkingDisabled;
+    static int  mDeviceId;
+    static char* const* mArgv;
+};
+
+}
+}
+}
+
+#endif
diff --git a/tests/camera2/camera2.cpp b/tests/camera2/camera2.cpp
index a15c5e6..de5b8c6 100644
--- a/tests/camera2/camera2.cpp
+++ b/tests/camera2/camera2.cpp
@@ -214,7 +214,7 @@
 
     }
 
-    void setUpStream(sp<ISurfaceTexture> consumer,
+    void setUpStream(sp<IGraphicBufferProducer> consumer,
             int width, int height, int format, int *id) {
         status_t res;
 
@@ -283,6 +283,26 @@
         *count = availableSizes.count;
     }
 
+    status_t waitUntilDrained() {
+        static const uint32_t kSleepTime = 50000; // 50 ms
+        static const uint32_t kMaxSleepTime = 10000000; // 10 s
+        ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
+
+        // TODO: Set up notifications from HAL, instead of sleeping here
+        uint32_t totalTime = 0;
+        while (mDevice->ops->get_in_progress_count(mDevice) > 0) {
+            usleep(kSleepTime);
+            totalTime += kSleepTime;
+            if (totalTime > kMaxSleepTime) {
+                ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
+                        mDevice->ops->get_in_progress_count(mDevice), totalTime);
+                return TIMED_OUT;
+            }
+        }
+        ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
+        return OK;
+    }
+
     virtual void SetUp() {
         TEST_EXTENSION_FORKING_SET_UP;
 
@@ -408,6 +428,10 @@
         add_camera_metadata_entry(request,
                 ANDROID_SENSOR_SENSITIVITY,
                 (void**)&sensitivity, 1);
+        uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_TYPE,
+                (void**)&requestType, 1);
 
         uint32_t hourOfDay = 12;
         add_camera_metadata_entry(request,
@@ -460,6 +484,7 @@
         res = rawConsumer->unlockBuffer(buffer);
         ASSERT_EQ(NO_ERROR, res);
 
+        ASSERT_EQ(OK, waitUntilDrained());
         ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
 
         res = closeCameraDevice(mDevice);
@@ -521,6 +546,10 @@
         add_camera_metadata_entry(request,
                 ANDROID_SENSOR_SENSITIVITY,
                 (void**)&sensitivity, 1);
+        uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_TYPE,
+                (void**)&requestType, 1);
 
         uint32_t hourOfDay = 12;
         add_camera_metadata_entry(request,
@@ -690,6 +719,10 @@
         add_camera_metadata_entry(request,
                 ANDROID_SENSOR_SENSITIVITY,
                 (void**)&sensitivity, 1);
+        uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_TYPE,
+                (void**)&requestType, 1);
 
         uint32_t hourOfDay = 12;
         add_camera_metadata_entry(request,
@@ -742,6 +775,7 @@
         res = jpegConsumer->unlockBuffer(buffer);
         ASSERT_EQ(NO_ERROR, res);
 
+        ASSERT_EQ(OK, waitUntilDrained());
         ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
 
         res = closeCameraDevice(mDevice);
diff --git a/tests/camera2/camera2_utils.cpp b/tests/camera2/camera2_utils.cpp
index 166ac52..501cd71 100644
--- a/tests/camera2/camera2_utils.cpp
+++ b/tests/camera2/camera2_utils.cpp
@@ -317,7 +317,7 @@
     (type *)((char*)(ptr) - offsetof(type, member))
 #endif
 
-StreamAdapter::StreamAdapter(sp<ISurfaceTexture> consumer):
+StreamAdapter::StreamAdapter(sp<IGraphicBufferProducer> consumer):
         mState(UNINITIALIZED), mDevice(NULL),
         mId(-1),
         mWidth(0), mHeight(0), mFormat(0)
diff --git a/tests/camera2/camera2_utils.h b/tests/camera2/camera2_utils.h
index 757044b..c846317 100644
--- a/tests/camera2/camera2_utils.h
+++ b/tests/camera2/camera2_utils.h
@@ -161,12 +161,12 @@
 };
 
 /**
- * Adapter from an ISurfaceTexture interface to camera2 device stream ops.
+ * Adapter from an IGraphicBufferProducer interface to camera2 device stream ops.
  * Also takes care of allocating/deallocating stream in device interface
  */
 class StreamAdapter: public camera2_stream_ops {
   public:
-    StreamAdapter(sp<ISurfaceTexture> consumer);
+    StreamAdapter(sp<IGraphicBufferProducer> consumer);
 
     ~StreamAdapter();
 
diff --git a/tests/camera2/main.cpp b/tests/camera2/main.cpp
index 92c117a..e0ebbe9 100644
--- a/tests/camera2/main.cpp
+++ b/tests/camera2/main.cpp
@@ -14,25 +14,22 @@
  * limitations under the License.
  */
 
-#include <stdlib.h>
-
 #include <gtest/gtest.h>
 #include "TestForkerEventListener.h"
+#include "TestSettings.h"
 
 using android::camera2::tests::TestForkerEventListener;
+using android::camera2::tests::TestSettings;
 
 int main(int argc, char **argv) {
 
+    bool printUsage = !TestSettings::ParseArgs(argc, 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);
-        }
+    if (printUsage) {
+        TestSettings::PrintUsage();
+        return 0;
     }
 
     // Gets hold of the event listener list.