blob: a80da24a47e90744d683972756ded19b45c023fa [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
32class ANativeWindowTest : public ::testing::Test {
33protected:
34 void SetUp() override {
35 const ::testing::TestInfo* const test_info =
36 ::testing::UnitTest::GetInstance()->current_test_info();
37 ALOGV("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
38 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
39 mItemConsumer = new BufferItemConsumer(mConsumer, GRALLOC_USAGE_SW_READ_OFTEN);
40 mWindow = new Surface(mProducer);
41 const int success = native_window_api_connect(mWindow.get(), NATIVE_WINDOW_API_CPU);
42 EXPECT_EQ(0, success);
43 }
44
45 void TearDown() override {
46 const ::testing::TestInfo* const test_info =
47 ::testing::UnitTest::GetInstance()->current_test_info();
48 ALOGV("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
49 const int success = native_window_api_disconnect(mWindow.get(), NATIVE_WINDOW_API_CPU);
50 EXPECT_EQ(0, success);
51 }
52 sp<IGraphicBufferProducer> mProducer;
53 sp<IGraphicBufferConsumer> mConsumer;
54 sp<BufferItemConsumer> mItemConsumer;
55 sp<ANativeWindow> mWindow;
56};
57
58TEST_F(ANativeWindowTest, getLastDequeueDuration_noDequeue_returnsZero) {
59 int result = ANativeWindow_getLastDequeueDuration(mWindow.get());
60 EXPECT_EQ(0, result);
61}
62
63TEST_F(ANativeWindowTest, getLastDequeueDuration_withDequeue_returnsTime) {
64 ANativeWindowBuffer* buffer;
65 int fd;
66 int result = ANativeWindow_dequeueBuffer(mWindow.get(), &buffer, &fd);
67 close(fd);
68 EXPECT_EQ(0, result);
69
70 result = ANativeWindow_getLastDequeueDuration(mWindow.get());
71 EXPECT_GT(result, 0);
72}