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 <log/log.h>
23#include <VtsHalHidlTargetTestBase.h>
24
25#include <atomic>
26#include <chrono>
27#include <cmath>
28#include <inttypes.h>
29#include <thread>
30
31using ::android::frameworks::displayservice::V1_0::IDisplayEventReceiver;
32using ::android::frameworks::displayservice::V1_0::IDisplayService;
33using ::android::frameworks::displayservice::V1_0::IEventCallback;
34using ::android::frameworks::displayservice::V1_0::Status;
35using ::android::hardware::Return;
36using ::android::hardware::Void;
37using ::android::sp;
38using namespace ::std::chrono_literals;
39
40#define ASSERT_OK(ret) ASSERT_TRUE((ret).isOk())
41#define EXPECT_SUCCESS(retExpr) do { \
42        Return<Status> retVal = (retExpr); \
43        ASSERT_OK(retVal); \
44        EXPECT_EQ(Status::SUCCESS, static_cast<Status>(retVal)); \
45    } while(false)
46#define EXPECT_BAD_VALUE(retExpr) do { \
47        Return<Status> retVal = (retExpr); \
48        ASSERT_OK(retVal); \
49        EXPECT_EQ(Status::BAD_VALUE, static_cast<Status>(retVal)); \
50    } while(false)
51
52#define MAX_INACCURACY 3
53
54class TestCallback : public IEventCallback {
55public:
56    Return<void> onVsync(uint64_t timestamp, uint32_t count) override {
57        ALOGE("onVsync: timestamp=%" PRIu64 " count=%d", timestamp, count);
58
59        vsyncs++;
60        return Void();
61    }
62    Return<void> onHotplug(uint64_t timestamp, bool connected) override {
63        ALOGE("onVsync: timestamp=%" PRIu64 " connected=%s", timestamp, connected ? "true" : "false");
64
65        hotplugs++;
66        return Void();
67    }
68
69    std::atomic<int> vsyncs{0};
70    std::atomic<int> hotplugs{0};
71};
72
73class DisplayServiceTest : public ::testing::VtsHalHidlTargetTestBase {
74public:
75    ~DisplayServiceTest() {}
76
77    virtual void SetUp() override {
78        sp<IDisplayService> service = ::testing::VtsHalHidlTargetTestBase::getService<IDisplayService>();
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
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 */
104TEST_F(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 */
116TEST_F(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 */
147TEST_F(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 */
157TEST_F(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
165int main(int argc, char **argv) {
166    ::testing::InitGoogleTest(&argc, argv);
167    int status = RUN_ALL_TESTS();
168    ALOGE("Test status = %d", status);
169    return status;
170}
171