austgl / robotium

Automatically exported from code.google.com/p/robotium
0 stars 0 forks source link

getCurrentActivity() returning previous activity instead of current after goBack() #243

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Create an ActivityInstrumentationTestCase2 test using ActivityA.
2.Send an intent to open ActivityB.
3.Send an intent to open ActivityC.
4.Call getCurrentActivity()
5.Call goBack()
6.Call getCurrentActivity()
7.Call goBack()

What is the expected output? What do you see instead?
In step 4 getCurrentActivity() returns ActiviyC what is correct.
After calling goBack() in step 5, step 6 returns ActivityC when it was expected 
to return ActivityB.
After calling goBack() in step 7, step 8 returns ActivityB when it was expected 
to return ActivityA.

What version of the product are you using? On what operating system?
Mac OS X version 10 and Ubuntu x86_64
Robotium 3.1
Android SDK 3.2, 4.0, 4.0.3

Please provide any additional information below.
Looking into Robotium code I can see it is using directly an Instrumentation 
Monitor to get the current activity. However the monitor after calling the 
goBack() method reports the correct activity for some miliseconds and after 
that starts to report the previous activity.

Checking instrumentation source code I see that it is reporting to attached 
monitors in callActivityOnDestroy. This causes the activity being destroyed to 
become the last activity in the monitor.

Please let me know if you need more information, I can attach the code I used 
for testing in case it is needed. 

Original issue reported on code.google.com by frdc...@gmail.com on 5 Apr 2012 at 7:35

GoogleCodeExporter commented 9 years ago
Thanks for reporting this. This is a known bug that is was introduced in the 
Android platform after 3.0. If you try your test case on 2.3 you will see that 
it works.

It seems as if you have been analyzing this issue quite a bit, do you have a 
proposed fix (that can be done from Robotium side)? Or can this only be fixed 
by the Android team?

Original comment by renasr...@gmail.com on 6 Apr 2012 at 7:48

GoogleCodeExporter commented 9 years ago
A workaround would be to maintain an internal stack of activities. This should 
work like this:

1.-Create a timer with a period small enough to catch all the activities and 
add every new activity to the internal stack(We would continue using the 
monitor getLastActivity for this purpose).
2.-Rewrite the goBack() method to store the lastDestroyedActivity and take out 
the top of the stack before calling goBack() method actual code.
3.-Before adding a new activity we would check if the monitor getLastActivity 
is different of lastDestroyedActivity, if yes the we add the activity to the 
internal stack, if not we don't add it.
4.-Rewrite getCurrentActivity method to return the peek of the stack.
5.-Rewrite getAllOpenedActivities to return the internal stack (This should fix 
another bug with goBackToActivity not being able to find an Activity).

If you think this a good workaround I could code it, just let me know. 

Original comment by frdc...@gmail.com on 6 Apr 2012 at 5:52

GoogleCodeExporter commented 9 years ago
Sounds like a good workaround. Please code it and perform a pull request in 
github. 

Original comment by renasr...@gmail.com on 6 Apr 2012 at 5:56

GoogleCodeExporter commented 9 years ago
This has been fixed in Robotium 3.2. 

Original comment by renasr...@gmail.com on 10 Apr 2012 at 6:46