12. If you can’t measure it,
you can’t manage it.
Peter F. Drucker
(1909~2005)
13. If you can’t measure it,
you can’t manage it.
Peter F. Drucker
(1909~2005)
If you can’t automate
the measurement of it,
it is difficult to improve it.
22. How to Track Activity Navigation & Lifecycle?
Override onCreate, onStart, …
of your Activity classes
23. How to Track Activity Navigation & Lifecycle?
Modify android.app.Activity
in the framework
24. How to Track Activity Navigation & Lifecycle?
Is there a way that does not
require the modification of
an application or framework?
25. How to Track Activity Navigation & Lifecycle?
Is there a way that does not
require the modification of
an application or framework?
Instrumentation
26.
27. Android instrumentation is a set of control methods
or "hooks" in the Android system.
These hooks control an Android component
independently of its normal lifecycle.
They also control how Android loads applications.
(http://developer.android.com/tools/testing/testing_android.html)
28. Historical Analysis w/ kmemtracer
Application
Package
Control
android.test.
Instrumentation
TestRunner
Test
Package
29. Historical Analysis w/ kmemtracer
https://github.com/snailee/kmemtracer-libs
Application
Package
Control
Track
android.test.
Instrumentation
TestRunner
org.kandroid.
memtracer.Memory
Instrumentation
Test
Package
Trace
Package
35. How to Use kmemtracer?
1. Create an Android Test Project for the trace package.
36. How to Use kmemtracer?
1. Create an Android Test Project for the trace package.
2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.
37. How to Use kmemtracer?
1. Create an Android Test Project for the trace package.
2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.
3. Edit the <instrumentation> element in the Manifest file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.apis.test"
android:versionCode="1"
android:versionName="1.0">
<instrumentation
android:name="org.kandroid.memtracer.MemoryInstrumentation"
android:targetPackage="com.example.android.apis" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher“>
<uses-library android:theme="android.text.runner“>
</application>
</manifest>
38. How to Use kmemtracer?
4. Install the trace package as well as the application package.
$ adb install –r ApiDemos.apk
$ adb install –r ApiDemosTest.apk
39. How to Use kmemtracer?
4. Install the trace package as well as the application package.
$ adb install –r ApiDemos.apk
$ adb install –r ApiDemosTest.apk
5. Start the instrumentation with ‘am instrument’ in the shell.
$
>
>
>
adb shell am instrument -e class
com.example.android.apis.ApiDemos
com.example.android.apis.tests/
org.kandroid.memtracer.MemoryInstrumentation
40. How to Use kmemtracer?
4. Install the trace package as well as the application package.
$ adb install –r ApiDemos.apk
$ adb install –r ApiDemosTest.apk
5. Start the instrumentation with ‘am instrument’ in the shell.
$
>
>
>
adb shell am instrument -e class
com.example.android.apis.ApiDemos
com.example.android.apis.tests/
org.kandroid.memtracer.MemoryInstrumentation
6. Do interact with the application.
41. How to Use kmemtracer?
4. Install the trace package as well as the application package.
$ adb install –r ApiDemos.apk
$ adb install –r ApiDemosTest.apk
5. Start the instrumentation with ‘am instrument’ in the shell.
$
>
>
>
adb shell am instrument -e class
com.example.android.apis.ApiDemos
com.example.android.apis.tests/
org.kandroid.memtracer.MemoryInstrumentation
6. Do interact with the application.
7. Pull the trace file ‘kmemtrace.csv’ from /sdcard/kmemtracer.
$ adb pull /sdcard/kmemtracer/kmemtrace.csv .
42. How to Use kmemtracer?
8. Open the trace file with Excel and create charts.
43. How to Automate Performance Testing?
- Historical Analysis w/ kmemtracer
- Measuring Performance
- Controlling UI
44. What to Measure?
Rendering (or GUI) Performance
Frame Rate (FPS), Frame Time , Jankiness, Input Latency, …
Resource Consumption
Memory, CPU, I/O (Disk, Network), Battery, …
Network Performance
Response time, Throughput, …
…
45. What to Measure?
Rendering (or GUI) Performance
Frame Rate (FPS), Frame Time , Jankiness, Input Latency, …
Resource Consumption
Memory, CPU, I/O (Disk, Network), Battery, …
Network Performance
Response time, Throughput, …
…
75. MemoryTracer$addSnapshot
public Bundle addSnapshot(String label) {
...
long nativeMax = Debug.getNativeHeapSize() / 1024;
long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memInfo);
...
Get usage data
76. MemoryTracer$addSnapshot
public Bundle addSnapshot(String label) {
...
long nativeMax = Debug.getNativeHeapSize() / 1024;
long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memInfo);
...
Bundle snapshot = new Bundle();
snapshot.putString(METRIC_KEY_LABEL, label);
...
snapshot.putLong(METRIC_KEY_NATIVE_SIZE, nativeMax);
snapshot.putLong(METRIC_KEY_NATIVE_ALLOCATED, nativeAllocated);
snapshot.putLong(METRIC_KEY_NATIVE_FREE, nativeFree);
...
Get usage data
Save data to
a bundle
77. MemoryTracer$addSnapshot
public Bundle addSnapshot(String label) {
...
long nativeMax = Debug.getNativeHeapSize() / 1024;
long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memInfo);
...
Bundle snapshot = new Bundle();
snapshot.putString(METRIC_KEY_LABEL, label);
...
snapshot.putLong(METRIC_KEY_NATIVE_SIZE, nativeMax);
snapshot.putLong(METRIC_KEY_NATIVE_ALLOCATED, nativeAllocated);
snapshot.putLong(METRIC_KEY_NATIVE_FREE, nativeFree);
...
if (mResultsWriter != null) {
mResultsWriter.writeTraceSnapshot(snapshot);
}
return snapshot;
}
Get usage data
Save data to
a bundle
Write date using
ResultsWriter
80. MemoryTracerCsvWriter$writeTraceSnapshot
public class MemoryTraceCsvWriter implements MemoryTracer.ResultsWriter {
...
public MemoryTraceCsvWriter(String[] metricKeys) {
mMetricKeys = metricKeys;
}
...
private void openTraceFile(String filename) {
...
mTraceOut = new PrintStream(new FileOutputStream(traceFile));
...
}
...
Create an instance
with metrics to print
Open a trace file in
the sdcard at init
81. MemoryTracerCsvWriter$writeTraceSnapshot
public class MemoryTraceCsvWriter implements MemoryTracer.ResultsWriter {
...
public MemoryTraceCsvWriter(String[] metricKeys) {
mMetricKeys = metricKeys;
}
...
private void openTraceFile(String filename) {
...
mTraceOut = new PrintStream(new FileOutputStream(traceFile));
...
}
...
@Override
public void writeTraceSnapshot(Bundle snapshot) {
PrintStream out = mTraceOut;
for (String key : mMetricKeys) {
out.print(snapshot.get(key));
out.print(',');
}
out.println();
out.flush();
}
...
}
Create an instance
with metrics to print
Open a trace file in
the sdcard at init
Write snapshot to
a line of the file
84. To Do’s
Add more tracers
CpuTracer
IOTracer
BatteryTracer
Make metrics to gather configurable
85. To Do’s
Add more tracers
CpuTracer
IOTracer
BatteryTracer
Make metrics to gather configurable
Make trace output destination configurable
Trace out to internal file (not on sdcard)
Trace out to an analytics server
86. To Do’s
Add more tracers
CpuTracer
IOTracer
BatteryTracer
Make metrics to gather configurable
Make trace output destination configurable
Trace out to internal file (not on sdcard)
Trace out to an analytics server
Welcome contributions
https://github.com/snailee/kmemtracer-libs
87. How to Automate Performance Testing?
- Historical Analysis w/ kmemtracer
- Measuring Performance
- Controlling UI
105. Controlling UI Using Instrumentation
1. Create an Android Test Project for the test package.
$ android create test-project --main ../ApiDemos
> –-name ApiDemosInstrumentTest --path ApiDemosInstrumentTest
106. Controlling UI Using Instrumentation
1. Create an Android Test Project for the test package.
$ android create test-project --main ../ApiDemos
> –-name ApiDemosInstrumentTest --path ApiDemosInstrumentTest
2. Add a test class inheriting ActivityInstrumentationTestCase2.
public class ApiDemosInstrumentTest extends
ActivityInstrumentationTestCase2<ApiDemos> {
...
public ApiDemosInstrumentTest() {
super(ApiDemos.class);
}
107. Controlling UI Using Instrumentation
1. Create an Android Test Project for the test package.
$ android create test-project --main ../ApiDemos
> –-name ApiDemosInstrumentTest --path ApiDemosInstrumentTest
2. Add a test class inheriting ActivityInstrumentationTestCase2.
public class ApiDemosInstrumentTest extends
ActivityInstrumentationTestCase2<ApiDemos> {
...
public ApiDemosInstrumentTest() {
super(ApiDemos.class);
}
3. Add a test method and launch the activity.
public void testNavigate() {
final ApiDemos apiDemos = getActivity();
...
}
108. Controlling UI Using Instrumentation
4. Find and act on a control.
public void testNavigate() {
109. Controlling UI Using Instrumentation
4. Find and act on a control.
Find a view
to control
public void testNavigate() {
...
final ListView demoList = apiDemos.getListView();
final View graphicsDemo = getChildViewByText(demoList, "Graphics");
assertNotNull("Graphics demo should exist", graphicsDemo);
110. Controlling UI Using Instrumentation
4. Find and act on a control.
Find a view
to control
public void testNavigate() {
...
final ListView demoList = apiDemos.getListView();
final View graphicsDemo = getChildViewByText(demoList, "Graphics");
assertNotNull("Graphics demo should exist", graphicsDemo);
apiDemos.runOnUiThread(new Runnable() {
public void run() {
int pos = demoList.getPositionForView(graphicsDemo);
demoList.smoothScrollToPosition(pos);
demoList.setSelection(pos);
demoList.performItemClick(graphicsDemo, pos,
graphicsDemo.getId());
}
});
...
Act on
the view
111. Controlling UI Using Instrumentation
4. Find and act on a control.
Find a view
to control
public void testNavigate() {
...
final ListView demoList = apiDemos.getListView();
final View graphicsDemo = getChildViewByText(demoList, "Graphics");
assertNotNull("Graphics demo should exist", graphicsDemo);
apiDemos.runOnUiThread(new Runnable() {
public void run() {
int pos = demoList.getPositionForView(graphicsDemo);
demoList.smoothScrollToPosition(pos);
demoList.setSelection(pos);
demoList.performItemClick(graphicsDemo, pos,
graphicsDemo.getId());
}
});
...
Act on
the view
112. Controlling UI Using Instrumentation
4. Find and act on a control: getChildViewByText.
private View getChildViewByText(ListView listView, String text) {
113. Controlling UI Using Instrumentation
4. Find and act on a control: getChildViewByText.
private View getChildViewByText(ListView listView, String text) {
View view = null;
int count = listView.getCount();
Log.e(TAG, “no. children = " + count);
for (int i = 0; i < count; i++) {
TextView textView = (TextView) listView.getChildAt(i);
Log.e(TAG, i + "-th text view is " + textView);
Iterate through
child views
114. Controlling UI Using Instrumentation
4. Find and act on a control: getChildViewByText.
private View getChildViewByText(ListView listView, String text) {
View view = null;
int count = listView.getCount();
Log.e(TAG, “no. children = " + count);
for (int i = 0; i < count; i++) {
TextView textView = (TextView) listView.getChildAt(i);
Log.e(TAG, i + "-th text view is " + textView);
if (textView != null && textView.getText().equals(text)) {
view = textView;
break;
}
Iterate through
child views
Compare
texts
115. Controlling UI Using Instrumentation
4. Find and act on a control: getChildViewByText.
private View getChildViewByText(ListView listView, String text) {
View view = null;
int count = listView.getCount();
Log.e(TAG, “no. children = " + count);
for (int i = 0; i < count; i++) {
TextView textView = (TextView) listView.getChildAt(i);
Log.e(TAG, i + "-th text view is " + textView);
if (textView != null && textView.getText().equals(text)) {
view = textView;
break;
}
}
return view;
}
Iterate through
child views
Compare
texts
116. Controlling UI Using Instrumentation
4. Find and act on a control: getChildViewByText.
private View getChildViewByText(ListView listView, String text) {
View view = null;
int count = listView.getCount();
Log.e(TAG, “no. children = " + count);
for (int i = 0; i < count; i++) {
TextView textView = (TextView) listView.getChildAt(i);
Log.e(TAG, i + "-th text view is " + textView);
if (textView != null && textView.getText().equals(text)) {
view = textView;
break;
}
}
return view;
}
com.example.android.apis.instrument.test.ApiDemosInstrumentTest:
INSTRUMENTATION_RESULT: shortMsg=junit.framework.AssertionFailedError
INSTRUMENTATION_RESULT: longMsg=
junit.framework.AssertionFailedError: OpenGL ES demo should exist
INSTRUMENTATION_CODE: 0
Iterate through
child views
Compare
texts
117. Controlling UI Using Instrumentation
4. Find and act on a control: Revised getChildViewByText.
private View getChildViewByText(final ListView listView, String text) {
...
for (int i = 0; i < count; i++) {
TextView textView = (TextView) listView.getChildAt(i);
if (textView != null && textView.getText().equals(text)) {
view = textView;
break;
}
if (textView == null) {
final int lastPos = listView.getLastVisiblePosition();
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
listView.smoothScrollToPosition(lastPos + 5, lastPos);
}
});
getInstrumentation().waitForIdleSync();
i = listView.getFirstVisiblePosition();
}
}
return view;
}
118. Controlling UI Using Instrumentation
4. Find and act on a control: Revised getChildViewByText.
private View getChildViewByText(final ListView listView, String text) {
...
for (int i = 0; i < count; i++) {
TextView textView = (TextView) listView.getChildAt(i);
if (textView != null && textView.getText().equals(text)) {
view = textView;
break;
}
if (textView == null) {
final int lastPos = listView.getLastVisiblePosition();
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
listView.smoothScrollToPosition(lastPos + 5, lastPos);
}
});
getInstrumentation().waitForIdleSync();
i = listView.getFirstVisiblePosition();
}
}
return view;
}
Scroll down
119. Controlling UI Using Instrumentation
5. Monitor a new Activity to launch.
public void testNavigate() {
...
Instrumentation.ActivityMonitor graphicsDemoActivityMonitor =
getInstrumentation().addMonitor(
ApiDemos.class.getName(), null, false);
final ApiDemos graphicsDemoActivity =
(ApiDemos) graphicsDemoActivityMonitor.waitForActivity();
assertNotNull("Graphics Demo (ApiDemos) activity should not be null",
graphicsDemoActivity);
...
120. Controlling UI Using Instrumentation
5. Monitor a new Activity to launch.
public void testNavigate() {
...
Instrumentation.ActivityMonitor graphicsDemoActivityMonitor =
getInstrumentation().addMonitor(
ApiDemos.class.getName(), null, false);
final ApiDemos graphicsDemoActivity =
(ApiDemos) graphicsDemoActivityMonitor.waitForActivity();
assertNotNull("Graphics Demo (ApiDemos) activity should not be null",
graphicsDemoActivity);
...
6. Iterate step 4 & 5.
121. Controlling UI Using Instrumentation
7. Build and Install the test package.
$ cd ApiDemosInstrumentTest
$ ant debug
$ adb install –r bin/ApiDemosInstrumentTest-debug.apk
122. Controlling UI Using Instrumentation
7. Build and Install the test package.
$ cd ApiDemosInstrumentTest
$ ant debug
$ adb install –r bin/ApiDemosInstrumentTest-debug.apk
8. Run the test and see the result.
$ adb shell am instrument -w
> com.example.android.apis.instrument.test/
> android.test.InstrumentationTestRunner
123. Controlling UI Using Instrumentation
7. Build and Install the test package.
$ cd ApiDemosInstrumentTest
$ ant debug
$ adb install –r bin/ApiDemosInstrumentTest-debug.apk
8. Run the test and see the result.
$ adb shell am instrument -w
> com.example.android.apis.instrument.test/
> android.test.InstrumentationTestRunner
$ ant test
125. Controlling UI Using Robotium
1. Create an Android Test Project for the test package.
$ android create test-project --main ../ApiDemos
> –-name ApiDemosRobotiumTest --path ApiDemosRobotiumTest
126. Controlling UI Using Robotium
1. Create an Android Test Project for the test package.
$ android create test-project --main ../ApiDemos
> –-name ApiDemosRobotiumTest --path ApiDemosRobotiumTest
2. Add ‘robotium-solo-<ver>.jar’ in the ‘libs’ directory.
127. Controlling UI Using Robotium
1. Create an Android Test Project for the test package.
$ android create test-project --main ../ApiDemos
> –-name ApiDemosRobotiumTest --path ApiDemosRobotiumTest
2. Add ‘robotium-solo-<ver>.jar’ in the ‘libs’ directory.
3. Add a test class inheriting ActivityInstrumentationTestCase2.
public class ApiDemosRobotiumTest extends
ActivityInstrumentationTestCase2<ApiDemos> {
...
public ApiDemosRobotiumTest() {
super(ApiDemos.class);
}
...
128. Controlling UI Using Robotium
4. Create an instance of Solo in the setUp method and
close open activities in the tearDown method.
...
private Solo solo;
...
@Override
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
...
129. Controlling UI Using Robotium
4. Find and act on a control.
public void testNavigate() {
solo.assertCurrentActivity("Should be on ApiDemos",
ApiDemos.class);
solo.clickOnText("^Graphics$", 1, true);
solo.assertCurrentActivity("Should be on a new ApiDemos",
ApiDemos.class, true);
solo.clickOnText("^OpenGL ES$", 1, true);
solo.assertCurrentActivity("Should be on a new ApiDemos",
ApiDemos.class, true);
solo.clickOnText("^Kube$", 1, true);
solo.assertCurrentActivity("Should be on a Kube",
Kube.class, true);
solo.sleep(3000);
}
130. Controlling UI Using Robotium
6. Build and Install the test package.
$ cd ApiDemosRobotiumTest
$ ant debug
$ adb install –r bin/ApiDemosRobotiumTest-debug.apk
131. Controlling UI Using Robotium
6. Build and Install the test package.
$ cd ApiDemosRobotiumTest
$ ant debug
$ adb install –r bin/ApiDemosRobotiumTest-debug.apk
7. Run the test and see the result.
$ adb shell am instrument -w
> com.example.android.apis.robotium.test/
> android.test.InstrumentationTestRunner
132. Controlling UI Using Robotium
6. Build and Install the test package.
$ cd ApiDemosRobotiumTest
$ ant debug
$ adb install –r bin/ApiDemosRobotiumTest-debug.apk
7. Run the test and see the result.
$ adb shell am instrument -w
> com.example.android.apis.robotium.test/
> android.test.InstrumentationTestRunner
$ ant test
134. Controlling UI Using uiautomator
1. Create an Java Project for the test jar.
2. Add ‘android.jar’ and ‘uiautomator.jar’ in the build path.
135. Controlling UI Using uiautomator
1. Create an Java Project for the test jar.
2. Add ‘android.jar’ and ‘uiautomator.jar’ in the build path.
3. Add a test class inheriting UiAutomatorTestCase.
public class ApiDemosUiAutoTest extends UiAutomatorTestCase {
...
}
136. Controlling UI Using uiautomator
4. Launch ApiDemos.
public void testNavigate() throws UiObjectNotFoundException {
137. Controlling UI Using uiautomator
4. Launch ApiDemos.
public void testNavigate() throws UiObjectNotFoundException {
getUiDevice().pressHome();
Press home button
138. Controlling UI Using uiautomator
4. Launch ApiDemos.
public void testNavigate() throws UiObjectNotFoundException {
getUiDevice().pressHome();
UiObject allAppsButton = new UiObject(
new UiSelector().description("Apps"));
allAppsButton.clickAndWaitForNewWindow();
Press home button
Click ‘All Apps’
button
139. Controlling UI Using uiautomator
4. Launch ApiDemos.
public void testNavigate() throws UiObjectNotFoundException {
getUiDevice().pressHome();
UiObject allAppsButton = new UiObject(
new UiSelector().description("Apps"));
allAppsButton.clickAndWaitForNewWindow();
Press home button
Click ‘All Apps’
button
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
appsTab.click();
Click ‘Apps’ tab
140. Controlling UI Using uiautomator
4. Launch ApiDemos.
public void testNavigate() throws UiObjectNotFoundException {
getUiDevice().pressHome();
UiObject allAppsButton = new UiObject(
new UiSelector().description("Apps"));
allAppsButton.clickAndWaitForNewWindow();
Press home button
Click ‘All Apps’
button
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
appsTab.click();
Click ‘Apps’ tab
UiScrollable appList = new UiScrollable(
new UiSelector().scrollable(true));
appList.setAsHorizontalList();
UiObject apiDemos = appList.getChildByText(
new UiSelector().className(TextView.class.getName()), "API Demos");
apiDemos.clickAndWaitForNewWindow();
Click ‘ApiDemos’
icon
141. Controlling UI Using uiautomator
4. Launch ApiDemos.
public void testNavigate() throws UiObjectNotFoundException {
getUiDevice().pressHome();
UiObject allAppsButton = new UiObject(
new UiSelector().description("Apps"));
allAppsButton.clickAndWaitForNewWindow();
Press home button
Click ‘All Apps’
button
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
appsTab.click();
Click ‘Apps’ tab
UiScrollable appList = new UiScrollable(
new UiSelector().scrollable(true));
appList.setAsHorizontalList();
UiObject apiDemos = appList.getChildByText(
new UiSelector().className(TextView.class.getName()), "API Demos");
apiDemos.clickAndWaitForNewWindow();
Click ‘ApiDemos’
icon
Validate the launch
of ‘ApiDemos’
UiObject apiDemosPackage = new UiObject(
new UiSelector().packageName("com.example.android.apis"));
assertTrue(“Should be on ApiDemos", apiDemosPackage.exists());
...
143. Controlling UI Using uiautomator
5. Find and act on a control.
...
UiScrollable demoList = new UiScrollable(
new UiSelector().className("android.widget.ListView"));
144. Controlling UI Using uiautomator
5. Find and act on a control.
...
UiScrollable demoList = new UiScrollable(
new UiSelector().className("android.widget.ListView"));
UiObject graphicsDemo = demoList.getChildByText(
new UiSelector().className(TextView.class.getName()), “Graphics");
145. Controlling UI Using uiautomator
5. Find and act on a control.
...
UiScrollable demoList = new UiScrollable(
new UiSelector().className("android.widget.ListView"));
UiObject graphicsDemo = demoList.getChildByText(
new UiSelector().className(TextView.class.getName()), “Graphics");
graphicsDemo.clickAndWaitForNewWindow();
146. Controlling UI Using uiautomator
5. Find and act on a control.
...
UiScrollable demoList = new UiScrollable(
new UiSelector().className("android.widget.ListView"));
UiObject graphicsDemo = demoList.getChildByText(
new UiSelector().className(TextView.class.getName()), “Graphics");
graphicsDemo.clickAndWaitForNewWindow();
UiObject alphaBitmapDemo = new UiObject(
new UiSelector().text(“AlphaBitmap"));
assertTrue(“AlphaBitmap should be visible", alpahBitmapDemo.exists());
...
147. Controlling UI Using uiautomator
5. Find and act on a control.
...
UiScrollable demoList = new UiScrollable(
new UiSelector().className("android.widget.ListView"));
UiObject graphicsDemo = demoList.getChildByText(
new UiSelector().className(TextView.class.getName()), “Graphics");
graphicsDemo.clickAndWaitForNewWindow();
UiObject alphaBitmapDemo = new UiObject(
new UiSelector().text(“AlphaBitmap"));
assertTrue(“AlphaBitmap should be visible", alpahBitmapDemo.exists());
...
6. Iterate step 5.
148. Controlling UI Using uiautomator
7. Compile your test case into a JAR file.
$
>
$
$
android create uitest-project --name ApiDemosUiAutoTest
--target 1 --path ApiDemosUiAutoTest
cd ApiDemosUiAutoTest
ant build
149. Controlling UI Using uiautomator
7. Compile your test case into a JAR file.
$
>
$
$
android create uitest-project --name ApiDemosUiAutoTest
--target 1 --path ApiDemosUiAutoTest
cd ApiDemosUiAutoTest
ant build
8. Install the jar on your test device.
$ adb push bin/ApiDemosUiAutoTest.jar /data/local/tmp/
150. Controlling UI Using uiautomator
7. Compile your test case into a JAR file.
$
>
$
$
android create uitest-project --name ApiDemosUiAutoTest
--target 1 --path ApiDemosUiAutoTest
cd ApiDemosUiAutoTest
ant build
8. Install the jar on your test device.
$ adb push bin/ApiDemosUiAutoTest.jar /data/local/tmp/
9. Run the test and view the test result.
$ adb shell uiautomator runtest ApiDemosUiAutoTest.jar
> –c com.example.android.apis.uiauto.test.ApiDemosUiAutoTest
151. Controlling UI Using uiautomator
7. Compile your test case into a JAR file.
$
>
$
$
android create uitest-project --name ApiDemosUiAutoTest
--target 1 --path ApiDemosUiAutoTest
cd ApiDemosUiAutoTest
ant build
8. Install the jar on your test device.
$ adb push bin/ApiDemosUiAutoTest.jar /data/local/tmp/
9. Run the test and view the test result.
$ adb shell uiautomator runtest ApiDemosUiAutoTest.jar
> –c com.example.android.apis.uiauto.test.ApiDemosUiAutoTest
$ ant test
152. Controlling UI Using uiautomator
7. Compile your test case into a JAR file.
$
>
$
$
android create uitest-project --name ApiDemosUiAutoTest
--target 1 --path ApiDemosUiAutoTest
cd ApiDemosUiAutoTest
ant build
8. Install the jar on your test device.
$ adb push bin/ApiDemosUiAutoTest.jar /data/local/tmp/
9. Run the test and view the test result.
$ adb shell uiautomator runtest ApiDemosUiAutoTest.jar
> –c com.example.android.apis.uiauto.test.ApiDemosUiAutoTest
$ ant test
154. Doing both Controlling and Tracking
1. Modify org.kandroid.memtracer.MemoryInstrumentation.
public class MemoryInstrumentation extends InstrumentationTestRunner {
...
@Override
public void onCreate(Bundle arguments) {
...
start();
super.onCreate(arguments);
}
@Override
public void onStart() {
String mainActivityName = getMainActivityName();
if (mainActivityName != null && mainActivityName.length() > 0) {
launchMainActivity(getTargetContext().getPackageName(),
getMainActivityName());
} else {
super.onStart();
}
}
...
155. Doing both Controlling and Tracking
1. Modify org.kandroid.memtracer.MemoryInstrumentation.
public class MemoryInstrumentation extends InstrumentationTestRunner {
...
@Override
public void onCreate(Bundle arguments) {
...
start();
super.onCreate(arguments);
}
@Override
public void onStart() {
String mainActivityName = getMainActivityName();
if (mainActivityName != null && mainActivityName.length() > 0) {
launchMainActivity(getTargetContext().getPackageName(),
getMainActivityName());
} else {
super.onStart();
}
}
...
156. Doing both Controlling and Tracking
1. Modify org.kandroid.memtracer.MemoryInstrumentation.
public class MemoryInstrumentation extends InstrumentationTestRunner {
...
@Override
public void onCreate(Bundle arguments) {
...
start();
super.onCreate(arguments);
}
@Override
public void onStart() {
String mainActivityName = getMainActivityName();
if (mainActivityName != null && mainActivityName.length() > 0) {
launchMainActivity(getTargetContext().getPackageName(),
getMainActivityName());
} else {
super.onStart();
}
}
...
157. Doing both Controlling and Tracking
1. Modify org.kandroid.memtracer.MemoryInstrumentation.
public class MemoryInstrumentation extends InstrumentationTestRunner {
...
@Override
public void onCreate(Bundle arguments) {
...
start();
super.onCreate(arguments);
}
@Override
public void onStart() {
String mainActivityName = getMainActivityName();
if (mainActivityName != null && mainActivityName.length() > 0) {
launchMainActivity(getTargetContext().getPackageName(),
getMainActivityName());
} else {
super.onStart();
}
}
...
158. Doing both Controlling and Tracking
2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.
159. Doing both Controlling and Tracking
2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.
3. Edit the <instrumentation> element in the manifest file.
160. Doing both Controlling and Tracking
2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.
3. Edit the <instrumentation> element in the manifest file.
4. Build and Install the test package.
$ cd ApiDemosInstrumentTest
$ ant debug
$ adb install –r bin/ApiDemosInstrumentTest-debug.apk
161. Doing both Controlling and Tracking
2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.
3. Edit the <instrumentation> element in the manifest file.
4. Build and Install the test package.
$ cd ApiDemosInstrumentTest
$ ant debug
$ adb install –r bin/ApiDemosInstrumentTest-debug.apk
5. Run the test and see the result.
$ adb shell am instrument -w
> com.example.android.apis.instrument.test/
> org.kandroid.memtracer.MemoryInstrumentation
162. Doing both Controlling and Tracking
2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.
3. Edit the <instrumentation> element in the manifest file.
4. Build and Install the test package.
$ cd ApiDemosInstrumentTest
$ ant debug
$ adb install –r bin/ApiDemosInstrumentTest-debug.apk
5. Run the test and see the result.
$ adb shell am instrument -w
> com.example.android.apis.instrument.test/
> org.kandroid.memtracer.MemoryInstrumentation
6. Pull the trace file ‘kmemtrace.csv’ from /sdcard/kmemtracer.
$ adb pull /sdcard/kmemtracer/kmemtrace.csv .