观心静

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
package rom;

import java.lang.*;
import java.util.Random;
import java.io.File;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import android.R.id;
import android.R.string;
import android.graphics.Rect;
import android.graphics.SumPathEffect;
import android.os.RemoteException;
import android.util.Log;
import com.android.uiautomator.core.UiWatcher;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiDevice;

public class BTtestReceive extends UiAutomatorTestCase{
	public int startapp(String componentName){
		StringBuffer app = new StringBuffer();
		app.append("am start -n");
		app.append(componentName);
		int ret = 1;
		try{
			Process process = Runtime.getRuntime().exec(app.toString());
			ret = process.waitFor();
			
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("startapp Exception!!! ");
		}
		return ret;
	}
	public void Screenshot(String name) { //截取当前屏幕图片的方法
		File file = new File("/mnt/sdcard/Podcasts/log"+name+".png");
		UiDevice.getInstance().takeScreenshot(file);
	}
	public void longclick (UiObject ojb){ //长按方法
		try{
		Rect ojbValue = ojb.getBounds();
		UiDevice.getInstance().swipe(ojbValue.centerX(), ojbValue.centerY(), ojbValue.centerX()+5, ojbValue.centerY()+5, 250);
		}catch(UiObjectNotFoundException e ){
			e.printStackTrace();
		}
	}
	public void testbtReceive() throws UiObjectNotFoundException,RemoteException{
		String btname = "HG11";
		UiDevice.getInstance().sleep();
		sleep(1000);
		UiDevice.getInstance().wakeUp();
		sleep(1000);
		try{
			UiObject Unlocked = new UiObject(new UiSelector().resourceId("com.android.systemui:id/notification_stack_scroller"));
			assertTrue("Unlocked fail!...",Unlocked.exists());
			Unlocked.swipeUp(25);
			sleep(1000);
			}catch(UiObjectNotFoundException e1){
				e1.printStackTrace();
				Log.d("Unlocked", "Exception!!!", e1);
				System.out.println("Unlocked Exception!!!");
				Screenshot("Unlocked");
				UiDevice.getInstance().pressBack();
				sleep(1000);
				UiDevice.getInstance().sleep();
				sleep(1000);
				UiDevice.getInstance().wakeUp();
				sleep(1000);
			}
		for(int i=0;i<50;i++){
		UiDevice.getInstance().pressBack();
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
		startapp("com.android.settings/com.android.settings.Settings");
		sleep(1000);
		UiObject bt = new UiObject(new UiSelector().text("蓝牙").resourceId("android:id/title"));
		assertTrue("bt fail!....",bt.exists());
		bt.clickAndWaitForNewWindow();
		sleep(1000);
		UiObject Pop_ups = new UiObject(new UiSelector().text("要与"+btname+"配对吗?").resourceId("android:id/alertTitle"));
		sleep(1000);
		do {
			sleep(1000);
		} while (!Pop_ups.exists());
		sleep(1000);
		UiObject pair = new UiObject(new UiSelector().text("配对").resourceId("android:id/button1"));
		Screenshot("pair_"+i);
		pair.clickAndWaitForNewWindow();
		sleep(1000);
		UiDevice.getInstance().openNotification();
		sleep(3000);
		UiObject Wait_for_a_file = new UiObject(new UiSelector().text("有人发送文件给您").resourceId("android:id/title"));
		do {
			sleep(1000);
		} while (!Wait_for_a_file.exists());
		sleep(1000);
		UiObject receive = new UiObject(new UiSelector().text("接受").resourceId("android:id/action0").description("接受"));
		assertTrue("receive fail!....", receive.exists());
		receive.click();
		sleep(1000);
		UiDevice.getInstance().openNotification();
		sleep(1000);
		Screenshot("Share_Bluetooth_"+i);
		sleep(1000);
		UiObject Share_progress1 = new UiObject(new UiSelector().resourceId("android:id/progress")
				.className("android.widget.ProgressBar"));
		for(int j=0;j<5;j++){ //判断蓝牙文件是否传输完毕
		do {
			sleep(1000);
		} while (Share_progress1.exists());
		}
		Screenshot("Share_progress_End_"+i);
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
		UiDevice.getInstance().pressBack();
		sleep(1000);
		startapp("com.android.settings/com.android.settings.Settings"); //再次进入设置
		sleep(1000);
		UiObject bt2 = new UiObject(new UiSelector().text("蓝牙").resourceId("android:id/title")); //点击蓝牙
		assertTrue("bt fail!....",bt2.exists());
		bt2.clickAndWaitForNewWindow();
		UiObject Bluetooth_settings = new UiObject(new UiSelector().description("设备设置") //点击连接的蓝牙设置
				.resourceId("com.android.settings:id/deviceDetails"));
		assertTrue("Bluetooth_settings fail!...",Bluetooth_settings.exists());
		Bluetooth_settings.clickAndWaitForNewWindow();
		sleep(1000);
		UiObject Cancel_save = new UiObject(new UiSelector().text("取消保存").resourceId("android:id/button2"));  //取消已经连接的蓝牙
		Cancel_save.clickAndWaitForNewWindow();
		sleep(1000);
		Screenshot("Cancel_save_"+i);
		sleep(1000);
		}
	
	}

}

posted on 2017-07-26 14:52  观心静  阅读(253)  评论(0编辑  收藏  举报