Skip to content

Commit

Permalink
[WebLayer] Fix flaky WebLayer camera permission test
Browse files Browse the repository at this point in the history
Actually granting and revoking permissions caused a lot of problems,
instead this modifies the WebLayer implementation context to return the
permission we want.

Bug: 1021050
Change-Id: Ibd8dc3d42fc420b86060d8d8647df528aa0cef0e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1898626
Reviewed-by: Bo <boliu@chromium.org>
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712664}
  • Loading branch information
clarkduvall authored and Commit Bot committed Nov 5, 2019
1 parent 1de09a3 commit 83e34be
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@

import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.InMemorySharedPreferencesContext;
import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.weblayer.shell.InstrumentationActivity;

Expand All @@ -49,6 +51,7 @@ public class InputTypesTest {

private EmbeddedTestServer mTestServer;
private File mTempFile;
private int mCameraPermission = PackageManager.PERMISSION_GRANTED;

private class FileIntentInterceptor implements InstrumentationActivity.IntentInterceptor {
public Intent mLastIntent;
Expand Down Expand Up @@ -94,9 +97,7 @@ public boolean requestPermissions(
new Handler().post(() -> {
int[] results = new int[permissions.length];
Arrays.fill(results, mResult);
if (mResult == PackageManager.PERMISSION_GRANTED) {
grantCameraPermission();
}
mCameraPermission = mResult;
activity.onRequestPermissionsResult(requestCode, permissions, results);
mCallbackHelper.notifyCalled();
});
Expand Down Expand Up @@ -132,8 +133,22 @@ public void setUp() throws Exception {
mTestServer.addDefaultHandlers("weblayer/test/data");
Assert.assertTrue(mTestServer.start(0));

InstrumentationActivity activity =
mActivityTestRule.launchShellWithUrl(mTestServer.getURL("/input_types.html"));
InstrumentationActivity activity = mActivityTestRule.launchShell(new Bundle());
TestThreadUtils.runOnUiThreadBlocking(() -> {
activity.createWebLayer(
new InMemorySharedPreferencesContext(activity.getApplication()) {
@Override
public int checkPermission(String permission, int pid, int uid) {
if (permission.equals(Manifest.permission.CAMERA)) {
return mCameraPermission;
}
return getBaseContext().checkPermission(permission, pid, uid);
}
},
null)
.get();
});
mActivityTestRule.navigateAndWait(mTestServer.getURL("/input_types.html"));
mTempFile = File.createTempFile("file", null);
activity.setIntentInterceptor(mIntentInterceptor);
ActivityCompat.setPermissionCompatDelegate(mPermissionCompatDelegate);
Expand All @@ -146,8 +161,6 @@ public void setUp() throws Exception {
@After
public void tearDown() {
mTempFile.delete();
// The test may have revoked camera permission, so grant it back now.
grantCameraPermission();
ActivityCompat.setPermissionCompatDelegate(null);
}

Expand All @@ -165,9 +178,9 @@ public void testFileInputBasic() {

@Test
@SmallTest
@MinAndroidSdkLevel(Build.VERSION_CODES.P)
public void testFileInputCameraPermissionGranted() {
revokeCameraPermission();
@MinAndroidSdkLevel(Build.VERSION_CODES.M)
public void testFileInputCameraPermissionGranted() throws Exception {
mCameraPermission = PackageManager.PERMISSION_DENIED;
mPermissionCompatDelegate.setResult(PackageManager.PERMISSION_GRANTED);
String id = "input_file";

Expand All @@ -184,9 +197,9 @@ public void testFileInputCameraPermissionGranted() {

@Test
@SmallTest
@MinAndroidSdkLevel(Build.VERSION_CODES.P)
public void testFileInputCameraPermissionDenied() {
revokeCameraPermission();
@MinAndroidSdkLevel(Build.VERSION_CODES.M)
public void testFileInputCameraPermissionDenied() throws Exception {
mCameraPermission = PackageManager.PERMISSION_DENIED;
mPermissionCompatDelegate.setResult(PackageManager.PERMISSION_DENIED);
String id = "input_file";

Expand Down Expand Up @@ -326,20 +339,4 @@ private Intent getContentIntent() {
Assert.assertNotNull(contentIntent);
return contentIntent;
}

@TargetApi(Build.VERSION_CODES.P)
private void revokeCameraPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return;
String packageName = InstrumentationRegistry.getTargetContext().getPackageName();
InstrumentationRegistry.getInstrumentation().getUiAutomation().revokeRuntimePermission(
packageName, Manifest.permission.CAMERA);
}

@TargetApi(Build.VERSION_CODES.P)
private void grantCameraPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return;
String packageName = InstrumentationRegistry.getTargetContext().getPackageName();
InstrumentationRegistry.getInstrumentation().getUiAutomation().grantRuntimePermission(
packageName, Manifest.permission.CAMERA);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,29 @@ public WebLayer getWebLayer() {
}

/**
* Starts the WebLayer activity with the given extras Bundle and completely loads the given URL
* (this calls navigateAndWait()).
* Starts the WebLayer activity with the given extras Bundle. This does not create and load
* WebLayer.
*/
public InstrumentationActivity launchShellWithUrl(String url, Bundle extras) {
public InstrumentationActivity launchShell(Bundle extras) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.putExtras(extras);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(
new ComponentName(InstrumentationRegistry.getInstrumentation().getTargetContext(),
InstrumentationActivity.class));
InstrumentationActivity activity = launchActivity(intent);
return launchActivity(intent);
}

/**
* Starts the WebLayer activity with the given extras Bundle and completely loads the given URL
* (this calls navigateAndWait()).
*/
public InstrumentationActivity launchShellWithUrl(String url, Bundle extras) {
InstrumentationActivity activity = launchShell(extras);
Assert.assertNotNull(activity);
TestThreadUtils.runOnUiThreadBlocking(
() -> { activity.createWebLayer(activity.getApplication(), null).get(); });
if (url != null) navigateAndWait(url);
return activity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package org.chromium.weblayer.shell;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
Expand All @@ -24,6 +25,7 @@
import org.chromium.weblayer.BrowserCallback;
import org.chromium.weblayer.BrowserController;
import org.chromium.weblayer.BrowserFragmentController;
import org.chromium.weblayer.ListenableFuture;
import org.chromium.weblayer.Profile;
import org.chromium.weblayer.UnsupportedVersionException;
import org.chromium.weblayer.WebLayer;
Expand Down Expand Up @@ -102,14 +104,20 @@ protected void onCreate(final Bundle savedInstanceState) {
mTopContentsContainer.addView(mUrlView,
new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

try {
// This ensures asynchronous initialization of WebLayer on first start of activity.
if (savedInstanceState != null) {
// If activity is re-created during process restart, FragmentManager attaches
// BrowserFragment immediately, resulting in synchronous init. By the time this line
// executes, the synchronous init has already happened.
WebLayer.create(getApplication())
.addCallback(webLayer -> onWebLayerReady(savedInstanceState));
createWebLayer(getApplication(), savedInstanceState);
}
}

public ListenableFuture<WebLayer> createWebLayer(
Context appContext, Bundle savedInstanceState) {
try {
ListenableFuture<WebLayer> future = WebLayer.create(appContext);
future.addCallback(webLayer -> onWebLayerReady(savedInstanceState));
return future;
} catch (UnsupportedVersionException e) {
throw new RuntimeException("Failed to initialize WebLayer", e);
}
Expand Down

0 comments on commit 83e34be

Please sign in to comment.