OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ios/web/web_state/web_state_impl.h" | 5 #import "ios/web/web_state/web_state_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #import "base/mac/bind_objc_block.h" | 14 #import "base/mac/bind_objc_block.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #import "base/test/ios/wait_util.h" | 16 #import "base/test/ios/wait_util.h" |
17 #import "ios/web/public/java_script_dialog_presenter.h" | 17 #import "ios/web/public/java_script_dialog_presenter.h" |
18 #include "ios/web/public/load_committed_details.h" | 18 #include "ios/web/public/load_committed_details.h" |
19 #include "ios/web/public/test/fakes/test_browser_state.h" | 19 #include "ios/web/public/test/fakes/test_browser_state.h" |
20 #import "ios/web/public/test/fakes/test_web_state_delegate.h" | 20 #import "ios/web/public/test/fakes/test_web_state_delegate.h" |
21 #import "ios/web/public/test/fakes/test_web_state_observer.h" | 21 #import "ios/web/public/test/fakes/test_web_state_observer.h" |
22 #include "ios/web/public/test/web_test.h" | 22 #include "ios/web/public/test/web_test.h" |
23 #import "ios/web/public/web_state/context_menu_params.h" | 23 #import "ios/web/public/web_state/context_menu_params.h" |
24 #include "ios/web/public/web_state/global_web_state_observer.h" | 24 #include "ios/web/public/web_state/global_web_state_observer.h" |
| 25 #include "ios/web/public/web_state/navigation_context.h" |
25 #import "ios/web/public/web_state/web_state_delegate.h" | 26 #import "ios/web/public/web_state/web_state_delegate.h" |
26 #include "ios/web/public/web_state/web_state_observer.h" | 27 #include "ios/web/public/web_state/web_state_observer.h" |
27 #import "ios/web/public/web_state/web_state_policy_decider.h" | 28 #import "ios/web/public/web_state/web_state_policy_decider.h" |
28 #include "ios/web/web_state/global_web_state_event_tracker.h" | 29 #include "ios/web/web_state/global_web_state_event_tracker.h" |
29 #import "ios/web/web_state/ui/crw_web_controller.h" | 30 #import "ios/web/web_state/ui/crw_web_controller.h" |
30 #include "net/http/http_response_headers.h" | 31 #include "net/http/http_response_headers.h" |
31 #include "net/http/http_util.h" | 32 #include "net/http/http_util.h" |
32 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
34 #import "testing/gtest_mac.h" | 35 #import "testing/gtest_mac.h" |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 EXPECT_NE("", web_state_->GetContentLanguageHeader()); | 245 EXPECT_NE("", web_state_->GetContentLanguageHeader()); |
245 | 246 |
246 // ... but not after loading another page, nor should there be specific | 247 // ... but not after loading another page, nor should there be specific |
247 // parsed values. | 248 // parsed values. |
248 web_state_->OnNavigationCommitted(GURL("http://elsewhere.com/")); | 249 web_state_->OnNavigationCommitted(GURL("http://elsewhere.com/")); |
249 EXPECT_EQ(NULL, web_state_->GetHttpResponseHeaders()); | 250 EXPECT_EQ(NULL, web_state_->GetHttpResponseHeaders()); |
250 EXPECT_EQ("", web_state_->GetContentsMimeType()); | 251 EXPECT_EQ("", web_state_->GetContentsMimeType()); |
251 EXPECT_EQ("", web_state_->GetContentLanguageHeader()); | 252 EXPECT_EQ("", web_state_->GetContentLanguageHeader()); |
252 } | 253 } |
253 | 254 |
| 255 // Tests forwarding to WebStateObserver callbacks. |
254 TEST_F(WebStateImplTest, ObserverTest) { | 256 TEST_F(WebStateImplTest, ObserverTest) { |
255 std::unique_ptr<TestWebStateObserver> observer( | 257 std::unique_ptr<TestWebStateObserver> observer( |
256 new TestWebStateObserver(web_state_.get())); | 258 new TestWebStateObserver(web_state_.get())); |
257 EXPECT_EQ(web_state_.get(), observer->web_state()); | 259 EXPECT_EQ(web_state_.get(), observer->web_state()); |
258 | 260 |
| 261 // Test that LoadProgressChanged() is called. |
| 262 ASSERT_FALSE(observer->change_loading_progress_info()); |
| 263 const double kTestLoadProgress = 0.75; |
| 264 web_state_->SendChangeLoadProgress(kTestLoadProgress); |
| 265 ASSERT_TRUE(observer->change_loading_progress_info()); |
| 266 EXPECT_EQ(web_state_.get(), |
| 267 observer->change_loading_progress_info()->web_state); |
| 268 EXPECT_EQ(kTestLoadProgress, |
| 269 observer->change_loading_progress_info()->progress); |
| 270 |
| 271 // Test that TitleWasSet() is called. |
| 272 ASSERT_FALSE(observer->title_was_set_info()); |
| 273 web_state_->OnTitleChanged(); |
| 274 ASSERT_TRUE(observer->title_was_set_info()); |
| 275 EXPECT_EQ(web_state_.get(), observer->title_was_set_info()->web_state); |
| 276 |
| 277 // Test that DocumentSubmitted() is called. |
| 278 ASSERT_FALSE(observer->submit_document_info()); |
| 279 std::string kTestFormName("form-name"); |
| 280 BOOL user_initiated = true; |
| 281 web_state_->OnDocumentSubmitted(kTestFormName, user_initiated); |
| 282 ASSERT_TRUE(observer->submit_document_info()); |
| 283 EXPECT_EQ(web_state_.get(), observer->submit_document_info()->web_state); |
| 284 EXPECT_EQ(kTestFormName, observer->submit_document_info()->form_name); |
| 285 EXPECT_EQ(user_initiated, observer->submit_document_info()->user_initiated); |
| 286 |
| 287 // Test that FormActivityRegistered() is called. |
| 288 ASSERT_FALSE(observer->form_activity_info()); |
| 289 std::string kTestFieldName("field-name"); |
| 290 std::string kTestTypeType("type"); |
| 291 std::string kTestValue("value"); |
| 292 web_state_->OnFormActivityRegistered(kTestFormName, kTestFieldName, |
| 293 kTestTypeType, kTestValue, true); |
| 294 ASSERT_TRUE(observer->form_activity_info()); |
| 295 EXPECT_EQ(web_state_.get(), observer->form_activity_info()->web_state); |
| 296 EXPECT_EQ(kTestFormName, observer->form_activity_info()->form_name); |
| 297 EXPECT_EQ(kTestFieldName, observer->form_activity_info()->field_name); |
| 298 EXPECT_EQ(kTestTypeType, observer->form_activity_info()->type); |
| 299 EXPECT_EQ(kTestValue, observer->form_activity_info()->value); |
| 300 EXPECT_TRUE(observer->form_activity_info()->input_missing); |
| 301 |
| 302 // Test that FaviconUrlUpdated() is called. |
| 303 ASSERT_FALSE(observer->update_favicon_url_candidates_info()); |
| 304 web::FaviconURL favicon_url(GURL("https://chromium.test/"), |
| 305 web::FaviconURL::TOUCH_ICON, {gfx::Size(5, 6)}); |
| 306 web_state_->OnFaviconUrlUpdated({favicon_url}); |
| 307 ASSERT_TRUE(observer->update_favicon_url_candidates_info()); |
| 308 EXPECT_EQ(web_state_.get(), |
| 309 observer->update_favicon_url_candidates_info()->web_state); |
| 310 ASSERT_EQ(1U, |
| 311 observer->update_favicon_url_candidates_info()->candidates.size()); |
| 312 const web::FaviconURL& actual_favicon_url = |
| 313 observer->update_favicon_url_candidates_info()->candidates[0]; |
| 314 EXPECT_EQ(favicon_url.icon_url, actual_favicon_url.icon_url); |
| 315 EXPECT_EQ(favicon_url.icon_type, actual_favicon_url.icon_type); |
| 316 ASSERT_EQ(favicon_url.icon_sizes.size(), |
| 317 actual_favicon_url.icon_sizes.size()); |
| 318 EXPECT_EQ(favicon_url.icon_sizes[0].width(), |
| 319 actual_favicon_url.icon_sizes[0].width()); |
| 320 EXPECT_EQ(favicon_url.icon_sizes[0].height(), |
| 321 actual_favicon_url.icon_sizes[0].height()); |
| 322 |
| 323 // Test that RenderProcessGone() is called. |
| 324 SetIgnoreRenderProcessCrashesDuringTesting(true); |
| 325 ASSERT_FALSE(observer->render_process_gone_info()); |
| 326 web_state_->OnRenderProcessGone(); |
| 327 ASSERT_TRUE(observer->render_process_gone_info()); |
| 328 EXPECT_EQ(web_state_.get(), observer->render_process_gone_info()->web_state); |
| 329 |
259 // Test that ProvisionalNavigationStarted() is called. | 330 // Test that ProvisionalNavigationStarted() is called. |
260 EXPECT_FALSE(observer->provisional_navigation_started_called()); | 331 ASSERT_FALSE(observer->start_provisional_navigation_info()); |
261 web_state_->OnProvisionalNavigationStarted(GURL("http://test")); | 332 const GURL url("http://test"); |
262 EXPECT_TRUE(observer->provisional_navigation_started_called()); | 333 web_state_->OnProvisionalNavigationStarted(url); |
| 334 ASSERT_TRUE(observer->start_provisional_navigation_info()); |
| 335 EXPECT_EQ(web_state_.get(), |
| 336 observer->start_provisional_navigation_info()->web_state); |
| 337 EXPECT_EQ(url, observer->start_provisional_navigation_info()->url); |
263 | 338 |
264 // Test that NavigationItemsPruned() is called. | 339 // Test that NavigationItemsPruned() is called. |
265 EXPECT_FALSE(observer->navigation_items_pruned_called()); | 340 ASSERT_FALSE(observer->navigation_items_pruned_info()); |
266 web_state_->OnNavigationItemsPruned(1); | 341 web_state_->OnNavigationItemsPruned(1); |
267 EXPECT_TRUE(observer->navigation_items_pruned_called()); | 342 ASSERT_TRUE(observer->navigation_items_pruned_info()); |
| 343 EXPECT_EQ(web_state_.get(), |
| 344 observer->navigation_items_pruned_info()->web_state); |
268 | 345 |
269 // Test that NavigationItemChanged() is called. | 346 // Test that NavigationItemChanged() is called. |
270 EXPECT_FALSE(observer->navigation_item_changed_called()); | 347 ASSERT_FALSE(observer->navigation_item_changed_info()); |
271 web_state_->OnNavigationItemChanged(); | 348 web_state_->OnNavigationItemChanged(); |
272 EXPECT_TRUE(observer->navigation_item_changed_called()); | 349 ASSERT_TRUE(observer->navigation_item_changed_info()); |
| 350 EXPECT_EQ(web_state_.get(), |
| 351 observer->navigation_item_changed_info()->web_state); |
273 | 352 |
274 // Test that NavigationItemCommitted() is called. | 353 // Test that NavigationItemCommitted() is called. |
275 EXPECT_FALSE(observer->navigation_item_committed_called()); | 354 ASSERT_FALSE(observer->commit_navigation_info()); |
276 LoadCommittedDetails details; | 355 LoadCommittedDetails details; |
277 web_state_->OnNavigationItemCommitted(details); | 356 web_state_->OnNavigationItemCommitted(details); |
278 EXPECT_TRUE(observer->navigation_item_committed_called()); | 357 ASSERT_TRUE(observer->commit_navigation_info()); |
| 358 EXPECT_EQ(web_state_.get(), observer->commit_navigation_info()->web_state); |
| 359 LoadCommittedDetails actual_details = |
| 360 observer->commit_navigation_info()->load_details; |
| 361 EXPECT_EQ(details.item, actual_details.item); |
| 362 EXPECT_EQ(details.previous_item_index, actual_details.previous_item_index); |
| 363 EXPECT_EQ(details.previous_url, actual_details.previous_url); |
| 364 EXPECT_EQ(details.is_in_page, actual_details.is_in_page); |
279 | 365 |
280 // Test that OnPageLoaded() is called with success when there is no error. | 366 // Test that OnPageLoaded() is called with success when there is no error. |
281 EXPECT_FALSE(observer->page_loaded_called_with_success()); | 367 ASSERT_FALSE(observer->load_page_info()); |
282 web_state_->OnPageLoaded(GURL("http://test"), false); | 368 web_state_->OnPageLoaded(url, false); |
283 EXPECT_FALSE(observer->page_loaded_called_with_success()); | 369 ASSERT_TRUE(observer->load_page_info()); |
284 web_state_->OnPageLoaded(GURL("http://test"), true); | 370 EXPECT_EQ(web_state_.get(), observer->load_page_info()->web_state); |
285 EXPECT_TRUE(observer->page_loaded_called_with_success()); | 371 EXPECT_FALSE(observer->load_page_info()->success); |
| 372 web_state_->OnPageLoaded(url, true); |
| 373 ASSERT_TRUE(observer->load_page_info()); |
| 374 EXPECT_EQ(web_state_.get(), observer->load_page_info()->web_state); |
| 375 EXPECT_TRUE(observer->load_page_info()->success); |
286 | 376 |
287 // Test that DidFinishNavigation() is called for same page navigations. | 377 // Test that DidFinishNavigation() is called for same page navigations. |
288 EXPECT_FALSE(observer->did_finish_navigation_called()); | 378 ASSERT_FALSE(observer->did_finish_navigation_info()); |
289 web_state_->OnSamePageNavigation(GURL("http://test")); | 379 web_state_->OnSamePageNavigation(url); |
290 EXPECT_TRUE(observer->did_finish_navigation_called()); | 380 ASSERT_TRUE(observer->did_finish_navigation_info()); |
| 381 EXPECT_EQ(web_state_.get(), |
| 382 observer->did_finish_navigation_info()->web_state); |
| 383 NavigationContext* context = |
| 384 observer->did_finish_navigation_info()->context.get(); |
| 385 ASSERT_TRUE(context); |
| 386 EXPECT_EQ(url, context->GetUrl()); |
| 387 EXPECT_TRUE(context->IsSamePage()); |
| 388 EXPECT_FALSE(context->IsErrorPage()); |
291 | 389 |
292 // Reset the observer and test that DidFinishNavigation() is called | 390 // Reset the observer and test that DidFinishNavigation() is called |
293 // for error navigations. | 391 // for error navigations. |
294 observer = base::MakeUnique<TestWebStateObserver>(web_state_.get()); | 392 observer = base::MakeUnique<TestWebStateObserver>(web_state_.get()); |
295 EXPECT_FALSE(observer->did_finish_navigation_called()); | 393 ASSERT_FALSE(observer->did_finish_navigation_info()); |
296 web_state_->OnErrorPageNavigation(GURL("http://test")); | 394 web_state_->OnErrorPageNavigation(url); |
297 EXPECT_TRUE(observer->did_finish_navigation_called()); | 395 ASSERT_TRUE(observer->did_finish_navigation_info()); |
| 396 EXPECT_EQ(web_state_.get(), |
| 397 observer->did_finish_navigation_info()->web_state); |
| 398 context = observer->did_finish_navigation_info()->context.get(); |
| 399 ASSERT_TRUE(context); |
| 400 EXPECT_EQ(url, context->GetUrl()); |
| 401 EXPECT_FALSE(context->IsSamePage()); |
| 402 EXPECT_TRUE(context->IsErrorPage()); |
298 | 403 |
299 // Test that OnTitleChanged() is called. | 404 // Test that OnTitleChanged() is called. |
300 EXPECT_FALSE(observer->title_was_set_called()); | 405 ASSERT_FALSE(observer->title_was_set_info()); |
301 web_state_->OnTitleChanged(); | 406 web_state_->OnTitleChanged(); |
302 EXPECT_TRUE(observer->title_was_set_called()); | 407 ASSERT_TRUE(observer->title_was_set_info()); |
| 408 EXPECT_EQ(web_state_.get(), observer->title_was_set_info()->web_state); |
303 | 409 |
304 // Test that WebStateDestroyed() is called. | 410 // Test that WebStateDestroyed() is called. |
305 EXPECT_FALSE(observer->web_state_destroyed_called()); | 411 EXPECT_FALSE(observer->web_state_destroyed_info()); |
306 web_state_.reset(); | 412 web_state_.reset(); |
307 EXPECT_TRUE(observer->web_state_destroyed_called()); | 413 EXPECT_TRUE(observer->web_state_destroyed_info()); |
308 | 414 |
309 EXPECT_EQ(nullptr, observer->web_state()); | 415 EXPECT_EQ(nullptr, observer->web_state()); |
310 } | 416 } |
311 | 417 |
312 // Tests that WebStateDelegate methods appropriately called. | 418 // Tests that WebStateDelegate methods appropriately called. |
313 TEST_F(WebStateImplTest, DelegateTest) { | 419 TEST_F(WebStateImplTest, DelegateTest) { |
314 TestWebStateDelegate delegate; | 420 TestWebStateDelegate delegate; |
315 web_state_->SetDelegate(&delegate); | 421 web_state_->SetDelegate(&delegate); |
316 | 422 |
317 // Test that HandleContextMenu() is called. | 423 // Test that HandleContextMenu() is called. |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 // Check that a false return value is forwarded correctly. | 621 // Check that a false return value is forwarded correctly. |
516 EXPECT_FALSE( | 622 EXPECT_FALSE( |
517 web_state_->OnScriptCommandReceived(kCommand2, value_2, kUrl2, false)); | 623 web_state_->OnScriptCommandReceived(kCommand2, value_2, kUrl2, false)); |
518 EXPECT_FALSE(is_called_1); | 624 EXPECT_FALSE(is_called_1); |
519 EXPECT_TRUE(is_called_2); | 625 EXPECT_TRUE(is_called_2); |
520 | 626 |
521 web_state_->RemoveScriptCommandCallback(kPrefix2); | 627 web_state_->RemoveScriptCommandCallback(kPrefix2); |
522 } | 628 } |
523 | 629 |
524 } // namespace web | 630 } // namespace web |
OLD | NEW |