• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 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
16import { paramMock } from "../utils"
17
18export function mockDeviceManager() {
19  const deviceInfoMock = {
20    deviceId: "[PC Preview] unknow mDeviceId",
21    deviceName: "[PC Preview] unknow mDeviceName",
22    deviceType: 0x0E,
23    networkId: "[PC Preview] unknow mNetworkId"
24  }
25  const deviceStateChangeMock = {
26    action: 0,
27    device: deviceInfoMock
28  }
29  const deviceManagerMock = {
30    release: function () {
31      console.warn("DeviceManager.release interface mocked in the Previewer. How this interface works on the Previewer" +
32        " may be different from that on a real device.")
33    },
34    getTrustedDeviceListSync: function () {
35      console.warn("DeviceManager.getTrustedDeviceListSync interface mocked in the Previewer. How this interface works on the" +
36        " Previewer may be different from that on a real device.")
37      var array = new Array();
38      array.push(deviceInfoMock);
39      return array;
40    },
41    getTrustedDeviceList: function (...args) {
42      console.warn("DeviceManager.getTrustedDeviceList interface mocked in the Previewer. How this interface works on the" +
43        " Previewer may be different from that on a real device.")
44      var array = new Array();
45      array.push(deviceInfoMock);
46      const len = args.length
47      if (typeof args[len - 1] === 'function') {
48        args[len - 1].call(this, paramMock.businessErrorMock, array);
49      } else {
50        return new Promise((resolve, reject) => {
51          resolve(array);
52        })
53      }
54    },
55    getLocalDeviceInfoSync: function () {
56      console.warn("DeviceManager.getLocalDeviceInfoSync interface mocked in the Previewer. How this interface works on the" +
57        " Previewer may be different from that on a real device.")
58      return deviceInfoMock;
59    },
60    getLocalDeviceInfo: function (...args) {
61      console.warn("DeviceManager.getLocalDeviceInfo interface mocked in the Previewer. How this interface works on the" +
62        " Previewer may be different from that on a real device.")
63      const len = args.length
64      if (typeof args[len - 1] === 'function') {
65        args[len - 1].call(this, paramMock.businessErrorMock, deviceInfoMock);
66      } else {
67        return new Promise((resolve, reject) => {
68          resolve(deviceInfoMock);
69        })
70      }
71    },
72    startDeviceDiscovery: function (...args) {
73      console.warn("DeviceManager.startDeviceDiscovery interface mocked in the Previewer. How this interface works on the" +
74        " Previewer may be different from that on a real device.")
75    },
76    stopDeviceDiscovery: function (...args) {
77      console.warn("DeviceManager.stopDeviceDiscovery interface mocked in the Previewer. How this interface works on the" +
78        " Previewer may be different from that on a real device.")
79    },
80    authenticateDevice: function (...args) {
81      console.warn("DeviceManager.authenticateDevice interface mocked in the Previewer. How this interface works on the" +
82        " Previewer may be different from that on a real device.")
83      const len = args.length
84      if (typeof args[len - 1] === 'function') {
85        args[len - 1].call(this, paramMock.businessErrorMock, {
86          deviceId: "[PC Preview] unknow deviceId",
87          pinToken: "[PC Preview] unknow pinToken"
88        });
89      }
90    },
91    unAuthenticateDevice: function (...args) {
92      console.warn("DeviceManager.unAuthenticateDevice interface mocked in the Previewer. How this interface works on the" +
93        " Previewer may be different from that on a real device.")
94    },
95    verifyAuthInfo: function (...args) {
96      console.warn("DeviceManager.verifyAuthInfo interface mocked in the Previewer. How this interface works on the" +
97        " Previewer may be different from that on a real device.")
98      const len = args.length
99      if (typeof args[len - 1] === 'function') {
100        args[len - 1].call(this, paramMock.businessErrorMock, {
101          deviceId: "[PC Preview] unknow deviceId",
102          level: "[PC Preview] unknow level"
103        });
104      }
105    },
106    on: function (...args) {
107      console.warn("DeviceManager.on interface mocked in the Previewer. How this interface works on the Previewer may" +
108        " be different from that on a real device.")
109      const len = args.length
110      if (typeof args[len - 1] === 'function') {
111        if (args[0] == 'deviceStateChange') {
112          args[len - 1].call(this, paramMock.businessErrorMock, deviceStateChangeMock);
113        } else if (args[0] == 'deviceFound') {
114          args[len - 1].call(this, paramMock.businessErrorMock, {
115            subscribeId: "[PC Preview] unknow subscribeId",
116            device: deviceInfoMock
117          });
118        } else if (args[0] == 'discoverFail') {
119          args[len - 1].call(this, paramMock.businessErrorMock, {
120            subscribeId: "[PC Preview] unknow subscribeId",
121            reason: "[PC Preview] unknow reason"
122          });
123        } else {
124          args[len - 1].call(this);
125        }
126      }
127    },
128    off: function (...args) {
129      console.warn("DeviceManager.off interface mocked in the Previewer. How this interface works on the Previewer may" +
130        " be different from that on a real device.")
131    }
132  }
133  const deviceManager = {
134    deviceManagerMock,
135    createDeviceManager: function (...args) {
136      console.warn("distributedHardware.deviceManager.createDeviceManager interface mocked in the Previewer. How this interface works on the Previewer" +
137        " may be different from that on a real device.")
138      const len = args.length
139      if (typeof args[len - 1] === 'function') {
140        args[len - 1].call(this, paramMock.businessErrorMock, deviceManagerMock)
141      }
142    }
143  }
144  return deviceManager
145}