Commit 04693202 authored by Alex Clarke's avatar Alex Clarke Committed by Commit Bot

WebLayer: Add GoToIndex to NavigationController

Seems like we should add this to allow more complicated history navigation.

Bug: 1024922
Change-Id: I12f4570c9883d1e203100fc515cc0d764a5c43a9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1991342
Commit-Queue: Alex Clarke <alexclarke@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#729691}
parent d3623fc5
...@@ -26,13 +26,16 @@ import org.chromium.weblayer.Navigation; ...@@ -26,13 +26,16 @@ import org.chromium.weblayer.Navigation;
import org.chromium.weblayer.NavigationCallback; import org.chromium.weblayer.NavigationCallback;
import org.chromium.weblayer.NavigationController; import org.chromium.weblayer.NavigationController;
import org.chromium.weblayer.NavigationState; import org.chromium.weblayer.NavigationState;
import org.chromium.weblayer.Tab;
import org.chromium.weblayer.shell.InstrumentationActivity; import org.chromium.weblayer.shell.InstrumentationActivity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* Example test that just starts the weblayer shell. * Example test that just starts the weblayer shell.
...@@ -47,6 +50,7 @@ public class NavigationTest { ...@@ -47,6 +50,7 @@ public class NavigationTest {
private static final String URL1 = "data:text,foo"; private static final String URL1 = "data:text,foo";
private static final String URL2 = "data:text,bar"; private static final String URL2 = "data:text,bar";
private static final String URL3 = "data:text,baz"; private static final String URL3 = "data:text,baz";
private static final String URL4 = "data:text,bat";
private static class Callback extends NavigationCallback { private static class Callback extends NavigationCallback {
public static class NavigationCallbackHelper extends CallbackHelper { public static class NavigationCallbackHelper extends CallbackHelper {
...@@ -273,6 +277,23 @@ public class NavigationTest { ...@@ -273,6 +277,23 @@ public class NavigationTest {
runOnUiThreadBlocking(() -> { assertFalse(navigationController.canGoForward()); }); runOnUiThreadBlocking(() -> { assertFalse(navigationController.canGoForward()); });
} }
@Test
@SmallTest
public void testGoToIndex() throws Exception {
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(URL1);
setNavigationCallback(activity);
mActivityTestRule.navigateAndWait(URL2);
mActivityTestRule.navigateAndWait(URL3);
mActivityTestRule.navigateAndWait(URL4);
// Navigate back to the 2nd url.
assertEquals(URL2, goToIndexAndReturnUrl(activity.getTab(), 1));
// Navigate forwards to the 4th url.
assertEquals(URL4, goToIndexAndReturnUrl(activity.getTab(), 3));
}
@Test @Test
@SmallTest @SmallTest
public void testSameDocument() throws Exception { public void testSameDocument() throws Exception {
...@@ -396,4 +417,31 @@ public class NavigationTest { ...@@ -396,4 +417,31 @@ public class NavigationTest {
runOnUiThreadBlocking(navigateRunnable); runOnUiThreadBlocking(navigateRunnable);
mCallback.onCompletedCallback.assertCalledWith(currentCallCount, expectedUrl); mCallback.onCompletedCallback.assertCalledWith(currentCallCount, expectedUrl);
} }
private String goToIndexAndReturnUrl(Tab tab, int index) throws Exception {
NavigationController navigationController =
runOnUiThreadBlocking(() -> tab.getNavigationController());
final CountDownLatch navigationComplete = new CountDownLatch(1);
final AtomicReference<String> navigationUrl = new AtomicReference<String>();
NavigationCallback navigationCallback = new NavigationCallback() {
@Override
public void onNavigationCompleted(Navigation navigation) {
navigationComplete.countDown();
navigationUrl.set(navigation.getUri().toString());
}
};
runOnUiThreadBlocking(() -> {
navigationController.registerNavigationCallback(navigationCallback);
navigationController.goToIndex(index);
});
navigationComplete.await();
runOnUiThreadBlocking(
() -> { navigationController.unregisterNavigationCallback(navigationCallback); });
return navigationUrl.get();
}
} }
...@@ -66,6 +66,13 @@ public final class NavigationControllerImpl extends INavigationController.Stub { ...@@ -66,6 +66,13 @@ public final class NavigationControllerImpl extends INavigationController.Stub {
mNativeNavigationController, NavigationControllerImpl.this); mNativeNavigationController, NavigationControllerImpl.this);
} }
@Override
public void goToIndex(int index) {
StrictModeWorkaround.apply();
NavigationControllerImplJni.get().goToIndex(
mNativeNavigationController, NavigationControllerImpl.this, index);
}
@Override @Override
public void reload() { public void reload() {
StrictModeWorkaround.apply(); StrictModeWorkaround.apply();
...@@ -158,6 +165,8 @@ public final class NavigationControllerImpl extends INavigationController.Stub { ...@@ -158,6 +165,8 @@ public final class NavigationControllerImpl extends INavigationController.Stub {
void goForward(long nativeNavigationControllerImpl, NavigationControllerImpl caller); void goForward(long nativeNavigationControllerImpl, NavigationControllerImpl caller);
boolean canGoBack(long nativeNavigationControllerImpl, NavigationControllerImpl caller); boolean canGoBack(long nativeNavigationControllerImpl, NavigationControllerImpl caller);
boolean canGoForward(long nativeNavigationControllerImpl, NavigationControllerImpl caller); boolean canGoForward(long nativeNavigationControllerImpl, NavigationControllerImpl caller);
void goToIndex(
long nativeNavigationControllerImpl, NavigationControllerImpl caller, int index);
void reload(long nativeNavigationControllerImpl, NavigationControllerImpl caller); void reload(long nativeNavigationControllerImpl, NavigationControllerImpl caller);
void stop(long nativeNavigationControllerImpl, NavigationControllerImpl caller); void stop(long nativeNavigationControllerImpl, NavigationControllerImpl caller);
int getNavigationListSize( int getNavigationListSize(
......
...@@ -24,4 +24,6 @@ interface INavigationController { ...@@ -24,4 +24,6 @@ interface INavigationController {
boolean canGoBack() = 8; boolean canGoBack() = 8;
boolean canGoForward() = 9; boolean canGoForward() = 9;
void goToIndex(in int index) = 10;
} }
...@@ -36,6 +36,13 @@ void NavigationControllerImpl::SetNavigationControllerImpl( ...@@ -36,6 +36,13 @@ void NavigationControllerImpl::SetNavigationControllerImpl(
java_controller_.Reset(env, java_controller); java_controller_.Reset(env, java_controller);
} }
void NavigationControllerImpl::GoToIndex(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
int index) {
return GoToIndex(index);
}
void NavigationControllerImpl::Navigate( void NavigationControllerImpl::Navigate(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj, const base::android::JavaParamRef<jobject>& obj,
...@@ -88,6 +95,10 @@ bool NavigationControllerImpl::CanGoForward() { ...@@ -88,6 +95,10 @@ bool NavigationControllerImpl::CanGoForward() {
return web_contents()->GetController().CanGoForward(); return web_contents()->GetController().CanGoForward();
} }
void NavigationControllerImpl::GoToIndex(int index) {
web_contents()->GetController().GoToIndex(index);
}
void NavigationControllerImpl::Reload() { void NavigationControllerImpl::Reload() {
web_contents()->GetController().Reload(content::ReloadType::NORMAL, false); web_contents()->GetController().Reload(content::ReloadType::NORMAL, false);
} }
......
...@@ -47,6 +47,9 @@ class NavigationControllerImpl : public NavigationController, ...@@ -47,6 +47,9 @@ class NavigationControllerImpl : public NavigationController,
const base::android::JavaParamRef<jobject>& obj) { const base::android::JavaParamRef<jobject>& obj) {
return CanGoForward(); return CanGoForward();
} }
void GoToIndex(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
int index);
void Reload(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { void Reload(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) {
Reload(); Reload();
} }
...@@ -77,6 +80,7 @@ class NavigationControllerImpl : public NavigationController, ...@@ -77,6 +80,7 @@ class NavigationControllerImpl : public NavigationController,
void GoForward() override; void GoForward() override;
bool CanGoBack() override; bool CanGoBack() override;
bool CanGoForward() override; bool CanGoForward() override;
void GoToIndex(int index) override;
void Reload() override; void Reload() override;
void Stop() override; void Stop() override;
int GetNavigationListSize() override; int GetNavigationListSize() override;
......
...@@ -84,6 +84,15 @@ public final class NavigationController { ...@@ -84,6 +84,15 @@ public final class NavigationController {
} }
} }
public void goToIndex(int index) {
ThreadCheck.ensureOnUiThread();
try {
mNavigationController.goToIndex(index);
} catch (RemoteException e) {
throw new APICallException(e);
}
}
public void reload() { public void reload() {
ThreadCheck.ensureOnUiThread(); ThreadCheck.ensureOnUiThread();
try { try {
......
...@@ -30,6 +30,9 @@ class NavigationController { ...@@ -30,6 +30,9 @@ class NavigationController {
virtual bool CanGoForward() = 0; virtual bool CanGoForward() = 0;
// Navigates to the specified absolute index.
virtual void GoToIndex(int index) = 0;
virtual void Reload() = 0; virtual void Reload() = 0;
virtual void Stop() = 0; virtual void Stop() = 0;
......
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