Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | using namespace android; |
| 31 | |
Alec Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 32 | class TestableSurface final : public Surface { |
| 33 | public: |
| 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 Mouri | 0d1398b | 2019-08-14 10:53:50 -0700 | [diff] [blame^] | 39 | |
| 40 | // Exposes the internal last queue duration that's stored on the Surface. |
| 41 | nsecs_t getLastQueueDuration() const { return mLastQueueDuration; } |
Alec Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 44 | class ANativeWindowTest : public ::testing::Test { |
| 45 | protected: |
| 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 Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 52 | mWindow = new TestableSurface(mProducer); |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 53 | 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 Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 67 | |
| 68 | sp<TestableSurface> mWindow; |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | TEST_F(ANativeWindowTest, getLastDequeueDuration_noDequeue_returnsZero) { |
| 72 | int result = ANativeWindow_getLastDequeueDuration(mWindow.get()); |
| 73 | EXPECT_EQ(0, result); |
Alec Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 74 | EXPECT_EQ(0, mWindow->getLastDequeueDuration() / 1000); |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_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 Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 86 | EXPECT_EQ(result, mWindow->getLastDequeueDuration() / 1000); |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 87 | } |
Alec Mouri | 0d1398b | 2019-08-14 10:53:50 -0700 | [diff] [blame^] | 88 | |
| 89 | TEST_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 | |
| 95 | TEST_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 | |
| 107 | TEST_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 | } |