Compare commits

...

1 commit

Author SHA1 Message Date
ligi
b2f4eafe5b Attack flaky test 2015-12-10 17:16:06 +01:00
4 changed files with 35 additions and 16 deletions

View file

@ -1,9 +1,10 @@
package org.ligi.passandroid;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.test.suitebuilder.annotation.SmallTest;
import org.junit.Before;
import org.ligi.passandroid.model.PastLocationsStore;
import javax.inject.Inject;
@ -22,12 +23,22 @@ public class ThePastLocationsStore extends BaseIntegration<Activity> {
@Override
public void setUp() throws Exception {
super.setUp();
((TestComponent)App.component()).inject(this);
((TestComponent) App.component()).inject(this);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
getSharedPrefs().edit().clear();
}
private SharedPreferences getSharedPrefs() {
return getInstrumentation().getContext().getSharedPreferences("" + (System.currentTimeMillis() / 100000), Context.MODE_PRIVATE);
}
@SmallTest
public void testPastLocationsStoreShouldNeverContainMoreThanMaxElements() {
PastLocationsStore tested = new PastLocationsStore(getInstrumentation().getContext(), tracker);
PastLocationsStore tested = new PastLocationsStore(getSharedPrefs(), tracker);
for (int i = 0; i < PastLocationsStore.MAX_ELEMENTS * 2; i++) {
tested.putLocation("" + i);
@ -39,13 +50,13 @@ public class ThePastLocationsStore extends BaseIntegration<Activity> {
@SmallTest
public void testPastLocationsStoreShouldStoreOnlyOneOfAKind() {
PastLocationsStore tested = new PastLocationsStore(getInstrumentation().getContext(), tracker);
PastLocationsStore tested = new PastLocationsStore(getSharedPrefs(), tracker);
for (int i = 0; i < 3; i++) {
tested.putLocation("foo");
}
assertThat(tested.getLocations().size()).isLessThan(2);
assertThat(tested.getLocations()).containsOnly("foo");
}

View file

@ -1,5 +1,8 @@
package org.ligi.passandroid;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;
@ -33,6 +36,11 @@ public class AppModule {
return new Settings(app);
}
@Singleton
@Provides
SharedPreferences provideSharedPreferences() {
return PreferenceManager.getDefaultSharedPreferences(app);
}
@Singleton
@Provides

View file

@ -1,7 +1,6 @@
package org.ligi.passandroid.model;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
@ -17,12 +16,12 @@ public class PastLocationsStore {
public static final String KEY_PAST_LOCATIONS = "past_locations";
public static final int MAX_ELEMENTS = 5;
private final Context context;
private final SharedPreferences sharedPreferences;
private final Tracker tracker;
@Inject
public PastLocationsStore(Context context, Tracker tracker) {
this.context = context;
public PastLocationsStore(SharedPreferences sharedPreferences, Tracker tracker) {
this.sharedPreferences = sharedPreferences;
this.tracker = tracker;
}
@ -32,8 +31,7 @@ public class PastLocationsStore {
// feature not available for these versions
return;
}
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final Set<String> pastLocations = prefs.getStringSet(KEY_PAST_LOCATIONS, new HashSet<String>());
final Set<String> pastLocations = sharedPreferences.getStringSet(KEY_PAST_LOCATIONS, new HashSet<String>());
if (pastLocations.size() >= MAX_ELEMENTS) {
deleteOneElementFromSet(pastLocations);
@ -44,7 +42,7 @@ public class PastLocationsStore {
}
tracker.trackEvent("scan", "put location", "count", (long) pastLocations.size());
prefs.edit().putStringSet(KEY_PAST_LOCATIONS, pastLocations).apply();
sharedPreferences.edit().putStringSet(KEY_PAST_LOCATIONS, pastLocations).apply();
}
private void deleteOneElementFromSet(Set<String> pastLocations) {
@ -65,7 +63,6 @@ public class PastLocationsStore {
// feature not available for these versions
return new HashSet<>();
}
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getStringSet(KEY_PAST_LOCATIONS, new HashSet<String>());
return sharedPreferences.getStringSet(KEY_PAST_LOCATIONS, new HashSet<String>());
}
}

View file

@ -5,9 +5,11 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import com.squareup.otto.Bus;
@ -79,7 +81,8 @@ public class SearchPassesIntentService extends IntentService {
findNotificationBuilder = new NotificationCompat.Builder(this).setAutoCancel(true).setSmallIcon(R.drawable.ic_launcher);
for (String path : new PastLocationsStore(getApplicationContext(), tracker).getLocations()) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
for (String path : new PastLocationsStore(preferences, tracker).getLocations()) {
search_in(new File(path), false);
}