summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/utils/PerfTestingUtils.java
blob: 543ae5e6687a4322253ffe9e7bd9e57b0318df78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.mapbox.mapboxsdk.testapp.utils;

import android.content.Context;
import android.os.Environment;
import android.support.test.InstrumentationRegistry;

import java.io.File;

public class PerfTestingUtils {
  /**
   * Retrieve the directory where test files should be written to.
   * This directory is local to the application and removed when the app is uninstalled.
   */
  public static File getTestDir(String className, String testName) {

    File rootDir = Environment.getExternalStorageDirectory();//getAppContext().getFilesDir();

    // Create a test data subdirectory.
    File testFileDir = new File(rootDir, "testdata");
    if (getTranslatedTestName(className, testName) != null) {
      testFileDir = new File(testFileDir, getTranslatedTestName(className, testName));
    }

    if (!testFileDir.exists()) {
      if (!testFileDir.mkdirs()) {
        throw new RuntimeException("Unable to create test file directory.");
      }
    }
    return testFileDir;

  }

  /**
   * Retrieve the app-under-test's {@code Context}.
   */
  public static Context getAppContext() {
    return InstrumentationRegistry.getInstrumentation().getTargetContext();
  }

  /**
   * Retrieve a file handle that is within the testing directory where tests should be written to.
   */
  public static File getTestRunFile(String filename) {
    return new File(getTestDir(null, null), filename);
  }

  /**
   * Retrieve the test run directory where tests should be written to.
   */
  public static File getTestRunDir() {
    return getTestDir(null, null);
  }

  private static String getTranslatedTestName(String className, String testName) {
    if (className == null || testName == null) {
      return null;
    }
    String base = className + "_" + testName;

    // Shorten the common strings so "com.google.android" comes out as "c.g.a" for brevity.
    base = base.replace("com", "c")
      .replace("google", "g")
      .replace("android", "a")
      .replace("perfmatters", "pm")
      .replace("automatingperformancetesting", "apt");
    return base;

  }

  /**
   * Retrieve a file handle that is within the testing directory where tests should be written to.
   */
  public static File getTestFile(String className, String testName, String filename) {
    return new File(getTestDir(className, testName), filename);
  }
}