Commit ece13e5d authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[WebLayer] Add initial instrumentation test of infobars

This CL sets up the infrastructure for private instrumentation tests of
WebLayer's infobars and adds an initial test: that addition of an
infobar impacts the visual state of the infobar container as expected.

BUG=1093846

Change-Id: Id5cdc73f02a4eadb46b3733451541fbd1f796441
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2247836
Commit-Queue: Colin Blundell <blundell@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779772}
parent 92b9a2f6
...@@ -569,10 +569,15 @@ source_set("weblayer_lib_base") { ...@@ -569,10 +569,15 @@ source_set("weblayer_lib_base") {
if (is_android) { if (is_android) {
source_set("weblayer_android_test_jni_impl") { source_set("weblayer_android_test_jni_impl") {
testonly = true testonly = true
sources = [ "browser/test/test_weblayer_impl.cc" ] sources = [
"browser/test/test_infobar.cc",
"browser/test/test_infobar.h",
"browser/test/test_weblayer_impl.cc",
]
deps = [ deps = [
":weblayer_lib_base", ":weblayer_lib_base",
"//base", "//base",
"//components/infobars/core",
"//content/public/browser", "//content/public/browser",
"//content/test:test_support", "//content/test:test_support",
"//testing/gtest", "//testing/gtest",
......
...@@ -64,6 +64,7 @@ android_library("weblayer_private_java_tests") { ...@@ -64,6 +64,7 @@ android_library("weblayer_private_java_tests") {
sources = [ sources = [
"src/org/chromium/weblayer/test/BrowserControlsTest.java", "src/org/chromium/weblayer/test/BrowserControlsTest.java",
"src/org/chromium/weblayer/test/GeolocationTest.java", "src/org/chromium/weblayer/test/GeolocationTest.java",
"src/org/chromium/weblayer/test/InfoBarTest.java",
"src/org/chromium/weblayer/test/MediaCaptureTest.java", "src/org/chromium/weblayer/test/MediaCaptureTest.java",
"src/org/chromium/weblayer/test/NetworkChangeNotifierTest.java", "src/org/chromium/weblayer/test/NetworkChangeNotifierTest.java",
"src/org/chromium/weblayer/test/PopupTest.java", "src/org/chromium/weblayer/test/PopupTest.java",
......
// 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.os.RemoteException;
import android.view.View;
import androidx.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.Tab;
import org.chromium.weblayer.TestWebLayer;
import org.chromium.weblayer.shell.InstrumentationActivity;
/**
* Test for infobars.
*/
@RunWith(WebLayerJUnit4ClassRunner.class)
public class InfoBarTest {
@Rule
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private Tab getActiveTab() {
return mActivityTestRule.getActivity().getBrowser().getActiveTab();
}
private TestWebLayer getTestWebLayer() {
return TestWebLayer.getTestWebLayer(
mActivityTestRule.getActivity().getApplicationContext());
}
private View getInfoBarContainerView() throws Exception {
return TestThreadUtils.runOnUiThreadBlocking(() -> {
try {
return getTestWebLayer().getInfoBarContainerView(getActiveTab());
} catch (RemoteException e) {
throw new RuntimeException(e);
}
});
}
private void addInfoBarToActiveTab() throws Exception {
CallbackHelper helper = new CallbackHelper();
TestThreadUtils.runOnUiThreadBlocking(() -> {
try {
getTestWebLayer().addInfoBar(getActiveTab(), () -> { helper.notifyCalled(); });
} catch (RemoteException e) {
throw new RuntimeException(e);
}
});
helper.waitForFirst();
}
@Before
public void setUp() throws Throwable {
final String url = mActivityTestRule.getTestDataURL("tall_page.html");
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(url);
}
@Test
@SmallTest
/**
* Tests that creation of an infobar impacts the state of the infobar container view as
* expected.
*
*/
public void testAddInfoBar() throws Exception {
View infoBarContainerView = getInfoBarContainerView();
Assert.assertEquals(infoBarContainerView.getHeight(), 0);
addInfoBarToActiveTab();
Assert.assertTrue(infoBarContainerView.getHeight() > 0);
Assert.assertEquals(View.VISIBLE, infoBarContainerView.getVisibility());
}
}
...@@ -236,7 +236,10 @@ android_resources("weblayer_test_resources") { ...@@ -236,7 +236,10 @@ android_resources("weblayer_test_resources") {
android_library("test_java") { android_library("test_java") {
testonly = true testonly = true
sources = [ "org/chromium/weblayer_private/test/TestWebLayerImpl.java" ] sources = [
"org/chromium/weblayer_private/test/TestInfoBar.java",
"org/chromium/weblayer_private/test/TestWebLayerImpl.java",
]
deps = [ deps = [
":interfaces_java", ":interfaces_java",
":java", ":java",
...@@ -256,7 +259,10 @@ android_library("test_java") { ...@@ -256,7 +259,10 @@ android_library("test_java") {
generate_jni("test_jni") { generate_jni("test_jni") {
testonly = true testonly = true
sources = [ "org/chromium/weblayer_private/test/TestWebLayerImpl.java" ] sources = [
"org/chromium/weblayer_private/test/TestInfoBar.java",
"org/chromium/weblayer_private/test/TestWebLayerImpl.java",
]
} }
generate_jni("jni") { generate_jni("jni") {
......
...@@ -227,6 +227,11 @@ public class InfoBarContainer implements KeyboardVisibilityListener, InfoBar.Con ...@@ -227,6 +227,11 @@ public class InfoBarContainer implements KeyboardVisibilityListener, InfoBar.Con
mInfoBarContainerView.addInfoBar(infoBar); mInfoBarContainerView.addInfoBar(infoBar);
} }
@VisibleForTesting
public View getViewForTesting() {
return mInfoBarContainerView;
}
/** /**
* Adds an InfoBar to the view hierarchy. * Adds an InfoBar to the view hierarchy.
* @param infoBar InfoBar to add to the View hierarchy. * @param infoBar InfoBar to add to the View hierarchy.
......
...@@ -396,6 +396,11 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer { ...@@ -396,6 +396,11 @@ public final class TabImpl extends ITab.Stub implements LoginPrompt.Observer {
return mNativeTab; return mNativeTab;
} }
@VisibleForTesting
public InfoBarContainer getInfoBarContainerForTesting() {
return mInfoBarContainer;
}
@Override @Override
public NavigationControllerImpl createNavigationController(INavigationControllerClient client) { public NavigationControllerImpl createNavigationController(INavigationControllerClient client) {
StrictModeWorkaround.apply(); StrictModeWorkaround.apply();
......
// 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_private.test;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.content_public.browser.WebContents;
import org.chromium.weblayer_private.InfoBar;
import org.chromium.weblayer_private.InfoBarCompactLayout;
import org.chromium.weblayer_private.TabImpl;
/**
* A test infobar.
*/
@JNINamespace("weblayer")
public class TestInfoBar extends InfoBar {
@VisibleForTesting
public TestInfoBar() {
super(0, 0, null, null);
}
@Override
protected boolean usesCompactLayout() {
return true;
}
@Override
protected void createCompactLayoutContent(InfoBarCompactLayout layout) {
new InfoBarCompactLayout.MessageBuilder(layout)
.withText("I am a compact infobar")
.buildAndInsert();
}
@CalledByNative
private static TestInfoBar create() {
return new TestInfoBar();
}
public static void show(TabImpl tab) {
TestInfoBarJni.get().show(tab.getWebContents());
}
@NativeMethods
interface Natives {
void show(WebContents webContents);
}
}
...@@ -16,6 +16,8 @@ import org.chromium.device.geolocation.LocationProviderOverrider; ...@@ -16,6 +16,8 @@ import org.chromium.device.geolocation.LocationProviderOverrider;
import org.chromium.device.geolocation.MockLocationProvider; import org.chromium.device.geolocation.MockLocationProvider;
import org.chromium.net.NetworkChangeNotifier; import org.chromium.net.NetworkChangeNotifier;
import org.chromium.ui.modaldialog.ModalDialogProperties; import org.chromium.ui.modaldialog.ModalDialogProperties;
import org.chromium.weblayer_private.InfoBarContainer;
import org.chromium.weblayer_private.InfoBarUiItem;
import org.chromium.weblayer_private.TabImpl; import org.chromium.weblayer_private.TabImpl;
import org.chromium.weblayer_private.WebLayerAccessibilityUtil; import org.chromium.weblayer_private.WebLayerAccessibilityUtil;
import org.chromium.weblayer_private.interfaces.IObjectWrapper; import org.chromium.weblayer_private.interfaces.IObjectWrapper;
...@@ -108,6 +110,31 @@ public final class TestWebLayerImpl extends ITestWebLayer.Stub { ...@@ -108,6 +110,31 @@ public final class TestWebLayerImpl extends ITestWebLayer.Stub {
WebLayerAccessibilityUtil.get().setAccessibilityEnabledForTesting(value); WebLayerAccessibilityUtil.get().setAccessibilityEnabledForTesting(value);
} }
@Override
public void addInfoBar(ITab tab, IObjectWrapper runnable) {
Runnable unwrappedRunnable = ObjectWrapper.unwrap(runnable, Runnable.class);
TabImpl tabImpl = (TabImpl) tab;
InfoBarContainer infoBarContainer = tabImpl.getInfoBarContainerForTesting();
infoBarContainer.addAnimationListener(new InfoBarContainer.InfoBarAnimationListener() {
@Override
public void notifyAnimationFinished(int animationType) {}
@Override
public void notifyAllAnimationsFinished(InfoBarUiItem frontInfoBar) {
unwrappedRunnable.run();
infoBarContainer.removeAnimationListener(this);
}
});
TestInfoBar.show((TabImpl) tab);
}
@Override
public IObjectWrapper getInfoBarContainerView(ITab tab) {
return ObjectWrapper.wrap(
((TabImpl) tab).getInfoBarContainerForTesting().getViewForTesting());
}
@Override @Override
public boolean canBrowserControlsScroll(ITab tab) { public boolean canBrowserControlsScroll(ITab tab) {
return ((TabImpl) tab).canBrowserControlsScrollForTesting(); return ((TabImpl) tab).canBrowserControlsScrollForTesting();
......
...@@ -32,4 +32,11 @@ interface ITestWebLayer { ...@@ -32,4 +32,11 @@ interface ITestWebLayer {
void setAccessibilityEnabled(in boolean enabled) = 8; void setAccessibilityEnabled(in boolean enabled) = 8;
boolean canBrowserControlsScroll(in ITab tab) = 9; boolean canBrowserControlsScroll(in ITab tab) = 9;
// Creates and shows a test infobar in |tab|, calling |runnable| when the addition (including
// animations) is complete.
void addInfoBar(in ITab tab, in IObjectWrapper runnable) = 10;
// Gets the infobar container view associated with |tab|.
IObjectWrapper getInfoBarContainerView(in ITab tab) = 11;
} }
// 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.
#include "weblayer/browser/test/test_infobar.h"
#include <memory>
#include <utility>
#include "components/infobars/core/infobar_delegate.h"
#include "weblayer/browser/infobar_service.h"
#include "weblayer/browser/java/test_jni/TestInfoBar_jni.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
namespace weblayer {
class TestInfoBarDelegate : public infobars::InfoBarDelegate {
public:
TestInfoBarDelegate() {}
~TestInfoBarDelegate() override {}
infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override {
return InfoBarDelegate::InfoBarIdentifier::TEST_INFOBAR;
}
};
TestInfoBar::TestInfoBar(std::unique_ptr<TestInfoBarDelegate> delegate)
: InfoBarAndroid(std::move(delegate)) {}
TestInfoBar::~TestInfoBar() {}
void TestInfoBar::ProcessButton(int action) {}
ScopedJavaLocalRef<jobject> TestInfoBar::CreateRenderInfoBar(JNIEnv* env) {
return Java_TestInfoBar_create(env);
}
// static
void TestInfoBar::Show(content::WebContents* web_contents) {
InfoBarService* service = InfoBarService::FromWebContents(web_contents);
service->AddInfoBar(
std::make_unique<TestInfoBar>(std::make_unique<TestInfoBarDelegate>()));
}
static void JNI_TestInfoBar_Show(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_web_contents) {
auto* web_contents =
content::WebContents::FromJavaWebContents(j_web_contents);
TestInfoBar::Show(web_contents);
}
} // namespace weblayer
// 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.
#ifndef WEBLAYER_BROWSER_TEST_TEST_INFOBAR_H_
#define WEBLAYER_BROWSER_TEST_TEST_INFOBAR_H_
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "components/infobars/core/infobar_delegate.h"
#include "content/public/browser/web_contents.h"
#include "weblayer/browser/infobar_android.h"
namespace weblayer {
class TestInfoBarDelegate;
// A test infobar.
class TestInfoBar : public InfoBarAndroid {
public:
explicit TestInfoBar(std::unique_ptr<TestInfoBarDelegate> delegate);
~TestInfoBar() override;
TestInfoBar(const TestInfoBar&) = delete;
TestInfoBar& operator=(const TestInfoBar&) = delete;
static void Show(content::WebContents* web_contents);
protected:
infobars::InfoBarDelegate* GetDelegate();
// InfoBarAndroid overrides.
void ProcessButton(int action) override;
base::android::ScopedJavaLocalRef<jobject> CreateRenderInfoBar(
JNIEnv* env) override;
private:
};
} // namespace weblayer
#endif // WEBLAYER_BROWSER_TEST_TEST_INFOBAR_H_
...@@ -9,6 +9,7 @@ import android.content.pm.PackageManager; ...@@ -9,6 +9,7 @@ import android.content.pm.PackageManager;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.AndroidRuntimeException; import android.util.AndroidRuntimeException;
import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
...@@ -92,4 +93,13 @@ public final class TestWebLayer { ...@@ -92,4 +93,13 @@ public final class TestWebLayer {
public boolean canBrowserControlsScroll(Tab tab) throws RemoteException { public boolean canBrowserControlsScroll(Tab tab) throws RemoteException {
return mITestWebLayer.canBrowserControlsScroll(tab.getITab()); return mITestWebLayer.canBrowserControlsScroll(tab.getITab());
} }
public void addInfoBar(Tab tab, Runnable runnable) throws RemoteException {
mITestWebLayer.addInfoBar(tab.getITab(), ObjectWrapper.wrap(runnable));
}
public View getInfoBarContainerView(Tab tab) throws RemoteException {
return (View) ObjectWrapper.unwrap(
mITestWebLayer.getInfoBarContainerView(tab.getITab()), View.class);
}
} }
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