michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.helpers; michael@0: michael@0: import java.io.File; michael@0: michael@0: import android.app.Activity; michael@0: import android.content.Context; michael@0: import android.content.SharedPreferences; michael@0: import android.test.ActivityInstrumentationTestCase2; michael@0: import java.util.UUID; michael@0: michael@0: import org.mozilla.gecko.background.common.GlobalConstants; michael@0: michael@0: import org.mozilla.gecko.background.common.TestUtils; michael@0: michael@0: public abstract class FakeProfileTestCase extends ActivityInstrumentationTestCase2 { michael@0: michael@0: protected Context context; michael@0: protected File fakeProfileDirectory; michael@0: private String sharedPrefsName; michael@0: michael@0: public FakeProfileTestCase() { michael@0: super(Activity.class); michael@0: } michael@0: michael@0: /** michael@0: * Returns the profile cache suffix. This is computed once for each test function (in setUp()). michael@0: * Note that the return value is not cached. michael@0: */ michael@0: protected String getCacheSuffix() { michael@0: return this.getClass().getName() + "-" + System.currentTimeMillis(); michael@0: } michael@0: michael@0: @Override michael@0: protected void setUp() throws Exception { michael@0: super.setUp(); michael@0: context = getInstrumentation().getTargetContext(); michael@0: File cache = context.getCacheDir(); michael@0: fakeProfileDirectory = new File(cache.getAbsolutePath() + getCacheSuffix()); michael@0: if (fakeProfileDirectory.exists()) { michael@0: TestUtils.deleteDirectoryRecursively(fakeProfileDirectory); michael@0: } michael@0: if (!fakeProfileDirectory.mkdir()) { michael@0: throw new IllegalStateException("Could not create temporary directory."); michael@0: } michael@0: // Class name of the form: ActivityInstrumentationTestCase2$FakeProfileTestCase$extendingClass. michael@0: sharedPrefsName = this.getClass().getName() + "-" + UUID.randomUUID(); michael@0: } michael@0: michael@0: @Override michael@0: protected void tearDown() throws Exception { michael@0: TestUtils.deleteDirectoryRecursively(fakeProfileDirectory); michael@0: super.tearDown(); michael@0: } michael@0: michael@0: public SharedPreferences getSharedPreferences() { michael@0: return context.getSharedPreferences(sharedPrefsName, GlobalConstants.SHARED_PREFERENCES_MODE); michael@0: } michael@0: }