michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import org.mozilla.gecko.AppConstants; michael@0: michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: michael@0: import com.jayway.android.robotium.solo.Condition; michael@0: michael@0: import java.io.File; michael@0: import java.io.FileReader; michael@0: import java.io.FileWriter; michael@0: michael@0: import org.json.JSONObject; michael@0: michael@0: /** michael@0: * Tests the proper operation of the ANR reporter. michael@0: */ michael@0: public class testANRReporter extends BaseTest { michael@0: michael@0: private static final String ANR_ACTION = "android.intent.action.ANR"; michael@0: private static final String PING_DIR = "saved-telemetry-pings"; michael@0: private static final int WAIT_FOR_PING_TIMEOUT = 10000; michael@0: private static final String ANR_PATH = "/data/anr/traces.txt"; michael@0: private static final String SAMPLE_ANR michael@0: = "----- pid 1 at 2014-01-15 18:55:51 -----\n" michael@0: + "Cmd line: " + AppConstants.ANDROID_PACKAGE_NAME + "\n" michael@0: + "\n" michael@0: + "JNI: CheckJNI is off; workarounds are off; pins=0; globals=397\n" michael@0: + "\n" michael@0: + "DALVIK THREADS:\n" michael@0: + "(mutexes: tll=0 tsl=0 tscl=0 ghl=0)\n" michael@0: + "\n" michael@0: + "\"main\" prio=5 tid=1 WAIT\n" michael@0: + " | group=\"main\" sCount=1 dsCount=0 obj=0x41d6bc90 self=0x41d5a3c8\n" michael@0: + " | sysTid=3485 nice=0 sched=0/0 cgrp=apps handle=1074852180\n" michael@0: + " | state=S schedstat=( 0 0 0 ) utm=1065 stm=152 core=0\n" michael@0: + " at java.lang.Object.wait(Native Method)\n" michael@0: + " - waiting on <0x427ab340> (a org.mozilla.gecko.GeckoEditable$5)\n" michael@0: + " at java.lang.Object.wait(Object.java:364)\n" michael@0: + " at org.mozilla.gecko.GeckoEditable$5.run(GeckoEditable.java:746)\n" michael@0: + " at android.os.Handler.handleCallback(Handler.java:733)\n" michael@0: + " at android.os.Handler.dispatchMessage(Handler.java:95)\n" michael@0: + " at android.os.Looper.loop(Looper.java:137)\n" michael@0: + " at android.app.ActivityThread.main(ActivityThread.java:4998)\n" michael@0: + " at java.lang.reflect.Method.invokeNative(Native Method)\n" michael@0: + " at java.lang.reflect.Method.invoke(Method.java:515)\n" michael@0: + " at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)\n" michael@0: + " at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)\n" michael@0: + " at dalvik.system.NativeStart.main(Native Method)\n" michael@0: + "\n" michael@0: + "\"Gecko\" prio=5 tid=16 SUSPENDED\n" michael@0: + " | group=\"main\" sCount=1 dsCount=0 obj=0x426e2b28 self=0x76ae92e8\n" michael@0: + " | sysTid=3541 nice=0 sched=0/0 cgrp=apps handle=1991153472\n" michael@0: + " | state=S schedstat=( 0 0 0 ) utm=1118 stm=145 core=0\n" michael@0: + " #00 pc 00000904 /system/lib/libc.so (__futex_syscall3+4294832136)\n" michael@0: + " #01 pc 0000eec4 /system/lib/libc.so (__pthread_cond_timedwait_relative+48)\n" michael@0: + " #02 pc 0000ef24 /system/lib/libc.so (__pthread_cond_timedwait+64)\n" michael@0: + " #03 pc 000536b7 /system/lib/libdvm.so\n" michael@0: + " #04 pc 00053c79 /system/lib/libdvm.so (dvmChangeStatus(Thread*, ThreadStatus)+34)\n" michael@0: + " #05 pc 00049507 /system/lib/libdvm.so\n" michael@0: + " #06 pc 0004d84b /system/lib/libdvm.so\n" michael@0: + " #07 pc 0003f1df /dev/ashmem/libxul.so (deleted)\n" michael@0: + " at org.mozilla.gecko.mozglue.GeckoLoader.nativeRun(Native Method)\n" michael@0: + " at org.mozilla.gecko.GeckoAppShell.runGecko(GeckoAppShell.java:384)\n" michael@0: + " at org.mozilla.gecko.GeckoThread.run(GeckoThread.java:177)\n" michael@0: + "\n" michael@0: + "----- end 1 -----\n" michael@0: + "\n" michael@0: + "\n" michael@0: + "----- pid 2 at 2013-01-25 13:27:01 -----\n" michael@0: + "Cmd line: system_server\n" michael@0: + "\n" michael@0: + "----- end 2 -----\n"; michael@0: michael@0: private boolean mDone; michael@0: michael@0: private JSONObject readPingFile(final File pingFile) throws Exception { michael@0: final long fileSize = pingFile.length(); michael@0: if (fileSize == 0 || fileSize > Integer.MAX_VALUE) { michael@0: throw new Exception("Invalid ping file size"); michael@0: } michael@0: final char[] buffer = new char[(int) fileSize]; michael@0: final FileReader reader = new FileReader(pingFile); michael@0: try { michael@0: final int readSize = reader.read(buffer); michael@0: if (readSize == 0 || readSize > buffer.length) { michael@0: throw new Exception("Invalid number of bytes read"); michael@0: } michael@0: } finally { michael@0: reader.close(); michael@0: } michael@0: return new JSONObject(new String(buffer)); michael@0: } michael@0: michael@0: public void testANRReporter() throws Exception { michael@0: blockForGeckoReady(); michael@0: michael@0: // Cannot test ANR reporter if it's disabled. michael@0: if (!AppConstants.MOZ_ANDROID_ANR_REPORTER) { michael@0: mAsserter.ok(true, "ANR reporter is disabled", null); michael@0: return; michael@0: } michael@0: michael@0: // For the ANR reporter to work, we need to provide sample ANR traces to it. michael@0: // Therefore, we need the ANR file to exist and writable. If not, we don't michael@0: // have the right permissions to create the file, so we just bail. michael@0: final File anrFile = new File(ANR_PATH); michael@0: if (!anrFile.exists()) { michael@0: mAsserter.ok(true, "ANR file does not exist", null); michael@0: return; michael@0: } michael@0: if (!anrFile.canWrite()) { michael@0: mAsserter.ok(true, "ANR file is not writable", null); michael@0: return; michael@0: } michael@0: michael@0: final FileWriter anrWriter = new FileWriter(anrFile); michael@0: try { michael@0: anrWriter.write(SAMPLE_ANR); michael@0: } finally { michael@0: anrWriter.close(); michael@0: } michael@0: michael@0: // Block the UI thread to simulate an ANR michael@0: final Runnable uiBlocker = new Runnable() { michael@0: @Override michael@0: public synchronized void run() { michael@0: while (!mDone) { michael@0: try { michael@0: wait(); michael@0: } catch (final InterruptedException e) { michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: getActivity().runOnUiThread(uiBlocker); michael@0: michael@0: // Make sure our initial ping directory is empty. michael@0: final File pingDir = new File(mProfile, PING_DIR); michael@0: final String[] initialFiles = pingDir.list(); michael@0: mAsserter.ok(initialFiles == null || initialFiles.length == 0, michael@0: "Ping directory is empty", null); michael@0: michael@0: final Intent anrIntent = new Intent(ANR_ACTION); michael@0: anrIntent.setPackage(AppConstants.ANDROID_PACKAGE_NAME); michael@0: mAsserter.is(anrIntent.getPackage(), AppConstants.ANDROID_PACKAGE_NAME, michael@0: "Successfully set package name"); michael@0: michael@0: final Context testContext = getInstrumentation().getContext(); michael@0: mAsserter.isnot(testContext, null, "testContext should not be null"); michael@0: michael@0: // Trigger the ANR. michael@0: mAsserter.info("Triggering ANR", null); michael@0: testContext.sendBroadcast(anrIntent); michael@0: michael@0: // ANR reporter is supposed to ignore duplicate ANRs. michael@0: // This will be checked later when we look for ping files. michael@0: mAsserter.info("Triggering second ANR", null); michael@0: testContext.sendBroadcast(new Intent(anrIntent)); michael@0: michael@0: mAsserter.info("Waiting for ping", null); michael@0: waitForCondition(new Condition() { michael@0: @Override michael@0: public boolean isSatisfied() { michael@0: final File[] newFiles = pingDir.listFiles(); michael@0: if (newFiles == null || newFiles.length == 0) { michael@0: // Keep waiting. michael@0: return false; michael@0: } michael@0: // Make sure we have a complete file. We skip assertions and catch all michael@0: // exceptions here because the condition may not be satisfied now but may michael@0: // be satisfied later. After the wait is over, we will repeat the same michael@0: // steps with assertions and exceptions. michael@0: try { michael@0: return readPingFile(newFiles[0]).has("slug"); michael@0: } catch (final Exception e) { michael@0: return false; michael@0: } michael@0: } michael@0: }, WAIT_FOR_PING_TIMEOUT); michael@0: michael@0: mAsserter.ok(pingDir.exists(), "Ping directory exists", null); michael@0: mAsserter.ok(pingDir.isDirectory(), "Ping directory is a directory", null); michael@0: michael@0: final File[] newFiles = pingDir.listFiles(); michael@0: mAsserter.isnot(newFiles, null, "Ping directory is not empty"); michael@0: mAsserter.is(newFiles.length, 1, "ANR reporter wrote one ping"); michael@0: mAsserter.ok(newFiles[0].exists(), "Ping exists", null); michael@0: mAsserter.ok(newFiles[0].isFile(), "Ping is a file", null); michael@0: mAsserter.ok(newFiles[0].canRead(), "Ping is readable", null); michael@0: mAsserter.info("Found ping file", newFiles[0].getPath()); michael@0: michael@0: // Check standard properties required by Telemetry server. michael@0: final JSONObject pingObject = readPingFile(newFiles[0]); michael@0: mAsserter.ok(pingObject.has("slug"), "Ping has slug property", null); michael@0: mAsserter.ok(pingObject.has("reason"), "Ping has reason property", null); michael@0: mAsserter.ok(pingObject.has("payload"), "Ping has payload property", null); michael@0: michael@0: final JSONObject pingPayload = pingObject.getJSONObject("payload"); michael@0: mAsserter.ok(pingPayload.has("ver"), "Payload has ver property", null); michael@0: mAsserter.ok(pingPayload.has("info"), "Payload has info property", null); michael@0: mAsserter.ok(pingPayload.has("androidANR"), "Payload has androidANR property", null); michael@0: michael@0: final JSONObject pingInfo = pingPayload.getJSONObject("info"); michael@0: mAsserter.ok(pingInfo.has("reason"), "Info has reason property", null); michael@0: mAsserter.ok(pingInfo.has("appName"), "Info has appName property", null); michael@0: mAsserter.ok(pingInfo.has("appUpdateChannel"), "Info has appUpdateChannel property", null); michael@0: mAsserter.ok(pingInfo.has("appVersion"), "Info has appVersion property", null); michael@0: mAsserter.ok(pingInfo.has("appBuildID"), "Info has appBuildID property", null); michael@0: michael@0: // Do some profile clean up. This is not absolutely necessary because the profile michael@0: // is blown away after test runs anyways, so we don't check return values here. michael@0: for (final File ping : newFiles) { michael@0: ping.delete(); michael@0: } michael@0: pingDir.delete(); michael@0: michael@0: // Unblock UI thread michael@0: synchronized (uiBlocker) { michael@0: mDone = true; michael@0: uiBlocker.notify(); michael@0: } michael@0: michael@0: // Clear the sample ANR michael@0: final FileWriter anrClearer = new FileWriter(anrFile); michael@0: anrClearer.close(); michael@0: } michael@0: }