blob: 5247e04c32338b5a3f2b5bc817712f56ca27d66e [file] [log] [blame]
Alec Mouri9fa2cb62019-07-15 17:36:26 -07001/*
2 * Copyright 2019 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 "ANativeWindow_test"
18//#define LOG_NDEBUG 0
19
20#include <gtest/gtest.h>
21#include <gui/BufferItemConsumer.h>
22#include <gui/BufferQueue.h>
23#include <gui/Surface.h>
24#include <log/log.h>
25#include <sync/sync.h>
26// We need to use the private system apis since not everything is visible to
27// apexes yet.
28#include <system/window.h>
29
30using namespace android;
31
Alec Mourif629f102019-08-06 18:16:23 -070032class TestableSurface final : public Surface {
33public:
34 explicit TestableSurface(const sp<IGraphicBufferProducer>& bufferProducer)
35 : Surface(bufferProducer) {}
36
37 // Exposes the internal last dequeue duration that's stored on the Surface.
38 nsecs_t getLastDequeueDuration() const { return mLastDequeueDuration; }
39};
40
Alec Mouri9fa2cb62019-07-15 17:36:26 -070041class ANativeWindowTest : public ::testing::Test {
42protected:
43 void SetUp() override {
44 const ::testing::TestInfo* const test_info =
45 ::testing::UnitTest::GetInstance()->current_test_info();
46 ALOGV("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
47 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
48 mItemConsumer = new BufferItemConsumer(mConsumer, GRALLOC_USAGE_SW_READ_OFTEN);
Alec Mourif629f102019-08-06 18:16:23 -070049 mWindow = new TestableSurface(mProducer);
Alec Mouri9fa2cb62019-07-15 17:36:26 -070050 const int success = native_window_api_connect(mWindow.get(), NATIVE_WINDOW_API_CPU);
51 EXPECT_EQ(0, success);
52 }
53
54 void TearDown() override {
55 const ::testing::TestInfo* const test_info =
56 ::testing::UnitTest::GetInstance()->current_test_info();
57 ALOGV("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
58 const int success = native_window_api_disconnect(mWindow.get(), NATIVE_WINDOW_API_CPU);
59 EXPECT_EQ(0, success);
60 }
61 sp<IGraphicBufferProducer> mProducer;
62 sp<IGraphicBufferConsumer> mConsumer;
63 sp<BufferItemConsumer> mItemConsumer;
Alec Mourif629f102019-08-06 18:16:23 -070064
65 sp<TestableSurface> mWindow;
Alec Mouri9fa2cb62019-07-15 17:36:26 -070066};
67
68TEST_F(ANativeWindowTest, getLastDequeueDuration_noDequeue_returnsZero) {
69 int result = ANativeWindow_getLastDequeueDuration(mWindow.get());
70 EXPECT_EQ(0, result);
Alec Mourif629f102019-08-06 18:16:23 -070071 EXPECT_EQ(0, mWindow->getLastDequeueDuration() / 1000);
Alec Mouri9fa2cb62019-07-15 17:36:26 -070072}
73
74TEST_F(ANativeWindowTest, getLastDequeueDuration_withDequeue_returnsTime) {
75 ANativeWindowBuffer* buffer;
76 int fd;
77 int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
78 close(fd);
79 EXPECT_EQ(0, result);
80
81 result = ANativeWindow_getLastDequeueDuration(mWindow.get());
82 EXPECT_GT(result, 0);
Alec Mourif629f102019-08-06 18:16:23 -070083 EXPECT_EQ(result, mWindow->getLastDequeueDuration() / 1000);
Alec Mouri9fa2cb62019-07-15 17:36:26 -070084}