1 /*
2  * Copyright (C) 2023 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_NDEBUG 0
18 #define LOG_TAG "mediasyncevent_tests"
19 
20 #include "../SyncEvent.h"
21 
22 #include <gtest/gtest.h>
23 
24 using namespace android;
25 using namespace android::audioflinger;
26 
27 namespace {
28 #pragma clang diagnostic push
29 #pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
TEST(MediaSyncEventTests,Basic)30 TEST(MediaSyncEventTests, Basic) {
31     struct Cookie : public RefBase {};
32 
33     // These variables are set by trigger().
34     bool triggered = false;
35     wp<SyncEvent> param;
36 
37     constexpr auto type = AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
38     constexpr auto triggerSession = audio_session_t(10);
39     constexpr auto listenerSession = audio_session_t(11);
40     const SyncEventCallback callback =
41             [&](const wp<SyncEvent>& event) {
42                 triggered = true;
43                 param = event;
44             };
45     const auto cookie = sp<Cookie>::make();
46 
47     // Since the callback uses a weak pointer to this,
48     // don't allocate on the stack.
49     auto syncEvent = sp<SyncEvent>::make(
50             type,
51             triggerSession,
52             listenerSession,
53             callback,
54             cookie);
55 
56     ASSERT_EQ(type, syncEvent->type());
57     ASSERT_EQ(triggerSession, syncEvent->triggerSession());
58     ASSERT_EQ(listenerSession, syncEvent->listenerSession());
59     ASSERT_EQ(cookie, std::any_cast<decltype(cookie)>(syncEvent->cookie()));
60     ASSERT_FALSE(triggered);
61 
62     syncEvent->trigger();
63     ASSERT_TRUE(triggered);
64     ASSERT_EQ(param, syncEvent);
65 
66     ASSERT_FALSE(syncEvent->isCancelled());
67     syncEvent->cancel();
68     ASSERT_TRUE(syncEvent->isCancelled());
69 }
70 #pragma clang diagnostic pop
71 } // namespace
72