OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
6 #include "base/values.h" | 6 #include "base/values.h" |
7 #include "content/browser/frame_host/navigation_entry_impl.h" | 7 #include "content/browser/frame_host/navigation_entry_impl.h" |
8 #include "content/browser/web_contents/web_contents_impl.h" | 8 #include "content/browser/web_contents/web_contents_impl.h" |
9 #include "content/browser/web_contents/web_contents_view.h" | 9 #include "content/browser/web_contents/web_contents_view.h" |
10 #include "content/public/browser/load_notification_details.h" | 10 #include "content/public/browser/load_notification_details.h" |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 gfx::Size rwhv_create_size_; | 151 gfx::Size rwhv_create_size_; |
152 }; | 152 }; |
153 | 153 |
154 class LoadingStateChangedDelegate : public WebContentsDelegate { | 154 class LoadingStateChangedDelegate : public WebContentsDelegate { |
155 public: | 155 public: |
156 LoadingStateChangedDelegate() | 156 LoadingStateChangedDelegate() |
157 : loadingStateChangedCount_(0) | 157 : loadingStateChangedCount_(0) |
158 , loadingStateToDifferentDocumentCount_(0) { | 158 , loadingStateToDifferentDocumentCount_(0) { |
159 } | 159 } |
160 | 160 |
161 // WebContentsDelgate: | 161 // WebContentsDelegate: |
162 virtual void LoadingStateChanged(WebContents* contents, | 162 virtual void LoadingStateChanged(WebContents* contents, |
163 bool to_different_document) OVERRIDE { | 163 bool to_different_document) OVERRIDE { |
164 loadingStateChangedCount_++; | 164 loadingStateChangedCount_++; |
165 if (to_different_document) | 165 if (to_different_document) |
166 loadingStateToDifferentDocumentCount_++; | 166 loadingStateToDifferentDocumentCount_++; |
167 } | 167 } |
168 | 168 |
169 int loadingStateChangedCount() const { return loadingStateChangedCount_; } | 169 int loadingStateChangedCount() const { return loadingStateChangedCount_; } |
170 int loadingStateToDifferentDocumentCount() const { | 170 int loadingStateToDifferentDocumentCount() const { |
171 return loadingStateToDifferentDocumentCount_; | 171 return loadingStateToDifferentDocumentCount_; |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 | 430 |
431 // LoadingStateChanged should be called 4 times: start and stop for the | 431 // LoadingStateChanged should be called 4 times: start and stop for the |
432 // initial load of push_state.html, and start and stop for the "navigation" | 432 // initial load of push_state.html, and start and stop for the "navigation" |
433 // triggered by history.pushState(). However, the start notification for the | 433 // triggered by history.pushState(). However, the start notification for the |
434 // history.pushState() navigation should set to_different_document to false. | 434 // history.pushState() navigation should set to_different_document to false. |
435 EXPECT_EQ("pushState", shell()->web_contents()->GetURL().ref()); | 435 EXPECT_EQ("pushState", shell()->web_contents()->GetURL().ref()); |
436 EXPECT_EQ(4, delegate->loadingStateChangedCount()); | 436 EXPECT_EQ(4, delegate->loadingStateChangedCount()); |
437 EXPECT_EQ(3, delegate->loadingStateToDifferentDocumentCount()); | 437 EXPECT_EQ(3, delegate->loadingStateToDifferentDocumentCount()); |
438 } | 438 } |
439 | 439 |
| 440 struct LoadProgressDelegateAndObserver : public WebContentsDelegate, |
| 441 public WebContentsObserver { |
| 442 LoadProgressDelegateAndObserver(Shell* shell) |
| 443 : WebContentsObserver(shell->web_contents()), |
| 444 did_start_loading(false), |
| 445 did_stop_loading(false) { |
| 446 web_contents()->SetDelegate(this); |
| 447 } |
| 448 |
| 449 // WebContentsDelegate: |
| 450 virtual void LoadProgressChanged(WebContents* source, |
| 451 double progress) OVERRIDE { |
| 452 EXPECT_TRUE(did_start_loading); |
| 453 EXPECT_FALSE(did_stop_loading); |
| 454 progresses.push_back(progress); |
| 455 } |
| 456 |
| 457 // WebContentsObserver: |
| 458 virtual void DidStartLoading(RenderViewHost* render_view_host) OVERRIDE { |
| 459 EXPECT_FALSE(did_start_loading); |
| 460 EXPECT_EQ(0U, progresses.size()); |
| 461 EXPECT_FALSE(did_stop_loading); |
| 462 did_start_loading = true; |
| 463 } |
| 464 |
| 465 virtual void DidStopLoading(RenderViewHost* render_view_host) OVERRIDE { |
| 466 EXPECT_TRUE(did_start_loading); |
| 467 EXPECT_GE(progresses.size(), 1U); |
| 468 EXPECT_FALSE(did_stop_loading); |
| 469 did_stop_loading = true; |
| 470 } |
| 471 |
| 472 bool did_start_loading; |
| 473 std::vector<double> progresses; |
| 474 bool did_stop_loading; |
| 475 }; |
| 476 |
| 477 IN_PROC_BROWSER_TEST_F(WebContentsImplBrowserTest, LoadProgress) { |
| 478 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 479 scoped_ptr<LoadProgressDelegateAndObserver> delegate( |
| 480 new LoadProgressDelegateAndObserver(shell())); |
| 481 |
| 482 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); |
| 483 |
| 484 const std::vector<double>& progresses = delegate->progresses; |
| 485 // All updates should be in order ... |
| 486 if (std::adjacent_find(progresses.begin(), |
| 487 progresses.end(), |
| 488 std::greater<double>()) != progresses.end()) { |
| 489 ADD_FAILURE() << "Progress values should be in order: " |
| 490 << ::testing::PrintToString(progresses); |
| 491 } |
| 492 |
| 493 // ... and the last one should be 1.0, meaning complete. |
| 494 ASSERT_GE(progresses.size(), 1U) |
| 495 << "There should be at least one progress update"; |
| 496 EXPECT_EQ(1.0, *progresses.rbegin()); |
| 497 } |
| 498 |
440 } // namespace content | 499 } // namespace content |
441 | 500 |
OLD | NEW |