BufferQueue_test.cpp revision 4c00cc11141da7d159eb2323b186ed344115c0f1
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "BufferQueue_test"
18//#define LOG_NDEBUG 0
19
20#include <gtest/gtest.h>
21
22#include <utils/String8.h>
23#include <utils/threads.h>
24
25#include <ui/GraphicBuffer.h>
26#include <ui/FramebufferNativeWindow.h>
27
28#include <gui/BufferQueue.h>
29
30namespace android {
31
32class BufferQueueTest : public ::testing::Test {
33protected:
34
35    BufferQueueTest() {}
36
37    virtual void SetUp() {
38        const ::testing::TestInfo* const testInfo =
39            ::testing::UnitTest::GetInstance()->current_test_info();
40        ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
41                testInfo->name());
42
43        mBQ = new BufferQueue();
44    }
45
46    virtual void TearDown() {
47        mBQ.clear();
48
49        const ::testing::TestInfo* const testInfo =
50            ::testing::UnitTest::GetInstance()->current_test_info();
51        ALOGV("End test:   %s.%s", testInfo->test_case_name(),
52                testInfo->name());
53    }
54
55    sp<BufferQueue> mBQ;
56};
57
58struct DummyConsumer : public BufferQueue::ConsumerListener {
59    virtual void onFrameAvailable() {}
60    virtual void onBuffersReleased() {}
61};
62
63TEST_F(BufferQueueTest, AcquireBuffer_ExceedsMaxAcquireCount_Fails) {
64    sp<DummyConsumer> dc(new DummyConsumer);
65    mBQ->consumerConnect(dc);
66    IGraphicBufferProducer::QueueBufferOutput qbo;
67    mBQ->connect(NATIVE_WINDOW_API_CPU, &qbo);
68    mBQ->setBufferCount(4);
69
70    int slot;
71    sp<Fence> fence;
72    sp<GraphicBuffer> buf;
73    IGraphicBufferProducer::QueueBufferInput qbi(0, Rect(0, 0, 1, 1),
74            NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE);
75    BufferQueue::BufferItem item;
76
77    for (int i = 0; i < 2; i++) {
78        ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
79                mBQ->dequeueBuffer(&slot, &fence, 1, 1, 0,
80                    GRALLOC_USAGE_SW_READ_OFTEN));
81        ASSERT_EQ(OK, mBQ->requestBuffer(slot, &buf));
82        ASSERT_EQ(OK, mBQ->queueBuffer(slot, qbi, &qbo));
83        ASSERT_EQ(OK, mBQ->acquireBuffer(&item));
84    }
85
86    ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
87            mBQ->dequeueBuffer(&slot, &fence, 1, 1, 0,
88                GRALLOC_USAGE_SW_READ_OFTEN));
89    ASSERT_EQ(OK, mBQ->requestBuffer(slot, &buf));
90    ASSERT_EQ(OK, mBQ->queueBuffer(slot, qbi, &qbo));
91
92    // Acquire the third buffer, which should fail.
93    ASSERT_EQ(INVALID_OPERATION, mBQ->acquireBuffer(&item));
94}
95
96TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithIllegalValues_ReturnsError) {
97    sp<DummyConsumer> dc(new DummyConsumer);
98    mBQ->consumerConnect(dc);
99
100    ASSERT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(0));
101    ASSERT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(-3));
102    ASSERT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(
103            BufferQueue::MAX_MAX_ACQUIRED_BUFFERS+1));
104    ASSERT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(100));
105}
106
107TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithLegalValues_Succeeds) {
108    sp<DummyConsumer> dc(new DummyConsumer);
109    mBQ->consumerConnect(dc);
110
111    ASSERT_EQ(OK, mBQ->setMaxAcquiredBufferCount(1));
112    ASSERT_EQ(OK, mBQ->setMaxAcquiredBufferCount(2));
113    ASSERT_EQ(OK, mBQ->setMaxAcquiredBufferCount(
114            BufferQueue::MAX_MAX_ACQUIRED_BUFFERS));
115}
116
117} // namespace android
118