Robolectric Temp

Providing test data for SharedPreferences for Robolectric

Robolectric sharedpreferences 使用方式(尚未試驗過)

1
2
3
4
5
SharedPreferences sharedPreferences = ShadowPreferenceManager.getDefaultSharedPreferences(Robolectric.application.getApplicationContext());
sharedPreferences.edit().putString("testId", "12345").commit();

SharedPreferences sharedPreferences = Robolectric.application.getSharedPreferences("you_custom_pref_name", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("testId", "12345").commit();

testing the display of alert when internet connectivity unavailable roboelectric android

手動變更 NetworkInfo。

1
2
3
4
5
6
7
8
9
public ShadowConnectivityManager() {
NetworkInfo wifi = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.DISCONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, false);
networkTypeToNetworkInfo.put(ConnectivityManager.TYPE_WIFI, wifi);

NetworkInfo mobile = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_MOBILE, ConnectivityManager.TYPE_MOBILE_MMS, true, true);
networkTypeToNetworkInfo.put(ConnectivityManager.TYPE_MOBILE, mobile);

this.activeNetwork = mobile;
}