Commit 40521825 authored by Natalie Chouinard's avatar Natalie Chouinard Committed by Commit Bot

Add tests for Managed Preference toast messages

Bug: 1044401
Change-Id: I7b8c69a390f47e86b864360d1913bf593c8b69ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2124969Reviewed-by: default avatarPeter Wen <wnwen@chromium.org>
Reviewed-by: default avatarBrandon Wylie <wylieb@chromium.org>
Commit-Queue: Natalie Chouinard <chouinard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754650}
parent a6f170a2
......@@ -844,6 +844,7 @@ android_library("chrome_test_java") {
"//chrome/browser/preferences:java",
"//chrome/browser/profiles/android:java",
"//chrome/browser/settings:java",
"//chrome/browser/settings:javatests",
"//chrome/browser/thumbnail:java",
"//chrome/browser/thumbnail:javatests",
"//chrome/browser/ui/android/appmenu:java",
......
......@@ -11,3 +11,26 @@ android_library("java") {
"//components/browser_ui/settings/android:java",
]
}
android_library("javatests") {
testonly = true
sources = [
"android/java/src/org/chromium/chrome/browser/settings/DummySettingsForTest.java",
"android/java/src/org/chromium/chrome/browser/settings/ManagedPreferencesUtilsTest.java",
]
deps = [
":java",
"//chrome/android:chrome_java",
"//chrome/test/android:chrome_java_test_support",
"//content/public/test/android:content_java_test_support",
"//third_party/android_deps:android_support_v7_appcompat_java",
"//third_party/android_deps:androidx_preference_preference_java",
"//third_party/android_support_test_runner:rules_java",
"//third_party/android_support_test_runner:runner_java",
"//third_party/espresso:espresso_all_java",
"//third_party/hamcrest:hamcrest_java",
"//third_party/junit",
]
}
// 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.settings;
import android.content.Context;
import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
/**
* A dummy {@link PreferenceFragmentCompat} for use in tests.
*/
public class DummySettingsForTest extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
getActivity().setTitle("Dummy Settings");
Context context = getPreferenceManager().getContext();
PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);
setPreferenceScreen(screen);
}
}
// 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.settings;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.not;
import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import androidx.preference.Preference;
import androidx.test.filters.SmallTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.components.browser_ui.settings.ManagedPreferenceDelegate;
import org.chromium.components.browser_ui.settings.ManagedPreferencesUtils;
import org.chromium.components.browser_ui.settings.R;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Tests of {@link ManagedPreferencesUtils}.
*
* TODO(chouinard): Once SettingsLauncher and SettingsActivity have
* compontentized interfaces, these test should be moved to
* //components/browser_ui/settings/.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
public class ManagedPreferencesUtilsTest {
@Rule
public ActivityTestRule<SettingsActivity> mRule =
new ActivityTestRule<>(SettingsActivity.class);
@Before
public void setUp() {
Context context = InstrumentationRegistry.getInstrumentation().getContext();
Intent intent = SettingsLauncher.getInstance().createIntentForSettingsPage(
context, DummySettingsForTest.class.getName());
mRule.launchActivity(intent);
}
@Test
@SmallTest
public void testShowManagedByAdministratorToast() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
ManagedPreferencesUtils.showManagedByAdministratorToast(mRule.getActivity());
});
onView(withText(R.string.managed_by_your_organization))
.inRoot(withDecorView(not(mRule.getActivity().getWindow().getDecorView())))
.check(matches(isDisplayed()));
}
@Test
@SmallTest
public void testShowManagedByParentToastNullDelegate() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
ManagedPreferencesUtils.showManagedByParentToast(mRule.getActivity(), null);
});
onView(withText(R.string.managed_by_your_parent))
.inRoot(withDecorView(not(mRule.getActivity().getWindow().getDecorView())))
.check(matches(isDisplayed()));
}
@Test
@SmallTest
public void testShowManagedByParentToastSingleCustodian() {
ManagedPreferenceDelegate singleCustodianDelegate = new ManagedPreferenceDelegate() {
@Override
public boolean isPreferenceControlledByPolicy(Preference preference) {
return false;
}
@Override
public boolean isPreferenceControlledByCustodian(Preference preference) {
return true;
}
@Override
public boolean doesProfileHaveMultipleCustodians() {
return false;
}
};
TestThreadUtils.runOnUiThreadBlocking(() -> {
ManagedPreferencesUtils.showManagedByParentToast(
mRule.getActivity(), singleCustodianDelegate);
});
onView(withText(R.string.managed_by_your_parent))
.inRoot(withDecorView(not(mRule.getActivity().getWindow().getDecorView())))
.check(matches(isDisplayed()));
}
@Test
@SmallTest
public void testShowManagedByParentToastMultipleCustodians() {
ManagedPreferenceDelegate multiCustodianDelegate = new ManagedPreferenceDelegate() {
@Override
public boolean isPreferenceControlledByPolicy(Preference preference) {
return false;
}
@Override
public boolean isPreferenceControlledByCustodian(Preference preference) {
return true;
}
@Override
public boolean doesProfileHaveMultipleCustodians() {
return true;
}
};
TestThreadUtils.runOnUiThreadBlocking(() -> {
ManagedPreferencesUtils.showManagedByParentToast(
mRule.getActivity(), multiCustodianDelegate);
});
onView(withText(R.string.managed_by_your_parents))
.inRoot(withDecorView(not(mRule.getActivity().getWindow().getDecorView())))
.check(matches(isDisplayed()));
}
@Test
@SmallTest
public void testShowManagedSettingsCannotBeResetToast() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
ManagedPreferencesUtils.showManagedSettingsCannotBeResetToast(mRule.getActivity());
});
onView(withText(R.string.managed_settings_cannot_be_reset))
.inRoot(withDecorView(not(mRule.getActivity().getWindow().getDecorView())))
.check(matches(isDisplayed()));
}
}
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