blob: adc3c758be6fc4a86b6c32c88431506d4d28d52e [file] [log] [blame]
/*
* Copyright (C) 2013 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 <iostream>
#include <binder/IPCThreadState.h>
#include <utils/Thread.h>
#include "Camera.h"
#include "ProCamera.h"
#include <utils/Vector.h>
#include <utils/Mutex.h>
#include <utils/Condition.h>
namespace android {
namespace camera2 {
namespace tests {
namespace client {
#define CAMERA_ID 0
#define TEST_DEBUGGING 0
#define TEST_LISTENER_TIMEOUT 2000000000 // 2 second listener timeout
#if TEST_DEBUGGING
#define dout std::cerr
#else
#define dout if (0) std::cerr
#endif
#define EXPECT_OK(x) EXPECT_EQ(OK, (x))
#define ASSERT_OK(x) ASSERT_EQ(OK, (x))
class ProCameraTest;
enum LockEvent {
UNKNOWN,
ACQUIRED,
RELEASED,
STOLEN
};
typedef Vector<LockEvent> EventList;
class ProCameraTestThread : public Thread
{
public:
ProCameraTestThread() {
}
virtual bool threadLoop() {
mProc = ProcessState::self();
mProc->startThreadPool();
IPCThreadState *ptr = IPCThreadState::self();
ptr->joinThreadPool();
return false;
}
sp<ProcessState> mProc;
};
class ProCameraTestListener : public ProCameraListener {
public:
status_t WaitForEvent() {
Mutex::Autolock cal(mConditionMutex);
{
Mutex::Autolock al(mListenerMutex);
if (mLockEventList.size() > 0) {
return OK;
}
}
return mListenerCondition.waitRelative(mConditionMutex,
TEST_LISTENER_TIMEOUT);
}
/* Read events into out. Existing queue is flushed */
void ReadEvents(EventList& out) {
Mutex::Autolock al(mListenerMutex);
for (size_t i = 0; i < mLockEventList.size(); ++i) {
out.push(mLockEventList[i]);
}
mLockEventList.clear();
}
/**
* Dequeue 1 event from the event queue.
* Returns UNKNOWN if queue is empty
*/
LockEvent ReadEvent() {
Mutex::Autolock al(mListenerMutex);
if (mLockEventList.size() == 0) {
return UNKNOWN;
}
LockEvent ev = mLockEventList[0];
mLockEventList.removeAt(0);
return ev;
}
private:
void QueueEvent(LockEvent ev) {
{
Mutex::Autolock al(mListenerMutex);
mLockEventList.push(ev);
}
mListenerCondition.broadcast();
}
protected:
//////////////////////////////////////////////////
///////// ProCameraListener //////////////////////
//////////////////////////////////////////////////
// Lock has been acquired. Write operations now available.
virtual void onLockAcquired() {
QueueEvent(ACQUIRED);
}
// Lock has been released with exclusiveUnlock
virtual void onLockReleased() {
QueueEvent(RELEASED);
}
// Lock has been stolen by another client.
virtual void onLockStolen() {
QueueEvent(STOLEN);
}
// Lock free.
virtual void onTriggerNotify(int32_t ext1, int32_t ext2, int32_t ext3) {
dout << "Trigger notify: " << ext1 << " " << ext2
<< " " << ext3 << std::endl;
}
// TODO: remove
virtual void notify(int32_t , int32_t , int32_t ) {}
virtual void postData(int32_t , const sp<IMemory>& ,
camera_frame_metadata_t *) {}
virtual void postDataTimestamp(nsecs_t , int32_t , const sp<IMemory>& ) {}
Vector<LockEvent> mLockEventList;
Mutex mListenerMutex;
Mutex mConditionMutex;
Condition mListenerCondition;
};
class ProCameraTest : public ::testing::Test {
public:
ProCameraTest() {
}
static void SetUpTestCase() {
// Binder Thread Pool Initialization
mTestThread = new ProCameraTestThread();
mTestThread->run("ProCameraTestThread");
}
virtual void SetUp() {
mCamera = ProCamera::connect(CAMERA_ID);
ASSERT_NE((void*)NULL, mCamera.get());
mListener = new ProCameraTestListener();
mCamera->setListener(mListener);
}
virtual void TearDown() {
ASSERT_NE((void*)NULL, mCamera.get());
mCamera->disconnect();
}
protected:
sp<ProCamera> mCamera;
sp<ProCameraTestListener> mListener;
static sp<Thread> mTestThread;
};
sp<Thread> ProCameraTest::mTestThread;
// test around exclusiveTryLock (immediate locking)
TEST_F(ProCameraTest, LockingImmediate) {
if (HasFatalFailure()) {
return;
}
EXPECT_FALSE(mCamera->hasExclusiveLock());
EXPECT_EQ(OK, mCamera->exclusiveTryLock());
// at this point we definitely have the lock
EXPECT_EQ(OK, mListener->WaitForEvent());
EXPECT_EQ(ACQUIRED, mListener->ReadEvent());
EXPECT_TRUE(mCamera->hasExclusiveLock());
EXPECT_EQ(OK, mCamera->exclusiveUnlock());
EXPECT_EQ(OK, mListener->WaitForEvent());
EXPECT_EQ(RELEASED, mListener->ReadEvent());
EXPECT_FALSE(mCamera->hasExclusiveLock());
}
// test around exclusiveLock (locking at some future point in time)
TEST_F(ProCameraTest, LockingAsynchronous) {
if (HasFatalFailure()) {
return;
}
// TODO: Add another procamera that has a lock here.
// then we can be test that the lock wont immediately be acquired
EXPECT_FALSE(mCamera->hasExclusiveLock());
EXPECT_EQ(OK, mCamera->exclusiveLock());
// at this point we may or may not have the lock
// we cant be sure until we get an ACQUIRED event
EXPECT_EQ(OK, mListener->WaitForEvent());
EXPECT_EQ(ACQUIRED, mListener->ReadEvent());
EXPECT_TRUE(mCamera->hasExclusiveLock());
EXPECT_EQ(OK, mCamera->exclusiveUnlock());
EXPECT_EQ(OK, mListener->WaitForEvent());
EXPECT_EQ(RELEASED, mListener->ReadEvent());
EXPECT_FALSE(mCamera->hasExclusiveLock());
}
}
}
}
}