Unlock Home Screen after Booting

2,207 views
Skip to first unread message

Manoj Pahuja

unread,
Mar 14, 2011, 5:42:35 PM3/14/11
to Robotium Developers
Hi All,
I am new to Robotium Development and testing but I have spent some
time in investigating and setting it up for testing a new app. I was
able to automate testing the app successfully. Thanks to all the
online resources and help available. But I am stuck with one problem
where if I have the screen locked then my tests would fail because
they are waiting for the login screen from my app. I have searched and
tried some of the methods to workaround this but could not succeed
therefore I am reaching out to the group to please help me. Here is my
basic test:
____________________________________________________________________________
public class AndroidStartTest extends
ActivityInstrumentationTestCase2<MPApp> {
private Solo solo;

public AndroidStartTest() {
super("com.citrix", MPApp.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();
this.solo = new Solo(getInstrumentation(), getActivity());
}

public void testCanOpenSettings(){

solo.pressMenuItem(3);
solo.pressMenuItem(3);
/*
Context context = null;
KeyguardManager km = (KeyguardManager)
Context.getSystemService(Context.KEYGUARD_SERVICE);
if (km.inKeyguardRestrictedInputMode()) {
KeyguardManager.KeyguardLock lock =
km.newKeyguardLock("some_tag");
lock.disableKeyguard();
SystemClock.sleep(2000);
}*/

}


@Override
protected void tearDown() throws Exception{
try {
this.solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
/**
* @throws InterruptedException
* @throws Exception Exception
*/

public void testStart() throws InterruptedException {
solo.clearEditText(0);
assertTrue(solo.searchText("Email"));

.....
}}}
_______________________________________________________________________________

As you can see i have tried the two obvious ways. which is to Press
the actual Menu Button on the simulator to unlock which was I not able
to achieve using either of the above methods. I am using Robotium 2.2
version.

Any help would be really appreciated.

Miroslav Simudvarac

unread,
Mar 15, 2011, 2:49:41 AM3/15/11
to robotium-...@googlegroups.com
Hello Manoj,

this issue allready made me some headache before I found the answer.

Using Windows
telnet localhost 5554
event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0
quit

Using Linux:
echo "event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0" | nc -q1 localhost 5554

Please keep in mind that you only can unlock the screen on a fresh
started emulator.
So your test should be in this order: 1. Start emu, 2. Unlock screen
3. Run robotium tests
This is not working if you have an allready running emulator and then
lock it to see if this works.
This detail kept me busy for hours before found out. Somehow the
solution seamed to work only sometimes unless I figured this out.

Hope I was able to help.

Regards,


Miroslav Simudvarac

--------------------------------------------

Simvelop GmbH
Wilhelmstr. 40
65582 Diez, Germany
Phone +49 6432 50 82 70
Fax   +49 6432 80 09 621
Web www.simvelop.de
Mail in...@simvelop.de

Bankverbindung
Volksbank Limburg
BLZ 511 900 00
Kto.Nr.: 518 211 06

Registergericht Montabaur HRB 22502
Sitz der Gesellschaft ist Diez a.d. Lahn
Geschäftsführer:
Miroslav Simudvarac

> --
> You received this message because you are subscribed to the Google Groups "Robotium Developers" group.
> To post to this group, send email to robotium-...@googlegroups.com.
> To unsubscribe from this group, send email to robotium-develo...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/robotium-developers?hl=en.
>
>

anand paidiparthy

unread,
Mar 15, 2011, 1:17:47 PM3/15/11
to robotium-...@googlegroups.com, Miroslav Simudvarac
Hi Mirslav,
 
               I heard that disabling of keyguard should be done in the application onStart(). As u told for the emulator, is there any other way to disable keyguard in device. If there is any way which could be done for the device without application code dependency.
 
With regards,
Anand

Manoj Pahuja

unread,
Mar 15, 2011, 7:54:18 PM3/15/11
to robotium-...@googlegroups.com
Hi Miroslav,

Thanks a lot for that information I really appreciate it. But my only concern with that is that I have to run this manually everytime the simulator loads up. So is it possible to put this in setup so that every time the emulator starts it unlocks it?

Thanks
Manoj

Miroslav Simudvarac

unread,
Mar 16, 2011, 2:55:52 AM3/16/11
to robotium-...@googlegroups.com
Why would you run this manually?
Put it in a shell script and run it from the script that starts your test.
How do you execute the tests?
What OS are you using on you test machine?

Regards,

Miroslav Simudvarac

--------------------------------------------

Simvelop GmbH
Wilhelmstr. 40
65582 Diez, Germany
Phone +49 6432 50 82 70
Fax   +49 6432 80 09 621
Web www.simvelop.de
Mail in...@simvelop.de

Bankverbindung
Volksbank Limburg
BLZ 511 900 00
Kto.Nr.: 518 211 06

Registergericht Montabaur HRB 22502
Sitz der Gesellschaft ist Diez a.d. Lahn
Geschäftsführer:
Miroslav Simudvarac

chandra sekhar

unread,
Mar 16, 2011, 3:53:19 AM3/16/11
to robotium-...@googlegroups.com, Miroslav Simudvarac
Hi,
Thanks for your reply with details, i was also looking solution how to unlock screen. Its very appreciated.
I have a question?
As per my understanding the below solution works only in emulator. In my case i am using device for testing in linux, is this below command works? can you please help how we can unlock device screen....
I have tried below command (lunux) to unlock screen, I dont see any difference.
For your information i have used this command echo "event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0" | nc -q1 localhost 5554 to unlock. Please guide me if i am correct or not?
I hope you understood my concern...
sekhar
Singapore

Christopher Orr

unread,
Mar 17, 2011, 7:44:56 PM3/17/11
to robotium-...@googlegroups.com
On 15/03/2011 07:49, Miroslav Simudvarac wrote:
> this issue allready made me some headache before I found the answer.
>
> Using Linux:
> echo "event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0" | nc -q1 localhost 5554

That's suspiciously identical to an answer I posted on StackOverflow a
long time ago :)

However, I updated that answer[1] earlier this year to give an improved
solution that I discovered. As it's using "adb shell" rather than the
emulator telnet interface, it should work on both emulators and devices:

adb shell input keyevent 82

Alternatively, if you're running your tests in Hudson or Jenkins
continuous integration servers, the Android Emulator Plugin[2] will
automatically unlock the screen for you after its emulator has started up.

Regards,
Chris

[1]: http://stackoverflow.com/questions/3214531/#3366443
[2]: http://wiki.jenkins-ci.org/display/JENKINS/Android+Emulator+Plugin

Saneesh Joseph

unread,
Mar 18, 2011, 2:48:23 AM3/18/11
to robotium-...@googlegroups.com, Manoj Pahuja
This should work perfectly if you have <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> in application manifest file. You dont really have to worry about adding this entry in the manifest file, only thing is that this entry should be removed along with other changes to the manifest file before the production build.  
You can call  lock.disableKeyguard(); in setUp() which works perfect. 

Best Regards,
Saneesh Joseph.


Manoj Pahuja

unread,
Mar 18, 2011, 5:56:24 PM3/18/11
to Robotium Developers
Hi All,

Thanks a lot for all the feedback from everybody (this group is
great )and options for achieving this task. I was able to use all the
options for achieving this task some or the other way. So here is my
summary for future references:

1. If you are the developer or have access to the code of the
application under test then the best and the easiest way of achieving
this is using:
http://developer.android.com/guide/topics/testing/activity_testing.html
with some code required at the application level. The benefits of
using this method is that you can use the code and application for CI
with no modifications at the build machine level. Another important
benefit is that you can use this even if the emulator is already
booted and you want to lock and unlock it. Here is the code that you
can use to achieve so.
public boolean setKeyGuardEnabled(boolean enable) {
Object keyguardService =
getBaseContext().getSystemService(Context.KEYGUARD_SERVICE);

KeyguardLock keyguardLock =
((KeyguardManager)keyguardService).newKeyguardLock(TAG );

if (enable) {
keyguardLock.reenableKeyguard();
} else {
keyguardLock.disableKeyguard();
}
return true;
}


But the important cons are mentioned in the above link as well which
is basically to not put this code to live so you would have to comment
or remove this code for the app build going live.

2. Second best method is to use adb shell commands to unlock it at
boot of the emulator. The actual code would differ depending upon what
method you use to call these commands. This of course has the
limitation that it can be run only at the first start up of the
emulator, once the emulator has started this command would do nothing.

3. Third method is telnet into the emulator and then use the EV_KEY to
unlock it.
telnet localhost 5554
event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0
quit
You can put this in a bat or shell file and call it from your test to
unlock the Home screen. This again has the limitation of being able to
unlock at only the first boot and cannot be used to unlock the Home
screen after the first boot.

4. Fourth option is to use maven to unlock it.
http://issues.hudson-ci.org/browse/HUDSON-7185
This is very useful for CI purposes but is limited by inability to
unlock the screen from eclipse. So if you are writing your tests in
Eclipse you have to run the tests using command prompt if you need to
unlock the screen.

Hopefully this would help people to get around this problem in the
suitable manner for their purposes.
Disclaimer: This is just my list and preference, everybody may have a
different perspective and rationale to use one over the other. I think
all of these options are good as long as they are able to solve the
problem.

Thanks
Manoj Pahuja

Christopher Orr

unread,
Mar 28, 2011, 4:31:53 AM3/28/11
to robotium-...@googlegroups.com
Hi there,

A few thoughts inline:

On 18/03/11 22:56, Manoj Pahuja wrote:
> Thanks a lot for all the feedback from everybody (this group is
> great )and options for achieving this task. I was able to use all the
> options for achieving this task some or the other way. So here is my
> summary for future references:
>
> 1. If you are the developer or have access to the code of the
> application under test then the best and the easiest way of achieving
> this is using:
> http://developer.android.com/guide/topics/testing/activity_testing.html
>

> KeyguardLock keyguardLock =
> ((KeyguardManager)keyguardService).newKeyguardLock(TAG );
>
> if (enable) {
> keyguardLock.reenableKeyguard();
> } else {
> keyguardLock.disableKeyguard();
> }
> return true;
> }

