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; } |
| 39 | }; |
| 40 | |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 41 | class ANativeWindowTest : public ::testing::Test { |
| 42 | protected: |
| 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 Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 49 | mWindow = new TestableSurface(mProducer); |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 50 | 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 Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 64 | |
| 65 | sp<TestableSurface> mWindow; |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | TEST_F(ANativeWindowTest, getLastDequeueDuration_noDequeue_returnsZero) { |
| 69 | int result = ANativeWindow_getLastDequeueDuration(mWindow.get()); |
| 70 | EXPECT_EQ(0, result); |
Alec Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 71 | EXPECT_EQ(0, mWindow->getLastDequeueDuration() / 1000); |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST_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 Mouri | f629f10 | 2019-08-06 18:16:23 -0700 | [diff] [blame] | 83 | EXPECT_EQ(result, mWindow->getLastDequeueDuration() / 1000); |
Alec Mouri | 9fa2cb6 | 2019-07-15 17:36:26 -0700 | [diff] [blame] | 84 | } |