1 /*
2  * Copyright 2022 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 "test/common/jni_thread.h"
18 
19 #include <base/functional/callback.h>
20 #include <bluetooth/log.h>
21 
22 #include <map>
23 
24 // TODO(b/369381361) Enfore -Wmissing-prototypes
25 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
26 
27 std::queue<base::OnceClosure> do_in_jni_thread_task_queue;
28 
run_one_jni_thread_task()29 void run_one_jni_thread_task() {
30   bluetooth::log::assert_that(do_in_jni_thread_task_queue.size(),
31                               "JNI thread has no closures to execute");
32   base::OnceCallback callback = std::move(do_in_jni_thread_task_queue.front());
33   do_in_jni_thread_task_queue.pop();
34   std::move(callback).Run();
35 }
36 
run_all_jni_thread_task()37 void run_all_jni_thread_task() {
38   while (do_in_jni_thread_task_queue.size()) {
39     run_one_jni_thread_task();
40   }
41 }
42 
reset_mock_jni_thread_queue()43 void reset_mock_jni_thread_queue() {
44   while (do_in_jni_thread_task_queue.size()) {
45     do_in_jni_thread_task_queue.pop();
46   }
47 }
48