In my opinion this is a nasty solution as it requires you to include an
extra permission in your application -- one that virtually no
application will need.

As you mention, you should remove this, but people forget: I've seen a
few apps released on the Market that still have this permission
included, when it really should have been removed.

If you can *automatically* only include this permission for test builds,
or automatically remove the permission when doing a release build, then
fine.


> 2. Second best method is to use adb shell commands to unlock it at
> boot of the emulator. The actual code would differ depending upon what
> method you use to call these commands. This of course has the
> limitation that it can be run only at the first start up of the
> emulator, once the emulator has started this command would do nothing.

You can run the "adb shell" command as often as you like. On the first
startup, it will open the Android menu, which should be harmless. On
each subsequent startup, it will press Menu, which will unlock the
screen, as desired.


> 3. Third method is telnet into the emulator and then use the EV_KEY to
> unlock it.
> telnet localhost 5554
> event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0
> quit
> You can put this in a bat or shell file and call it from your test to
> unlock the Home screen. This again has the limitation of being able to
> unlock at only the first boot and cannot be used to unlock the Home
> screen after the first boot.

This seems backwards. The home screen is not locked when you start an
emulator for the first time.

In any case, I wouldn't recommend this at all, since we have the more
reliable "adb shell" solution above. Which also should work on real
devices.


