Commit 627630df authored by Ella Ge's avatar Ella Ge Committed by Commit Bot

Reland "Add location delegation instrumentation tests"

This reverts commit 412c3dd4.

Reason for revert: Ran the tests on bot 500 times no failure.
Reland the tests with minor changes

Original change's description:
> Revert "Add location delegation instrumentation tests"
>
> This reverts commit b7b42218.
>
> Reason for revert: the tests are flaky. See https://crbug.com/1099636
>
> Original change's description:
> > Add location delegation instrumentation tests
> >
> > Add some integration tests that runs javascript get geolocations and
> > verify results.
> >
> > Bug: 1069506
> > Change-Id: I368489922b13711b3e9d0f3d800a7cc3172a979c
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2249372
> > Reviewed-by: Peter Conn <peconn@chromium.org>
> > Reviewed-by: Michael Thiessen <mthiesse@chromium.org>
> > Commit-Queue: Ella Ge <eirage@chromium.org>
> > Cr-Commit-Position: refs/heads/master@{#782422}
>
> TBR=mthiesse@chromium.org,peconn@chromium.org,eirage@chromium.org
>
> Change-Id: I3b143731db0bcbae01f880cd1bb70474f0b32e22
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: 1069506, 1099636
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2270321
> Reviewed-by: David Roger <droger@chromium.org>
> Commit-Queue: David Roger <droger@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#782922}

TBR=droger@chromium.org,mthiesse@chromium.org,peconn@chromium.org,eirage@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 1069506, 1099636
Change-Id: Ieee32600afecf40dd28c4c92df8bc77f0341add5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2292776
Commit-Queue: Ella Ge <eirage@chromium.org>
Reviewed-by: default avatarElla Ge <eirage@chromium.org>
Cr-Commit-Position: refs/heads/master@{#788677}
parent f3965319
...@@ -79,6 +79,7 @@ chrome_test_java_sources = [ ...@@ -79,6 +79,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/browserservices/TrustedWebActivityTestUtil.java", "javatests/src/org/chromium/chrome/browser/browserservices/TrustedWebActivityTestUtil.java",
"javatests/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsTest.java", "javatests/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsTest.java",
"javatests/src/org/chromium/chrome/browser/browserservices/digitalgoods/FakeDigitalGoods.java", "javatests/src/org/chromium/chrome/browser/browserservices/digitalgoods/FakeDigitalGoods.java",
"javatests/src/org/chromium/chrome/browser/browserservices/permissiondelegation/TrustedWebActivityLocationDelegationTest.java",
"javatests/src/org/chromium/chrome/browser/browserservices/permissiondelegation/TrustedWebActivityPermissionsTest.java", "javatests/src/org/chromium/chrome/browser/browserservices/permissiondelegation/TrustedWebActivityPermissionsTest.java",
"javatests/src/org/chromium/chrome/browser/browserservices/permissiondelegation/TrustedWebActivityPreferencesUiTest.java", "javatests/src/org/chromium/chrome/browser/browserservices/permissiondelegation/TrustedWebActivityPreferencesUiTest.java",
"javatests/src/org/chromium/chrome/browser/browserservices/ui/controller/trustedwebactivity/TrustedWebActivityCurrentPageVerifierTest.java", "javatests/src/org/chromium/chrome/browser/browserservices/ui/controller/trustedwebactivity/TrustedWebActivityCurrentPageVerifierTest.java",
......
...@@ -60,6 +60,18 @@ public class TrustedWebActivityTestUtil { ...@@ -60,6 +60,18 @@ public class TrustedWebActivityTestUtil {
return intent; return intent;
} }
/**
* Creates an Intent that will launch a Custom Tab to the given |url|, caches a successful
* verification and creates the Custom Tabs Session from the intent.
*/
public static Intent createTrustedWebActivityIntentAndVerifiedSession(
String url, String packageName) throws TimeoutException {
Intent intent = createTrustedWebActivityIntent(url);
spoofVerification(packageName, url);
createSession(intent, packageName);
return intent;
}
/** Caches a successful verification for the given |packageName| and |url|. */ /** Caches a successful verification for the given |packageName| and |url|. */
public static void spoofVerification(String packageName, String url) { public static void spoofVerification(String packageName, String url) {
TestThreadUtils.runOnUiThreadBlocking( TestThreadUtils.runOnUiThreadBlocking(
......
// Copyright 2020 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.browser.browserservices.permissiondelegation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.chromium.chrome.browser.browserservices.TrustedWebActivityTestUtil.isTrustedWebActivity;
import android.net.Uri;
import android.os.RemoteException;
import android.support.test.filters.MediumTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.chromium.base.CommandLine;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.browser.browserservices.TrustedWebActivityTestUtil;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.permissions.PermissionTestRule.PermissionUpdateWaiter;
import org.chromium.chrome.browser.settings.SettingsActivity;
import org.chromium.chrome.browser.site_settings.SiteSettingsTestUtils;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.test.MockCertVerifierRuleAndroid;
import org.chromium.components.browser_ui.settings.ChromeSwitchPreference;
import org.chromium.components.browser_ui.site_settings.SingleCategorySettings;
import org.chromium.components.browser_ui.site_settings.SiteSettingsCategory;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.content_public.common.ContentSwitches;
import org.chromium.device.geolocation.LocationProviderOverrider;
import org.chromium.device.geolocation.MockLocationProvider;
import java.util.concurrent.TimeoutException;
/**
* Tests TrustedWebActivity location delegation.
*/
@RunWith(BaseJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
"enable-features=" + ChromeFeatureList.TRUSTED_WEB_ACTIVITY_LOCATION_DELEGATION})
public class TrustedWebActivityLocationDelegationTest {
public final CustomTabActivityTestRule mCustomTabActivityTestRule =
new CustomTabActivityTestRule();
public final MockCertVerifierRuleAndroid mCertVerifierRule =
new MockCertVerifierRuleAndroid(0 /* net::OK */);
@Rule
public final RuleChain mRuleChain =
RuleChain.emptyRuleChain().around(mCustomTabActivityTestRule).around(mCertVerifierRule);
private static final String TEST_FILE = "/content/test/data/android/geolocation.html";
private static final String TEST_SUPPORT_PACKAGE = "org.chromium.chrome.tests.support";
private String mTestPage;
@Before
public void setUp() throws TimeoutException, RemoteException {
// Initialize native.
LibraryLoader.getInstance().ensureInitialized();
mCustomTabActivityTestRule.getEmbeddedTestServerRule().setServerUsesHttps(true);
Uri mapToUri = Uri.parse(mCustomTabActivityTestRule.getTestServer().getURL("/"));
CommandLine.getInstance().appendSwitchWithValue(
ContentSwitches.HOST_RESOLVER_RULES, "MAP * " + mapToUri.getAuthority());
mTestPage = mCustomTabActivityTestRule.getTestServer().getURLWithHostName(
"www.example.com", TEST_FILE);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
TrustedWebActivityTestUtil.createTrustedWebActivityIntentAndVerifiedSession(
mTestPage, TEST_SUPPORT_PACKAGE));
}
@Test
@MediumTest
public void getLocationFromTestTwaService() throws TimeoutException, Exception {
assertTrue(ChromeFeatureList.isEnabled(
ChromeFeatureList.TRUSTED_WEB_ACTIVITY_LOCATION_DELEGATION));
Tab tab = mCustomTabActivityTestRule.getActivity().getActivityTab();
PermissionUpdateWaiter updateWaiter =
new PermissionUpdateWaiter("Count:", mCustomTabActivityTestRule.getActivity());
tab.addObserver(updateWaiter);
getGeolocation();
updateWaiter.waitForNumUpdates(1);
}
@Test
@MediumTest
@CommandLineFlags.
Add("disable-features=" + ChromeFeatureList.TRUSTED_WEB_ACTIVITY_LOCATION_DELEGATION)
public void getLocationFromChrome_delegationDisabled() throws TimeoutException, Exception {
assertFalse(ChromeFeatureList.isEnabled(
ChromeFeatureList.TRUSTED_WEB_ACTIVITY_LOCATION_DELEGATION));
verifyLocationFromChrome();
}
@Test
@MediumTest
public void getLocationFromChrome_noTwaService() throws TimeoutException, Exception {
String packageName = "other.package.name";
String testPage = mCustomTabActivityTestRule.getTestServer().getURLWithHostName(
"www.otherexample.com", TEST_FILE);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
TrustedWebActivityTestUtil.createTrustedWebActivityIntentAndVerifiedSession(
testPage, packageName));
assertTrue(isTrustedWebActivity(mCustomTabActivityTestRule.getActivity()));
verifyLocationFromChrome();
}
@Test
@MediumTest
public void getLocationFromChrome_afterNavigateAwayFromTrustedOrigin()
throws TimeoutException, Exception {
String other_page = mCustomTabActivityTestRule.getTestServer().getURLWithHostName(
"www.otherexample.com", TEST_FILE);
mCustomTabActivityTestRule.loadUrl(other_page);
assertFalse(isTrustedWebActivity(mCustomTabActivityTestRule.getActivity()));
verifyLocationFromChrome();
}
private void getGeolocation() throws TimeoutException {
mCustomTabActivityTestRule.runJavaScriptCodeInCurrentTab("initiate_getCurrentPosition()");
}
private void setAllowChromeSiteLocation(boolean enabled) {
LocationProviderOverrider.setLocationProviderImpl(new MockLocationProvider());
final SettingsActivity settingsActivity = SiteSettingsTestUtils.startSiteSettingsCategory(
SiteSettingsCategory.Type.DEVICE_LOCATION);
TestThreadUtils.runOnUiThreadBlocking(() -> {
SingleCategorySettings websitePreferences =
(SingleCategorySettings) settingsActivity.getMainFragment();
ChromeSwitchPreference location =
(ChromeSwitchPreference) websitePreferences.findPreference(
SingleCategorySettings.BINARY_TOGGLE_KEY);
websitePreferences.onPreferenceChange(location, enabled);
settingsActivity.finish();
});
}
private void verifyLocationFromChrome() throws TimeoutException, Exception {
Tab tab = mCustomTabActivityTestRule.getActivity().getActivityTab();
setAllowChromeSiteLocation(false);
PermissionUpdateWaiter errorWaiter =
new PermissionUpdateWaiter("deny", mCustomTabActivityTestRule.getActivity());
tab.addObserver(errorWaiter);
getGeolocation();
errorWaiter.waitForNumUpdates(0);
setAllowChromeSiteLocation(true);
PermissionUpdateWaiter updateWaiter =
new PermissionUpdateWaiter("Count:", mCustomTabActivityTestRule.getActivity());
tab.addObserver(updateWaiter);
getGeolocation();
errorWaiter.waitForNumUpdates(1);
}
}
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="org.chromium.chrome.tests.support"> package="org.chromium.chrome.tests.support">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application> <application>
<service android:name="org.chromium.chrome.browser.media.TestMediaRouteProviderService" <service android:name="org.chromium.chrome.browser.media.TestMediaRouteProviderService"
android:label="testMediaRouteProviderService" android:label="testMediaRouteProviderService"
......
...@@ -98,6 +98,7 @@ public class TestTrustedWebActivityService extends TrustedWebActivityService { ...@@ -98,6 +98,7 @@ public class TestTrustedWebActivityService extends TrustedWebActivityService {
Bundle locationResult = new Bundle(); Bundle locationResult = new Bundle();
locationResult.putDouble("latitude", 1.0); locationResult.putDouble("latitude", 1.0);
locationResult.putDouble("longitude", -2.0); locationResult.putDouble("longitude", -2.0);
locationResult.putDouble("accuracy", 0.5);
locationResult.putLong("timeStamp", System.currentTimeMillis()); locationResult.putLong("timeStamp", System.currentTimeMillis());
runCallback(callback, EXTRA_NEW_LOCATION_AVAILABLE_CALLBACK, locationResult); runCallback(callback, EXTRA_NEW_LOCATION_AVAILABLE_CALLBACK, locationResult);
break; break;
......
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