1 /* 2 * Copyright 2021 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 #pragma once 18 19 #include <unistd.h> 20 21 #include <functional> 22 #include <mutex> 23 #include <queue> 24 25 #include "common/bidi_queue.h" 26 #include "common/bind.h" 27 #include "common/callback.h" 28 #include "os/handler.h" 29 #include "os/log.h" 30 #include "os/queue.h" 31 32 namespace bluetooth { 33 namespace os { 34 35 template <typename T> 36 class MockIQueueEnqueue : public IQueueEnqueue<T> { 37 public: 38 using EnqueueCallback = common::Callback<std::unique_ptr<T>()>; 39 RegisterEnqueue(Handler * handler,EnqueueCallback callback)40 virtual void RegisterEnqueue(Handler* handler, EnqueueCallback callback) { 41 ASSERT(registered_handler == nullptr); 42 registered_handler = handler; 43 registered_enqueue_callback = callback; 44 } 45 UnregisterEnqueue()46 virtual void UnregisterEnqueue() { 47 ASSERT(registered_handler != nullptr); 48 registered_handler = nullptr; 49 registered_enqueue_callback = {}; 50 } 51 52 void run_enqueue(unsigned times = 1) { 53 while (registered_handler != nullptr && times > 0) { 54 times--; 55 enqueued.push(registered_enqueue_callback.Run()); 56 } 57 } 58 59 Handler* registered_handler = nullptr; 60 EnqueueCallback registered_enqueue_callback = {}; 61 std::queue<std::unique_ptr<T>> enqueued = {}; 62 }; 63 64 template <typename T> 65 class MockIQueueDequeue : public IQueueDequeue<T> { 66 public: 67 using DequeueCallback = common::Callback<void()>; 68 RegisterDequeue(Handler * handler,DequeueCallback callback)69 virtual void RegisterDequeue(Handler* handler, DequeueCallback callback) { 70 ASSERT(registered_handler == nullptr); 71 registered_handler = handler; 72 registered_dequeue_callback = callback; 73 } 74 UnregisterDequeue()75 virtual void UnregisterDequeue() { 76 ASSERT(registered_handler != nullptr); 77 registered_handler = nullptr; 78 registered_dequeue_callback = {}; 79 } 80 TryDequeue()81 virtual std::unique_ptr<T> TryDequeue() { 82 std::unique_ptr<T> front = std::move(enqueued.front()); 83 enqueued.pop(); 84 return front; 85 } 86 87 void run_dequeue(unsigned times = 1) { 88 while (registered_handler != nullptr && times > 0) { 89 times--; 90 registered_dequeue_callback.Run(); 91 } 92 } 93 94 Handler* registered_handler = nullptr; 95 DequeueCallback registered_dequeue_callback = {}; 96 std::queue<std::unique_ptr<T>> enqueued = {}; 97 }; 98 99 } // namespace os 100 } // namespace bluetooth 101