test_navigation_listener.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 FUCHSIA_WEB_COMMON_TEST_TEST_NAVIGATION_LISTENER_H_
  5. #define FUCHSIA_WEB_COMMON_TEST_TEST_NAVIGATION_LISTENER_H_
  6. #include <fuchsia/web/cpp/fidl.h>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "url/gurl.h"
  10. // Observes navigation events and enables test code to block until a desired
  11. // navigational state is observed.
  12. // When run with -v1, information about navigation state transitions and
  13. // unmet test expectations are logged.
  14. class TestNavigationListener final
  15. : public fuchsia::web::NavigationEventListener {
  16. public:
  17. using BeforeAckCallback =
  18. base::RepeatingCallback<void(const fuchsia::web::NavigationState& change,
  19. OnNavigationStateChangedCallback)>;
  20. TestNavigationListener();
  21. ~TestNavigationListener() override;
  22. TestNavigationListener(const TestNavigationListener&) = delete;
  23. TestNavigationListener& operator=(const TestNavigationListener&) = delete;
  24. // Spins a RunLoop until the navigation state of the page matches the fields
  25. // of |expected_state| that have been set.
  26. void RunUntilNavigationStateMatches(
  27. const fuchsia::web::NavigationState& expected_state);
  28. // Calls RunUntilNavigationStateMatches with a NavigationState that has
  29. // the main document loaded and the normal page type.
  30. void RunUntilLoaded();
  31. // Calls RunUntilNavigationStateMatches with a NavigationState that has
  32. // |expected_url|, the normal page type, and the main document loaded.
  33. void RunUntilUrlEquals(const GURL& expected_url);
  34. // Calls RunUntilNavigationStateMatches with a NavigationState that has
  35. // |expected_title| and the normal page type.
  36. void RunUntilTitleEquals(const base::StringPiece expected_title);
  37. // Calls RunUntilNavigationStateMatches with a NavigationState that has
  38. // |expected_url|, |expected_title|, and the normal page type.
  39. void RunUntilUrlAndTitleEquals(const GURL& expected_url,
  40. base::StringPiece expected_title);
  41. // Calls RunUntilNavigationStateMatches with a NavigationState that has
  42. // the error page type, |expected_title|, and the main document loaded.
  43. void RunUntilErrorPageIsLoadedAndTitleEquals(
  44. base::StringPiece expected_title);
  45. // Calls RunUntilNavigationStateMatches with a NavigationState that has
  46. // all the expected fields and the normal page type.
  47. void RunUntilUrlTitleBackForwardEquals(const GURL& expected_url,
  48. base::StringPiece expected_title,
  49. bool expected_can_go_back,
  50. bool expected_can_go_forward);
  51. // Returns the title.
  52. std::string title() { return current_state_.title(); }
  53. // Returns the current navigation state.
  54. fuchsia::web::NavigationState* current_state() { return &current_state_; }
  55. // Returns the last received changes.
  56. fuchsia::web::NavigationState* last_changes() { return &last_changes_; }
  57. // Register a callback which intercepts the execution of the event
  58. // acknowledgement callback. |before_ack| takes ownership of the
  59. // acknowledgement callback and the responsibility for executing it.
  60. // The default behavior can be restored by providing an unbound callback for
  61. // |before_ack|.
  62. void SetBeforeAckHook(BeforeAckCallback before_ack);
  63. private:
  64. struct FailureReason {
  65. const char* field_name;
  66. std::string expected;
  67. };
  68. // fuchsia::web::NavigationEventListener implementation.
  69. void OnNavigationStateChanged(
  70. fuchsia::web::NavigationState change,
  71. OnNavigationStateChangedCallback callback) override;
  72. // Compare the current state with all fields of |expected_state_| that have
  73. // been set. Any expectation mismatches will be recorded in |failure_reasons|,
  74. // if set.
  75. bool AllFieldsMatch(std::vector<FailureReason>* failure_reasons);
  76. void QuitLoopIfAllFieldsMatch(
  77. base::RepeatingClosure quit_run_loop_closure,
  78. BeforeAckCallback before_ack_callback,
  79. const fuchsia::web::NavigationState& change,
  80. fuchsia::web::NavigationEventListener::OnNavigationStateChangedCallback
  81. ack_callback);
  82. fuchsia::web::NavigationState current_state_;
  83. fuchsia::web::NavigationState last_changes_;
  84. // Set for the duration of a call to RunUntilNavigationStateMatches().
  85. const fuchsia::web::NavigationState* expected_state_ = nullptr;
  86. BeforeAckCallback before_ack_;
  87. };
  88. #endif // FUCHSIA_WEB_COMMON_TEST_TEST_NAVIGATION_LISTENER_H_