blob: 9b358da4b512213136bb881a781b1180ae2c441f [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; }
Alec Mouri0d1398b2019-08-14 10:53:50 -070039
40 // Exposes the internal last queue duration that's stored on the Surface.
41 nsecs_t getLastQueueDuration() const { return mLastQueueDuration; }
Alec Mourif629f102019-08-06 18:16:23 -070042};
43
Alec Mouri9fa2cb62019-07-15 17:36:26 -070044class ANativeWindowTest : public ::testing::Test {
45protected:
46 void SetUp() override {
47 const ::testing::TestInfo* const test_info =
48 ::testing::UnitTest::GetInstance()->current_test_info();
49 ALOGV("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
50 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
51 mItemConsumer = new BufferItemConsumer(mConsumer, GRALLOC_USAGE_SW_READ_OFTEN);
Alec Mourif629f102019-08-06 18:16:23 -070052 mWindow = new TestableSurface(mProducer);
Alec Mouri9fa2cb62019-07-15 17:36:26 -070053 const int success = native_window_api_connect(mWindow.get(), NATIVE_WINDOW_API_CPU);
54 EXPECT_EQ(0, success);
55 }
56
57 void TearDown() override {
58 const ::testing::TestInfo* const test_info =
59 ::testing::UnitTest::GetInstance()->current_test_info();
60 ALOGV("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
61 const int success = native_window_api_disconnect(mWindow.get(), NATIVE_WINDOW_API_CPU);
62 EXPECT_EQ(0, success);
63 }
64 sp<IGraphicBufferProducer> mProducer;
65 sp<IGraphicBufferConsumer> mConsumer;
66 sp<BufferItemConsumer> mItemConsumer;
Alec Mourif629f102019-08-06 18:16:23 -070067
68 sp<TestableSurface> mWindow;
Alec Mouri9fa2cb62019-07-15 17:36:26 -070069};
70
71TEST_F(ANativeWindowTest, getLastDequeueDuration_noDequeue_returnsZero) {
72 int result = ANativeWindow_getLastDequeueDuration(mWindow.get());
73 EXPECT_EQ(0, result);
Alec Mourif629f102019-08-06 18:16:23 -070074 EXPECT_EQ(0, mWindow->getLastDequeueDuration() / 1000);
Alec Mouri9fa2cb62019-07-15 17:36:26 -070075}
76
77TEST_F(ANativeWindowTest, getLastDequeueDuration_withDequeue_returnsTime) {
78 ANativeWindowBuffer* buffer;
79 int fd;
80 int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
81 close(fd);
82 EXPECT_EQ(0, result);
83
84 result = ANativeWindow_getLastDequeueDuration(mWindow.get());
85 EXPECT_GT(result, 0);
Alec Mourif629f102019-08-06 18:16:23 -070086 EXPECT_EQ(result, mWindow->getLastDequeueDuration() / 1000);
Alec Mouri9fa2cb62019-07-15 17:36:26 -070087}
Alec Mouri0d1398b2019-08-14 10:53:50 -070088
89TEST_F(ANativeWindowTest, getLastQueueDuration_noDequeue_returnsZero) {
90 int result = ANativeWindow_getLastQueueDuration(mWindow.get());
91 EXPECT_EQ(0, result);
92 EXPECT_EQ(0, mWindow->getLastQueueDuration());
93}
94
95TEST_F(ANativeWindowTest, getLastQueueDuration_noQueue_returnsZero) {
96 ANativeWindowBuffer* buffer;
97 int fd;
98 int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
99 close(fd);
100 EXPECT_EQ(0, result);
101
102 result = ANativeWindow_getLastQueueDuration(mWindow.get());
103 EXPECT_EQ(result, 0);
104 EXPECT_EQ(result, mWindow->getLastQueueDuration());
105}
106
107TEST_F(ANativeWindowTest, getLastQueueDuration_withQueue_returnsTime) {
108 ANativeWindowBuffer* buffer;
109 int fd;
110 int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
111 close(fd);
112 EXPECT_EQ(0, result);
113
114 result = ANativeWindow_queueBuffer(mWindow.get(), buffer, 0);
115
116 result = ANativeWindow_getLastQueueDuration(mWindow.get());
117 EXPECT_GT(result, 0);
118 EXPECT_EQ(result, mWindow->getLastQueueDuration() / 1000);
119}