| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/web_view/public/cpp/web_view.h" | 5 #include "components/web_view/public/cpp/web_view.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "components/view_manager/public/cpp/scoped_view_ptr.h" | 13 #include "components/view_manager/public/cpp/scoped_view_ptr.h" |
| 14 #include "components/view_manager/public/cpp/tests/view_manager_test_base.h" | 14 #include "components/view_manager/public/cpp/tests/view_manager_test_base.h" |
| 15 #include "components/view_manager/public/cpp/view.h" | 15 #include "components/view_manager/public/cpp/view.h" |
| 16 #include "components/view_manager/public/cpp/view_tree_connection.h" | 16 #include "components/view_manager/public/cpp/view_tree_connection.h" |
| 17 #include "mojo/util/filename_util.h" | 17 #include "mojo/util/filename_util.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace web_view { | 20 namespace web_view { |
| 21 | 21 |
| 22 namespace { | |
| 23 const char kTestOneFile[] = "test_one.html"; | |
| 24 const char kTestOneTitle[] = "Test Title One"; | |
| 25 const char kTestTwoFile[] = "test_two.html"; | |
| 26 const char kTestTwoTitle[] = "Test Title Two"; | |
| 27 const char kTestThreeFile[] = "test_three.html"; | |
| 28 const char kTestThreeTitle[] = "Test Title Three"; | |
| 29 } | |
| 30 | |
| 31 class WebViewTest : public mojo::ViewManagerTestBase, | 22 class WebViewTest : public mojo::ViewManagerTestBase, |
| 32 public mojom::WebViewClient { | 23 public mojom::WebViewClient { |
| 33 public: | 24 public: |
| 34 WebViewTest() : web_view_(this) {} | 25 WebViewTest() : web_view_(this) {} |
| 35 ~WebViewTest() override {} | 26 ~WebViewTest() override {} |
| 36 | 27 |
| 37 mojom::WebView* web_view() { return web_view_.web_view(); } | 28 mojom::WebView* web_view() { return web_view_.web_view(); } |
| 38 | 29 |
| 39 const std::string& last_title() { return last_title_; } | 30 const std::string& last_title() { return last_title_; } |
| 40 mojom::ButtonState last_back_button_state() { | |
| 41 return last_back_button_state_; | |
| 42 } | |
| 43 mojom::ButtonState last_forward_button_state() { | |
| 44 return last_forward_button_state_; | |
| 45 } | |
| 46 | 31 |
| 47 void StartNestedRunLoopUntilLoadingDone() { | 32 void StartNestedRunLoopUntilLoadingDone() { |
| 48 run_loop_.reset(new base::RunLoop); | 33 run_loop_.reset(new base::RunLoop); |
| 49 run_loop_->Run(); | 34 run_loop_->Run(); |
| 50 } | 35 } |
| 51 | 36 |
| 52 void NavigateTo(const std::string& file) { | |
| 53 base::FilePath data_file; | |
| 54 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); | |
| 55 data_file = data_file.AppendASCII("components/test/data/web_view") | |
| 56 .AppendASCII(file).NormalizePathSeparators(); | |
| 57 ASSERT_TRUE(base::PathExists(data_file)); | |
| 58 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
| 59 request->url = mojo::util::FilePathToFileURL(data_file).spec(); | |
| 60 web_view()->LoadRequest(request.Pass()); | |
| 61 StartNestedRunLoopUntilLoadingDone(); | |
| 62 } | |
| 63 | |
| 64 private: | 37 private: |
| 65 void QuitNestedRunLoop() { | 38 void QuitNestedRunLoop() { |
| 66 if (run_loop_) { | 39 if (run_loop_) { |
| 67 run_loop_->Quit(); | 40 run_loop_->Quit(); |
| 68 } | 41 } |
| 69 } | 42 } |
| 70 | 43 |
| 71 // Overridden from ApplicationDelegate: | 44 // Overridden from ApplicationDelegate: |
| 72 void Initialize(mojo::ApplicationImpl* app) override { | 45 void Initialize(mojo::ApplicationImpl* app) override { |
| 73 ViewManagerTestBase::Initialize(app); | 46 ViewManagerTestBase::Initialize(app); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 90 ViewManagerTestBase::TearDown(); | 63 ViewManagerTestBase::TearDown(); |
| 91 } | 64 } |
| 92 | 65 |
| 93 // Overridden from web_view::mojom::WebViewClient: | 66 // Overridden from web_view::mojom::WebViewClient: |
| 94 void TopLevelNavigate(mojo::URLRequestPtr request) override {} | 67 void TopLevelNavigate(mojo::URLRequestPtr request) override {} |
| 95 void LoadingStateChanged(bool is_loading) override { | 68 void LoadingStateChanged(bool is_loading) override { |
| 96 if (is_loading == false) | 69 if (is_loading == false) |
| 97 QuitNestedRunLoop(); | 70 QuitNestedRunLoop(); |
| 98 } | 71 } |
| 99 void ProgressChanged(double progress) override {} | 72 void ProgressChanged(double progress) override {} |
| 100 void BackForwardChanged(mojom::ButtonState back_button, | |
| 101 mojom::ButtonState forward_button) override { | |
| 102 last_back_button_state_ = back_button; | |
| 103 last_forward_button_state_ = forward_button; | |
| 104 } | |
| 105 void TitleChanged(const mojo::String& title) override { | 73 void TitleChanged(const mojo::String& title) override { |
| 106 last_title_ = title.get(); | 74 last_title_ = title.get(); |
| 107 } | 75 } |
| 108 | 76 |
| 109 mojo::ApplicationImpl* app_; | 77 mojo::ApplicationImpl* app_; |
| 110 | 78 |
| 111 mojo::View* content_; | 79 mojo::View* content_; |
| 112 | 80 |
| 113 web_view::WebView web_view_; | 81 web_view::WebView web_view_; |
| 114 | 82 |
| 115 scoped_ptr<base::RunLoop> run_loop_; | 83 scoped_ptr<base::RunLoop> run_loop_; |
| 116 | 84 |
| 117 std::string last_title_; | 85 std::string last_title_; |
| 118 mojom::ButtonState last_back_button_state_; | |
| 119 mojom::ButtonState last_forward_button_state_; | |
| 120 | 86 |
| 121 DISALLOW_COPY_AND_ASSIGN(WebViewTest); | 87 DISALLOW_COPY_AND_ASSIGN(WebViewTest); |
| 122 }; | 88 }; |
| 123 | 89 |
| 124 TEST_F(WebViewTest, TestTitleChanged) { | 90 TEST_F(WebViewTest, TestTitleChanged) { |
| 125 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | 91 base::FilePath data_file; |
| 92 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); |
| 93 data_file = data_file.AppendASCII("components"). |
| 94 AppendASCII("test"). |
| 95 AppendASCII("data"). |
| 96 AppendASCII("web_view"). |
| 97 AppendASCII("test_title_changed.html"); |
| 98 ASSERT_TRUE(base::PathExists(data_file)); |
| 126 | 99 |
| 127 // Our title should have been set on the navigation. | 100 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 128 EXPECT_EQ(kTestOneTitle, last_title()); | 101 request->url = mojo::util::FilePathToFileURL(data_file).spec(); |
| 129 } | 102 web_view()->LoadRequest(request.Pass()); |
| 130 | 103 |
| 131 TEST_F(WebViewTest, CanGoBackAndForward) { | 104 // Build a nested run loop. |
| 132 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | |
| 133 | |
| 134 // We can't go back on first navigation since there's nothing previously on | |
| 135 // the stack. | |
| 136 EXPECT_EQ(kTestOneTitle, last_title()); | |
| 137 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | |
| 138 last_back_button_state()); | |
| 139 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | |
| 140 last_forward_button_state()); | |
| 141 | |
| 142 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | |
| 143 | |
| 144 EXPECT_EQ(kTestTwoTitle, last_title()); | |
| 145 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | |
| 146 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | |
| 147 last_forward_button_state()); | |
| 148 | |
| 149 web_view()->GoBack(); | |
| 150 StartNestedRunLoopUntilLoadingDone(); | 105 StartNestedRunLoopUntilLoadingDone(); |
| 151 | 106 |
| 152 EXPECT_EQ(kTestOneTitle, last_title()); | 107 // Our title should have been set on the final. |
| 153 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 108 EXPECT_EQ("Test Title Changed", last_title()); |
| 154 last_back_button_state()); | |
| 155 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, | |
| 156 last_forward_button_state()); | |
| 157 | |
| 158 web_view()->GoForward(); | |
| 159 StartNestedRunLoopUntilLoadingDone(); | |
| 160 EXPECT_EQ(kTestTwoTitle, last_title()); | |
| 161 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | |
| 162 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | |
| 163 last_forward_button_state()); | |
| 164 } | |
| 165 | |
| 166 TEST_F(WebViewTest, NavigationClearsForward) { | |
| 167 // First navigate somewhere, navigate somewhere else, and go back so we have | |
| 168 // one item in the forward stack. | |
| 169 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | |
| 170 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | |
| 171 | |
| 172 web_view()->GoBack(); | |
| 173 StartNestedRunLoopUntilLoadingDone(); | |
| 174 | |
| 175 EXPECT_EQ(kTestOneTitle, last_title()); | |
| 176 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | |
| 177 last_back_button_state()); | |
| 178 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, | |
| 179 last_forward_button_state()); | |
| 180 | |
| 181 // Now navigate to a third file. This should clear the forward stack. | |
| 182 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); | |
| 183 | |
| 184 EXPECT_EQ(kTestThreeTitle, last_title()); | |
| 185 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | |
| 186 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | |
| 187 last_forward_button_state()); | |
| 188 } | 109 } |
| 189 | 110 |
| 190 } // namespace web_view | 111 } // namespace web_view |
| OLD | NEW |