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 16import appRecovery from '@ohos.app.ability.appRecovery'; 17import testNapi from 'libentry.so'; 18import ResUtil from '../common/ResUtil'; 19import { CurActiveAbility } from '../common/CurActiveAbility'; 20 21@Entry 22@Component 23struct FaultTriggerPage { 24 storage = LocalStorage.GetShared(); 25 @State counter: number = 0; 26 @State callerAbility: string = ''; 27 @State launchReason: string = ''; 28 @State lastExitReason: string = ''; 29 @State curAbilitySaveState: string = ''; 30 @State allAbilitySaveState: string = ''; 31 @State setRecoveryAbility: string = ''; 32 33 aboutToAppear() { 34 this.counter = this.storage.get<number>('FaultTriggerPageCounter') ?? 0; 35 this.callerAbility = this.storage.get<string>('CurrentAbilityName') ?? 'UnknownCallingAbility'; 36 this.launchReason = this.storage.get<string>('launchReason') ?? 'Unknown'; 37 this.lastExitReason = this.storage.get<string>('lastExitReason') ?? 'Unknown'; 38 ResUtil.getString($r('app.string.save_state').id).then((value: string) => this.curAbilitySaveState = value); 39 ResUtil.getString($r('app.string.save_state').id).then((value: string) => this.allAbilitySaveState = value); 40 ResUtil.getString($r('app.string.set_config').id).then((value: string) => this.setRecoveryAbility = value); 41 } 42 43 doHeavyWork() : void { 44 let i = 0; 45 while (i < 1000000000) { 46 i++; 47 let j = 0; 48 while (j < 1000000000) { 49 j++; 50 let k = 0; 51 while (k < 1000000000) { 52 k++; 53 } 54 } 55 } 56 } 57 58 build() { 59 Column() { 60 Text(this.callerAbility) 61 .fontSize('30fp') 62 .fontWeight(FontWeight.Bold) 63 .padding({ left: '24vp', right: '24vp', top: '7vp', bottom: '8vp' }) 64 .margin({ top: '14vp' }) 65 .textAlign(TextAlign.Start) 66 .height('56vp') 67 .width('100%') 68 .fontColor($r('app.color.text_grey')); 69 70 List() { 71 ListItem() { 72 Column() { 73 Row() { 74 Text($r('app.string.launch_reason')).fontSize('16fp').width('86%'); 75 Text('0' + this.launchReason) 76 .fontSize('14fp') 77 .width('14%') 78 .fontColor($r('app.color.text_grey')) 79 .opacity(0.6) 80 .textAlign(TextAlign.End); 81 } 82 } 83 }.width('100%').height('48vp').align(Alignment.Center); 84 85 ListItem() { 86 Column() { 87 Row() { 88 Text($r('app.string.last_exit_reason')).fontSize('16fp').width('86%'); 89 Text('0' + this.lastExitReason) 90 .fontSize('14fp') 91 .width('14%') 92 .fontColor($r('app.color.text_grey')) 93 .opacity(0.6) 94 .textAlign(TextAlign.End); 95 } 96 } 97 }.width('100%').height('48vp').align(Alignment.Center); 98 } 99 .alignListItem(ListItemAlign.Center) 100 .margin({ left: '12vp', right: '12vp', top: '8vp' }) 101 .width('93.3%') 102 .borderRadius('24vp') 103 .backgroundColor($r('sys.color.ohos_id_color_foreground_contrary')) 104 .padding({ left: '12vp', right: '12vp', top: '4vp', bottom: '4vp' }) 105 .divider({ strokeWidth: '0.5vp', color: $r('app.color.text_grey_opacity') }); 106 107 List() { 108 ListItem() { 109 Column() { 110 Row() { 111 Row() { 112 Text($r('app.string.click_counter')).fontSize('16fp') 113 Text(this.counter.toString()).fontSize('16fp') 114 }.align(Alignment.Center).width('76.3%'); 115 Button($r('app.string.counter_name')) 116 .fontSize('12fp') 117 .width('23.7%') 118 .fontColor($r('app.color.text_blue_opacity')) 119 .onClick(() => { 120 this.counter++; 121 this.storage.setOrCreate('FaultTriggerPageCounter', this.counter); 122 this.storage.setOrCreate('FaultTriggerPageString', 'TestFaultTriggerPageString'); 123 }) 124 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 125 .align(Alignment.Center) 126 .height('28vp'); 127 }.align(Alignment.Center).height('48vp'); 128 } 129 }.align(Alignment.Center).padding({ top: '4vp', bottom: '4vp' }).width('100%'); 130 } 131 .height('56vp') 132 .margin({ left: '12vp', right: '12vp', top: '12vp' }) 133 .width('93.3%') 134 .borderRadius('24vp') 135 .backgroundColor($r('sys.color.ohos_id_color_foreground_contrary')) 136 .padding({ left: '12vp', right: '12vp' }); 137 138 Text($r('app.string.recover_settings')) 139 .fontSize('14fp') 140 .margin({ left: '24vp', right: '24vp', top: '19.5vp', bottom: '9.5vp' }) 141 .textAlign(TextAlign.Start) 142 .fontColor($r('app.color.text_grey')) 143 .opacity(0.6) 144 .height('19vp') 145 .width('86.7%'); 146 147 List() { 148 ListItem() { 149 Column() { 150 Row() { 151 Text($r('app.string.save_current_ability_status')).fontSize('16fp').width('76.3%'); 152 Button(this.curAbilitySaveState) 153 .fontSize('12fp') 154 .width('23.7%') 155 .fontColor($r('app.color.text_blue_opacity')) 156 .onClick(() => { 157 appRecovery.saveAppState(CurActiveAbility.GetInstance().GetGlobalAbility().context); 158 ResUtil.getString($r('app.string.save_state_done').id) 159 .then((value: string) => this.curAbilitySaveState = value); 160 }) 161 .height('28vp') 162 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 163 }.height('48vp').width('100%'); 164 } 165 }.width('100%'); 166 167 ListItem() { 168 Column() { 169 Row() { 170 Text($r('app.string.save_all_ability_status')).fontSize('16fp').width('76.3%'); 171 Button(this.allAbilitySaveState) 172 .fontSize('12fp') 173 .width('23.7%') 174 .fontColor($r('app.color.text_blue_opacity')) 175 .onClick(() => { 176 appRecovery.saveAppState(); 177 ResUtil.getString($r('app.string.save_state_done').id) 178 .then((value: string) => this.allAbilitySaveState = value); 179 }) 180 .height('28vp') 181 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 182 }.height('48vp').width('100%'); 183 } 184 }.width('100%'); 185 186 ListItem() { 187 Column() { 188 Row() { 189 Text($r('app.string.set_recovery_ability')).fontSize('16fp').width('76.3%'); 190 Button(this.setRecoveryAbility) 191 .fontSize('12fp') 192 .width('23.7%') 193 .fontColor($r('app.color.text_blue_opacity')) 194 .onClick(() => { 195 let tmp: Record<string, string> = {'RecoverAbility': this.callerAbility } 196 let want: Record<string, Object> = { 197 'bundleName': 'com.samples.recovery', 198 'abilityName': 'RecoveryAbility', 199 'parameters': tmp, 200 }; 201 appRecovery.setRestartWant(want); 202 ResUtil.getString($r('app.string.set_config_done').id).then((value: string) => this.setRecoveryAbility = value); 203 }) 204 .height('28vp') 205 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 206 }.height('48vp').width('100%'); 207 } 208 }.width('100%'); 209 210 if (this.callerAbility === 'EntryAbility') { 211 ListItem() { 212 Column() { 213 Row() { 214 Text('SecondAbility').fontSize('16fp').width('76.3%'); 215 Button($r('app.string.launch_ability')) 216 .fontSize('12fp') 217 .width('23.7%') 218 .fontColor($r('app.color.text_blue_opacity')) 219 .onClick(() => { 220 if (CurActiveAbility.GetInstance().GetGlobalAbility() == undefined) { 221 return; 222 } 223 let want: Record<string, Object> = { 224 'bundleName': 'com.samples.recovery', 225 'abilityName': 'SecondAbility' 226 }; 227 CurActiveAbility.GetInstance().GetGlobalAbility().context.startAbility(want); 228 }) 229 .height('28vp') 230 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 231 }.height('48vp').width('100%'); 232 } 233 }.width('100%'); 234 } 235 } 236 .margin({ left: '12vp', right: '12vp' }) 237 .width('93.3%') 238 .borderRadius('24vp') 239 .backgroundColor($r('sys.color.ohos_id_color_foreground_contrary')) 240 .padding({ left: '12vp', right: '12vp', top: '4vp', bottom: '4vp' }) 241 .divider( 242 { strokeWidth: '0.5vp', color: $r('app.color.text_grey_opacity') } 243 ); 244 245 Text($r('app.string.trigger_fault_items')) 246 .fontSize('14fp') 247 .margin({ left: '24vp', right: '24vp', top: '19.5vp', bottom: '9.5vp' }) 248 .textAlign(TextAlign.Start) 249 .fontColor($r('app.color.text_grey')) 250 .opacity(0.6) 251 .height('19vp') 252 .width('86.7%'); 253 254 List() { 255 ListItem() { 256 Column() { 257 Row() { 258 Text($r('app.string.trigger_js_error')).fontSize('16fp').width('76.3%'); 259 Button($r('app.string.trigger')) 260 .fontSize('12fp') 261 .width('23.7%') 262 .fontColor($r('app.color.text_blue_opacity')) 263 .onClick(() => { 264 throw new Error('Force Crash'); 265 }) 266 .height('28vp') 267 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 268 }.height('48vp').align(Alignment.Center).width('100%'); 269 } 270 }.width('100%'); 271 272 ListItem() { 273 Column() { 274 Row() { 275 Text($r('app.string.trigger_freeze_in_js_code')).fontSize('16fp').width('76.3%'); 276 Button($r('app.string.trigger')) 277 .fontSize('12fp') 278 .width('23.7%') 279 .fontColor($r('app.color.text_blue_opacity')) 280 .onClick(() => { 281 this.doHeavyWork(); 282 }) 283 .height('28vp') 284 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 285 }.height('48vp').align(Alignment.Center).width('100%'); 286 } 287 }.width('100%'); 288 289 ListItem() { 290 Column() { 291 Row() { 292 Text($r('app.string.trigger_freeze_in_native_code')).fontSize('16fp').width('76.3%'); 293 Button($r('app.string.trigger')) 294 .fontSize('12fp') 295 .width('23.7%') 296 .fontColor($r('app.color.text_blue_opacity')) 297 .onClick(() => { 298 testNapi.add(1, 1); 299 }) 300 .height('28vp') 301 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 302 }.height('48vp').align(Alignment.Center).width('100%'); 303 } 304 }.align(Alignment.Center).width('100%'); 305 } 306 .margin({ left: '12vp', right: '12vp' }) 307 .width('93.3%') 308 .borderRadius('24vp') 309 .backgroundColor($r('sys.color.ohos_id_color_foreground_contrary')) 310 .padding({ left: '12vp', right: '12vp', top: '4vp', bottom: '4vp' }) 311 .divider({ strokeWidth: '0.5vp', color: $r('app.color.text_grey_opacity') }); 312 }.backgroundColor($r('sys.color.ohos_id_color_sub_background')).width('100%').height('100%') 313 .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]); 314 } 315} 316