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/mus/public/cpp/scoped_view_ptr.h" | 13 #include "components/mus/public/cpp/scoped_view_ptr.h" |
14 #include "components/mus/public/cpp/tests/view_manager_test_base.h" | 14 #include "components/mus/public/cpp/tests/view_manager_test_base.h" |
15 #include "components/mus/public/cpp/view.h" | 15 #include "components/mus/public/cpp/view.h" |
16 #include "components/mus/public/cpp/view_tree_connection.h" | 16 #include "components/mus/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 { | 22 namespace { |
23 const char kTestOneFile[] = "test_one.html"; | 23 const char kTestOneFile[] = "test_one.html"; |
24 const char kTestOneTitle[] = "Test Title One"; | 24 const char kTestOneTitle[] = "Test Title One"; |
25 const char kTestTwoFile[] = "test_two.html"; | 25 const char kTestTwoFile[] = "test_two.html"; |
26 const char kTestTwoTitle[] = "Test Title Two"; | 26 const char kTestTwoTitle[] = "Test Title Two"; |
27 const char kTestThreeFile[] = "test_three.html"; | 27 const char kTestThreeFile[] = "test_three.html"; |
28 const char kTestThreeTitle[] = "Test Title Three"; | 28 const char kTestThreeTitle[] = "Test Title Three"; |
29 const char kTheWordGreenFiveTimes[] = "the_word_green_five_times.html"; | |
30 const char kTwoIframesWithGreen[] = "two_iframes_with_green.html"; | |
29 | 31 |
30 GURL GetTestFileURL(const std::string& file) { | 32 GURL GetTestFileURL(const std::string& file) { |
31 base::FilePath data_file; | 33 base::FilePath data_file; |
32 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); | 34 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &data_file)); |
33 data_file = data_file.AppendASCII("components/test/data/web_view") | 35 data_file = data_file.AppendASCII("components/test/data/web_view") |
34 .AppendASCII(file) | 36 .AppendASCII(file) |
35 .NormalizePathSeparators(); | 37 .NormalizePathSeparators(); |
36 CHECK(base::PathExists(data_file)); | 38 CHECK(base::PathExists(data_file)); |
37 return mojo::util::FilePathToFileURL(data_file); | 39 return mojo::util::FilePathToFileURL(data_file); |
38 } | 40 } |
41 | |
39 } | 42 } |
40 | 43 |
41 class WebViewTest : public mus::ViewManagerTestBase, | 44 class WebViewTest : public mus::ViewManagerTestBase, |
42 public mojom::WebViewClient { | 45 public mojom::WebViewClient { |
43 public: | 46 public: |
44 WebViewTest() : web_view_(this) {} | 47 WebViewTest() |
48 : web_view_(this), | |
49 quit_condition_(NO_QUIT), | |
50 active_find_match_(0), | |
51 find_count_(0) {} | |
45 ~WebViewTest() override {} | 52 ~WebViewTest() override {} |
46 | 53 |
47 mojom::WebView* web_view() { return web_view_.web_view(); } | 54 mojom::WebView* web_view() { return web_view_.web_view(); } |
48 | 55 |
49 const std::string& navigation_url() const { return navigation_url_; } | 56 const std::string& navigation_url() const { return navigation_url_; } |
50 const std::string& last_title() const { return last_title_; } | 57 const std::string& last_title() const { return last_title_; } |
51 mojom::ButtonState last_back_button_state() { | 58 mojom::ButtonState last_back_button_state() { |
52 return last_back_button_state_; | 59 return last_back_button_state_; |
53 } | 60 } |
54 mojom::ButtonState last_forward_button_state() { | 61 mojom::ButtonState last_forward_button_state() { |
55 return last_forward_button_state_; | 62 return last_forward_button_state_; |
56 } | 63 } |
57 | 64 |
58 void StartNestedRunLoopUntilLoadingDone() { | 65 int32_t active_find_match() const { return active_find_match_; } |
66 int32_t find_count() const { return find_count_; } | |
67 | |
68 enum NestedLoopQuitCondition { | |
69 NO_QUIT, | |
70 LOADING_DONE, | |
71 FINAL_FIND_UPATE, | |
72 }; | |
73 | |
74 void StartNestedRunLoopUntil(NestedLoopQuitCondition quit_condition) { | |
75 quit_condition_ = quit_condition; | |
59 run_loop_.reset(new base::RunLoop); | 76 run_loop_.reset(new base::RunLoop); |
60 run_loop_->Run(); | 77 run_loop_->Run(); |
61 } | 78 } |
62 | 79 |
63 void NavigateTo(const std::string& file) { | 80 void NavigateTo(const std::string& file) { |
64 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 81 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
65 request->url = GetTestFileURL(file).spec(); | 82 request->url = GetTestFileURL(file).spec(); |
66 web_view()->LoadRequest(request.Pass()); | 83 web_view()->LoadRequest(request.Pass()); |
67 StartNestedRunLoopUntilLoadingDone(); | 84 StartNestedRunLoopUntil(LOADING_DONE); |
68 } | 85 } |
69 | 86 |
70 private: | 87 private: |
71 void QuitNestedRunLoop() { | 88 void QuitNestedRunLoop() { |
72 if (run_loop_) { | 89 if (run_loop_) { |
90 quit_condition_ = NO_QUIT; | |
73 run_loop_->Quit(); | 91 run_loop_->Quit(); |
74 } | 92 } |
75 } | 93 } |
76 | 94 |
77 // Overridden from ApplicationDelegate: | 95 // Overridden from ApplicationDelegate: |
78 void Initialize(mojo::ApplicationImpl* app) override { | 96 void Initialize(mojo::ApplicationImpl* app) override { |
79 ViewManagerTestBase::Initialize(app); | 97 ViewManagerTestBase::Initialize(app); |
80 app_ = app; | 98 app_ = app; |
81 } | 99 } |
82 | 100 |
83 // Overridden from ViewTreeDelegate: | 101 // Overridden from ViewTreeDelegate: |
84 void OnEmbed(mus::View* root) override { | 102 void OnEmbed(mus::View* root) override { |
85 content_ = root->connection()->CreateView(); | 103 content_ = root->connection()->CreateView(); |
104 content_->SetBounds(root->bounds()); | |
86 root->AddChild(content_); | 105 root->AddChild(content_); |
87 content_->SetVisible(true); | 106 content_->SetVisible(true); |
88 | 107 |
89 web_view_.Init(app_, content_); | 108 web_view_.Init(app_, content_); |
90 | 109 |
91 ViewManagerTestBase::OnEmbed(root); | 110 ViewManagerTestBase::OnEmbed(root); |
92 } | 111 } |
93 | 112 |
94 void TearDown() override { | 113 void TearDown() override { |
95 mus::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot()); | 114 mus::ScopedViewPtr::DeleteViewOrViewManager(window_manager()->GetRoot()); |
96 ViewManagerTestBase::TearDown(); | 115 ViewManagerTestBase::TearDown(); |
97 } | 116 } |
98 | 117 |
99 // Overridden from web_view::mojom::WebViewClient: | 118 // Overridden from web_view::mojom::WebViewClient: |
100 void TopLevelNavigateRequest(mojo::URLRequestPtr request) override {} | 119 void TopLevelNavigateRequest(mojo::URLRequestPtr request) override {} |
101 void TopLevelNavigationStarted(const mojo::String& url) override { | 120 void TopLevelNavigationStarted(const mojo::String& url) override { |
102 navigation_url_ = url.get(); | 121 navigation_url_ = url.get(); |
103 } | 122 } |
104 void LoadingStateChanged(bool is_loading, double progress) override { | 123 void LoadingStateChanged(bool is_loading, double progress) override { |
105 if (is_loading == false) | 124 if (is_loading == false && quit_condition_ == LOADING_DONE) |
106 QuitNestedRunLoop(); | 125 QuitNestedRunLoop(); |
107 } | 126 } |
108 void BackForwardChanged(mojom::ButtonState back_button, | 127 void BackForwardChanged(mojom::ButtonState back_button, |
109 mojom::ButtonState forward_button) override { | 128 mojom::ButtonState forward_button) override { |
110 last_back_button_state_ = back_button; | 129 last_back_button_state_ = back_button; |
111 last_forward_button_state_ = forward_button; | 130 last_forward_button_state_ = forward_button; |
112 } | 131 } |
113 void TitleChanged(const mojo::String& title) override { | 132 void TitleChanged(const mojo::String& title) override { |
114 last_title_ = title.get(); | 133 last_title_ = title.get(); |
115 } | 134 } |
135 void ReportFindInPageMatchCount(int32_t request_id, | |
136 int32_t count, | |
137 bool final_update) override { | |
138 find_count_ = count; | |
139 if (final_update && quit_condition_ == FINAL_FIND_UPATE) | |
140 QuitNestedRunLoop(); | |
141 } | |
142 void ReportFindInPageSelection(int32_t request_id, | |
143 int32_t active_match_ordinal) override { | |
144 active_find_match_ = active_match_ordinal; | |
145 } | |
116 | 146 |
117 mojo::ApplicationImpl* app_; | 147 mojo::ApplicationImpl* app_; |
118 | 148 |
119 mus::View* content_; | 149 mus::View* content_; |
120 | 150 |
121 web_view::WebView web_view_; | 151 web_view::WebView web_view_; |
122 | 152 |
123 scoped_ptr<base::RunLoop> run_loop_; | 153 scoped_ptr<base::RunLoop> run_loop_; |
124 | 154 |
125 std::string navigation_url_; | 155 std::string navigation_url_; |
126 std::string last_title_; | 156 std::string last_title_; |
127 mojom::ButtonState last_back_button_state_; | 157 mojom::ButtonState last_back_button_state_; |
128 mojom::ButtonState last_forward_button_state_; | 158 mojom::ButtonState last_forward_button_state_; |
129 | 159 |
160 NestedLoopQuitCondition quit_condition_; | |
161 | |
162 int32_t active_find_match_; | |
163 int32_t find_count_; | |
164 | |
130 DISALLOW_COPY_AND_ASSIGN(WebViewTest); | 165 DISALLOW_COPY_AND_ASSIGN(WebViewTest); |
131 }; | 166 }; |
132 | 167 |
133 TEST_F(WebViewTest, TestTitleChanged) { | 168 TEST_F(WebViewTest, TestTitleChanged) { |
134 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | 169 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
135 | 170 |
136 // Our title should have been set on the navigation. | 171 // Our title should have been set on the navigation. |
137 EXPECT_EQ(kTestOneTitle, last_title()); | 172 EXPECT_EQ(kTestOneTitle, last_title()); |
138 } | 173 } |
139 | 174 |
(...skipping 10 matching lines...) Expand all Loading... | |
150 last_forward_button_state()); | 185 last_forward_button_state()); |
151 | 186 |
152 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | 187 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
153 | 188 |
154 EXPECT_EQ(kTestTwoTitle, last_title()); | 189 EXPECT_EQ(kTestTwoTitle, last_title()); |
155 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 190 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
156 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 191 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
157 last_forward_button_state()); | 192 last_forward_button_state()); |
158 | 193 |
159 web_view()->GoBack(); | 194 web_view()->GoBack(); |
160 StartNestedRunLoopUntilLoadingDone(); | 195 StartNestedRunLoopUntil(LOADING_DONE); |
161 | 196 |
162 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); | 197 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); |
163 EXPECT_EQ(kTestOneTitle, last_title()); | 198 EXPECT_EQ(kTestOneTitle, last_title()); |
164 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 199 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
165 last_back_button_state()); | 200 last_back_button_state()); |
166 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, | 201 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
167 last_forward_button_state()); | 202 last_forward_button_state()); |
168 | 203 |
169 web_view()->GoForward(); | 204 web_view()->GoForward(); |
170 StartNestedRunLoopUntilLoadingDone(); | 205 StartNestedRunLoopUntil(LOADING_DONE); |
171 EXPECT_EQ(GetTestFileURL(kTestTwoFile).spec(), navigation_url()); | 206 EXPECT_EQ(GetTestFileURL(kTestTwoFile).spec(), navigation_url()); |
172 EXPECT_EQ(kTestTwoTitle, last_title()); | 207 EXPECT_EQ(kTestTwoTitle, last_title()); |
173 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 208 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
174 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 209 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
175 last_forward_button_state()); | 210 last_forward_button_state()); |
176 } | 211 } |
177 | 212 |
178 TEST_F(WebViewTest, NavigationClearsForward) { | 213 TEST_F(WebViewTest, NavigationClearsForward) { |
179 // First navigate somewhere, navigate somewhere else, and go back so we have | 214 // First navigate somewhere, navigate somewhere else, and go back so we have |
180 // one item in the forward stack. | 215 // one item in the forward stack. |
181 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); | 216 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestOneFile)); |
182 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); | 217 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestTwoFile)); |
183 | 218 |
184 web_view()->GoBack(); | 219 web_view()->GoBack(); |
185 StartNestedRunLoopUntilLoadingDone(); | 220 StartNestedRunLoopUntil(LOADING_DONE); |
186 | 221 |
187 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); | 222 EXPECT_EQ(GetTestFileURL(kTestOneFile).spec(), navigation_url()); |
188 EXPECT_EQ(kTestOneTitle, last_title()); | 223 EXPECT_EQ(kTestOneTitle, last_title()); |
189 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 224 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
190 last_back_button_state()); | 225 last_back_button_state()); |
191 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, | 226 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, |
192 last_forward_button_state()); | 227 last_forward_button_state()); |
193 | 228 |
194 // Now navigate to a third file. This should clear the forward stack. | 229 // Now navigate to a third file. This should clear the forward stack. |
195 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); | 230 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTestThreeFile)); |
196 | 231 |
197 EXPECT_EQ(GetTestFileURL(kTestThreeFile).spec(), navigation_url()); | 232 EXPECT_EQ(GetTestFileURL(kTestThreeFile).spec(), navigation_url()); |
198 EXPECT_EQ(kTestThreeTitle, last_title()); | 233 EXPECT_EQ(kTestThreeTitle, last_title()); |
199 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); | 234 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_ENABLED, last_back_button_state()); |
200 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, | 235 EXPECT_EQ(mojom::ButtonState::BUTTON_STATE_DISABLED, |
201 last_forward_button_state()); | 236 last_forward_button_state()); |
202 } | 237 } |
203 | 238 |
239 TEST_F(WebViewTest, Find) { | |
240 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTheWordGreenFiveTimes)); | |
241 | |
242 web_view()->Find(1, "Green"); | |
243 StartNestedRunLoopUntil(FINAL_FIND_UPATE); | |
244 EXPECT_EQ(1, active_find_match()); | |
245 EXPECT_EQ(5, find_count()); | |
246 } | |
247 | |
248 TEST_F(WebViewTest, FindAcrossIframes) { | |
249 ASSERT_NO_FATAL_FAILURE(NavigateTo(kTwoIframesWithGreen)); | |
250 | |
251 web_view()->Find(1, "Green"); | |
252 StartNestedRunLoopUntil(FINAL_FIND_UPATE); | |
253 EXPECT_EQ(13, find_count()); | |
254 } | |
255 | |
sky
2015/10/02 16:03:40
nit: only one newline.
| |
256 | |
204 } // namespace web_view | 257 } // namespace web_view |
OLD | NEW |