summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/util/Environment.java
blob: b2ed9df4d5b1a271903b0f85fa84ebb324c04dfc (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

package org.mozilla.geckoview.test.util;

import android.os.Build;
import android.os.Bundle;
import android.os.Debug;
import androidx.test.platform.app.InstrumentationRegistry;
import org.mozilla.geckoview.BuildConfig;

public class Environment {
  public static final long DEFAULT_TIMEOUT_MILLIS = 30000;
  public static final long DEFAULT_ARM_DEVICE_TIMEOUT_MILLIS = 30000;
  public static final long DEFAULT_ARM_EMULATOR_TIMEOUT_MILLIS = 120000;
  public static final long DEFAULT_X86_DEVICE_TIMEOUT_MILLIS = 30000;
  public static final long DEFAULT_X86_EMULATOR_TIMEOUT_MILLIS = 30000;
  public static final long DEFAULT_IDE_DEBUG_TIMEOUT_MILLIS = 86400000;

  private String getEnvVar(final String name) {
    final int nameLen = name.length();
    final Bundle args = InstrumentationRegistry.getArguments();
    String env = args.getString("env0", null);
    for (int i = 1; env != null; i++) {
      if (env.length() >= nameLen + 1 && env.startsWith(name) && env.charAt(nameLen) == '=') {
        return env.substring(nameLen + 1);
      }
      env = args.getString("env" + i, null);
    }
    return "";
  }

  public boolean isAutomation() {
    return !getEnvVar("MOZ_IN_AUTOMATION").isEmpty();
  }

  public boolean shouldShutdownOnCrash() {
    return !getEnvVar("MOZ_CRASHREPORTER_SHUTDOWN").isEmpty();
  }

  public boolean isDebugging() {
    return Debug.isDebuggerConnected();
  }

  public boolean isEmulator() {
    return "generic".equals(Build.DEVICE) || Build.DEVICE.startsWith("generic_");
  }

  public boolean isDebugBuild() {
    return BuildConfig.DEBUG_BUILD;
  }

  public boolean isX86() {
    final String abi;
    if (Build.VERSION.SDK_INT >= 21) {
      abi = Build.SUPPORTED_ABIS[0];
    } else {
      abi = Build.CPU_ABI;
    }

    return abi.startsWith("x86");
  }

  public boolean isFission() {
    // NOTE: This isn't accurate, as it doesn't take into account the default
    // value of the pref or environment variables like
    // `MOZ_FORCE_DISABLE_FISSION`.
    return getEnvVar("MOZ_FORCE_ENABLE_FISSION").equals("1");
  }

  public boolean isWebrender() {
    return getEnvVar("MOZ_WEBRENDER").equals("1");
  }

  public boolean isIsolatedProcess() {
    return BuildConfig.MOZ_ANDROID_CONTENT_SERVICE_ISOLATED_PROCESS;
  }

  public long getScaledTimeoutMillis() {
    if (isX86()) {
      return isEmulator() ? DEFAULT_X86_EMULATOR_TIMEOUT_MILLIS : DEFAULT_X86_DEVICE_TIMEOUT_MILLIS;
    }
    return isEmulator() ? DEFAULT_ARM_EMULATOR_TIMEOUT_MILLIS : DEFAULT_ARM_DEVICE_TIMEOUT_MILLIS;
  }

  public long getDefaultTimeoutMillis() {
    return isDebugging() ? DEFAULT_IDE_DEBUG_TIMEOUT_MILLIS : getScaledTimeoutMillis();
  }

  public boolean isNightly() {
    return BuildConfig.NIGHTLY_BUILD;
  }
}