test_navigation_observer.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef WEBLAYER_TEST_TEST_NAVIGATION_OBSERVER_H_
  5. #define WEBLAYER_TEST_TEST_NAVIGATION_OBSERVER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/run_loop.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. #include "url/gurl.h"
  10. #include "weblayer/public/navigation_observer.h"
  11. namespace weblayer {
  12. class Shell;
  13. class Tab;
  14. // A helper that waits for a navigation to finish.
  15. class TestNavigationObserver : public NavigationObserver {
  16. public:
  17. enum class NavigationEvent { kStart, kCompletion, kFailure };
  18. // Creates an instance that begins waiting for a Navigation within |shell| and
  19. // to |url| to reach the specified |target_event|.
  20. TestNavigationObserver(const GURL& url,
  21. NavigationEvent target_event,
  22. Shell* shell);
  23. TestNavigationObserver(const GURL& url,
  24. NavigationEvent target_event,
  25. Tab* tab);
  26. TestNavigationObserver(const TestNavigationObserver&) = delete;
  27. TestNavigationObserver& operator=(const TestNavigationObserver&) = delete;
  28. ~TestNavigationObserver() override;
  29. // Spins a RunLoop until the requested type of navigation event is observed.
  30. void Wait();
  31. private:
  32. // NavigationObserver implementation:
  33. void NavigationStarted(Navigation* navigation) override;
  34. void NavigationCompleted(Navigation* navigation) override;
  35. void NavigationFailed(Navigation* navigation) override;
  36. void LoadStateChanged(bool is_loading, bool should_show_loading_ui) override;
  37. void CheckNavigationCompleted();
  38. const GURL url_;
  39. absl::optional<NavigationEvent> observed_event_;
  40. NavigationEvent target_event_;
  41. raw_ptr<Tab> tab_;
  42. bool done_loading_ = false;
  43. base::RunLoop run_loop_;
  44. };
  45. } // namespace weblayer
  46. #endif // WEBLAYER_TEST_TEST_NAVIGATION_OBSERVER_H_