1 /*
2 * Copyright (C) 2016 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 "VtsFwkDisplayServiceV1_0TargetTest"
18
19 #include <android/frameworks/displayservice/1.0/IDisplayEventReceiver.h>
20 #include <android/frameworks/displayservice/1.0/IDisplayService.h>
21 #include <android/frameworks/displayservice/1.0/IEventCallback.h>
22 #include <gtest/gtest.h>
23 #include <hidl/GtestPrinter.h>
24 #include <hidl/ServiceManagement.h>
25 #include <log/log.h>
26
27 #include <atomic>
28 #include <chrono>
29 #include <cmath>
30 #include <inttypes.h>
31 #include <thread>
32
33 using ::android::frameworks::displayservice::V1_0::IDisplayEventReceiver;
34 using ::android::frameworks::displayservice::V1_0::IDisplayService;
35 using ::android::frameworks::displayservice::V1_0::IEventCallback;
36 using ::android::frameworks::displayservice::V1_0::Status;
37 using ::android::hardware::Return;
38 using ::android::hardware::Void;
39 using ::android::sp;
40 using namespace ::std::chrono_literals;
41
42 #define ASSERT_OK(ret) ASSERT_TRUE((ret).isOk())
43 #define EXPECT_SUCCESS(retExpr) do { \
44 Return<Status> retVal = (retExpr); \
45 ASSERT_OK(retVal); \
46 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(retVal)); \
47 } while(false)
48 #define EXPECT_BAD_VALUE(retExpr) do { \
49 Return<Status> retVal = (retExpr); \
50 ASSERT_OK(retVal); \
51 EXPECT_EQ(Status::BAD_VALUE, static_cast<Status>(retVal)); \
52 } while(false)
53
54 #define MAX_INACCURACY 3
55
56 class TestCallback : public IEventCallback {
57 public:
onVsync(uint64_t timestamp,uint32_t count)58 Return<void> onVsync(uint64_t timestamp, uint32_t count) override {
59 ALOGE("onVsync: timestamp=%" PRIu64 " count=%d", timestamp, count);
60
61 vsyncs++;
62 return Void();
63 }
onHotplug(uint64_t timestamp,bool connected)64 Return<void> onHotplug(uint64_t timestamp, bool connected) override {
65 ALOGE("onVsync: timestamp=%" PRIu64 " connected=%s", timestamp, connected ? "true" : "false");
66
67 hotplugs++;
68 return Void();
69 }
70
71 std::atomic<int> vsyncs{0};
72 std::atomic<int> hotplugs{0};
73 };
74
75 class DisplayServiceTest : public ::testing::TestWithParam<std::string> {
76 public:
SetUp()77 virtual void SetUp() override {
78 sp<IDisplayService> service = IDisplayService::getService(GetParam());
79
80 ASSERT_NE(service, nullptr);
81
82 Return<sp<IDisplayEventReceiver>> ret = service->getEventReceiver();
83 ASSERT_OK(ret);
84
85 receiver = ret;
86 ASSERT_NE(receiver, nullptr);
87
88
89 cb = new TestCallback();
90 EXPECT_SUCCESS(receiver->init(cb));
91 }
92
TearDown()93 virtual void TearDown() override {
94 EXPECT_SUCCESS(receiver->close());
95 }
96
97 sp<TestCallback> cb;
98 sp<IDisplayEventReceiver> receiver;
99 };
100
101 /**
102 * No vsync events should happen unless you explicitly request one.
103 */
TEST_P(DisplayServiceTest,TestAttachRequestVsync)104 TEST_P(DisplayServiceTest, TestAttachRequestVsync) {
105 EXPECT_EQ(0, cb->vsyncs);
106
107 EXPECT_SUCCESS(receiver->requestNextVsync());
108
109 std::this_thread::sleep_for(100ms); // framerate is not fixed on Android devices
110 EXPECT_EQ(1, cb->vsyncs);
111 }
112
113 /**
114 * Vsync rate respects count.
115 */
TEST_P(DisplayServiceTest,TestSetVsyncRate)116 TEST_P(DisplayServiceTest, TestSetVsyncRate) {
117 ASSERT_EQ(0, cb->vsyncs);
118
119 EXPECT_SUCCESS(receiver->setVsyncRate(1));
120 std::this_thread::sleep_for(250ms);
121 int at1 = cb->vsyncs;
122
123 cb->vsyncs = 0;
124 EXPECT_SUCCESS(receiver->setVsyncRate(2));
125 std::this_thread::sleep_for(250ms);
126 int at2 = cb->vsyncs;
127
128 cb->vsyncs = 0;
129 EXPECT_SUCCESS(receiver->setVsyncRate(4));
130 std::this_thread::sleep_for(250ms);
131 int at4 = cb->vsyncs;
132
133 EXPECT_NE(0, at1);
134 EXPECT_NE(0, at2);
135 EXPECT_NE(0, at4);
136
137 EXPECT_LE(std::abs(at1 - 2 * at2), 2 * MAX_INACCURACY);
138 EXPECT_LE(std::abs(at1 - 4 * at4), 4 * MAX_INACCURACY);
139 EXPECT_LE(std::abs(at2 - 2 * at4), 2 * MAX_INACCURACY);
140
141 ALOGE("Vsync counts: %d %d %d", at1, at2, at4);
142 }
143
144 /**
145 * Open/close should return proper error results.
146 */
TEST_P(DisplayServiceTest,TestOpenClose)147 TEST_P(DisplayServiceTest, TestOpenClose) {
148 EXPECT_BAD_VALUE(receiver->init(cb)); // already opened in SetUp
149 EXPECT_SUCCESS(receiver->close()); // can close what was originally opened
150 EXPECT_BAD_VALUE(receiver->close()); // can't close again
151 EXPECT_SUCCESS(receiver->init(cb)); // open so can close again in SetUp
152 }
153
154 /**
155 * Vsync must be given a value that is >= 0.
156 */
TEST_P(DisplayServiceTest,TestVsync)157 TEST_P(DisplayServiceTest, TestVsync) {
158 EXPECT_SUCCESS(receiver->setVsyncRate(0));
159 EXPECT_SUCCESS(receiver->setVsyncRate(5));
160 EXPECT_SUCCESS(receiver->setVsyncRate(0));
161 EXPECT_BAD_VALUE(receiver->setVsyncRate(-1));
162 EXPECT_BAD_VALUE(receiver->setVsyncRate(-1000));
163 }
164
165 INSTANTIATE_TEST_SUITE_P(
166 PerInstance, DisplayServiceTest,
167 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDisplayService::descriptor)),
168 android::hardware::PrintInstanceNameToString);
169