Commit d0957347 authored by Sky Malice's avatar Sky Malice Committed by Commit Bot

Use --policy in FirstRunAppRestrictionInfo.

Bug: 1113792
Change-Id: I728574f878420019da5205027ab0974b4e4adb85
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2364514
Commit-Queue: Sky Malice <skym@chromium.org>
Reviewed-by: default avatarWenyu Fu <wenyufu@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#800000}
parent 8e77f8f4
......@@ -841,6 +841,7 @@ junit_binary("chrome_junit_tests") {
"//components/page_info/android:java",
"//components/payments/content/android:java",
"//components/payments/mojom:mojom_java",
"//components/policy/android:policy_java",
"//components/prefs/android:java",
"//components/schema_org/common:mojom_java",
"//components/search_engines/android:java",
......
......@@ -13,6 +13,7 @@ import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.Callback;
import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
......@@ -20,6 +21,7 @@ import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.task.AsyncTask;
import org.chromium.base.task.TaskTraits;
import org.chromium.policy.AppRestrictionsProvider;
import org.chromium.policy.PolicySwitches;
import java.util.LinkedList;
import java.util.Locale;
......@@ -105,6 +107,15 @@ class FirstRunAppRestrictionInfo {
*/
public void getHasAppRestriction(Callback<Boolean> callback) {
ThreadUtils.assertOnUiThread();
// This is an imperfect system, and can sometimes return true when there will not actually
// be any app restrictions. But we do not have parsing logic in Java to understand if the
// switch sets valid policies.
if (CommandLine.getInstance().hasSwitch(PolicySwitches.CHROME_POLICY)) {
callback.onResult(true);
return;
}
if (mInitialized) {
callback.onResult(mHasAppRestriction);
} else {
......
......@@ -12,6 +12,7 @@ import android.os.UserManager;
import androidx.test.filters.SmallTest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
......@@ -21,6 +22,7 @@ import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils;
import org.chromium.base.metrics.test.ShadowRecordHistogram;
import org.chromium.base.task.TaskTraits;
......@@ -28,6 +30,7 @@ import org.chromium.base.task.test.ShadowPostTask;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.policy.PolicySwitches;
import org.chromium.testing.local.CustomShadowUserManager;
import java.util.Arrays;
......@@ -65,6 +68,9 @@ public class FirstRunAppRestrictionInfoTest {
@Mock
private Bundle mMockBundle;
@Mock
private CommandLine mCommandLine;
private boolean mPauseDuringPostTask;
@Before
......@@ -84,6 +90,11 @@ public class FirstRunAppRestrictionInfoTest {
shadowUserManager.setApplicationRestrictions(context.getPackageName(), mMockBundle);
}
@After
public void tearDown() {
CommandLine.reset();
}
private void verifyHistograms(int expectedCallCount) {
for (String name : HISTOGRAM_NAMES) {
Assert.assertEquals("Histogram record count doesn't match.", expectedCallCount,
......@@ -195,4 +206,22 @@ public class FirstRunAppRestrictionInfoTest {
completionCallbackHelper.getCallCount());
verifyHistograms(0);
}
@Test
@SmallTest
public void testCommandLine() throws TimeoutException {
// TODO(https://crbug.com/1119410): Switch to @CommandLineFlag once supported for junit.
CommandLine.setInstanceForTesting(mCommandLine);
Mockito.when(mCommandLine.hasSwitch(Mockito.eq(PolicySwitches.CHROME_POLICY)))
.thenReturn(true);
final BooleanInputCallbackHelper callbackHelper = new BooleanInputCallbackHelper();
TestThreadUtils.runOnUiThreadBlocking(
()
-> mAppRestrictionInfo.getHasAppRestriction(
callbackHelper::notifyCalledWithInput));
callbackHelper.assertCallbackHelperCalledWithInput(true);
verifyHistograms(0);
}
}
......@@ -10,12 +10,20 @@ _jni_sources = [
"java/src/org/chromium/policy/PolicyService.java",
]
java_cpp_strings("java_switches_srcjar") {
# External code should depend on ":policy_java" instead.
visibility = [ ":*" ]
sources = [ "//components/policy/core/common/policy_switches.cc" ]
template = "java_templates/PolicySwitches.java.tmpl"
}
android_library("policy_java") {
deps = [
"//base:base_java",
"//base:jni_java",
"//third_party/android_deps:androidx_annotation_annotation_java",
]
srcjar_deps = [ ":java_switches_srcjar" ]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources =
......
// 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.policy;
/**
* Contains command line switches that are specific to the policy component.
*/
public final class PolicySwitches {{
{NATIVE_STRINGS}
// Prevents instantiation.
private PolicySwitches() {{}}
}}
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