samedi 19 décembre 2015

Restarting application on android test

I'm making a library which will process information according to the user default settings and will save it on SharedPreferences, which can be modified by the developer on their application before initializing my library.

The SDK should only be initialized once per application instance, otherwised will trigger a RuntimeError. So on the application side on Application class should be something like this:

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

       //Here I can do something that will change the default configs

       //Here I initialize the SDK singleton method
       Sdk.initialize();
    }
}

The sdk abstract implementation:

public class Sdk {

    private static SampleApplication sInstance;

    private void Sdk(){
    }

    public static SampleApplication getInstance() throws RuntimeException {
        if (sInstance == null) {
            throw new RuntimeException();
        }
        return sInstance;
    }

    public static void initialize() {
        if (sInstance == null) {
            sInstance = new Sdk();
            //save some information according to what is on the default configurations
        } else {
            throw new RuntimeException("Method was already initialized");
        }
    }
}

The problem comes when I want to test several scenarios to call this method (which can only be called once per application instance).

So I created an Android Test which extends the ApplicationTest

ApplicationTest:

  public class ApplicationTest extends ApplicationTestCase<SampleApplication> {
        public ApplicationTest() {
            super(SampleApplication.class);
        }
    }

Android Test Sample:

public class SampleTest extends ApplicationTest {

    @Override
    protected void setUp() throws Exception {
// Here I restart the user preferences although I need to restart the application
//        terminateApplication();
//        createApplication();
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testDefaultSettings() {
        // Here is where I want to restart application input
        // some values on the user preferences settings in order 
        // to test the output on sharedpreferences by the initialized method  
    }
}

I tried to terminate and create application again but with no success. My question is it is possible to restart the application of a Android test? Am I doing something wrong here?

Aucun commentaire:

Enregistrer un commentaire