android.app.Instrumentation#getTargetContext ( )源码实例Demo

下面列出了android.app.Instrumentation#getTargetContext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: fdroidclient   文件: FileCompatTest.java
/**
 * Prefer internal over external storage, because external tends to be FAT filesystems,
 * which don't support symlinks (which we test using this method).
 */
public static File getWriteableDir(Instrumentation instrumentation) {
    Context context = instrumentation.getContext();
    Context targetContext = instrumentation.getTargetContext();

    File[] dirsToTry = new File[]{
            context.getCacheDir(),
            context.getFilesDir(),
            targetContext.getCacheDir(),
            targetContext.getFilesDir(),
            context.getExternalCacheDir(),
            context.getExternalFilesDir(null),
            targetContext.getExternalCacheDir(),
            targetContext.getExternalFilesDir(null),
            Environment.getExternalStorageDirectory(),
    };

    return getWriteableDir(dirsToTry);
}
 
源代码2 项目: firefox-echo-show   文件: ScreenshotTest.java
@Before
public void setUpScreenshots() {
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    targetContext = instrumentation.getTargetContext();
    device = UiDevice.getInstance(instrumentation);

    // Use this to switch between default strategy and HostScreencap strategy
    Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());
    //Screengrab.setDefaultScreenshotStrategy(new HostScreencapScreenshotStrategy(device));

    device.waitForIdle();
}
 
源代码3 项目: focus-android   文件: ScreenshotTest.java
@Before
public void setUpScreenshots() {
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    targetContext = instrumentation.getTargetContext();
    device = UiDevice.getInstance(instrumentation);

    // Use this to switch between default strategy and HostScreencap strategy
    Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());
    //Screengrab.setDefaultScreenshotStrategy(new HostScreencapScreenshotStrategy(device));

    device.waitForIdle();
}
 
源代码4 项目: StoreBox   文件: PreferencesTypeAndModeTestCase.java
public InjectedContext(
        Instrumentation instr,
        String expectedName,
        Integer expectedMode,
        AtomicInteger count) {
    
    super(instr.getTargetContext());
    
    this.expectedName = expectedName;
    this.expectedMode = expectedMode;
    this.count = count;
}
 
源代码5 项目: android-galaxyzoo   文件: ZooniverseClientTest.java
private static ZooniverseClient createZooniverseClient(final MockWebServer server) {
    final HttpUrl mockUrl = server.url("/");

    final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    final Context context = instrumentation.getTargetContext();

    return new ZooniverseClient(context, mockUrl.toString());
}
 
源代码6 项目: android-galaxyzoo   文件: TestUtils.java
static void setTheme() {
    //Avoid this exception:
    //java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    final Context context = instrumentation.getTargetContext();
    context.setTheme(R.style.AppTheme);

}
 
源代码7 项目: DoraemonKit   文件: InstrumentationLeakDetector.java
public @NonNull InstrumentationLeakResults detectLeaks() {
  Instrumentation instrumentation = getInstrumentation();
  Context context = instrumentation.getTargetContext();
  RefWatcher refWatcher = LeakCanary.installedRefWatcher();
  Set<String> retainedKeys = refWatcher.getRetainedKeys();

  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  instrumentation.waitForIdleSync();
  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  GcTrigger.DEFAULT.runGc();
  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  // Waiting for any delayed UI post (e.g. scroll) to clear. This shouldn't be needed, but
  // Android simply has way too many delayed posts that aren't canceled when views are detached.
  SystemClock.sleep(2000);

  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  // Aaand we wait some more.
  // 4 seconds (2+2) is greater than the 3 seconds delay for
  // FINISH_TOKEN in android.widget.Filter
  SystemClock.sleep(2000);
  GcTrigger.DEFAULT.runGc();

  if (refWatcher.isEmpty()) {
    return InstrumentationLeakResults.NONE;
  }

  // We're always reusing the same file since we only execute this once at a time.
  File heapDumpFile = new File(context.getFilesDir(), "instrumentation_tests_heapdump.hprof");
  try {
    Debug.dumpHprofData(heapDumpFile.getAbsolutePath());
  } catch (Exception e) {
    CanaryLog.d(e, "Could not dump heap");
    return InstrumentationLeakResults.NONE;
  }

  HeapDump.Builder heapDumpBuilder = refWatcher.getHeapDumpBuilder();
  HeapAnalyzer heapAnalyzer =
      new HeapAnalyzer(heapDumpBuilder.excludedRefs, AnalyzerProgressListener.NONE,
          heapDumpBuilder.reachabilityInspectorClasses);

  List<TrackedReference> trackedReferences = heapAnalyzer.findTrackedReferences(heapDumpFile);

  List<InstrumentationLeakResults.Result> detectedLeaks = new ArrayList<>();
  List<InstrumentationLeakResults.Result> excludedLeaks = new ArrayList<>();
  List<InstrumentationLeakResults.Result> failures = new ArrayList<>();

  for (TrackedReference trackedReference : trackedReferences) {
    // Ignore any Weak Reference that this test does not care about.
    if (!retainedKeys.contains(trackedReference.key)) {
      continue;
    }

    HeapDump heapDump = HeapDump.builder()
        .heapDumpFile(heapDumpFile)
        .referenceKey(trackedReference.key)
        .referenceName(trackedReference.name)
        .excludedRefs(heapDumpBuilder.excludedRefs)
        .reachabilityInspectorClasses(heapDumpBuilder.reachabilityInspectorClasses)
        .build();

    AnalysisResult analysisResult =
        heapAnalyzer.checkForLeak(heapDumpFile, trackedReference.key, false);

    InstrumentationLeakResults.Result leakResult =
        new InstrumentationLeakResults.Result(heapDump, analysisResult);

    if (analysisResult.leakFound) {
      if (!analysisResult.excludedLeak) {
        detectedLeaks.add(leakResult);
      } else {
        excludedLeaks.add(leakResult);
      }
    } else if (analysisResult.failure != null) {
      failures.add(leakResult);
    }
  }

  CanaryLog.d("Found %d proper leaks, %d excluded leaks and %d leak analysis failures",
      detectedLeaks.size(),
      excludedLeaks.size(),
      failures.size());

  return new InstrumentationLeakResults(detectedLeaks, excludedLeaks, failures);
}
 
源代码8 项目: StoreBox   文件: KeysAndDefaultValuesTestCase.java
public InjectedContext(Instrumentation instr, SharedPreferences prefs) {
    super(instr.getTargetContext());
    
    this.prefs = prefs;
}
 
private static ZooniverseClient createZooniverseClient() {
    final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    final Context context = instrumentation.getTargetContext();
    return new ZooniverseClient(context, Config.SERVER);
}
 
源代码10 项目: StoreBox   文件: SaveModeTestCase.java
public InjectedContext(
        Instrumentation instr, SharedPreferences prefs) {
    
    super(instr.getTargetContext());
    
    this.prefs = prefs;
}
 
源代码11 项目: StoreBox   文件: ForwardingMethodsTestCase.java
public InjectedContext(
        Instrumentation instr, SharedPreferences prefs) {

    super(instr.getTargetContext());

    this.prefs = prefs;
}