chromium/ui/base/interaction/polling_state_observer.h

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_INTERACTION_POLLING_STATE_OBSERVER_H_
#define UI_BASE_INTERACTION_POLLING_STATE_OBSERVER_H_

#include <optional>
#include <utility>

#include "base/functional/callback_forward.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/interactive_test_internal.h"
#include "ui/base/interaction/state_observer.h"

namespace ui::test {

// Observer which polls a state that cannot be observed via listeners/callbacks.
// Continues to poll for the lifetime of the test.
//
// Because this is a polling observer, the value is only valid at the moment it
// is observed, and transient values may be missed. For transient values, prefer
// deterministic state observation, custom events, etc.
//
// Designed for use with the `InteractiveTestApi::PollState()` verb.
template <typename T>
class PollingStateObserver : public StateObserver<T> {};

// Need out-of-line declaration of static class variables on some platforms.
template <typename T>
// static
constexpr base::TimeDelta PollingStateObserver<T>::kDefaultPollingInterval;

// Observer which polls a specific element that cannot be observed via
// listeners/callbacks. If an element with `identifier` in `context` is present,
// the observed value will be updated to the result of `poll_element_callback`,
// whereas if the element is not present in the context, it will be
// std::nullopt.
//
// If `context` is not specified, then the element will be located in any
// context.
//
// Because this is a polling observer, the value is only valid at the moment it
// is observed, and transient values may be missed. For transient values, prefer
// deterministic state observation, custom events, etc.
//
// Designed for use with the `InteractiveTestApi::PollElement()` verb.
template <typename T>
class PollingElementStateObserver
    : public PollingStateObserver<std::optional<T>> {};

}  // namespace ui::test

#endif  // UI_BASE_INTERACTION_POLLING_STATE_OBSERVER_H_