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 "synchronizedrecordstate_tests"
19
20 #include "../SynchronizedRecordState.h"
21
22 #include <gtest/gtest.h>
23
24 using namespace android;
25 using namespace android::audioflinger;
26
27 namespace {
28
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
TEST(SynchronizedRecordStateTests,Basic)31 TEST(SynchronizedRecordStateTests, Basic) {
32 struct Cookie : public RefBase {};
33
34 // These variables are set by trigger().
35 bool triggered = false;
36 wp<SyncEvent> param;
37
38 constexpr auto type = AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
39 constexpr auto triggerSession = audio_session_t(10);
40 constexpr auto listenerSession = audio_session_t(11);
41 const SyncEventCallback callback =
42 [&](const wp<SyncEvent>& event) {
43 triggered = true;
44 param = event;
45 };
46 const auto cookie = sp<Cookie>::make();
47
48 // Check timeout.
49 SynchronizedRecordState recordState(48000 /* sampleRate */);
50 auto syncEvent = sp<SyncEvent>::make(
51 type,
52 triggerSession,
53 listenerSession,
54 callback,
55 cookie);
56 recordState.startRecording(syncEvent);
57 recordState.updateRecordFrames(2);
58 ASSERT_FALSE(triggered);
59 ASSERT_EQ(0, recordState.updateRecordFrames(1'000'000'000));
60 ASSERT_FALSE(triggered);
61 ASSERT_TRUE(syncEvent->isCancelled());
62 ASSERT_EQ(cookie, std::any_cast<decltype(cookie)>(syncEvent->cookie()));
63
64 // Check count down after track is complete.
65 syncEvent = sp<SyncEvent>::make(
66 type,
67 triggerSession,
68 listenerSession,
69 callback,
70 cookie);
71 recordState.startRecording(syncEvent);
72 recordState.onPlaybackFinished(syncEvent, 10);
73 ASSERT_EQ(1, recordState.updateRecordFrames(9));
74 ASSERT_FALSE(triggered);
75 ASSERT_EQ(0, recordState.updateRecordFrames(2));
76 ASSERT_FALSE(triggered);
77 ASSERT_TRUE(syncEvent->isCancelled());
78 }
79 #pragma clang diagnostic pop
80 }
81