1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.commands.uiautomator; 18 19import android.accessibilityservice.UiTestAutomationBridge; 20import android.os.Environment; 21 22import com.android.commands.uiautomator.Launcher.Command; 23import com.android.uiautomator.core.AccessibilityNodeInfoDumper; 24 25import java.io.File; 26 27/** 28 * Implementation of the dump subcommand 29 * 30 * This creates an XML dump of current UI hierarchy 31 */ 32public class DumpCommand extends Command { 33 34 private static final File DEFAULT_DUMP_FILE = new File( 35 Environment.getExternalStorageDirectory(), "window_dump.xml"); 36 37 public DumpCommand() { 38 super("dump"); 39 } 40 41 @Override 42 public String shortHelp() { 43 return "creates an XML dump of current UI hierarchy"; 44 } 45 46 @Override 47 public String detailedOptions() { 48 return " dump [file]\n" 49 + " [file]: the location where the dumped XML should be stored, default is\n " 50 + DEFAULT_DUMP_FILE.getAbsolutePath() + "\n"; 51 } 52 53 @Override 54 public void run(String[] args) { 55 File dumpFile = DEFAULT_DUMP_FILE; 56 if (args.length > 0) { 57 dumpFile = new File(args[0]); 58 } 59 UiTestAutomationBridge bridge = new UiTestAutomationBridge(); 60 bridge.connect(); 61 // It appears that the bridge needs time to be ready. Making calls to the 62 // bridge immediately after connecting seems to cause exceptions. So let's also 63 // do a wait for idle in case the app is busy. 64 bridge.waitForIdle(1000, 1000 * 10); 65 AccessibilityNodeInfoDumper.dumpWindowToFile( 66 bridge.getRootAccessibilityNodeInfoInActiveWindow(), dumpFile); 67 bridge.disconnect(); 68 System.out.println( 69 String.format("UI hierchary dumped to: %s", dumpFile.getAbsolutePath())); 70 } 71 72} 73