18eb69d60c09c1c4683066a94c889df28d0e9d233James Dong/*
28eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * Copyright (C) 2010 The Android Open Source Project
38eb69d60c09c1c4683066a94c889df28d0e9d233James Dong *
48eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * Licensed under the Apache License, Version 2.0 (the "License");
58eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * you may not use this file except in compliance with the License.
68eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * You may obtain a copy of the License at
78eb69d60c09c1c4683066a94c889df28d0e9d233James Dong *
88eb69d60c09c1c4683066a94c889df28d0e9d233James Dong *      http://www.apache.org/licenses/LICENSE-2.0
98eb69d60c09c1c4683066a94c889df28d0e9d233James Dong *
108eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * Unless required by applicable law or agreed to in writing, software
118eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * distributed under the License is distributed on an "AS IS" BASIS,
128eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * See the License for the specific language governing permissions and
148eb69d60c09c1c4683066a94c889df28d0e9d233James Dong * limitations under the License.
158eb69d60c09c1c4683066a94c889df28d0e9d233James Dong */
168eb69d60c09c1c4683066a94c889df28d0e9d233James Dong
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define LOG_TAG "CameraServiceTest"
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdio.h>
2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdlib.h>
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <string.h>
2265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/types.h>
2365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/wait.h>
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <unistd.h>
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <camera/Camera.h>
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <camera/CameraParameters.h>
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <ui/GraphicBuffer.h>
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <camera/ICamera.h>
2965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <camera/ICameraClient.h>
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <camera/ICameraService.h>
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IPCThreadState.h>
3265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IServiceManager.h>
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/ProcessState.h>
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/KeyedVector.h>
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Log.h>
3665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Vector.h>
3765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h>
3865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianusing namespace android;
4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
4265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//  Assertion and Logging utilities
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define INFO(...) \
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    do { \
4665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        printf(__VA_ARGS__); \
4765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        printf("\n"); \
48b8a805261bf0282e992d3608035e47d05a898710Steve Block        ALOGD(__VA_ARGS__); \
4965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } while(0)
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid assert_fail(const char *file, int line, const char *func, const char *expr) {
5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("assertion failed at file %s, line %d, function %s:",
5365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            file, line, func);
5465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", expr);
5565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    abort();
5665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
5765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid assert_eq_fail(const char *file, int line, const char *func,
5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const char *expr, int actual) {
6065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("assertion failed at file %s, line %d, function %s:",
6165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            file, line, func);
6265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("(expected) %s != (actual) %d", expr, actual);
6365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    abort();
6465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
6565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define ASSERT(e) \
6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    do { \
6865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!(e)) \
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            assert_fail(__FILE__, __LINE__, __func__, #e); \
7065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } while(0)
7165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define ASSERT_EQ(expected, actual) \
7365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    do { \
7465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int _x = (actual); \
7565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (_x != (expected)) \
7665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            assert_eq_fail(__FILE__, __LINE__, __func__, #expected, _x); \
7765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } while(0)
7865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
8065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//  Holder service for pass objects between processes.
8165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
8265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass IHolder : public IInterface {
8365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprotected:
8465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    enum {
8565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        HOLDER_PUT = IBinder::FIRST_CALL_TRANSACTION,
8665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        HOLDER_GET,
8765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        HOLDER_CLEAR
8865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
8965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
9065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    DECLARE_META_INTERFACE(Holder);
9165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void put(sp<IBinder> obj) = 0;
9365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual sp<IBinder> get() = 0;
9465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void clear() = 0;
9565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
9665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass BnHolder : public BnInterface<IHolder> {
9865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual status_t onTransact(uint32_t code,
9965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                const Parcel& data,
10065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                Parcel* reply,
10165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                uint32_t flags = 0);
10265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
10365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
10465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass BpHolder : public BpInterface<IHolder> {
10565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
10665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    BpHolder(const sp<IBinder>& impl)
10765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        : BpInterface<IHolder>(impl) {
10865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
10965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
11065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void put(sp<IBinder> obj) {
11165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Parcel data, reply;
11265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        data.writeStrongBinder(obj);
11365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        remote()->transact(HOLDER_PUT, data, &reply, IBinder::FLAG_ONEWAY);
11465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
11565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
11665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual sp<IBinder> get() {
11765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Parcel data, reply;
11865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        remote()->transact(HOLDER_GET, data, &reply);
11965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return reply.readStrongBinder();
12065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
12165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
12265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void clear() {
12365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Parcel data, reply;
12465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        remote()->transact(HOLDER_CLEAR, data, &reply);
12565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
12665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
12765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
12865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianIMPLEMENT_META_INTERFACE(Holder, "CameraServiceTest.Holder");
12965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
13065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t BnHolder::onTransact(
13165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
13265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    switch(code) {
13365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case HOLDER_PUT: {
13465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            put(data.readStrongBinder());
13565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return NO_ERROR;
13665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
13765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case HOLDER_GET: {
13865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            reply->writeStrongBinder(get());
13965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return NO_ERROR;
14065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
14165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case HOLDER_CLEAR: {
14265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            clear();
14365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return NO_ERROR;
14465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
14565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        default:
14665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return BBinder::onTransact(code, data, reply, flags);
14765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
14865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
14965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass HolderService : public BnHolder {
15165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void put(sp<IBinder> obj) {
15265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mObj = obj;
15365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
15465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual sp<IBinder> get() {
15565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return mObj;
15665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
15765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void clear() {
15865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mObj.clear();
15965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
16065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprivate:
16165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<IBinder> mObj;
16265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
16365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
16565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//  A mock CameraClient
16665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
16765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass MCameraClient : public BnCameraClient {
16865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
16965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2);
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void dataCallback(int32_t msgType, const sp<IMemory>& data);
17165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void dataCallbackTimestamp(nsecs_t timestamp,
17265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            int32_t msgType, const sp<IMemory>& data);
17365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // new functions
17565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void clearStat();
17665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    enum OP { EQ, GE, LE, GT, LT };
17765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void assertNotify(int32_t msgType, OP op, int count);
17865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void assertData(int32_t msgType, OP op, int count);
17965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void waitNotify(int32_t msgType, OP op, int count);
18065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void waitData(int32_t msgType, OP op, int count);
18165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void assertDataSize(int32_t msgType, OP op, int dataSize);
18265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
18365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void setReleaser(ICamera *releaser) {
18465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mReleaser = releaser;
18565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
18665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprivate:
18765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex mLock;
18865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Condition mCond;
18965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    DefaultKeyedVector<int32_t, int> mNotifyCount;
19065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    DefaultKeyedVector<int32_t, int> mDataCount;
19165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    DefaultKeyedVector<int32_t, int> mDataSize;
19265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool test(OP op, int v1, int v2);
19365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void assertTest(OP op, int v1, int v2);
19465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ICamera *mReleaser;
19665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
19765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::clearStat() {
19965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
20065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mNotifyCount.clear();
20165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mDataCount.clear();
20265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mDataSize.clear();
20365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
20465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
20565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool MCameraClient::test(OP op, int v1, int v2) {
20665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    switch (op) {
20765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case EQ: return v1 == v2;
20865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case GT: return v1 > v2;
20965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case LT: return v1 < v2;
21065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case GE: return v1 >= v2;
21165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case LE: return v1 <= v2;
21265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        default: ASSERT(0); break;
21365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
21465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return false;
21565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
21665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
21765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::assertTest(OP op, int v1, int v2) {
21865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!test(op, v1, v2)) {
21929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("assertTest failed: op=%d, v1=%d, v2=%d", op, v1, v2);
22065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(0);
22165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
22265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
22365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
22465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::assertNotify(int32_t msgType, OP op, int count) {
22565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
22665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int v = mNotifyCount.valueFor(msgType);
22765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    assertTest(op, v, count);
22865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
22965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
23065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::assertData(int32_t msgType, OP op, int count) {
23165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
23265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int v = mDataCount.valueFor(msgType);
23365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    assertTest(op, v, count);
23465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
23565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
23665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::assertDataSize(int32_t msgType, OP op, int dataSize) {
23765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
23865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int v = mDataSize.valueFor(msgType);
23965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    assertTest(op, v, dataSize);
24065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
24165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
24265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) {
24365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
24465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
24565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ssize_t i = mNotifyCount.indexOfKey(msgType);
24665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (i < 0) {
24765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mNotifyCount.add(msgType, 1);
24865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
24965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ++mNotifyCount.editValueAt(i);
25065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
25165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mCond.signal();
25265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
25365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
25465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::dataCallback(int32_t msgType, const sp<IMemory>& data) {
25565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
25665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int dataSize = data->size();
25765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("data type = %d, size = %d", msgType, dataSize);
25865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
25965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ssize_t i = mDataCount.indexOfKey(msgType);
26065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (i < 0) {
26165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mDataCount.add(msgType, 1);
26265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mDataSize.add(msgType, dataSize);
26365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
26465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ++mDataCount.editValueAt(i);
26565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mDataSize.editValueAt(i) = dataSize;
26665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
26765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mCond.signal();
26865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
26965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (msgType == CAMERA_MSG_VIDEO_FRAME) {
27065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(mReleaser != NULL);
27165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mReleaser->releaseRecordingFrame(data);
27265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
27365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
27465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
27565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
27665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const sp<IMemory>& data) {
27765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    dataCallback(msgType, data);
27865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
27965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
28065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::waitNotify(int32_t msgType, OP op, int count) {
28165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("waitNotify: %d, %d, %d", msgType, op, count);
28265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
28365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (true) {
28465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int v = mNotifyCount.valueFor(msgType);
28565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (test(op, v, count)) {
28665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
28765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
28865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mCond.wait(mLock);
28965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
29065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
29165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
29265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MCameraClient::waitData(int32_t msgType, OP op, int count) {
29365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("waitData: %d, %d, %d", msgType, op, count);
29465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
29565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (true) {
29665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int v = mDataCount.valueFor(msgType);
29765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (test(op, v, count)) {
29865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
29965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
30065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mCond.wait(mLock);
30165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
30265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
30365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
30465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
30565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//  A mock Surface
30665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
30765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass MSurface : public BnSurface {
30865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
30965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual status_t registerBuffers(const BufferHeap& buffers);
31065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void postBuffer(ssize_t offset);
31165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void unregisterBuffers();
31265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual sp<GraphicBuffer> requestBuffer(int bufferIdx, int usage);
31365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual status_t setBufferCount(int bufferCount);
31465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
31565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // new functions
31665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void clearStat();
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void waitUntil(int c0, int c1, int c2);
31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprivate:
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check callback count
32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Condition mCond;
32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex mLock;
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int registerBuffersCount;
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int postBufferCount;
32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int unregisterBuffersCount;
32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
32765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
32865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t MSurface::registerBuffers(const BufferHeap& buffers) {
32965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
33065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
33165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ++registerBuffersCount;
33265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mCond.signal();
33365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
33465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
33565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
33665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MSurface::postBuffer(ssize_t offset) {
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // INFO("%s", __func__);
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ++postBufferCount;
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mCond.signal();
34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MSurface::unregisterBuffers() {
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ++unregisterBuffersCount;
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mCond.signal();
34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<GraphicBuffer> MSurface::requestBuffer(int bufferIdx, int usage) {
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NULL;
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t MSurface::setBufferCount(int bufferCount) {
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NULL;
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MSurface::clearStat() {
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    registerBuffersCount = 0;
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    postBufferCount = 0;
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    unregisterBuffersCount = 0;
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid MSurface::waitUntil(int c0, int c1, int c2) {
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("waitUntil: %d %d %d", c0, c1, c2);
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (true) {
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (registerBuffersCount >= c0 &&
37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            postBufferCount >= c1 &&
37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            unregisterBuffersCount >= c2) {
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
37565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mCond.wait(mLock);
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//  Utilities to use the Holder service
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IHolder> getHolder() {
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<IServiceManager> sm = defaultServiceManager();
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(sm != 0);
38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<IBinder> binder = sm->getService(String16("CameraServiceTest.Holder"));
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(binder != 0);
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<IHolder> holder = interface_cast<IHolder>(binder);
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(holder != 0);
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return holder;
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid putTempObject(sp<IBinder> obj) {
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    getHolder()->put(obj);
39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IBinder> getTempObject() {
39965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return getHolder()->get();
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
40265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid clearTempObject() {
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
40565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    getHolder()->clear();
40665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
40765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
40965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//  Get a Camera Service
41065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
41165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<ICameraService> getCameraService() {
41265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<IServiceManager> sm = defaultServiceManager();
41365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(sm != 0);
41465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<IBinder> binder = sm->getService(String16("media.camera"));
41565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(binder != 0);
41665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICameraService> cs = interface_cast<ICameraService>(binder);
41765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(cs != 0);
41865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return cs;
41965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
42065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianint getNumberOfCameras() {
42265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICameraService> cs = getCameraService();
42365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return cs->getNumberOfCameras();
42465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
42565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
42765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// Various Connect Tests
42865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
42965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testConnect(int cameraId) {
43065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
43165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICameraService> cs = getCameraService();
43265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc = new MCameraClient();
43365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = cs->connect(cc, cameraId);
43465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c != 0);
43565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    c->disconnect();
43665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
43865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testAllowConnectOnceOnly(int cameraId) {
43965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
44065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICameraService> cs = getCameraService();
44165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Connect the first client.
44265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc = new MCameraClient();
44365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = cs->connect(cc, cameraId);
44465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c != 0);
44565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Same client -- ok.
44665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(cs->connect(cc, cameraId) != 0);
44765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Different client -- not ok.
44865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc2 = new MCameraClient();
44965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(cs->connect(cc2, cameraId) == 0);
45065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    c->disconnect();
45165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
45265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
45365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testReconnectFailed() {
45465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
45565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = interface_cast<ICamera>(getTempObject());
45665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc = new MCameraClient();
45765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->connect(cc) != NO_ERROR);
45865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
45965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
46065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testReconnectSuccess() {
46165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
46265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = interface_cast<ICamera>(getTempObject());
46365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc = new MCameraClient();
46465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->connect(cc) == NO_ERROR);
46565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    c->disconnect();
46665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
46765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
46865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testLockFailed() {
46965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
47065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = interface_cast<ICamera>(getTempObject());
47165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->lock() != NO_ERROR);
47265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
47365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
47465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testLockUnlockSuccess() {
47565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
47665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = interface_cast<ICamera>(getTempObject());
47765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->lock() == NO_ERROR);
47865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->unlock() == NO_ERROR);
47965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
48065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
48165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testLockSuccess() {
48265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
48365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = interface_cast<ICamera>(getTempObject());
48465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->lock() == NO_ERROR);
48565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    c->disconnect();
48665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
48765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
48865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
48965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// Run the connect tests in another process.
49065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//
49165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianconst char *gExecutable;
49265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
49365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstruct FunctionTableEntry {
49465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const char *name;
49565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void (*func)();
49665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
49765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
49865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianFunctionTableEntry function_table[] = {
49965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define ENTRY(x) {#x, &x}
50065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ENTRY(testReconnectFailed),
50165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ENTRY(testReconnectSuccess),
50265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ENTRY(testLockUnlockSuccess),
50365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ENTRY(testLockFailed),
50465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ENTRY(testLockSuccess),
50565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#undef ENTRY
50665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
50765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
50865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid runFunction(const char *tag) {
50965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("runFunction: %s", tag);
51065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int entries = sizeof(function_table) / sizeof(function_table[0]);
51165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < entries; i++) {
51265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (strcmp(function_table[i].name, tag) == 0) {
51365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (*function_table[i].func)();
51465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return;
51565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
51665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
51765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(0);
51865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
51965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
52065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid runInAnotherProcess(const char *tag) {
52165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    pid_t pid = fork();
52265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (pid == 0) {
52365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        execlp(gExecutable, gExecutable, tag, NULL);
52465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(0);
52565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
52665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int status;
52765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT_EQ(pid, wait(&status));
52865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT_EQ(0, status);
52965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
53065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
53165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
53265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testReconnect(int cameraId) {
53365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
53465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICameraService> cs = getCameraService();
53565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc = new MCameraClient();
53665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = cs->connect(cc, cameraId);
53765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c != 0);
53865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Reconnect to the same client -- ok.
53965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->connect(cc) == NO_ERROR);
54065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Reconnect to a different client (but the same pid) -- ok.
54165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc2 = new MCameraClient();
54265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->connect(cc2) == NO_ERROR);
54365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    c->disconnect();
54465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
54565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
54665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
54765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testLockUnlock(int cameraId) {
54865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICameraService> cs = getCameraService();
54965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc = new MCameraClient();
55065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = cs->connect(cc, cameraId);
55165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c != 0);
55265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // We can lock as many times as we want.
55365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->lock() == NO_ERROR);
55465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->lock() == NO_ERROR);
55565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Lock from a different process -- not ok.
55665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    putTempObject(c->asBinder());
55765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    runInAnotherProcess("testLockFailed");
55865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Unlock then lock from a different process -- ok.
55965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->unlock() == NO_ERROR);
56065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    runInAnotherProcess("testLockUnlockSuccess");
56165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Unlock then lock from a different process -- ok.
56265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    runInAnotherProcess("testLockSuccess");
56365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    clearTempObject();
56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
56565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
56665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid testReconnectFromAnotherProcess(int cameraId) {
56765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    INFO("%s", __func__);
56865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
56965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICameraService> cs = getCameraService();
57065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc = new MCameraClient();
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c = cs->connect(cc, cameraId);
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c != 0);
57365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Reconnect from a different process -- not ok.
57465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    putTempObject(c->asBinder());
57565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    runInAnotherProcess("testReconnectFailed");
57665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Unlock then reconnect from a different process -- ok.
57765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(c->unlock() == NO_ERROR);
57865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    runInAnotherProcess("testReconnectSuccess");
57965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    clearTempObject();
58065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
58165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
58265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// We need to flush the command buffer after the reference
58365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// to ICamera is gone. The sleep is for the server to run
58465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// the destructor for it.
58565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatic void flushCommands() {
58665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    IPCThreadState::self()->flushCommands();
58765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    usleep(200000);  // 200ms
58865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
58965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
59065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// Run a test case
59165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define RUN(class_name, cameraId) do { \
59265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    { \
59365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        INFO(#class_name); \
59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        class_name instance; \
59565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        instance.init(cameraId); \
59665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        instance.run(); \
59765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } \
59865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    flushCommands(); \
59965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} while(0)
60065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
60165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// Base test case after the the camera is connected.
60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AfterConnect {
60365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
60465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void init(int cameraId) {
60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cs = getCameraService();
60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc = new MCameraClient();
60765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c = cs->connect(cc, cameraId);
60865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c != 0);
60965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprotected:
61265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICameraService> cs;
61365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MCameraClient> cc;
61465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ICamera> c;
61565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ~AfterConnect() {
61765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->disconnect();
61865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c.clear();
61965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc.clear();
62065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cs.clear();
62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
62265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
62365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestSetPreviewDisplay : public AfterConnect {
62565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
62665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
62765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<MSurface> surface = new MSurface();
62865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->setPreviewDisplay(surface) == NO_ERROR);
62965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->disconnect();
63065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
63165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
63265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
63365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestStartPreview : public AfterConnect {
63565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
63665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
63765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<MSurface> surface = new MSurface();
63865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->setPreviewDisplay(surface) == NO_ERROR);
63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->startPreview() == NO_ERROR);
64165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->previewEnabled() == true);
64265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        surface->waitUntil(1, 10, 0); // needs 1 registerBuffers and 10 postBuffer
64465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        surface->clearStat();
64565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<MSurface> another_surface = new MSurface();
64765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->setPreviewDisplay(another_surface);  // just to make sure unregisterBuffers
64865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                                // is called.
64965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        surface->waitUntil(0, 0, 1);  // needs unregisterBuffers
65065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
65165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
65265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
65365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
65465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
65565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestStartPreviewWithoutDisplay : public AfterConnect {
65665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
65765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
65865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->startPreview() == NO_ERROR);
65965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->previewEnabled() == true);
66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->disconnect();
66165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
66265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
66565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// Base test case after the the camera is connected and the preview is started.
66665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AfterStartPreview : public AfterConnect {
66765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
66865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void init(int cameraId) {
66965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AfterConnect::init(cameraId);
67065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        surface = new MSurface();
67165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->setPreviewDisplay(surface) == NO_ERROR);
67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->startPreview() == NO_ERROR);
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
67565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprotected:
67665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<MSurface> surface;
67765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ~AfterStartPreview() {
67965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        surface.clear();
68065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
68365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestAutoFocus : public AfterStartPreview {
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_FOCUS, MCameraClient::EQ, 0);
68765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->autoFocus();
68865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->waitNotify(CAMERA_MSG_FOCUS, MCameraClient::EQ, 1);
68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->disconnect();
69065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
69365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
69465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestStopPreview : public AfterStartPreview {
69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
69665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->previewEnabled() == true);
69865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->stopPreview();
69965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->previewEnabled() == false);
70065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->disconnect();
70165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
70365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
70465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
70565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestTakePicture: public AfterStartPreview {
70665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
70765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->takePicture() == NO_ERROR);
70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->waitNotify(CAMERA_MSG_SHUTTER, MCameraClient::EQ, 1);
71065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->waitData(CAMERA_MSG_RAW_IMAGE, MCameraClient::EQ, 1);
71165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->waitData(CAMERA_MSG_COMPRESSED_IMAGE, MCameraClient::EQ, 1);
71265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->stopPreview();
71365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->disconnect();
71465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
71565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
71665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
71765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
71865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestTakeMultiplePictures: public AfterStartPreview {
71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
72165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (int i = 0; i < 10; i++) {
72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            cc->clearStat();
72365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            ASSERT(c->takePicture() == NO_ERROR);
72465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            cc->waitNotify(CAMERA_MSG_SHUTTER, MCameraClient::EQ, 1);
72565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            cc->waitData(CAMERA_MSG_RAW_IMAGE, MCameraClient::EQ, 1);
72665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            cc->waitData(CAMERA_MSG_COMPRESSED_IMAGE, MCameraClient::EQ, 1);
72765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
72865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->disconnect();
72965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
73165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
73365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestGetParameters: public AfterStartPreview {
73465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
73565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
73665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        String8 param_str = c->getParameters();
73765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        INFO("%s", static_cast<const char*>(param_str));
73865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
73965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
74065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatic bool getNextSize(const char **ptrS, int *w, int *h) {
74265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const char *s = *ptrS;
74365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // skip over ','
74565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (*s == ',') s++;
74665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // remember start position in p
74865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const char *p = s;
74965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (*s != '\0' && *s != 'x') {
75065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        s++;
75165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
75265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (*s == '\0') return false;
75365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // get the width
75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    *w = atoi(p);
75665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // skip over 'x'
75865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ASSERT(*s == 'x');
75965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    p = s + 1;
76065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (*s != '\0' && *s != ',') {
76165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        s++;
76265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
76365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
76465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // get the height
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    *h = atoi(p);
76665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    *ptrS = s;
76765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return true;
76865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
76965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestPictureSize : public AfterStartPreview {
77165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
77265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void checkOnePicture(int w, int h) {
77365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const float rate = 0.9;  // byte per pixel limit
77465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int pixels = w * h;
77565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        CameraParameters param(c->getParameters());
77765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        param.setPictureSize(w, h);
77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // disable thumbnail to get more accurate size.
77965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        param.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, 0);
78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        param.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, 0);
78165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->setParameters(param.flatten());
78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->clearStat();
78465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->takePicture() == NO_ERROR);
78565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->waitData(CAMERA_MSG_RAW_IMAGE, MCameraClient::EQ, 1);
78665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        //cc->assertDataSize(CAMERA_MSG_RAW_IMAGE, MCameraClient::EQ, pixels*3/2);
78765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->waitData(CAMERA_MSG_COMPRESSED_IMAGE, MCameraClient::EQ, 1);
78865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertDataSize(CAMERA_MSG_COMPRESSED_IMAGE, MCameraClient::LT,
78965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                int(pixels * rate));
79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertDataSize(CAMERA_MSG_COMPRESSED_IMAGE, MCameraClient::GT, 0);
79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertNotify(CAMERA_MSG_ERROR, MCameraClient::EQ, 0);
79265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
79365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
79565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        CameraParameters param(c->getParameters());
79665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int w, h;
79765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const char *s = param.get(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES);
79865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        while (getNextSize(&s, &w, &h)) {
799b8a805261bf0282e992d3608035e47d05a898710Steve Block            ALOGD("checking picture size %dx%d", w, h);
80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            checkOnePicture(w, h);
80165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
80265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
80365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestPreviewCallbackFlag : public AfterConnect {
80665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
80765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
80865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<MSurface> surface = new MSurface();
80965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->setPreviewDisplay(surface) == NO_ERROR);
81065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
81165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // Try all flag combinations.
81265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (int v = 0; v < 8; v++) {
813b8a805261bf0282e992d3608035e47d05a898710Steve Block            ALOGD("TestPreviewCallbackFlag: flag=%d", v);
81465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            usleep(100000); // sleep a while to clear the in-flight callbacks.
81565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            cc->clearStat();
81665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            c->setPreviewCallbackFlag(v);
81765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            ASSERT(c->previewEnabled() == false);
81865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            ASSERT(c->startPreview() == NO_ERROR);
81965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            ASSERT(c->previewEnabled() == true);
82065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            sleep(2);
82165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            c->stopPreview();
8229e626526453f91999bdf3de4c2ec8e55c5d90511Iliyan Malchev            if ((v & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) == 0) {
82365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                cc->assertData(CAMERA_MSG_PREVIEW_FRAME, MCameraClient::EQ, 0);
82465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
8259e626526453f91999bdf3de4c2ec8e55c5d90511Iliyan Malchev                if ((v & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) == 0) {
82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    cc->assertData(CAMERA_MSG_PREVIEW_FRAME, MCameraClient::GE, 10);
82765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                } else {
82865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    cc->assertData(CAMERA_MSG_PREVIEW_FRAME, MCameraClient::EQ, 1);
82965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
83065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
83165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
83265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
83365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
83465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
83565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestRecording : public AfterConnect {
83665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
83765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
83865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->recordingEnabled() == false);
83965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<MSurface> surface = new MSurface();
84065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->setPreviewDisplay(surface) == NO_ERROR);
8419e626526453f91999bdf3de4c2ec8e55c5d90511Iliyan Malchev        c->setPreviewCallbackFlag(CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->setReleaser(c.get());
84365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->startRecording();
84465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        ASSERT(c->recordingEnabled() == true);
84565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sleep(2);
84665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->stopRecording();
84765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        usleep(100000); // sleep a while to clear the in-flight callbacks.
84865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->setReleaser(NULL);
84965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertData(CAMERA_MSG_VIDEO_FRAME, MCameraClient::GE, 10);
85065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
85165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
85265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass TestPreviewSize : public AfterStartPreview {
85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void checkOnePicture(int w, int h) {
85665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int size = w*h*3/2;  // should read from parameters
85765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->stopPreview();
85965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        CameraParameters param(c->getParameters());
86165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        param.setPreviewSize(w, h);
8629e626526453f91999bdf3de4c2ec8e55c5d90511Iliyan Malchev        c->setPreviewCallbackFlag(CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
86365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->setParameters(param.flatten());
86465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        c->startPreview();
86665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->clearStat();
86865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->waitData(CAMERA_MSG_PREVIEW_FRAME, MCameraClient::GE, 1);
86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        cc->assertDataSize(CAMERA_MSG_PREVIEW_FRAME, MCameraClient::EQ, size);
87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void run() {
87365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        CameraParameters param(c->getParameters());
87465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int w, h;
87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const char *s = param.get(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES);
87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        while (getNextSize(&s, &w, &h)) {
877b8a805261bf0282e992d3608035e47d05a898710Steve Block            ALOGD("checking preview size %dx%d", w, h);
87865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            checkOnePicture(w, h);
87965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
88065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
88165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid runHolderService() {
88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    defaultServiceManager()->addService(
885