summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Andreas Gampe <agampe@google.com> 2016-11-28 15:07:39 -0800
committer Michael Rosenfeld <mrosenfeld@google.com> 2016-11-29 15:22:02 -0800
commit437474990d53ea96f038205c1dc49ae3a8cc9fc1 (patch)
tree871be853c154ed08b366531057bd5933e57a32c7
parent6063357ae69f29c415591b173cf411f241f129c6 (diff)
Preload2: Add sequence UI
Add a fake UI that can run a sequence of actions (and replies). Bug: 31961946 Test: manual Change-Id: Iee0fac5ddf75a97eff7b401c9c932634489349de
-rw-r--r--tools/preload2/preload-tool2
-rw-r--r--tools/preload2/src/com/android/preload/Main.java35
-rw-r--r--tools/preload2/src/com/android/preload/ui/SequenceUI.java222
3 files changed, 255 insertions, 4 deletions
diff --git a/tools/preload2/preload-tool b/tools/preload2/preload-tool
index 36dbc1c775b1..322b62fda071 100644
--- a/tools/preload2/preload-tool
+++ b/tools/preload2/preload-tool
@@ -34,4 +34,4 @@ PROG_NAME="$(follow_links)"
PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
ANDROID_ROOT=$PROG_DIR/..
-java -cp $ANDROID_ROOT/framework/preload2.jar com.android.preload.Main
+java -cp $ANDROID_ROOT/framework/preload2.jar com.android.preload.Main $@
diff --git a/tools/preload2/src/com/android/preload/Main.java b/tools/preload2/src/com/android/preload/Main.java
index cc54a8d9c715..8e6010559d21 100644
--- a/tools/preload2/src/com/android/preload/Main.java
+++ b/tools/preload2/src/com/android/preload/Main.java
@@ -33,10 +33,13 @@ import com.android.preload.classdataretrieval.ClassDataRetriever;
import com.android.preload.classdataretrieval.hprof.Hprof;
import com.android.preload.classdataretrieval.jdwp.JDWPClassDataRetriever;
import com.android.preload.ui.IUI;
+import com.android.preload.ui.SequenceUI;
import com.android.preload.ui.SwingUI;
-
+import java.io.File;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -90,9 +93,14 @@ public class Main {
* @param args
*/
public static void main(String[] args) {
- Main m = new Main(new SwingUI());
- top = m;
+ Main m;
+ if (args.length > 0 && args[0].equals("--seq")) {
+ m = createSequencedMain(args);
+ } else {
+ m = new Main(new SwingUI());
+ }
+ top = m;
m.startUp();
}
@@ -130,6 +138,27 @@ public class Main {
ui.prepare(clientListModel, dataTableModel, actions);
}
+ /**
+ * @param args
+ * @return
+ */
+ private static Main createSequencedMain(String[] args) {
+ SequenceUI ui = new SequenceUI();
+ Main main = new Main(ui);
+
+ Iterator<String> it = Arrays.asList(args).iterator();
+ it.next(); // --seq
+
+ ui.choice("#" + it.next()); // Device.
+ ui.confirmNo(); // Prepare: no.
+ ui.action(ScanPackageAction.class); // Take hprof dump.
+ ui.client("system_process"); // Select system server.
+ ui.action(ExportAction.class); // Export data.
+ ui.output(new File("/tmp/system_server.data")); // Write to file.
+
+ return main;
+ }
+
public static IUI getUI() {
return top.ui;
}
diff --git a/tools/preload2/src/com/android/preload/ui/SequenceUI.java b/tools/preload2/src/com/android/preload/ui/SequenceUI.java
new file mode 100644
index 000000000000..dc6a4f389b10
--- /dev/null
+++ b/tools/preload2/src/com/android/preload/ui/SequenceUI.java
@@ -0,0 +1,222 @@
+package com.android.preload.ui;
+
+import com.android.ddmlib.Client;
+import com.android.ddmlib.ClientData;
+import java.io.File;
+import java.util.LinkedList;
+import java.util.List;
+import javax.swing.Action;
+import javax.swing.ListModel;
+import javax.swing.table.TableModel;
+
+public class SequenceUI implements IUI {
+
+ private ListModel<Client> clientListModel;
+ @SuppressWarnings("unused")
+ private TableModel dataTableModel;
+ private List<Action> actions;
+
+ private List<Object> sequence = new LinkedList<>();
+
+ public SequenceUI() {
+ }
+
+ @Override
+ public boolean isSingleThreaded() {
+ return true;
+ }
+
+ @Override
+ public void prepare(ListModel<Client> clientListModel, TableModel dataTableModel,
+ List<Action> actions) {
+ this.clientListModel = clientListModel;
+ this.dataTableModel = dataTableModel;
+ this.actions = actions;
+ }
+
+ public SequenceUI action(Action a) {
+ sequence.add(a);
+ return this;
+ }
+
+ public SequenceUI action(Class<? extends Action> actionClass) {
+ for (Action a : actions) {
+ if (actionClass.equals(a.getClass())) {
+ sequence.add(a);
+ return this;
+ }
+ }
+ throw new IllegalArgumentException("No action of class " + actionClass + " found.");
+ }
+
+ public SequenceUI confirmYes() {
+ sequence.add(Boolean.TRUE);
+ return this;
+ }
+
+ public SequenceUI confirmNo() {
+ sequence.add(Boolean.FALSE);
+ return this;
+ }
+
+ public SequenceUI input(String input) {
+ sequence.add(input);
+ return this;
+ }
+
+ public SequenceUI input(File... f) {
+ sequence.add(f);
+ return this;
+ }
+
+ public SequenceUI output(File f) {
+ sequence.add(f);
+ return this;
+ }
+
+ public SequenceUI tableRow(int i) {
+ sequence.add(i);
+ return this;
+ }
+
+ private class ClientSelector {
+ private String pkg;
+
+ public ClientSelector(String pkg) {
+ this.pkg = pkg;
+ }
+
+ public Client getClient() {
+ for (int i = 0; i < clientListModel.getSize(); i++) {
+ ClientData cd = clientListModel.getElementAt(i).getClientData();
+ if (cd != null) {
+ String s = cd.getClientDescription();
+ if (pkg.equals(s)) {
+ return clientListModel.getElementAt(i);
+ }
+ }
+ }
+ throw new RuntimeException("Didn't find client " + pkg);
+ }
+ }
+
+ public SequenceUI client(String pkg) {
+ sequence.add(new ClientSelector(pkg));
+ return this;
+ }
+
+ public SequenceUI choice(String pattern) {
+ sequence.add(pattern);
+ return this;
+ }
+
+ @Override
+ public void ready() {
+ // Run the actions.
+ // No iterator or foreach loop as the sequence will be emptied while running.
+ try {
+ while (!sequence.isEmpty()) {
+ Object next = sequence.remove(0);
+ if (next instanceof Action) {
+ ((Action)next).actionPerformed(null);
+ } else {
+ throw new IllegalStateException("Didn't expect a non-action: " + next);
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace(System.out);
+ }
+
+ // Now shut down.
+ System.exit(0);
+ }
+
+ @Override
+ public Client getSelectedClient() {
+ Object next = sequence.remove(0);
+ if (next instanceof ClientSelector) {
+ return ((ClientSelector)next).getClient();
+ }
+ throw new IllegalStateException("Unexpected: " + next);
+ }
+
+ @Override
+ public int getSelectedDataTableRow() {
+ Object next = sequence.remove(0);
+ if (next instanceof Integer) {
+ return ((Integer)next).intValue();
+ }
+ throw new IllegalStateException("Unexpected: " + next);
+ }
+
+ @Override
+ public void showWaitDialog() {
+ }
+
+ @Override
+ public void updateWaitDialog(String s) {
+ System.out.println(s);
+ }
+
+ @Override
+ public void hideWaitDialog() {
+ }
+
+ @Override
+ public void showMessageDialog(String s) {
+ System.out.println(s);
+ }
+
+ @Override
+ public boolean showConfirmDialog(String title, String message) {
+ Object next = sequence.remove(0);
+ if (next instanceof Boolean) {
+ return ((Boolean)next).booleanValue();
+ }
+ throw new IllegalStateException("Unexpected: " + next);
+ }
+
+ @Override
+ public String showInputDialog(String message) {
+ Object next = sequence.remove(0);
+ if (next instanceof String) {
+ return (String)next;
+ }
+ throw new IllegalStateException("Unexpected: " + next);
+ }
+
+ @Override
+ public <T> T showChoiceDialog(String title, String message, T[] choices) {
+ Object next = sequence.remove(0);
+ if (next instanceof String) {
+ String s = (String)next;
+ for (T t : choices) {
+ if (t.toString().contains(s)) {
+ return t;
+ }
+ }
+ return null;
+ }
+ throw new IllegalStateException("Unexpected: " + next);
+ }
+
+ @Override
+ public File showSaveDialog() {
+ Object next = sequence.remove(0);
+ if (next instanceof File) {
+ System.out.println(next);
+ return (File)next;
+ }
+ throw new IllegalStateException("Unexpected: " + next);
+ }
+
+ @Override
+ public File[] showOpenDialog(boolean multi) {
+ Object next = sequence.remove(0);
+ if (next instanceof File[]) {
+ return (File[])next;
+ }
+ throw new IllegalStateException("Unexpected: " + next);
+ }
+
+}