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 Log from './Log' 17import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from '@ohos/hypium' 18import { Driver, MatchPattern, ON } from '@ohos.UiTest' 19import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry' 20 21export default function abilityTest() { 22 const BUNDLE = 'MediaProvider_'; 23 let driver = Driver.create(); 24 25 describe('ActsAbilityTest', () => { 26 /** 27 * Start ability 28 */ 29 it(BUNDLE + 'StartAbility_001', 0, async (done: Function) => { 30 Log.info(BUNDLE + `StartAbility_001 begin`); 31 let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); 32 try { 33 await abilityDelegator.startAbility({ 34 bundleName: 'com.samples.mediaprovider', 35 abilityName: 'EntryAbility' 36 }) 37 expect(true).assertTrue(); 38 } catch (error) { 39 Log.info(`Failed to start ability, error info: ${error}`); 40 expect().assertFail(); 41 } 42 Log.info(BUNDLE + `StartAbility_001 end`); 43 done(); 44 }) 45 46 /** 47 * [Index] 48 * After start the ability, check if text elements exist 49 */ 50 it(BUNDLE + 'CheckPageElement_001', 0, async (done: Function) => { 51 Log.info(BUNDLE + `CheckPageElement_001 begin`); 52 try { 53 driver = Driver.create(); 54 await driver.delayMs(1000); 55 await driver.assertComponentExist(ON.id('Title')); 56 await driver.assertComponentExist(ON.id('Artist')); 57 await driver.assertComponentExist(ON.id('Lyric')); 58 expect(true).assertTrue(); 59 } catch (error) { 60 Log.info(`Failed to get text elements, error info: ${error}`); 61 expect().assertFail(); 62 } 63 Log.info(BUNDLE + `CheckPageElement_001 end`); 64 done(); 65 }) 66 67 /** 68 * [Index] 69 * After start the ability, check if Button elements exist 70 */ 71 it(BUNDLE + 'CheckPageElement_002', 0, async (done: Function) => { 72 Log.info(BUNDLE + `CheckPageElement_002 begin`); 73 try { 74 await driver.delayMs(1000); 75 await driver.assertComponentExist(ON.id('Previous')); 76 await driver.assertComponentExist(ON.id('PlayOrPause')); 77 await driver.assertComponentExist(ON.id('Next')); 78 expect(true).assertTrue(); 79 } catch (error) { 80 Log.info(`Failed to get button elements, error info: ${error}`); 81 expect().assertFail(); 82 } 83 Log.info(BUNDLE + `CheckPageElement_002 end`); 84 done(); 85 }) 86 87 /** 88 * [Index] 89 * Click PlayOrPause button 90 */ 91 it(BUNDLE + 'PlayThenPause_001', 0, async (done: Function) => { 92 Log.info(BUNDLE + `PlayThenPause_001 begin`); 93 try { 94 await driver.delayMs(1000); 95 let pageTitleText = await driver.findComponent(ON.id('PageTitle')); 96 let playOrPauseButton = await driver.findComponent(ON.id('PlayOrPause')); 97 await playOrPauseButton.click(); 98 let pageTitle = await pageTitleText.getText(); 99 Log.info(`After click play button, the page title is ${pageTitle}`); 100 expect(pageTitle === '正在播放').assertTrue(); 101 await playOrPauseButton.click(); 102 pageTitle = await pageTitleText.getText(); 103 Log.info(`After click pause button, the page title is ${pageTitle}`); 104 expect(pageTitle === '未在播放').assertTrue(); 105 await driver.delayMs(1000); 106 } catch (error) { 107 Log.info(`Failed to get all page element, error info: ${error}`); 108 expect().assertFail(); 109 } 110 Log.info(BUNDLE + `PlayThenPause_001 end`); 111 done(); 112 }) 113 114 /** 115 * [Index] 116 * Click previous button 117 */ 118 it(BUNDLE + 'PreviousMusic_001', 0, async (done: Function) => { 119 Log.info(BUNDLE + `PreviousMusic_001 begin`); 120 try { 121 await driver.delayMs(1000); 122 let titleText = await driver.findComponent(ON.id('Title')); 123 let currentTitle = await titleText.getText(); 124 let previousButton = await driver.findComponent(ON.id('Previous')); 125 await previousButton.click(); 126 await driver.delayMs(1000); 127 let playOrPauseButton = await driver.findComponent(ON.id('PlayOrPause')); 128 await playOrPauseButton.click(); 129 let previousTitle = await titleText.getText(); 130 Log.error(`a: ${currentTitle} b: ${previousTitle}`); 131 expect(currentTitle !== previousTitle).assertTrue(); 132 } catch (error) { 133 Log.info(`Failed to play previous music, error info: ${error}`); 134 expect().assertFail(); 135 } 136 Log.info(BUNDLE + `PreviousMusic_001 end`); 137 done(); 138 }) 139 140 /** 141 * [Index] 142 * Click next button 143 */ 144 it(BUNDLE + 'NextMusic_001', 0, async (done: Function) => { 145 Log.info(BUNDLE + `NextMusic_001 begin`); 146 try { 147 await driver.delayMs(1000); 148 let titleText = await driver.findComponent(ON.id('Title')); 149 let currentTitle = await titleText.getText(); 150 let nextButton = await driver.findComponent(ON.id('Next')); 151 await nextButton.click(); 152 await driver.delayMs(1000); 153 let playOrPauseButton = await driver.findComponent(ON.id('PlayOrPause')); 154 await playOrPauseButton.click(); 155 let nextTitle = await titleText.getText(); 156 expect(currentTitle !== nextTitle).assertTrue(); 157 } catch (error) { 158 Log.info(`Failed to play next music, error info: ${error}`); 159 expect().assertFail(); 160 } 161 Log.info(BUNDLE + `NextMusic_001 end`); 162 done(); 163 }) 164 }) 165}