OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/web_view/public/cpp/web_view.h" |
| 6 |
| 7 #include "base/base_paths.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/run_loop.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" |
| 15 #include "components/view_manager/public/cpp/view.h" |
| 16 #include "components/view_manager/public/cpp/view_tree_connection.h" |
| 17 #include "mojo/util/filename_util.h" |
| 18 #include "url/gurl.h" |
| 19 |
| 20 namespace web_view { |
| 21 |
| 22 class WebViewTest : public mojo::ViewManagerTestBase, |
| 23 public mojom::WebViewClient { |
| 24 public: |
| 25 WebViewTest() : web_view_(this) {} |
| 26 ~WebViewTest() override {} |
| 27 |
| 28 mojom::WebView* web_view() { return web_view_.web_view(); } |
| 29 |
| 30 const std::string& last_title() { return last_title_; } |
| 31 |
| 32 void StartNestedRunLoopUntilLoadingDone() { |
| 33 run_loop_.reset(new base::RunLoop); |
| 34 run_loop_->Run(); |
| 35 } |
| 36 |
| 37 private: |
| 38 void QuitNestedRunLoop() { |
| 39 if (run_loop_) { |
| 40 run_loop_->Quit(); |
| 41 } |
| 42 } |
| 43 |
| 44 // Overridden from ApplicationDelegate: |
| 45 void Initialize(mojo::ApplicationImpl* app) override { |
| 46 ViewManagerTestBase::Initialize(app); |
| 47 app_ = app; |
| 48 } |
| 49 |
| 50 // Overridden from ViewTreeDelegate: |
| 51 void OnEmbed(mojo::View* root) override { |
| 52 content_ = root->connection()->CreateView(); |
| 53 root->AddChild(content_); |
| 54 content_->SetVisible(true); |
| 55 |
| 56 web_view_.Init(app_, content_); |
| 57 |
| 58 ViewManagerTestBase::OnEmbed(root); |
| 59 } |
| 60 |
| 61 void TearDown() override { |
| 62 mojo::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot()); |
| 63 ViewManagerTestBase::TearDown(); |
| 64 } |
| 65 |
| 66 // Overridden from web_view::mojom::WebViewClient: |
| 67 void TopLevelNavigate(mojo::URLRequestPtr request) override {} |
| 68 void LoadingStateChanged(bool is_loading) override { |
| 69 if (is_loading == false) |
| 70 QuitNestedRunLoop(); |
| 71 } |
| 72 void ProgressChanged(double progress) override {} |
| 73 void TitleChanged(const mojo::String& title) override { |
| 74 last_title_ = title.get(); |
| 75 } |
| 76 |
| 77 mojo::ApplicationImpl* app_; |
| 78 |
| 79 mojo::View* content_; |
| 80 |
| 81 web_view::WebView web_view_; |
| 82 |
| 83 scoped_ptr<base::RunLoop> run_loop_; |
| 84 |
| 85 std::string last_title_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(WebViewTest); |
| 88 }; |
| 89 |
| 90 TEST_F(WebViewTest, TestTitleChanged) { |
| 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)); |
| 99 |
| 100 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 101 request->url = mojo::util::FilePathToFileURL(data_file).spec(); |
| 102 web_view()->LoadRequest(request.Pass()); |
| 103 |
| 104 // Build a nested run loop. |
| 105 StartNestedRunLoopUntilLoadingDone(); |
| 106 |
| 107 // Our title should have been set on the final. |
| 108 EXPECT_EQ("Test Title Changed", last_title()); |
| 109 } |
| 110 |
| 111 } // namespace web_view |
OLD | NEW |