test_navigation_observer.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 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. #include "weblayer/test/test_navigation_observer.h"
  5. #include "base/test/bind.h"
  6. #include "url/gurl.h"
  7. #include "weblayer/public/navigation.h"
  8. #include "weblayer/public/navigation_controller.h"
  9. #include "weblayer/public/tab.h"
  10. #include "weblayer/shell/browser/shell.h"
  11. namespace weblayer {
  12. TestNavigationObserver::TestNavigationObserver(const GURL& url,
  13. NavigationEvent target_event,
  14. Shell* shell)
  15. : TestNavigationObserver(url, target_event, shell->tab()) {}
  16. TestNavigationObserver::TestNavigationObserver(const GURL& url,
  17. NavigationEvent target_event,
  18. Tab* tab)
  19. : url_(url), target_event_(target_event), tab_(tab) {
  20. tab_->GetNavigationController()->AddObserver(this);
  21. }
  22. TestNavigationObserver::~TestNavigationObserver() {
  23. tab_->GetNavigationController()->RemoveObserver(this);
  24. }
  25. void TestNavigationObserver::NavigationStarted(Navigation* navigation) {
  26. // Note: We don't go through CheckNavigationCompleted() here as that waits
  27. // for the load to be complete, which isn't appropriate when just waiting for
  28. // the navigation to be started.
  29. if (navigation->GetURL() == url_ &&
  30. target_event_ == NavigationEvent::kStart) {
  31. run_loop_.Quit();
  32. }
  33. }
  34. void TestNavigationObserver::NavigationCompleted(Navigation* navigation) {
  35. if (navigation->GetURL() == url_)
  36. observed_event_ = NavigationEvent::kCompletion;
  37. CheckNavigationCompleted();
  38. }
  39. void TestNavigationObserver::NavigationFailed(Navigation* navigation) {
  40. if (navigation->GetURL() == url_)
  41. observed_event_ = NavigationEvent::kFailure;
  42. CheckNavigationCompleted();
  43. }
  44. void TestNavigationObserver::LoadStateChanged(bool is_loading,
  45. bool should_show_loading_ui) {
  46. done_loading_ = !is_loading;
  47. CheckNavigationCompleted();
  48. }
  49. void TestNavigationObserver::CheckNavigationCompleted() {
  50. if (done_loading_ && observed_event_ == target_event_)
  51. run_loop_.Quit();
  52. }
  53. void TestNavigationObserver::Wait() {
  54. run_loop_.Run();
  55. }
  56. } // namespace weblayer