|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 package org.mozilla.gecko.background.helpers; |
|
5 |
|
6 import java.io.File; |
|
7 |
|
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; |
|
13 |
|
14 import org.mozilla.gecko.background.common.GlobalConstants; |
|
15 |
|
16 import org.mozilla.gecko.background.common.TestUtils; |
|
17 |
|
18 public abstract class FakeProfileTestCase extends ActivityInstrumentationTestCase2<Activity> { |
|
19 |
|
20 protected Context context; |
|
21 protected File fakeProfileDirectory; |
|
22 private String sharedPrefsName; |
|
23 |
|
24 public FakeProfileTestCase() { |
|
25 super(Activity.class); |
|
26 } |
|
27 |
|
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 } |
|
35 |
|
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 } |
|
51 |
|
52 @Override |
|
53 protected void tearDown() throws Exception { |
|
54 TestUtils.deleteDirectoryRecursively(fakeProfileDirectory); |
|
55 super.tearDown(); |
|
56 } |
|
57 |
|
58 public SharedPreferences getSharedPreferences() { |
|
59 return context.getSharedPreferences(sharedPrefsName, GlobalConstants.SHARED_PREFERENCES_MODE); |
|
60 } |
|
61 } |