> 4. Fourth option is to use maven to unlock it.
> http://issues.hudson-ci.org/browse/HUDSON-7185
> This is very useful for CI purposes but is limited by inability to
> unlock the screen from eclipse. So if you are writing your tests in
> Eclipse you have to run the tests using command prompt if you need to
> unlock the screen.

Clarification: that bug report is about the Android Emulator plugin for
Jenkins/Hudson, and not Maven. The Maven solution mentioned there just
uses the "telnet" option above.


Finally, a further option is to use Android emulator snapshots.
Introduced in Android SDK Tools v9, the emulator now supports saving the
current state of the emulator (similar to other virtualisation products
like VMware).

So you could start the emulator once, so that it becomes unlocked, then
shut it down. Starting the emulator subsequently will immediately load
up a clean emulator with the screen unlocked (so long as you do not save
emulator state at shutdown (i.e. the "--no-snapshot-save" option).

BTW, this snapshot functionality will be automated and included in the
next release of the Android Emulator Plugin for Jenkins/Hudson.

Regards,
Chris

Tatka D.

unread,
Sep 11, 2013, 9:05:41 AM9/11/13
to robotium-...@googlegroups.com, in...@simvelop.de
thanks a lot. it works fine.

вторник, 15 марта 2011 г., 8:49:41 UTC+2 пользователь Miroslav Simudvarac написал:
Reply all
Reply to author
Forward
0 new messages