Commit 073da00a authored by Sharon Yang's avatar Sharon Yang Committed by Commit Bot

[Fuchsia] Expose data for CanGoForward and Back for Navigation State

Add support for can_go_forward and can_go_back in implementation of
frame.

Bug: 948873
Change-Id: I82c5bb5eaad60dd44428d5376617e34b43d56c29
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1542228
Commit-Queue: Sharon Yang <yangsharon@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarFabrice de Gans-Riberi <fdegans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#654735}
parent 0661f899
...@@ -59,10 +59,23 @@ void TestNavigationListener::RunUntilUrlEquals(const GURL& expected_url) { ...@@ -59,10 +59,23 @@ void TestNavigationListener::RunUntilUrlEquals(const GURL& expected_url) {
void TestNavigationListener::RunUntilUrlAndTitleEquals( void TestNavigationListener::RunUntilUrlAndTitleEquals(
const GURL& expected_url, const GURL& expected_url,
const std::string& expected_title) { const base::StringPiece expected_title) {
fuchsia::web::NavigationState state; fuchsia::web::NavigationState state;
state.set_url(expected_url.spec()); state.set_url(expected_url.spec());
state.set_title(expected_title); state.set_title(expected_title.as_string());
RunUntilNavigationStateMatches(state);
}
void TestNavigationListener::RunUntilUrlTitleBackForwardEquals(
const GURL& expected_url,
base::StringPiece expected_title,
bool expected_can_go_back,
bool expected_can_go_forward) {
fuchsia::web::NavigationState state;
state.set_url(expected_url.spec());
state.set_title(expected_title.as_string());
state.set_can_go_back(expected_can_go_back);
state.set_can_go_forward(expected_can_go_forward);
RunUntilNavigationStateMatches(state); RunUntilNavigationStateMatches(state);
} }
...@@ -76,10 +89,10 @@ void TestNavigationListener::OnNavigationStateChanged( ...@@ -76,10 +89,10 @@ void TestNavigationListener::OnNavigationStateChanged(
current_state_.set_url(change.url()); current_state_.set_url(change.url());
if (change.has_title()) if (change.has_title())
current_state_.set_title(change.title()); current_state_.set_title(change.title());
if (change.has_can_go_forward())
current_state_.set_can_go_forward(change.can_go_forward());
if (change.has_can_go_back()) if (change.has_can_go_back())
current_state_.set_can_go_back(change.can_go_back()); current_state_.set_can_go_back(change.can_go_back());
if (change.has_can_go_forward())
current_state_.set_can_go_forward(change.can_go_forward());
// Signal readiness for the next navigation event. // Signal readiness for the next navigation event.
before_ack_.Run(change, std::move(callback)); before_ack_.Run(change, std::move(callback));
...@@ -105,18 +118,18 @@ bool TestNavigationListener::AllFieldsMatch( ...@@ -105,18 +118,18 @@ bool TestNavigationListener::AllFieldsMatch(
all_equal = false; all_equal = false;
} }
} }
if (expected.has_can_go_forward()) {
if (!current_state_.has_can_go_forward() ||
expected.can_go_forward() != current_state_.can_go_forward()) {
all_equal = false;
}
}
if (expected.has_can_go_back()) { if (expected.has_can_go_back()) {
if (!current_state_.has_can_go_back() || if (!current_state_.has_can_go_back() ||
expected.can_go_back() != current_state_.can_go_back()) { expected.can_go_back() != current_state_.can_go_back()) {
all_equal = false; all_equal = false;
} }
} }
if (expected.has_can_go_forward()) {
if (!current_state_.has_can_go_forward() ||
expected.can_go_forward() != current_state_.can_go_forward()) {
all_equal = false;
}
}
return all_equal; return all_equal;
} }
......
...@@ -37,7 +37,14 @@ class TestNavigationListener : public fuchsia::web::NavigationEventListener { ...@@ -37,7 +37,14 @@ class TestNavigationListener : public fuchsia::web::NavigationEventListener {
// Calls RunUntilNavigationStateMatches with a NagivationState that has // Calls RunUntilNavigationStateMatches with a NagivationState that has
// |expected_url| and |expected_title|. // |expected_url| and |expected_title|.
void RunUntilUrlAndTitleEquals(const GURL& expected_url, void RunUntilUrlAndTitleEquals(const GURL& expected_url,
const std::string& expected_title); base::StringPiece expected_title);
// Calls RunUntilNavigationStateMatches with a NagivationState that has
// all the expected fields.
void RunUntilUrlTitleBackForwardEquals(const GURL& expected_url,
base::StringPiece expected_title,
bool expected_can_go_back,
bool expected_can_go_forward);
// Register a callback which intercepts the execution of the event // Register a callback which intercepts the execution of the event
// acknowledgement callback. |before_ack| takes ownership of the // acknowledgement callback. |before_ack| takes ownership of the
......
...@@ -90,6 +90,9 @@ component("web_engine_core") { ...@@ -90,6 +90,9 @@ component("web_engine_core") {
data_deps = [ data_deps = [
":web_engine_pak", ":web_engine_pak",
] ]
public_deps = [
"//content/public/browser",
]
data = [ data = [
"$root_out_dir/web_engine.pak", "$root_out_dir/web_engine.pak",
] ]
...@@ -214,6 +217,7 @@ test("web_engine_browsertests") { ...@@ -214,6 +217,7 @@ test("web_engine_browsertests") {
test("web_engine_unittests") { test("web_engine_unittests") {
sources = [ sources = [
"browser/frame_impl_unittest.cc",
"context_provider_impl_unittest.cc", "context_provider_impl_unittest.cc",
"fake_context.cc", "fake_context.cc",
"fake_context.h", "fake_context.h",
......
...@@ -78,7 +78,8 @@ class LayoutManagerImpl : public aura::LayoutManager { ...@@ -78,7 +78,8 @@ class LayoutManagerImpl : public aura::LayoutManager {
}; };
fuchsia::web::NavigationState ConvertContentNavigationEntry( fuchsia::web::NavigationState ConvertContentNavigationEntry(
content::NavigationEntry* entry) { content::NavigationEntry* entry,
content::WebContents* web_contents) {
DCHECK(entry); DCHECK(entry);
fuchsia::web::NavigationState converted; fuchsia::web::NavigationState converted;
...@@ -94,39 +95,11 @@ fuchsia::web::NavigationState ConvertContentNavigationEntry( ...@@ -94,39 +95,11 @@ fuchsia::web::NavigationState ConvertContentNavigationEntry(
converted.set_page_type(fuchsia::web::PageType::ERROR); converted.set_page_type(fuchsia::web::PageType::ERROR);
break; break;
} }
return converted;
}
// Computes the observable differences between |old_entry| and |new_entry|.
// Returns true if they are different, |false| if their observable fields are
// identical.
bool DiffNavigationEntries(const fuchsia::web::NavigationState& old_entry,
const fuchsia::web::NavigationState& new_entry,
fuchsia::web::NavigationState* difference) {
DCHECK(difference);
bool is_changed = false;
DCHECK(new_entry.has_title());
if (!old_entry.has_title() || (new_entry.title() != old_entry.title())) {
is_changed = true;
difference->set_title(new_entry.title());
}
DCHECK(new_entry.has_url());
if (!old_entry.has_url() || (new_entry.url() != old_entry.url())) {
is_changed = true;
difference->set_url(new_entry.url());
}
DCHECK(new_entry.has_page_type()); converted.set_can_go_back(web_contents->GetController().CanGoBack());
if (!old_entry.has_page_type() || converted.set_can_go_forward(web_contents->GetController().CanGoForward());
(new_entry.page_type() != old_entry.page_type())) {
is_changed = true;
difference->set_page_type(new_entry.page_type());
}
return is_changed; return converted;
} }
class FrameFocusRules : public wm::BaseFocusRules { class FrameFocusRules : public wm::BaseFocusRules {
...@@ -496,7 +469,7 @@ void FrameImpl::TearDownView() { ...@@ -496,7 +469,7 @@ void FrameImpl::TearDownView() {
void FrameImpl::OnNavigationEntryChanged(content::NavigationEntry* entry) { void FrameImpl::OnNavigationEntryChanged(content::NavigationEntry* entry) {
fuchsia::web::NavigationState entry_converted = fuchsia::web::NavigationState entry_converted =
ConvertContentNavigationEntry(entry); ConvertContentNavigationEntry(entry, web_contents_.get());
pending_navigation_event_is_dirty_ |= DiffNavigationEntries( pending_navigation_event_is_dirty_ |= DiffNavigationEntries(
cached_navigation_state_, entry_converted, &pending_navigation_event_); cached_navigation_state_, entry_converted, &pending_navigation_event_);
cached_navigation_state_ = std::move(entry_converted); cached_navigation_state_ = std::move(entry_converted);
...@@ -609,7 +582,7 @@ void FrameImpl::GetVisibleEntry( ...@@ -609,7 +582,7 @@ void FrameImpl::GetVisibleEntry(
return; return;
} }
callback(ConvertContentNavigationEntry(entry)); callback(ConvertContentNavigationEntry(entry, web_contents_.get()));
} }
bool FrameImpl::ShouldCreateWebContents( bool FrameImpl::ShouldCreateWebContents(
...@@ -712,3 +685,46 @@ void FrameImpl::ReadyToCommitNavigation( ...@@ -712,3 +685,46 @@ void FrameImpl::ReadyToCommitNavigation(
void FrameImpl::TitleWasSet(content::NavigationEntry* entry) { void FrameImpl::TitleWasSet(content::NavigationEntry* entry) {
OnNavigationEntryChanged(entry); OnNavigationEntryChanged(entry);
} }
bool DiffNavigationEntries(const fuchsia::web::NavigationState& old_entry,
const fuchsia::web::NavigationState& new_entry,
fuchsia::web::NavigationState* difference) {
DCHECK(difference);
bool is_changed = false;
DCHECK(new_entry.has_title());
if (!old_entry.has_title() || (new_entry.title() != old_entry.title())) {
is_changed = true;
difference->set_title(new_entry.title());
}
DCHECK(new_entry.has_url());
if (!old_entry.has_url() || (new_entry.url() != old_entry.url())) {
is_changed = true;
difference->set_url(new_entry.url());
}
DCHECK(new_entry.has_page_type());
if (!old_entry.has_page_type() ||
(new_entry.page_type() != old_entry.page_type())) {
is_changed = true;
difference->set_page_type(new_entry.page_type());
}
DCHECK(new_entry.has_can_go_back());
if (!old_entry.has_can_go_back() ||
old_entry.can_go_back() != new_entry.can_go_back()) {
is_changed = true;
difference->set_can_go_back(new_entry.can_go_back());
}
DCHECK(new_entry.has_can_go_forward());
if (!old_entry.has_can_go_forward() ||
old_entry.can_go_forward() != new_entry.can_go_forward()) {
is_changed = true;
difference->set_can_go_forward(new_entry.can_go_forward());
}
return is_changed;
}
...@@ -180,4 +180,10 @@ class FrameImpl : public fuchsia::web::Frame, ...@@ -180,4 +180,10 @@ class FrameImpl : public fuchsia::web::Frame,
DISALLOW_COPY_AND_ASSIGN(FrameImpl); DISALLOW_COPY_AND_ASSIGN(FrameImpl);
}; };
// Computes the observable differences between |old_entry| and |new_entry|.
// Returns true if they are different, |false| if their observable fields are
// identical.
bool DiffNavigationEntries(const fuchsia::web::NavigationState& old_entry,
const fuchsia::web::NavigationState& new_entry,
fuchsia::web::NavigationState* difference);
#endif // FUCHSIA_ENGINE_BROWSER_FRAME_IMPL_H_ #endif // FUCHSIA_ENGINE_BROWSER_FRAME_IMPL_H_
...@@ -119,6 +119,21 @@ class FrameImplTest : public cr_fuchsia::WebEngineBrowserTest { ...@@ -119,6 +119,21 @@ class FrameImplTest : public cr_fuchsia::WebEngineBrowserTest {
DISALLOW_COPY_AND_ASSIGN(FrameImplTest); DISALLOW_COPY_AND_ASSIGN(FrameImplTest);
}; };
void VerifyCanGoBackAndForward(fuchsia::web::NavigationController* controller,
bool can_go_back_expected,
bool can_go_forward_expected) {
base::RunLoop run_loop;
cr_fuchsia::ResultReceiver<fuchsia::web::NavigationState> visible_entry(
run_loop.QuitClosure());
controller->GetVisibleEntry(
cr_fuchsia::CallbackToFitFunction(visible_entry.GetReceiveCallback()));
run_loop.Run();
EXPECT_TRUE(visible_entry->has_can_go_back());
EXPECT_EQ(visible_entry->can_go_back(), can_go_back_expected);
EXPECT_TRUE(visible_entry->has_can_go_forward());
EXPECT_EQ(visible_entry->can_go_forward(), can_go_forward_expected);
}
// Verifies that the browser will navigate and generate a navigation listener // Verifies that the browser will navigate and generate a navigation listener
// event when LoadUrl() is called. // event when LoadUrl() is called.
IN_PROC_BROWSER_TEST_F(FrameImplTest, NavigateFrame) { IN_PROC_BROWSER_TEST_F(FrameImplTest, NavigateFrame) {
...@@ -229,25 +244,33 @@ IN_PROC_BROWSER_TEST_F(FrameImplTest, GoBackAndForward) { ...@@ -229,25 +244,33 @@ IN_PROC_BROWSER_TEST_F(FrameImplTest, GoBackAndForward) {
EXPECT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse( EXPECT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
&controller, fuchsia::web::LoadUrlParams(), title1.spec())); &controller, fuchsia::web::LoadUrlParams(), title1.spec()));
navigation_listener_.RunUntilUrlAndTitleEquals(title1, kPage1Title); navigation_listener_.RunUntilUrlTitleBackForwardEquals(title1, kPage1Title,
false, false);
EXPECT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse( EXPECT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse(
&controller, fuchsia::web::LoadUrlParams(), title2.spec())); &controller, fuchsia::web::LoadUrlParams(), title2.spec()));
navigation_listener_.RunUntilUrlAndTitleEquals(title2, kPage2Title); navigation_listener_.RunUntilUrlTitleBackForwardEquals(title2, kPage2Title,
true, false);
VerifyCanGoBackAndForward(controller.get(), true, false);
controller->GoBack(); controller->GoBack();
navigation_listener_.RunUntilUrlAndTitleEquals(title1, kPage1Title); navigation_listener_.RunUntilUrlTitleBackForwardEquals(title1, kPage1Title,
false, true);
// At the top of the navigation entry list; this should be a no-op. // At the top of the navigation entry list; this should be a no-op.
VerifyCanGoBackAndForward(controller.get(), false, true);
controller->GoBack(); controller->GoBack();
// Process the navigation request message. // Process the navigation request message.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
VerifyCanGoBackAndForward(controller.get(), false, true);
controller->GoForward(); controller->GoForward();
navigation_listener_.RunUntilUrlAndTitleEquals(title2, kPage2Title); navigation_listener_.RunUntilUrlTitleBackForwardEquals(title2, kPage2Title,
true, false);
// At the end of the navigation entry list; this should be a no-op. // At the end of the navigation entry list; this should be a no-op.
VerifyCanGoBackAndForward(controller.get(), true, false);
controller->GoForward(); controller->GoForward();
// Process the navigation request message. // Process the navigation request message.
......
// Copyright 2019 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 "fuchsia/engine/browser/frame_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
using NavigationState = fuchsia::web::NavigationState;
namespace {
const char kUrl1[] = "http://www.url1.com/";
const char kUrl2[] = "http://www.url2.com/";
const char kTitle1[] = "title1";
const char kTitle2[] = "title2";
NavigationState CreateNavigationState(const GURL& url,
base::StringPiece title,
fuchsia::web::PageType page_type,
bool can_go_back,
bool can_go_forward) {
NavigationState navigation_state;
navigation_state.set_url(url.spec());
navigation_state.set_title(title.as_string());
navigation_state.set_page_type(fuchsia::web::PageType(page_type));
navigation_state.set_can_go_back(can_go_back);
navigation_state.set_can_go_forward(can_go_forward);
return navigation_state;
}
} // namespace
// Verifies that two NavigationStates that are the same are differenced
// correctly.
TEST(FrameImplUnitTest, DiffNavigationEntriesNoChange) {
fuchsia::web::NavigationState difference;
NavigationState state = CreateNavigationState(
GURL(kUrl1), kTitle1, fuchsia::web::PageType::NORMAL, true, true);
EXPECT_FALSE(DiffNavigationEntries(state, state, &difference));
}
// Verifies that states with different URL and title are correctly checked.
TEST(FrameImplUnitTest, DiffNavigationEntriesTitleUrl) {
fuchsia::web::NavigationState difference;
NavigationState state1 = CreateNavigationState(
GURL(kUrl1), kTitle1, fuchsia::web::PageType::NORMAL, true, true);
NavigationState state2 = CreateNavigationState(
GURL(kUrl2), kTitle2, fuchsia::web::PageType::NORMAL, true, true);
bool is_changed = DiffNavigationEntries(state1, state2, &difference);
EXPECT_TRUE(is_changed);
EXPECT_TRUE(difference.has_title());
EXPECT_EQ(difference.title(), kTitle2);
EXPECT_TRUE(difference.has_url());
EXPECT_EQ(difference.url(), kUrl2);
}
// Verifies that states with different can_go_back and can_go_forward are
// correctly checked.
TEST(FrameImplUnitTest, DiffNavigationEntriesGoBackAndForward) {
fuchsia::web::NavigationState difference;
NavigationState state1 = CreateNavigationState(
GURL(kUrl1), kTitle1, fuchsia::web::PageType::NORMAL, true, false);
NavigationState state2 = CreateNavigationState(
GURL(kUrl1), kTitle1, fuchsia::web::PageType::NORMAL, false, true);
bool is_changed = DiffNavigationEntries(state1, state2, &difference);
EXPECT_TRUE(difference.has_can_go_back());
EXPECT_TRUE(difference.has_can_go_back());
EXPECT_TRUE(is_changed);
EXPECT_TRUE(difference.can_go_forward());
EXPECT_FALSE(difference.can_go_back());
}
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