Commit 53830713 authored by Yoland Yan's avatar Yoland Yan Committed by Commit Bot

Remove multiple ITR from manifest and fix service test run on J4 issue

Use InstrumentationRegistry to get actual target context in the two
service test cases. Andr remove BaseITR, BaseChromiumITR, and ChromeITR

Bug: 807251
Change-Id: I9d945ce7c2ce264f84f10124af6b399cba690eef
Reviewed-on: https://chromium-review.googlesource.com/894125Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Commit-Queue: Yoland Yan <yolandyan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533165}
parent 94092b8f
......@@ -2788,8 +2788,6 @@ if (is_android) {
"//third_party/ub-uiautomator:ub_uiautomator_java",
]
java_files = [
"test/android/javatests/src/org/chromium/base/test/BaseChromiumInstrumentationTestRunner.java",
"test/android/javatests/src/org/chromium/base/test/BaseInstrumentationTestRunner.java",
"test/android/javatests/src/org/chromium/base/test/BaseJUnit4ClassRunner.java",
"test/android/javatests/src/org/chromium/base/test/BaseChromiumAndroidJUnitRunner.java",
"test/android/javatests/src/org/chromium/base/test/BaseChromiumRunnerCommon.java",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.test;
import android.app.Application;
import android.content.Context;
import org.chromium.base.multidex.ChromiumMultiDexInstaller;
import org.chromium.base.test.util.CommandLineFlags;
// TODO(jbudorick): Add support for on-device handling of timeouts.
/**
* An Instrumentation test runner for applications that are based on
* {@code org.chromium.base.BaseChromiumApplication}
*/
public class BaseChromiumInstrumentationTestRunner extends BaseInstrumentationTestRunner {
@Override
public Application newApplication(ClassLoader cl, String className, Context context)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexContextWrapper(
getContext(), getTargetContext()));
BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTargetContext());
return super.newApplication(cl, className, context);
}
/**
* Override this method to register hooks and checks to be run for each test. Make sure to call
* the base implementation if you do so.
*
* @see BaseTestResult#addSkipCheck(BaseTestResult.SkipCheck)
* @see BaseTestResult#addPreTestHook(BaseTestResult.PreTestHook)
*/
@Override
protected void addTestHooks(BaseTestResult result) {
super.addTestHooks(result);
result.addPreTestHook(CommandLineFlags.getRegistrationHook());
}
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.test;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.test.AndroidTestRunner;
import android.test.InstrumentationTestRunner;
import junit.framework.TestResult;
import org.chromium.base.test.util.DisableIfSkipCheck;
import org.chromium.base.test.util.MinAndroidSdkLevelSkipCheck;
import org.chromium.base.test.util.RestrictionSkipCheck;
import org.chromium.test.reporter.TestStatusListener;
// TODO(jbudorick): Add support for on-device handling of timeouts.
/**
* An Instrumentation test runner that checks SDK level for tests with specific requirements.
*
* If the package application for which the instrumetation targets is based on
* {@code org.chromium.base.BaseChromiumApplication}, one should use
* {@code BaseChromiumInstrumentationTestRunner}
*/
public class BaseInstrumentationTestRunner extends InstrumentationTestRunner {
@Override
protected AndroidTestRunner getAndroidTestRunner() {
AndroidTestRunner runner = new AndroidTestRunner() {
@Override
protected TestResult createTestResult() {
BaseTestResult r = new BaseTestResult(BaseInstrumentationTestRunner.this);
addTestHooks(r);
return r;
}
};
runner.addTestListener(new TestStatusListener(getContext()));
return runner;
}
/**
* Override this method to register hooks and checks to be run for each test. Make sure to call
* the base implementation if you do so.
*
* @see BaseTestResult#addSkipCheck(BaseTestResult.SkipCheck)
* @see BaseTestResult#addPreTestHook(BaseTestResult.PreTestHook)
*/
protected void addTestHooks(BaseTestResult result) {
result.addSkipCheck(new MinAndroidSdkLevelSkipCheck());
result.addSkipCheck(new RestrictionSkipCheck(getTargetContext()));
result.addSkipCheck(new DisableIfSkipCheck());
}
@Override
public Context getTargetContext() {
return new ContextWrapper(super.getTargetContext()) {
@Override
public void startActivity(Intent intent) {
Context context = getApplicationContext();
ActivityOptions activityOptions =
ActivityOptions.makeCustomAnimation(context, 0, 0);
startActivity(intent, activityOptions.toBundle());
}
};
}
}
......@@ -58,7 +58,4 @@
android:targetPackage="{{manifest_package}}"
android:label="Tests for {{manifest_package}}"
chromium-junit4="true"/>
<instrumentation android:name="org.chromium.chrome.test.ChromeInstrumentationTestRunner"
android:targetPackage="{{manifest_package}}"
android:label="Tests for {{manifest_package}}"/>
</manifest>
......@@ -10,11 +10,14 @@ import android.content.SharedPreferences;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.test.ServiceTestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -112,17 +115,20 @@ public class DownloadNotificationServiceTest
});
}
@Before
@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
setContext(InstrumentationRegistry.getTargetContext());
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
// TODO(yolandyan): added for debugging reasons, remove if tests no longer flakes
Assert.assertNull(sharedPrefs.getStringSet(
DownloadSharedPreferenceHelper.KEY_PENDING_DOWNLOAD_NOTIFICATIONS, null));
}
@After
@Override
protected void tearDown() throws Exception {
public void tearDown() throws Exception {
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.remove(DownloadSharedPreferenceHelper.KEY_PENDING_DOWNLOAD_NOTIFICATIONS);
......@@ -261,7 +267,7 @@ public class DownloadNotificationServiceTest
@DisabledTest(message = "crbug.com/773346")
public void testPausingWithOngoingDownloads() {
setupService();
Context mockContext = new AdvancedMockContext(getSystemContext());
Context mockContext = new AdvancedMockContext(InstrumentationRegistry.getTargetContext());
getService().setContext(mockContext);
Set<String> notifications = new HashSet<>();
notifications.add(buildEntryString(1, "test1", true, true));
......@@ -295,7 +301,7 @@ public class DownloadNotificationServiceTest
@DisabledTest(message = "crbug.com/773346")
public void testAddingAndCancelingNotifications() {
setupService();
Context mockContext = new AdvancedMockContext(getSystemContext());
Context mockContext = new AdvancedMockContext(InstrumentationRegistry.getTargetContext());
getService().setContext(mockContext);
Set<String> notifications = new HashSet<>();
String guid1 = UUID.randomUUID().toString();
......@@ -382,7 +388,7 @@ public class DownloadNotificationServiceTest
@DisabledTest(message = "crbug.com/773346")
public void testResumeAllPendingDownloads() throws Exception {
setupService();
Context mockContext = new AdvancedMockContext(getSystemContext());
Context mockContext = new AdvancedMockContext(InstrumentationRegistry.getTargetContext());
getService().setContext(mockContext);
Set<String> notifications = new HashSet<>();
String guid1 = UUID.randomUUID().toString();
......@@ -402,8 +408,8 @@ public class DownloadNotificationServiceTest
DownloadNotificationService service = bindNotificationService();
DownloadManagerService.disableNetworkListenerForTest();
final MockDownloadManagerService manager =
new MockDownloadManagerService(getSystemContext().getApplicationContext());
final MockDownloadManagerService manager = new MockDownloadManagerService(
InstrumentationRegistry.getTargetContext().getApplicationContext());
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
......@@ -431,7 +437,7 @@ public class DownloadNotificationServiceTest
@DisabledTest(message = "crbug.com/773346")
public void testIncognitoDownloadCanceledOnServiceShutdown() throws Exception {
setupService();
Context mockContext = new AdvancedMockContext(getSystemContext());
Context mockContext = new AdvancedMockContext(InstrumentationRegistry.getTargetContext());
getService().setContext(mockContext);
Set<String> notifications = new HashSet<>();
String uuid = UUID.randomUUID().toString();
......
......@@ -9,7 +9,6 @@ android_library("chrome_java_test_support") {
java_files = [
"javatests/src/org/chromium/chrome/test/BottomSheetTestRule.java",
"javatests/src/org/chromium/chrome/test/ChromeActivityTestRule.java",
"javatests/src/org/chromium/chrome/test/ChromeInstrumentationTestRunner.java",
"javatests/src/org/chromium/chrome/test/ChromeJUnit4ClassRunner.java",
"javatests/src/org/chromium/chrome/test/ChromeJUnit4RunnerDelegate.java",
"javatests/src/org/chromium/chrome/test/ChromeTabbedActivityTestRule.java",
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.test;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import org.chromium.base.CommandLine;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseChromiumInstrumentationTestRunner;
import org.chromium.base.test.BaseTestResult;
import org.chromium.base.test.util.RestrictionSkipCheck;
import org.chromium.chrome.browser.ChromeVersionInfo;
import org.chromium.chrome.browser.vr_shell.VrClassesWrapper;
import org.chromium.chrome.browser.vr_shell.VrDaydreamApi;
import org.chromium.chrome.test.util.ChromeRestriction;
import org.chromium.content.browser.test.ChildProcessAllocatorSettingsHook;
import org.chromium.policy.test.annotations.Policies;
import org.chromium.ui.test.util.UiDisableIfSkipCheck;
import org.chromium.ui.test.util.UiRestrictionSkipCheck;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* An Instrumentation test runner that optionally spawns a test HTTP server.
* The server's root directory is the device's external storage directory.
*/
public class ChromeInstrumentationTestRunner extends BaseChromiumInstrumentationTestRunner {
@Override
public void onCreate(Bundle arguments) {
super.onCreate(arguments);
}
@Override
protected void addTestHooks(BaseTestResult result) {
super.addTestHooks(result);
result.addSkipCheck(new ChromeRestrictionSkipCheck(getTargetContext()));
result.addSkipCheck(new UiDisableIfSkipCheck(getTargetContext()));
result.addSkipCheck(new UiRestrictionSkipCheck(getTargetContext()));
result.addPreTestHook(Policies.getRegistrationHook());
result.addPreTestHook(new ChildProcessAllocatorSettingsHook());
}
static class ChromeRestrictionSkipCheck extends RestrictionSkipCheck {
private VrDaydreamApi mDaydreamApi;
private boolean mAttemptedToGetApi;
public ChromeRestrictionSkipCheck(Context targetContext) {
super(targetContext);
}
@SuppressWarnings("unchecked")
private VrDaydreamApi getDaydreamApi() {
if (!mAttemptedToGetApi) {
mAttemptedToGetApi = true;
try {
Class<? extends VrClassesWrapper> vrClassesBuilderClass =
(Class<? extends VrClassesWrapper>) Class.forName(
"org.chromium.chrome.browser.vr_shell.VrClassesWrapperImpl");
Constructor<?> vrClassesBuilderConstructor =
vrClassesBuilderClass.getConstructor();
VrClassesWrapper vrClassesBuilder =
(VrClassesWrapper) vrClassesBuilderConstructor.newInstance();
mDaydreamApi = vrClassesBuilder.createVrDaydreamApi(getTargetContext());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException e) {
return null;
}
}
return mDaydreamApi;
}
private boolean isDaydreamReady() {
return getDaydreamApi() == null ? false :
getDaydreamApi().isDaydreamReadyDevice();
}
private boolean isDaydreamViewPaired() {
if (getDaydreamApi() == null) {
return false;
}
// isDaydreamCurrentViewer() creates a concrete instance of DaydreamApi,
// which can only be done on the main thread
FutureTask<Boolean> checker = new FutureTask<>(new Callable<Boolean>() {
@Override
public Boolean call() {
return getDaydreamApi().isDaydreamCurrentViewer();
}
});
ThreadUtils.runOnUiThreadBlocking(checker);
try {
return checker.get().booleanValue();
} catch (CancellationException | InterruptedException | ExecutionException
| IllegalArgumentException e) {
return false;
}
}
private boolean isDonEnabled() {
// We can't directly check whether the VR DON flow is enabled since
// we don't have permission to read the VrCore settings file. Instead,
// pass a flag.
return CommandLine.getInstance().hasSwitch("don-enabled");
}
@Override
protected boolean restrictionApplies(String restriction) {
if (TextUtils.equals(
restriction, ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
&& (ConnectionResult.SUCCESS
!= GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
getTargetContext()))) {
return true;
}
if (TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_OFFICIAL_BUILD)
&& (!ChromeVersionInfo.isOfficialBuild())) {
return true;
}
if (TextUtils.equals(restriction, ChromeRestriction.RESTRICTION_TYPE_DEVICE_DAYDREAM)
|| TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_DEVICE_NON_DAYDREAM)) {
boolean isDaydream = isDaydreamReady();
if (TextUtils.equals(
restriction, ChromeRestriction.RESTRICTION_TYPE_DEVICE_DAYDREAM)
&& !isDaydream) {
return true;
} else if (TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_DEVICE_NON_DAYDREAM)
&& isDaydream) {
return true;
}
}
if (TextUtils.equals(restriction, ChromeRestriction.RESTRICTION_TYPE_VIEWER_DAYDREAM)
|| TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_VIEWER_NON_DAYDREAM)) {
boolean daydreamViewPaired = isDaydreamViewPaired();
if (TextUtils.equals(
restriction, ChromeRestriction.RESTRICTION_TYPE_VIEWER_DAYDREAM)
&& !daydreamViewPaired) {
return true;
} else if (TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_VIEWER_NON_DAYDREAM)
&& daydreamViewPaired) {
return true;
}
}
if (TextUtils.equals(restriction, ChromeRestriction.RESTRICTION_TYPE_DON_ENABLED)) {
return !isDonEnabled();
}
return false;
}
}
}
......@@ -4,23 +4,40 @@
package org.chromium.chrome.test;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.text.TextUtils;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import org.junit.runners.model.InitializationError;
import org.chromium.base.CollectionUtil;
import org.chromium.base.CommandLine;
import org.chromium.base.CommandLineInitUtil;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.BaseTestResult.PreTestHook;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.RestrictionSkipCheck;
import org.chromium.base.test.util.SkipCheck;
import org.chromium.chrome.test.ChromeInstrumentationTestRunner.ChromeRestrictionSkipCheck;
import org.chromium.chrome.browser.ChromeVersionInfo;
import org.chromium.chrome.browser.vr_shell.VrClassesWrapper;
import org.chromium.chrome.browser.vr_shell.VrDaydreamApi;
import org.chromium.chrome.test.util.ChromeRestriction;
import org.chromium.content.browser.test.ChildProcessAllocatorSettingsHook;
import org.chromium.policy.test.annotations.Policies;
import org.chromium.ui.test.util.UiDisableIfSkipCheck;
import org.chromium.ui.test.util.UiRestrictionSkipCheck;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* A custom runner for //chrome JUnit4 tests.
......@@ -52,4 +69,114 @@ public class ChromeJUnit4ClassRunner extends BaseJUnit4ClassRunner {
CommandLineInitUtil.initCommandLine(
InstrumentationRegistry.getTargetContext(), CommandLineFlags.getTestCmdLineFile());
}
private static class ChromeRestrictionSkipCheck extends RestrictionSkipCheck {
private VrDaydreamApi mDaydreamApi;
private boolean mAttemptedToGetApi;
public ChromeRestrictionSkipCheck(Context targetContext) {
super(targetContext);
}
@SuppressWarnings("unchecked")
private VrDaydreamApi getDaydreamApi() {
if (!mAttemptedToGetApi) {
mAttemptedToGetApi = true;
try {
Class<? extends VrClassesWrapper> vrClassesBuilderClass =
(Class<? extends VrClassesWrapper>) Class.forName(
"org.chromium.chrome.browser.vr_shell.VrClassesWrapperImpl");
Constructor<?> vrClassesBuilderConstructor =
vrClassesBuilderClass.getConstructor();
VrClassesWrapper vrClassesBuilder =
(VrClassesWrapper) vrClassesBuilderConstructor.newInstance();
mDaydreamApi = vrClassesBuilder.createVrDaydreamApi(getTargetContext());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException e) {
return null;
}
}
return mDaydreamApi;
}
private boolean isDaydreamReady() {
return getDaydreamApi() == null ? false : getDaydreamApi().isDaydreamReadyDevice();
}
private boolean isDaydreamViewPaired() {
if (getDaydreamApi() == null) {
return false;
}
// isDaydreamCurrentViewer() creates a concrete instance of DaydreamApi,
// which can only be done on the main thread
FutureTask<Boolean> checker = new FutureTask<>(new Callable<Boolean>() {
@Override
public Boolean call() {
return getDaydreamApi().isDaydreamCurrentViewer();
}
});
ThreadUtils.runOnUiThreadBlocking(checker);
try {
return checker.get().booleanValue();
} catch (CancellationException | InterruptedException | ExecutionException
| IllegalArgumentException e) {
return false;
}
}
private boolean isDonEnabled() {
// We can't directly check whether the VR DON flow is enabled since
// we don't have permission to read the VrCore settings file. Instead,
// pass a flag.
return CommandLine.getInstance().hasSwitch("don-enabled");
}
@Override
protected boolean restrictionApplies(String restriction) {
if (TextUtils.equals(
restriction, ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
&& (ConnectionResult.SUCCESS
!= GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
getTargetContext()))) {
return true;
}
if (TextUtils.equals(restriction, ChromeRestriction.RESTRICTION_TYPE_OFFICIAL_BUILD)
&& (!ChromeVersionInfo.isOfficialBuild())) {
return true;
}
if (TextUtils.equals(restriction, ChromeRestriction.RESTRICTION_TYPE_DEVICE_DAYDREAM)
|| TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_DEVICE_NON_DAYDREAM)) {
boolean isDaydream = isDaydreamReady();
if (TextUtils.equals(
restriction, ChromeRestriction.RESTRICTION_TYPE_DEVICE_DAYDREAM)
&& !isDaydream) {
return true;
} else if (TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_DEVICE_NON_DAYDREAM)
&& isDaydream) {
return true;
}
}
if (TextUtils.equals(restriction, ChromeRestriction.RESTRICTION_TYPE_VIEWER_DAYDREAM)
|| TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_VIEWER_NON_DAYDREAM)) {
boolean daydreamViewPaired = isDaydreamViewPaired();
if (TextUtils.equals(
restriction, ChromeRestriction.RESTRICTION_TYPE_VIEWER_DAYDREAM)
&& !daydreamViewPaired) {
return true;
} else if (TextUtils.equals(restriction,
ChromeRestriction.RESTRICTION_TYPE_VIEWER_NON_DAYDREAM)
&& daydreamViewPaired) {
return true;
}
}
if (TextUtils.equals(restriction, ChromeRestriction.RESTRICTION_TYPE_DON_ENABLED)) {
return !isDonEnabled();
}
return false;
}
}
}
......@@ -7,6 +7,7 @@ package org.chromium.components.invalidation;
import android.accounts.Account;
import android.content.ComponentName;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.test.ServiceTestCase;
......@@ -71,7 +72,7 @@ public class InvalidationClientServiceTest extends
public void setUp() throws Exception {
super.setUp();
mStartServiceIntents = new ArrayList<>();
setContext(new AdvancedMockContext(getContext()) {
setContext(new AdvancedMockContext(InstrumentationRegistry.getTargetContext()) {
@Override
public ComponentName startService(Intent intent) {
mStartServiceIntents.add(intent);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment