1 /* 2 * Copyright (C) 2016 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 #ifndef WIFICOND_TESTS_INTEGRATION_TEST_BINDER_DISPATCHER_H_ 18 #define WIFICOND_TESTS_INTEGRATION_TEST_BINDER_DISPATCHER_H_ 19 20 #include <unistd.h> 21 22 #include <android-base/logging.h> 23 #include <android-base/macros.h> 24 #include <binder/IPCThreadState.h> 25 #include <binder/IServiceManager.h> 26 #include <binder/ProcessState.h> 27 28 #include "looper_backed_event_loop.h" 29 30 namespace android { 31 namespace wificond { 32 namespace tests { 33 namespace integration { 34 35 // Class to use for waiting for binder callbacks during integration tests. 36 // 37 // The class provides function to: 38 // 1. |DispatchFor|: Process binder events for the provided timeout_millis or 39 // until interrupted by |InterruptDispatch|. 40 // 2. |InterruptDispatch|: Indicate the occurence of the required event. 41 // 42 // The |DispatchFor| is meant to be called from within the tests, while the 43 // |InterruptDispatch| is expected to be called from the appropriate binder 44 // object callback. 45 class BinderDispatcher { 46 public: 47 BinderDispatcher(); 48 ~BinderDispatcher(); 49 // Dispatch binder events for |timeout_millis| or until interrupted by 50 // a call to |InterruptDispatch|. 51 // Returns true if interrupted, false otherwise. 52 bool DispatchFor(int timeout_millis); 53 // Interrupt the |DispatchFor| method when the required event is found. 54 void InterruptDispatch(); 55 56 private: 57 // Prepare the thread for receiving binder events and setup the looper. 58 void Init(); 59 void OnBinderEvent(int fd); 60 61 std::unique_ptr<LooperBackedEventLoop> event_dispatcher_; 62 // This fd is reinitialized for every test, by tearing down the static 63 // |ProcessState| instance associated with this process after every test. 64 int binder_fd_; 65 bool needs_init_; 66 bool was_interrupted_; 67 68 DISALLOW_COPY_AND_ASSIGN(BinderDispatcher); 69 }; 70 71 } // namespace integration 72 } // namespace tests 73 } // namespace wificond 74 } // namespace android 75 76 #endif // WIFICOND_TESTS_INTEGRATION_TEST_BINDER_DISPATCHER_H_ 77