4

I am using the InstrumentationTestCase class in order to unit test some things within an activity.

I need to be able to check the SharedPreferences's contents and edit them, before this activity is launched.

I cannot use the setUp method to create the Activity and access it's SharedPreferences object to edit it, and then close that activity before finishing the setUp method because it apparently is locking the tests processing.

I also cannot access the SharedPreferences after I have launched the activity inside the test because as soon as the Activity is launched, it will already change the SharedPreferences object and act according to it, before I had the chance to get it's reference.

I apparently cannot access the SharedPreferences before either, because I have no Activity object... and as soon as I do, it is already executing code and being launched...

So, my question is, is there any way to access the SharedPreferences (and any other Activity information) of this Activity before I have the Activity actually created through an Intent?

I cannot change it to an ActivityInstrumentationTestCase2 because my test uses a second activity in it's process, so I can't just change to this class and use it's setUp() method to access the SharedPreferences.

Luis Miguel Serrano
  • 5,029
  • 2
  • 41
  • 41

3 Answers3

8

I found the best simpler way to do this through the instrumentation only, without having to edit the application's architecture or any of the access attributes.

I achieved it through this:

Instrumentation instrumentation = getInstrumentation();
instrumentation.getTargetContext().getSharedPreferences(..);

This way I can access the SharedPreferences before any Activity is launched by the instrumentation.

Thanks for all the help, hints and other alternatives anyway.

Luis Miguel Serrano
  • 5,029
  • 2
  • 41
  • 41
  • Could you explain where you did this at? – theSociableme Feb 27 '13 at 03:27
  • It has been a while since I have done this, but I was doing it in the tests class that extends InstrumentationTestCase, inside one of the methods that would run the test, and before that method would launch the activity. Alternatively, I think you can also perform this on the setUp method of your test class. – Luis Miguel Serrano Mar 17 '13 at 13:19
3

Well... To tell you frankly.. I am not able to visualize your scenario. But is checking for info in application is doable ?

Create a class which extends android.app.Application and specify class name in Manifests child application element.

Sample Code:

import android.app.Application;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        //try and access activity info here.
    }
}

When your application is launched first class method to execture is onCreate of your application and has all the lifecyle events of that of any activity..

You must define extended application class in manifest by:

<application
    android:name=".MyApplication"
    android:label="@string/application_name">

I hope this ca give you some overview.

Shardul
  • 27,760
  • 6
  • 37
  • 35
  • That still does not solve the problem because i am unit testing the activity. If i would have an application to unit test, this application would also have to do this SharedPreferences access at startup, or launch the Activity that is currently doing it. And so, when testing, as soon as i would launch the Application, I would have already missed the SharedPreferences. Unless there is a way to access an Application that was not launched. Thank you though. – Luis Miguel Serrano Dec 15 '10 at 05:17
0

I haven't tried it, but if you set the mode to MODE_WORLD_READABLE and possibly MODE_WORLD_WRITEABLE instead of MODE_PRIVATE, I would think you could access the shared preferences from another application before the activity under test starts.

You could probably also use a different activity or service within the apk, or another apk that establishes a shared user ID and has the same certificate, to do the access without changing the access mode.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • No you can't. You can't access private data from another app. – Falmarri Dec 15 '10 at 04:59
  • 1
    Falmarri, you are are wrong. Please see the documentation of MODE_WORLD_READABLE which can be used to make data non-private. Also please take time to understand what a shared user ID does with respect to private data at the underlying unix level. – Chris Stratton Dec 15 '10 at 05:05