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.
5 #include "components/web_view/public/cpp/web_view.h"
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/mus/public/cpp/scoped_view_ptr.h"
14 #include "components/mus/public/cpp/tests/view_manager_test_base.h"
15 #include "components/mus/public/cpp/view.h"
16 #include "components/mus/public/cpp/view_tree_connection.h"
17 #include "mojo/util/filename_util.h"
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";
31 class WebViewTest
: public mus::ViewManagerTestBase
,
32 public mojom::WebViewClient
{
34 WebViewTest() : web_view_(this) {}
35 ~WebViewTest() override
{}
37 mojom::WebView
* web_view() { return web_view_
.web_view(); }
39 const std::string
& last_title() { return last_title_
; }
40 mojom::ButtonState
last_back_button_state() {
41 return last_back_button_state_
;
43 mojom::ButtonState
last_forward_button_state() {
44 return last_forward_button_state_
;
47 void StartNestedRunLoopUntilLoadingDone() {
48 run_loop_
.reset(new base::RunLoop
);
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();
65 void QuitNestedRunLoop() {
71 // Overridden from ApplicationDelegate:
72 void Initialize(mojo::ApplicationImpl
* app
) override
{
73 ViewManagerTestBase::Initialize(app
);
77 // Overridden from ViewTreeDelegate:
78 void OnEmbed(mus::View
* root
) override
{
79 content_
= root
->connection()->CreateView();
80 root
->AddChild(content_
);
81 content_
->SetVisible(true);
83 web_view_
.Init(app_
, content_
);
85 ViewManagerTestBase::OnEmbed(root
);
88 void TearDown() override
{
89 mus::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot());
90 ViewManagerTestBase::TearDown();
93 // Overridden from web_view::mojom::WebViewClient:
94 void TopLevelNavigate(mojo::URLRequestPtr request
) override
{}
95 void LoadingStateChanged(bool is_loading
, double progress
) override
{
96 if (is_loading
== false)
99 void BackForwardChanged(mojom::ButtonState back_button
,
100 mojom::ButtonState forward_button
) override
{
101 last_back_button_state_
= back_button
;
102 last_forward_button_state_
= forward_button
;
104 void TitleChanged(const mojo::String
& title
) override
{
105 last_title_
= title
.get();
108 mojo::ApplicationImpl
* app_
;
112 web_view::WebView web_view_
;
114 scoped_ptr
<base::RunLoop
> run_loop_
;
116 std::string last_title_
;
117 mojom::ButtonState last_back_button_state_
;
118 mojom::ButtonState last_forward_button_state_
;
120 DISALLOW_COPY_AND_ASSIGN(WebViewTest
);
123 TEST_F(WebViewTest
, TestTitleChanged
) {
124 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile
));
126 // Our title should have been set on the navigation.
127 EXPECT_EQ(kTestOneTitle
, last_title());
130 TEST_F(WebViewTest
, CanGoBackAndForward
) {
131 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile
));
133 // We can't go back on first navigation since there's nothing previously on
135 EXPECT_EQ(kTestOneTitle
, last_title());
136 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED
,
137 last_back_button_state());
138 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED
,
139 last_forward_button_state());
141 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile
));
143 EXPECT_EQ(kTestTwoTitle
, last_title());
144 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED
, last_back_button_state());
145 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED
,
146 last_forward_button_state());
148 web_view()->GoBack();
149 StartNestedRunLoopUntilLoadingDone();
151 EXPECT_EQ(kTestOneTitle
, last_title());
152 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED
,
153 last_back_button_state());
154 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED
,
155 last_forward_button_state());
157 web_view()->GoForward();
158 StartNestedRunLoopUntilLoadingDone();
159 EXPECT_EQ(kTestTwoTitle
, last_title());
160 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED
, last_back_button_state());
161 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED
,
162 last_forward_button_state());
165 TEST_F(WebViewTest
, NavigationClearsForward
) {
166 // First navigate somewhere, navigate somewhere else, and go back so we have
167 // one item in the forward stack.
168 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile
));
169 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile
));
171 web_view()->GoBack();
172 StartNestedRunLoopUntilLoadingDone();
174 EXPECT_EQ(kTestOneTitle
, last_title());
175 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED
,
176 last_back_button_state());
177 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED
,
178 last_forward_button_state());
180 // Now navigate to a third file. This should clear the forward stack.
181 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile
));
183 EXPECT_EQ(kTestThreeTitle
, last_title());
184 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED
, last_back_button_state());
185 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED
,
186 last_forward_button_state());
189 } // namespace web_view