1 /*
2 * Copyright (C) 2024 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 #include <audio_utils/RunRemote.h>
18 #include <gtest/gtest.h>
19 #include <memory>
20
WorkerThread(android::audio_utils::RunRemote & runRemote)21 static void WorkerThread(android::audio_utils::RunRemote& runRemote) {
22 while (true) {
23 const int c = runRemote.getc();
24 switch (c) {
25 case 'a':
26 runRemote.putc('a'); // send ack
27 break;
28 case 'b':
29 runRemote.putc('b');
30 break;
31 default:
32 runRemote.putc('x');
33 break;
34 }
35 }
36 }
37
TEST(RunRemote,basic)38 TEST(RunRemote, basic) {
39 auto remoteWorker = std::make_shared<android::audio_utils::RunRemote>(WorkerThread);
40 remoteWorker->run();
41
42 remoteWorker->putc('a');
43 EXPECT_EQ('a', remoteWorker->getc());
44
45 remoteWorker->putc('b');
46 EXPECT_EQ('b', remoteWorker->getc());
47
48 remoteWorker->putc('c');
49 EXPECT_EQ('x', remoteWorker->getc());
50
51 remoteWorker->stop();
52 EXPECT_EQ(-1, remoteWorker->getc()); // remote closed
53 }
54