• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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 mockUpdate() {
19    const updater = {
20        checkNewVersion: function (...args) {
21            console.warn("Updater.checkNewVersion interface mocked in the Previewer. How this interface works on the" +
22                " Previewer may be different from that on a real device.")
23            const len = args.length
24            if (len > 0 && typeof args[len - 1] === 'function') {
25                args[len - 1].call(this, paramMock.businessErrorMock, NewVersionInfo);
26            } else {
27                return new Promise((resolve, reject) => {
28                    resolve(NewVersionInfo);
29                })
30            }
31        },
32        download: function (...args) {
33            console.warn("Updater.download interface mocked in the Previewer. How this interface works on the" +
34                " Previewer may be different from that on a real device.")
35        },
36        upgrade: function (...args) {
37            console.warn("Updater.upgrade interface mocked in the Previewer. How this interface works on the" +
38                " Previewer may be different from that on a real device.")
39        },
40        getNewVersionInfo: function (...args) {
41            console.warn("Updater.getNewVersionInfo interface mocked in the Previewer. How this interface works on the" +
42                " Previewer may be different from that on a real device.")
43            const len = args.length
44            if (len > 0 && typeof args[len - 1] === 'function') {
45                args[len - 1].call(this, paramMock.businessErrorMock, NewVersionInfo);
46            } else {
47                return new Promise((resolve, reject) => {
48                    resolve(NewVersionInfo);
49                })
50            }
51        },
52        getUpdatePolicy: function (...args) {
53            console.warn("Updater.getUpdatePolicy interface mocked in the Previewer. How this interface works on the" +
54                " Previewer may be different from that on a real device.")
55            const len = args.length
56            if (len > 0 && typeof args[len - 1] === 'function') {
57                args[len - 1].call(this, paramMock.businessErrorMock, UpdatePolicy);
58            } else {
59                return new Promise((resolve, reject) => {
60                    resolve(UpdatePolicy);
61                })
62            }
63        },
64        setUpdatePolicy: function (...args) {
65            console.warn("Updater.setUpdatePolicy interface mocked in the Previewer. How this interface works on the" +
66                " Previewer may be different from that on a real device.")
67            const len = args.length
68            if (len > 0 && typeof args[len - 1] === 'function') {
69                args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock);
70            } else {
71                return new Promise((resolve, reject) => {
72                    resolve(paramMock.paramNumberMock);
73                })
74            }
75        },
76        applyNewVersion: function (...args) {
77            console.warn("Updater.applyNewVersion interface mocked in the Previewer. How this interface works on the" +
78                " Previewer may be different from that on a real device.")
79            const len = args.length
80            if (len > 0 && typeof args[len - 1] === 'function') {
81                args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock);
82            } else {
83                return new Promise((resolve, reject) => {
84                    resolve(paramMock.paramNumberMock);
85                })
86            }
87        },
88        rebootAndCleanCache: function (...args) {
89            console.warn("Updater.rebootAndCleanCache interface mocked in the Previewer. How this interface works on the" +
90                " Previewer may be different from that on a real device.")
91            const len = args.length
92            if (len > 0 && typeof args[len - 1] === 'function') {
93                args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock);
94            } else {
95                return new Promise((resolve, reject) => {
96                    resolve(paramMock.paramNumberMock);
97                })
98            }
99        },
100        verifyUpdatePackage: function (...args) {
101            console.warn("Updater.verifyUpdatePackage interface mocked in the Previewer. How this interface works on the" +
102                " Previewer may be different from that on a real device.")
103        },
104        on: function (...args) {
105            console.warn("Updater.on interface mocked in the Previewer. How this interface works on the" +
106                " Previewer may be different from that on a real device.")
107            const len = args.length
108            if (len > 0 && typeof args[len - 1] === 'function') {
109                args[len - 1].call(this, Progress);
110            }
111        },
112        off: function (...args) {
113            console.warn("Updater.off interface mocked in the Previewer. How this interface works on the" +
114                " Previewer may be different from that on a real device.")
115            const len = args.length
116            if (len > 0 && typeof args[len - 1] === 'function') {
117                args[len - 1].call(this, Progress);
118            }
119        },
120        cancel: function (...args) {
121            console.warn("Updater.cancel interface mocked in the Previewer. How this interface works on the" +
122                " Previewer may be different from that on a real device.")
123        },
124    }
125    const update = {
126        PackageTypes,
127        NewVersionStatus,
128        UpdateState,
129        InstallMode,
130        getUpdater: function (...args) {
131            console.warn("update.getUpdater interface mocked in the Previewer. How this interface works on the" +
132                " Previewer may be different from that on a real device.")
133            return updater;
134        },
135        getUpdaterForOther: function (...args) {
136            console.warn("update.getUpdaterForOther interface mocked in the Previewer. How this interface works on the" +
137                " Previewer may be different from that on a real device.")
138            return updater;
139        },
140        getUpdaterFromOther: function (...args) {
141            console.warn("update.getUpdaterFromOther interface mocked in the Previewer. How this interface works on the" +
142                " Previewer may be different from that on a real device.")
143            return updater;
144        }
145    }
146    return update;
147}
148
149const PackageTypes = {
150    PACKAGE_TYPE_NORMAL: 1,
151    PACKAGE_TYPE_BASE: 2,
152    PACKAGE_TYPE_CUST: 3,
153    PACKAGE_TYPE_PRELOAD: 4,
154    PACKAGE_TYPE_COTA: 5,
155    PACKAGE_TYPE_VERSION: 6,
156    PACKAGE_TYPE_PATCH: 7,
157}
158
159const CheckResult = {
160    versionName: "[PC Preview] unknow versionName",
161    versionCode: "[PC Preview] unknow versionCode",
162    size: "[PC Preview] unknow size",
163    verifyInfo: "[PC Preview] unknow verifyInfo",
164    packageType: PackageTypes.PACKAGE_TYPE_NORMAL,
165    descriptionId: "[PC Preview] unknow descriptionId",
166}
167
168const DescriptionInfo = {
169    descriptionId: "[PC Preview] unknow descriptionId",
170    content: "[PC Preview] unknow content",
171}
172
173const NewVersionStatus = {
174    VERSION_STATUS_ERR: -1,
175    VERSION_STATUS_NEW: 0,
176    VERSION_STATUS_NONE: 1,
177    VERSION_STATUS_BUSY: 2,
178}
179
180const NewVersionInfo = {
181    status: NewVersionStatus.VERSION_STATUS_NEW,
182    errMsg: "[PC Preview] unknow errMsg",
183    checkResults: [CheckResult],
184    descriptionInfo: [DescriptionInfo]
185}
186
187const UpdateState = {
188    UPDATE_STATE_INIT: 0,
189    UPDATE_STATE_CHECK_VERSION_ON: 10,
190    UPDATE_STATE_CHECK_VERSION_FAIL: 11,
191    UPDATE_STATE_CHECK_VERSION_SUCCESS: 12,
192    UPDATE_STATE_DOWNLOAD_ON: 20,
193    UPDATE_STATE_DOWNLOAD_PAUSE: 21,
194    UPDATE_STATE_DOWNLOAD_CANCEL: 22,
195    UPDATE_STATE_DOWNLOAD_FAIL: 23,
196    UPDATE_STATE_DOWNLOAD_SUCCESS: 24,
197    UPDATE_STATE_VERIFY_ON: 30,
198    UPDATE_STATE_VERIFY_FAIL: 31,
199    UPDATE_STATE_VERIFY_SUCCESS: 32,
200    UPDATE_STATE_PACKAGE_TRANS_ON: 70,
201    UPDATE_STATE_PACKAGE_TRANS_FAIL: 71,
202    UPDATE_STATE_PACKAGE_TRANS_SUCCESS: 72,
203    UPDATE_STATE_INSTALL_ON: 80,
204    UPDATE_STATE_INSTALL_FAIL: 81,
205    UPDATE_STATE_INSTALL_SUCCESS: 82,
206    UPDATE_STATE_UPDATE_ON: 90,
207    UPDATE_STATE_UPDATE_FAIL: 91,
208    UPDATE_STATE_UPDATE_SUCCESS: 92
209}
210
211const Progress = {
212    percent: "[PC Preview] unknow percent",
213    status: UpdateState.UPDATE_STATE_INIT,
214    endReason: "[PC Preview] unknow endReason"
215}
216
217const InstallMode = {
218    INSTALL_MODE_NORMAL: "[PC Preview] unknow INSTALL_MODE_NORMAL",
219    INSTALL_MODE_NIGHT: "[PC Preview] unknow INSTALL_MODE_NIGHT",
220    INSTALL_MODE_AUTO: "[PC Preview] unknow INSTALL_MODE_AUTO"
221}
222
223const UpdatePolicy = {
224    autoDownload: "[PC Preview] unknow autoDownload",
225    installMode: InstallMode.INSTALL_MODE_NORMAL,
226    autoUpgradeInterval: [0]
227}