Commit 31904d7b authored by Robbie McElrath's avatar Robbie McElrath Committed by Commit Bot

[WebLayer] Add basic Site Settings test.

This CL just tests that the activity launches.

Bug: 1049683
Change-Id: I22ac17028eb34d3effb3d69474971972c9959430
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2219347
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#775797}
parent 1723bf2a
...@@ -27,6 +27,7 @@ android_library("weblayer_java_tests") { ...@@ -27,6 +27,7 @@ android_library("weblayer_java_tests") {
"src/org/chromium/weblayer/test/OnTabRemovedTabListCallbackImpl.java", "src/org/chromium/weblayer/test/OnTabRemovedTabListCallbackImpl.java",
"src/org/chromium/weblayer/test/ProfileTest.java", "src/org/chromium/weblayer/test/ProfileTest.java",
"src/org/chromium/weblayer/test/RenderingTest.java", "src/org/chromium/weblayer/test/RenderingTest.java",
"src/org/chromium/weblayer/test/SiteSettingsTest.java",
"src/org/chromium/weblayer/test/SmokeTest.java", "src/org/chromium/weblayer/test/SmokeTest.java",
"src/org/chromium/weblayer/test/TabCallbackTest.java", "src/org/chromium/weblayer/test/TabCallbackTest.java",
"src/org/chromium/weblayer/test/TabListCallbackTest.java", "src/org/chromium/weblayer/test/TabListCallbackTest.java",
...@@ -49,6 +50,7 @@ android_library("weblayer_java_tests") { ...@@ -49,6 +50,7 @@ android_library("weblayer_java_tests") {
"//third_party/android_deps:androidx_fragment_fragment_java", "//third_party/android_deps:androidx_fragment_fragment_java",
"//third_party/android_support_test_runner:rules_java", "//third_party/android_support_test_runner:rules_java",
"//third_party/android_support_test_runner:runner_java", "//third_party/android_support_test_runner:runner_java",
"//third_party/espresso:espresso_all_java",
"//third_party/hamcrest:hamcrest_java", "//third_party/hamcrest:hamcrest_java",
"//third_party/junit:junit", "//third_party/junit:junit",
"//weblayer/browser/java:interfaces_java", "//weblayer/browser/java:interfaces_java",
...@@ -93,6 +95,8 @@ android_library("weblayer_java_test_support") { ...@@ -93,6 +95,8 @@ android_library("weblayer_java_test_support") {
"src/org/chromium/weblayer/test/MinWebLayerVersionSkipCheck.java", "src/org/chromium/weblayer/test/MinWebLayerVersionSkipCheck.java",
"src/org/chromium/weblayer/test/NavigationWaiter.java", "src/org/chromium/weblayer/test/NavigationWaiter.java",
"src/org/chromium/weblayer/test/ResourceUtil.java", "src/org/chromium/weblayer/test/ResourceUtil.java",
"src/org/chromium/weblayer/test/SiteSettingsActivityTestRule.java",
"src/org/chromium/weblayer/test/WebLayerActivityTestRule.java",
"src/org/chromium/weblayer/test/WebLayerJUnit4ClassRunner.java", "src/org/chromium/weblayer/test/WebLayerJUnit4ClassRunner.java",
] ]
deps = [ deps = [
......
...@@ -7,13 +7,11 @@ package org.chromium.weblayer.test; ...@@ -7,13 +7,11 @@ package org.chromium.weblayer.test;
import android.app.Activity; import android.app.Activity;
import android.app.Instrumentation.ActivityMonitor; import android.app.Instrumentation.ActivityMonitor;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule; import android.support.test.rule.ActivityTestRule;
import android.text.TextUtils;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
...@@ -24,7 +22,6 @@ import org.junit.Rule; ...@@ -24,7 +22,6 @@ import org.junit.Rule;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.junit.runners.model.Statement; import org.junit.runners.model.Statement;
import org.chromium.base.CommandLine;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
import org.chromium.content_public.browser.test.util.CriteriaHelper; import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
...@@ -35,9 +32,6 @@ import org.chromium.weblayer.Tab; ...@@ -35,9 +32,6 @@ import org.chromium.weblayer.Tab;
import org.chromium.weblayer.WebLayer; import org.chromium.weblayer.WebLayer;
import org.chromium.weblayer.shell.InstrumentationActivity; import org.chromium.weblayer.shell.InstrumentationActivity;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
...@@ -47,9 +41,8 @@ import java.util.concurrent.TimeoutException; ...@@ -47,9 +41,8 @@ import java.util.concurrent.TimeoutException;
* *
* Test can use this ActivityTestRule to launch or get InstrumentationActivity. * Test can use this ActivityTestRule to launch or get InstrumentationActivity.
*/ */
public class InstrumentationActivityTestRule extends ActivityTestRule<InstrumentationActivity> { public class InstrumentationActivityTestRule
private static final String COMMAND_LINE_FILE = "weblayer-command-line"; extends WebLayerActivityTestRule<InstrumentationActivity> {
@Rule @Rule
private EmbeddedTestServerRule mTestServerRule = new EmbeddedTestServerRule(); private EmbeddedTestServerRule mTestServerRule = new EmbeddedTestServerRule();
...@@ -67,37 +60,12 @@ public class InstrumentationActivityTestRule extends ActivityTestRule<Instrument ...@@ -67,37 +60,12 @@ public class InstrumentationActivityTestRule extends ActivityTestRule<Instrument
} }
public InstrumentationActivityTestRule() { public InstrumentationActivityTestRule() {
super(InstrumentationActivity.class, false, false); super(InstrumentationActivity.class);
} }
@Override @Override
public Statement apply(final Statement base, Description description) { public Statement apply(final Statement base, Description description) {
Statement testServer = super.apply(mTestServerRule.apply(base, description), description); return super.apply(mTestServerRule.apply(base, description), description);
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
// The CommandLine instance we have here will not be picked up in the
// implementation since they use different class loaders, so we need to write
// all the switches to the WebLayer command line file.
try (Writer writer = new OutputStreamWriter(
InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.openFileOutput(COMMAND_LINE_FILE, Context.MODE_PRIVATE),
"UTF-8")) {
writer.write(TextUtils.join(" ", CommandLine.getJavaSwitchesOrNull()));
}
testServer.evaluate();
} finally {
new File(InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.getFilesDir(),
COMMAND_LINE_FILE)
.delete();
}
}
};
} }
public WebLayer getWebLayer() { public WebLayer getWebLayer() {
......
// 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.weblayer.test;
import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.SiteSettingsActivity;
import org.chromium.weblayer.WebLayer;
/**
* ActivityTestRule for SiteSettingsActivity.
*
* Test can use this ActivityTestRule to launch SiteSettingsActivity.
*/
public class SiteSettingsActivityTestRule extends WebLayerActivityTestRule<SiteSettingsActivity> {
public SiteSettingsActivityTestRule() {
super(SiteSettingsActivity.class);
}
public SiteSettingsActivity launchCategoryListWithProfile(String profileName) {
Context appContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.getApplicationContext();
Intent siteSettingsIntent = TestThreadUtils.runOnUiThreadBlockingNoException(() -> {
// We load WebLayer here so it gets initialized with the test/instrumentation Context,
// which has an in-memory SharedPreferences. Without this call, WebLayer gets
// initialized with the Application as its appContext, which breaks tests because a
// SharedPreferences xml file is persisted to disk.
WebLayer.loadSync(appContext);
return SiteSettingsActivity.createIntentForCategoryList(appContext, profileName);
});
return launchActivity(siteSettingsIntent);
}
}
// 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.weblayer.test;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import android.support.test.filters.SmallTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.weblayer.SiteSettingsActivity;
/**
* Tests the behavior of the Site Settings UI.
*/
@RunWith(WebLayerJUnit4ClassRunner.class)
public class SiteSettingsTest {
private static final String PROFILE_NAME = "DefaultProfile";
@Rule
public SiteSettingsActivityTestRule mActivityTestRule = new SiteSettingsActivityTestRule();
@Test
@SmallTest
@MinWebLayerVersion(84)
public void testSiteSettingsLaunches() throws InterruptedException {
SiteSettingsActivity siteSettingsActivity =
mActivityTestRule.launchCategoryListWithProfile(PROFILE_NAME);
// Check that the "All sites" option is visible.
onView(withText("All sites")).check(matches(isDisplayed()));
}
}
// 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.weblayer.test;
import android.app.Activity;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.text.TextUtils;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.chromium.base.CommandLine;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Writer;
/**
* Base ActivityTestRule for WebLayer instrumentation tests.
*
* This rule contains some common setup needed to deal with WebLayer's multiple classloaders.
*/
abstract class WebLayerActivityTestRule<T extends Activity> extends ActivityTestRule<T> {
private static final String COMMAND_LINE_FILE = "weblayer-command-line";
public WebLayerActivityTestRule(Class<T> clazz) {
super(clazz, /*initialTouchMode=*/false, /*launchActivity=*/false);
}
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
// The CommandLine instance we have here will not be picked up in the
// implementation since they use different class loaders, so we need to write
// all the switches to the WebLayer command line file.
try (Writer writer = new OutputStreamWriter(
InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.openFileOutput(COMMAND_LINE_FILE, Context.MODE_PRIVATE),
"UTF-8")) {
writer.write(TextUtils.join(" ", CommandLine.getJavaSwitchesOrNull()));
}
base.evaluate();
} finally {
new File(InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.getFilesDir(),
COMMAND_LINE_FILE)
.delete();
}
}
};
}
}
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