1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/tests/background/junit3/src/helpers/FakeProfileTestCase.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,61 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +package org.mozilla.gecko.background.helpers; 1.8 + 1.9 +import java.io.File; 1.10 + 1.11 +import android.app.Activity; 1.12 +import android.content.Context; 1.13 +import android.content.SharedPreferences; 1.14 +import android.test.ActivityInstrumentationTestCase2; 1.15 +import java.util.UUID; 1.16 + 1.17 +import org.mozilla.gecko.background.common.GlobalConstants; 1.18 + 1.19 +import org.mozilla.gecko.background.common.TestUtils; 1.20 + 1.21 +public abstract class FakeProfileTestCase extends ActivityInstrumentationTestCase2<Activity> { 1.22 + 1.23 + protected Context context; 1.24 + protected File fakeProfileDirectory; 1.25 + private String sharedPrefsName; 1.26 + 1.27 + public FakeProfileTestCase() { 1.28 + super(Activity.class); 1.29 + } 1.30 + 1.31 + /** 1.32 + * Returns the profile cache suffix. This is computed once for each test function (in setUp()). 1.33 + * Note that the return value is not cached. 1.34 + */ 1.35 + protected String getCacheSuffix() { 1.36 + return this.getClass().getName() + "-" + System.currentTimeMillis(); 1.37 + } 1.38 + 1.39 + @Override 1.40 + protected void setUp() throws Exception { 1.41 + super.setUp(); 1.42 + context = getInstrumentation().getTargetContext(); 1.43 + File cache = context.getCacheDir(); 1.44 + fakeProfileDirectory = new File(cache.getAbsolutePath() + getCacheSuffix()); 1.45 + if (fakeProfileDirectory.exists()) { 1.46 + TestUtils.deleteDirectoryRecursively(fakeProfileDirectory); 1.47 + } 1.48 + if (!fakeProfileDirectory.mkdir()) { 1.49 + throw new IllegalStateException("Could not create temporary directory."); 1.50 + } 1.51 + // Class name of the form: ActivityInstrumentationTestCase2$FakeProfileTestCase$extendingClass. 1.52 + sharedPrefsName = this.getClass().getName() + "-" + UUID.randomUUID(); 1.53 + } 1.54 + 1.55 + @Override 1.56 + protected void tearDown() throws Exception { 1.57 + TestUtils.deleteDirectoryRecursively(fakeProfileDirectory); 1.58 + super.tearDown(); 1.59 + } 1.60 + 1.61 + public SharedPreferences getSharedPreferences() { 1.62 + return context.getSharedPreferences(sharedPrefsName, GlobalConstants.SHARED_PREFERENCES_MODE); 1.63 + } 1.64 +}