1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef USB_IMPL_MOCK_H 17 #define USB_IMPL_MOCK_H 18 19 #include <gmock/gmock.h> 20 #include <gtest/gtest.h> 21 22 #include "bundle_mgr_interface.h" 23 #include "bundle_mgr_proxy.h" 24 #include "delayed_sp_singleton.h" 25 #include "usb_device.h" 26 #include "usb_service.h" 27 #include "usb_srv_support.h" 28 #include "v1_0/iusb_interface.h" 29 #include "v1_0/usb_types.h" 30 31 using OHOS::HDI::Usb::V1_0::IUsbdBulkCallback; 32 using OHOS::HDI::Usb::V1_0::IUsbdSubscriber; 33 using OHOS::HDI::Usb::V1_0::IUsbInterface; 34 using OHOS::HDI::Usb::V1_0::PortInfo; 35 using OHOS::HDI::Usb::V1_0::UsbCtrlTransfer; 36 using OHOS::HDI::Usb::V1_0::UsbDev; 37 using OHOS::HDI::Usb::V1_0::USBDeviceInfo; 38 using OHOS::HDI::Usb::V1_0::UsbPipe; 39 40 namespace OHOS { 41 namespace USB { 42 constexpr int32_t DEV_ADDR_OK = 2; 43 constexpr int32_t BUS_NUM_OK = 6; 44 constexpr int32_t DEV_ADDR_INVALID = 255; 45 constexpr int32_t BUS_NUM_INVALID = 255; 46 constexpr int32_t DEFAULT_PORT_ID = 1; 47 constexpr int32_t RETVAL_INVALID = -1; 48 49 class MockUsbImpl : public IUsbInterface { 50 DECLARE_DELAYED_SP_SINGLETON(MockUsbImpl); 51 52 public: 53 static UsbDevice FindDeviceInfo(std::vector<UsbDevice> &devi); 54 int32_t SubscriberDeviceEvent(const USBDeviceInfo &info); 55 int32_t BindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber) override; 56 int32_t UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber) override; 57 int32_t GetRawDescriptor(const UsbDev &dev, std::vector<uint8_t> &decriptor) override; 58 int32_t GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor) override; 59 int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode) override; 60 int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override; 61 MOCK_METHOD1(OpenDevice, int32_t(const UsbDev &dev)); 62 MOCK_METHOD1(CloseDevice, int32_t(const UsbDev &dev)); 63 MOCK_METHOD2(GetDeviceDescriptor, int32_t(const UsbDev &dev, std::vector<uint8_t> &decriptor)); 64 MOCK_METHOD3(GetConfigDescriptor, int32_t(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor)); 65 MOCK_METHOD2(GetFileDescriptor, int32_t(const UsbDev &dev, int32_t &fd)); 66 MOCK_METHOD2(SetConfig, int32_t(const UsbDev &dev, uint8_t configIndex)); 67 MOCK_METHOD2(GetConfig, int32_t(const UsbDev &dev, uint8_t &configIndex)); 68 MOCK_METHOD3(ClaimInterface, int32_t(const UsbDev &dev, uint8_t interfaceid, uint8_t force)); 69 MOCK_METHOD2(ReleaseInterface, int32_t(const UsbDev &dev, uint8_t interfaceid)); 70 MOCK_METHOD3(SetInterface, int32_t(const UsbDev &dev, uint8_t interfaceid, uint8_t altIndex)); 71 MOCK_METHOD4( 72 BulkTransferRead, int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data)); 73 MOCK_METHOD4(BulkTransferWrite, 74 int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data)); 75 MOCK_METHOD3( 76 ControlTransferRead, int32_t(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &data)); 77 MOCK_METHOD3(ControlTransferWrite, 78 int32_t(const UsbDev &dev, const UsbCtrlTransfer &ctrl, const std::vector<uint8_t> &data)); 79 MOCK_METHOD4(InterruptTransferRead, 80 int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data)); 81 MOCK_METHOD4(InterruptTransferWrite, 82 int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data)); 83 MOCK_METHOD4( 84 IsoTransferRead, int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data)); 85 MOCK_METHOD4(IsoTransferWrite, 86 int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data)); 87 MOCK_METHOD4(RequestQueue, 88 int32_t(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData, 89 const std::vector<uint8_t> &buffer)); 90 MOCK_METHOD4(RequestWait, 91 int32_t(const UsbDev &dev, std::vector<uint8_t> &clientData, std::vector<uint8_t> &buffer, int32_t timeout)); 92 MOCK_METHOD2(RequestCancel, int32_t(const UsbDev &dev, const UsbPipe &pipe)); 93 MOCK_METHOD1(GetCurrentFunctions, int32_t(int32_t &funcs)); 94 MOCK_METHOD1(SetCurrentFunctions, int32_t(int32_t funcs)); 95 MOCK_METHOD3(RegBulkCallback, int32_t(const UsbDev &dev, const UsbPipe &pipe, const sptr<IUsbdBulkCallback> &cb)); 96 MOCK_METHOD2(UnRegBulkCallback, int32_t(const UsbDev &dev, const UsbPipe &pipe)); 97 MOCK_METHOD3(BulkRead, int32_t(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem)); 98 MOCK_METHOD3(BulkWrite, int32_t(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem)); 99 MOCK_METHOD2(BulkCancel, int32_t(const UsbDev &dev, const UsbPipe &pipe)); 100 101 private: 102 sptr<IUsbdSubscriber> subscriber_ = nullptr; 103 PortInfo portInfo_; 104 }; 105 } // namespace USB 106 } // namespace OHOS 107 #endif 108