Reputation: 3717
I am black-box testing an application by using the Robotium framework. The app under test send a notification in the status bar each time I install a new application. I would like to click on that notification, but I still did not find a proper way to do it.
When I manually click on one notification I get this logcat
lines:
I/ActivityManager( 148): START {flg=0x14000000 cmp=com.test.package/.activity.FrontActivity bnds=[0,38][240,86] (has extras) u=0} from pid -1
I/ActivityManager( 148): START {flg=0x14000000 cmp=com.test.package/.activity.ResultActivity u=0} from pid 8600
I/ActivityManager( 148): Displayed com.test.package/.activity.FrontActivity: +1s183ms
I/ActivityManager( 148): Displayed com.test.package/.activity.ResultActivity: +744ms
I know Robotium cannot test 2 different applications at the same time, as well as I know you can't get the notification of an external application.
I also tried to get the pending intent and fire it up by using the following code:
Context context = this.getInstrumentation().getTargetContext().getApplicationContext();
String intentClassString = "com.test.package.activity.FrontActivity";
Class<?> intentClass = null;
try {
intentClass = Class.forName(intentClassString);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Intent intent = new Intent(context, intentClass);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
try {
pendingIntent.send();
} catch (CanceledException e) {
e.printStackTrace();
}
The activity is actually showed, but it is different from what I obtain by manually invoking it. I think something is wrong in the context I pass, or the way I call the pending intent.
Any hint about this issue? Is there a better way to simulate the click on a notification?
Upvotes: 7
Views: 3157
Reputation: 303
The answer that Ranjith KP gave you contains the solution. Be aware that you need the phone who is running the app rooted, as these are commands used with sudo priviledges.
This is the code that is working for me. It first extends the notification bar (swipe command), waits 1 second and finally taps the first notification on the list. Modify the axis parameters as you wish.
Process su = null;
try {
su = Runtime.getRuntime().exec("su");
su.getOutputStream().write("input swipe 270 010 270 900\n".getBytes());
Thread.sleep(1000);
su.getOutputStream().write("input tap 200 200\n".getBytes());
su.getOutputStream().write("exit\n".getBytes());
su.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (su != null) {
su.destroy();
}
}
Upvotes: 0
Reputation: 810
You can trigger the swipe on notification bar and tap event on notification.
Refer commands :
adb shell input swipe 100 500 400 100 1000
adb shell input tap 400 400
by changing the arguments you can use these commands.
Upvotes: 1
Reputation: 582
Please find good dicussion and a few options in this other stackoverflow question:
Can I test status bar notifications using Android's testing framework?
Since you already have code to fire up an intent, instead of trying to recreate what the notify() call would do, try adding a special activity just for testing that actually calls notify() on the notification.
Upvotes: 0