Index: components/web_view/web_view_apptest.cc |
diff --git a/components/web_view/web_view_apptest.cc b/components/web_view/web_view_apptest.cc |
index 038bd75328e20488049c7d2c22d878bee9f39459..440d80c68f0da3c03e76d91694b7da01041458c3 100644 |
--- a/components/web_view/web_view_apptest.cc |
+++ b/components/web_view/web_view_apptest.cc |
@@ -19,6 +19,15 @@ |
namespace web_view { |
+namespace { |
+const char kTestOneFile[] = "test_one.html"; |
+const char kTestOneTitle[] = "Test Title One"; |
+const char kTestTwoFile[] = "test_two.html"; |
+const char kTestTwoTitle[] = "Test Title Two"; |
+const char kTestThreeFile[] = "test_three.html"; |
+const char kTestThreeTitle[] = "Test Title Three"; |
+} |
+ |
class WebViewTest : public mojo::ViewManagerTestBase, |
public mojom::WebViewClient { |
public: |
@@ -28,12 +37,30 @@ class WebViewTest : public mojo::ViewManagerTestBase, |
mojom::WebView* web_view() { return web_view_.web_view(); } |
const std::string& last_title() { return last_title_; } |
+ mojom::ButtonState last_back_button_state() { |
+ return last_back_button_state_; |
+ } |
+ mojom::ButtonState last_forward_button_state() { |
+ return last_forward_button_state_; |
+ } |
void StartNestedRunLoopUntilLoadingDone() { |
run_loop_.reset(new base::RunLoop); |
run_loop_->Run(); |
} |
+ void NavigateTo(const std::string& file) { |
+ base::FilePath data_file; |
+ ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); |
+ data_file = data_file.AppendASCII("components/test/data/web_view") |
+ .AppendASCII(file).NormalizePathSeparators(); |
+ ASSERT_TRUE(base::PathExists(data_file)); |
+ mojo::URLRequestPtr request(mojo::URLRequest::New()); |
+ request->url = mojo::util::FilePathToFileURL(data_file).spec(); |
+ web_view()->LoadRequest(request.Pass()); |
+ StartNestedRunLoopUntilLoadingDone(); |
+ } |
+ |
private: |
void QuitNestedRunLoop() { |
if (run_loop_) { |
@@ -70,6 +97,11 @@ class WebViewTest : public mojo::ViewManagerTestBase, |
QuitNestedRunLoop(); |
} |
void ProgressChanged(double progress) override {} |
+ void BackForwardChanged(mojom::ButtonState back_button, |
+ mojom::ButtonState forward_button) override { |
+ last_back_button_state_ = back_button; |
+ last_forward_button_state_ = forward_button; |
+ } |
void TitleChanged(const mojo::String& title) override { |
last_title_ = title.get(); |
} |
@@ -83,29 +115,76 @@ class WebViewTest : public mojo::ViewManagerTestBase, |
scoped_ptr<base::RunLoop> run_loop_; |
std::string last_title_; |
+ mojom::ButtonState last_back_button_state_; |
+ mojom::ButtonState last_forward_button_state_; |
DISALLOW_COPY_AND_ASSIGN(WebViewTest); |
}; |
TEST_F(WebViewTest, TestTitleChanged) { |
- base::FilePath data_file; |
- ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); |
- data_file = data_file.AppendASCII("components"). |
- AppendASCII("test"). |
- AppendASCII("data"). |
- AppendASCII("web_view"). |
- AppendASCII("test_title_changed.html"); |
- ASSERT_TRUE(base::PathExists(data_file)); |
- |
- mojo::URLRequestPtr request(mojo::URLRequest::New()); |
- request->url = mojo::util::FilePathToFileURL(data_file).spec(); |
- web_view()->LoadRequest(request.Pass()); |
- |
- // Build a nested run loop. |
+ ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
+ |
+ // Our title should have been set on the navigation. |
+ EXPECT_EQ(kTestOneTitle, last_title()); |
+} |
+ |
+TEST_F(WebViewTest, CanGoBackAndForward) { |
+ ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
+ |
+ // We can't go back on first navigation since there's nothing previously on |
+ // the stack. |
+ EXPECT_EQ(kTestOneTitle, last_title()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
+ last_back_button_state()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
+ last_forward_button_state()); |
+ |
+ ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
+ |
+ EXPECT_EQ(kTestTwoTitle, last_title()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
+ last_forward_button_state()); |
+ |
+ web_view()->GoBack(); |
+ StartNestedRunLoopUntilLoadingDone(); |
+ |
+ EXPECT_EQ(kTestOneTitle, last_title()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
+ last_back_button_state()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
+ last_forward_button_state()); |
+ |
+ web_view()->GoForward(); |
StartNestedRunLoopUntilLoadingDone(); |
+ EXPECT_EQ(kTestTwoTitle, last_title()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
+ last_forward_button_state()); |
+} |
+ |
+TEST_F(WebViewTest, NavigationClearsForward) { |
+ // First navigate somewhere, navigate somewhere else, and go back so we have |
+ // one item in the forward stack. |
+ ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
+ ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
+ |
+ web_view()->GoBack(); |
+ StartNestedRunLoopUntilLoadingDone(); |
+ |
+ EXPECT_EQ(kTestOneTitle, last_title()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
+ last_back_button_state()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
+ last_forward_button_state()); |
+ |
+ // Now navigate to a third file. This should clear the forward stack. |
+ ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); |
- // Our title should have been set on the final. |
- EXPECT_EQ("Test Title Changed", last_title()); |
+ EXPECT_EQ(kTestThreeTitle, last_title()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
+ EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
+ last_forward_button_state()); |
} |
} // namespace web_view |