mobile/android/tests/background/junit3/src/helpers/FakeProfileTestCase.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 package org.mozilla.gecko.background.helpers;
michael@0 5
michael@0 6 import java.io.File;
michael@0 7
michael@0 8 import android.app.Activity;
michael@0 9 import android.content.Context;
michael@0 10 import android.content.SharedPreferences;
michael@0 11 import android.test.ActivityInstrumentationTestCase2;
michael@0 12 import java.util.UUID;
michael@0 13
michael@0 14 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 15
michael@0 16 import org.mozilla.gecko.background.common.TestUtils;
michael@0 17
michael@0 18 public abstract class FakeProfileTestCase extends ActivityInstrumentationTestCase2<Activity> {
michael@0 19
michael@0 20 protected Context context;
michael@0 21 protected File fakeProfileDirectory;
michael@0 22 private String sharedPrefsName;
michael@0 23
michael@0 24 public FakeProfileTestCase() {
michael@0 25 super(Activity.class);
michael@0 26 }
michael@0 27
michael@0 28 /**
michael@0 29 * Returns the profile cache suffix. This is computed once for each test function (in setUp()).
michael@0 30 * Note that the return value is not cached.
michael@0 31 */
michael@0 32 protected String getCacheSuffix() {
michael@0 33 return this.getClass().getName() + "-" + System.currentTimeMillis();
michael@0 34 }
michael@0 35
michael@0 36 @Override
michael@0 37 protected void setUp() throws Exception {
michael@0 38 super.setUp();
michael@0 39 context = getInstrumentation().getTargetContext();
michael@0 40 File cache = context.getCacheDir();
michael@0 41 fakeProfileDirectory = new File(cache.getAbsolutePath() + getCacheSuffix());
michael@0 42 if (fakeProfileDirectory.exists()) {
michael@0 43 TestUtils.deleteDirectoryRecursively(fakeProfileDirectory);
michael@0 44 }
michael@0 45 if (!fakeProfileDirectory.mkdir()) {
michael@0 46 throw new IllegalStateException("Could not create temporary directory.");
michael@0 47 }
michael@0 48 // Class name of the form: ActivityInstrumentationTestCase2$FakeProfileTestCase$extendingClass.
michael@0 49 sharedPrefsName = this.getClass().getName() + "-" + UUID.randomUUID();
michael@0 50 }
michael@0 51
michael@0 52 @Override
michael@0 53 protected void tearDown() throws Exception {
michael@0 54 TestUtils.deleteDirectoryRecursively(fakeProfileDirectory);
michael@0 55 super.tearDown();
michael@0 56 }
michael@0 57
michael@0 58 public SharedPreferences getSharedPreferences() {
michael@0 59 return context.getSharedPreferences(sharedPrefsName, GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0 60 }
michael@0 61 }

mercurial