2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "BufferQueue_test"
18 //#define LOG_NDEBUG 0
20 #include <gtest/gtest.h>
22 #include <utils/String8.h>
23 #include <utils/threads.h>
25 #include <ui/GraphicBuffer.h>
26 #include <ui/FramebufferNativeWindow.h>
28 #include <gui/BufferQueue.h>
32 class BufferQueueTest : public ::testing::Test {
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(),
43 mBQ = new BufferQueue();
46 virtual void TearDown() {
49 const ::testing::TestInfo* const testInfo =
50 ::testing::UnitTest::GetInstance()->current_test_info();
51 ALOGV("End test: %s.%s", testInfo->test_case_name(),
58 struct DummyConsumer : public BufferQueue::ConsumerListener {
59 virtual void onFrameAvailable() {}
60 virtual void onBuffersReleased() {}
63 TEST_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);
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;
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));
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));
92 // Acquire the third buffer, which should fail.
93 ASSERT_EQ(INVALID_OPERATION, mBQ->acquireBuffer(&item));
96 TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithIllegalValues_ReturnsError) {
97 sp<DummyConsumer> dc(new DummyConsumer);
98 mBQ->consumerConnect(dc);
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));
107 TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithLegalValues_Succeeds) {
108 sp<DummyConsumer> dc(new DummyConsumer);
109 mBQ->consumerConnect(dc);
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));
117 } // namespace android