• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "usb_mock.h"
17 #include "disc_log.h"
18 #include "softbus_error_code.h"
19 
20 using testing::NotNull;
21 using testing::Return;
22 
DiscUsbDispatcherInit(DiscInnerCallback * callback)23 DiscoveryFuncInterface *DiscUsbDispatcherInit(DiscInnerCallback *callback)
24 {
25     return UsbMock::Get()->DiscUsbDispatcherInit(callback);
26 }
27 
DiscUsbDispatcherDeinit()28 void DiscUsbDispatcherDeinit()
29 {
30     DISC_LOGI(DISC_TEST, "destroy");
31 }
32 
UsbPublish(const PublishOption * option)33 int32_t UsbMock::UsbPublish(const PublishOption *option)
34 {
35     return Get()->Publish(option);
36 }
37 
UsbStartScan(const PublishOption * option)38 int32_t UsbMock::UsbStartScan(const PublishOption *option)
39 {
40     return Get()->StartScan(option);
41 }
42 
UsbUnpublish(const PublishOption * option)43 int32_t UsbMock::UsbUnpublish(const PublishOption *option)
44 {
45     return Get()->Unpublish(option);
46 }
47 
UsbStopScan(const PublishOption * option)48 int32_t UsbMock::UsbStopScan(const PublishOption *option)
49 {
50     return Get()->StopScan(option);
51 }
52 
UsbStartAdvertise(const SubscribeOption * option)53 int32_t UsbMock::UsbStartAdvertise(const SubscribeOption *option)
54 {
55     return Get()->StartAdvertise(option);
56 }
57 
UsbSubscribe(const SubscribeOption * option)58 int32_t UsbMock::UsbSubscribe(const SubscribeOption *option)
59 {
60     return Get()->Subscribe(option);
61 }
62 
UsbUnsubscribe(const SubscribeOption * option)63 int32_t UsbMock::UsbUnsubscribe(const SubscribeOption *option)
64 {
65     return Get()->Unsubscribe(option);
66 }
67 
UsbStopAdvertise(const SubscribeOption * option)68 int32_t UsbMock::UsbStopAdvertise(const SubscribeOption *option)
69 {
70     return Get()->StopAdvertise(option);
71 }
72 
UsbLinkStatusChanged(LinkStatus status)73 void UsbMock::UsbLinkStatusChanged(LinkStatus status)
74 {
75     Get()->LinkStatusChanged(status);
76 }
77 
UsbUpdateLocalDeviceInfo(InfoTypeChanged type)78 void UsbMock::UsbUpdateLocalDeviceInfo(InfoTypeChanged type)
79 {
80     Get()->UpdateLocalDeviceInfo(type);
81 }
82 
Get()83 UsbMock* UsbMock::Get()
84 {
85     return instance_;
86 }
87 
UsbMock()88 UsbMock::UsbMock()
89 {
90     instance_ = this;
91 }
92 
~UsbMock()93 UsbMock::~UsbMock()
94 {
95     instance_ = nullptr;
96 }
97 
InjectDeviceFoundEvent(const DeviceInfo * device)98 void UsbMock::InjectDeviceFoundEvent(const DeviceInfo *device)
99 {
100     if (deviceFoundCallback_.OnDeviceFound) {
101         InnerDeviceInfoAddtions additions;
102         additions.medium = USB;
103         deviceFoundCallback_.OnDeviceFound(device, &additions);
104     }
105 }
SetupStub()106 void UsbMock::SetupStub()
107 {
108     EXPECT_CALL(*this, DiscUsbDispatcherInit(NotNull())).WillRepeatedly([](DiscInnerCallback *callback) {
109         deviceFoundCallback_ = *callback;
110         return &interface_;
111     });
112     EXPECT_CALL(*this, Publish(NotNull())).WillRepeatedly(Return(SOFTBUS_OK));
113     EXPECT_CALL(*this, StartScan(NotNull())).WillRepeatedly(Return(SOFTBUS_OK));
114     EXPECT_CALL(*this, Unpublish(NotNull())).WillRepeatedly(Return(SOFTBUS_OK));
115     EXPECT_CALL(*this, StopScan(NotNull())).WillRepeatedly(Return(SOFTBUS_OK));
116     EXPECT_CALL(*this, StartAdvertise(NotNull())).WillRepeatedly(Return(SOFTBUS_OK));
117     EXPECT_CALL(*this, Subscribe(NotNull())).WillRepeatedly(Return(SOFTBUS_OK));
118     EXPECT_CALL(*this, Unsubscribe(NotNull())).WillRepeatedly(Return(SOFTBUS_OK));
119     EXPECT_CALL(*this, StopAdvertise(NotNull())).WillRepeatedly(Return(SOFTBUS_OK));
120 }
121