Search code examples
androidlibgdxsharedpreferencesandroid-testing

Let test know it can run once app has got to specific point - possibly sharedPref


tl;dr - Find a way to let a test know it can run, only once my game main menu has loaded

I am writing a game in LibGDX, which doesn't use Android code really, and I have a single test class, that I am using to test various visual aspects of my game.

I want to find a way of letting the test know that the game has loaded to the main menu before running tests.

I have tried to share a shared pref over the two apps, but haven't managed due to using SDK 25 and certain modes being deprecated.

Does anyone have any idea? I have thought of using the sdcard to save some sort of file, but every now and then (I'm testing on the emulator) the sdcard doesn't work (that's a whole other issue)


Solution

  • https://codexample.org/questions/57868/data-sharing-between-two-applications.c

    I managed to get the dual shared pref stuff working with the below code

    Send data from Application 1 (for ex:Application 1 package name is "com.sharedpref1" ).

    SharedPreferences prefs = getSharedPreferences("demopref",
                    Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("demostring", strShareValue);
            editor.commit();
    

    Receive the data in Application 2( to get data from Shared Preferences in Application 1).

    try {
            con = createPackageContext("com.sharedpref1", 0);//first app package name is "com.sharedpref1"
            SharedPreferences pref = con.getSharedPreferences(
                        "demopref", Context.MODE_PRIVATE);
            String your_data = pref.getString("demostring", "No Value");
        } 
    catch (NameNotFoundException e) {
                Log.e("Not data shared", e.toString());
         }
    

    In both application manifest files add same shared user id & label,

    android:sharedUserId="any string" 
    android:sharedUserLabel="@string/any_string"
    

    both are same... and shared user label must from string.xml

    like this example.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.xxxx"
        android:versionCode="1"
        android:versionName="1.0"
        android:sharedUserId="aaa.bbb.ccc" 
        android:sharedUserLabel="@string/any_string">
    

    I had a small issue with the sharedUserId not working when I just had a single word, and apparently it needs to follow a packagename style format (the example I found had 3 sections to it, and this worked for me first time, so I didn't investigate anything else)