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.

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

mercurial