#ifndef COMPONENTS_AUTOFILL_IOS_BROWSER_IOS_TEST_EVENT_WAITER_H_
#define COMPONENTS_AUTOFILL_IOS_BROWSER_IOS_TEST_EVENT_WAITER_H_
#import <list>
#import "base/test/ios/wait_util.h"
#import "base/time/time.h"
namespace autofill {
template <typename Event>
class IOSTestEventWaiter {
public:
IOSTestEventWaiter(std::list<Event> expected_events, base::TimeDelta timeout);
IOSTestEventWaiter(const IOSTestEventWaiter&) = delete;
IOSTestEventWaiter& operator=(const IOSTestEventWaiter&) = delete;
~IOSTestEventWaiter() = default;
bool Wait();
bool OnEvent(Event event);
private:
std::list<Event> expected_events_;
bool runloop_running_;
base::TimeDelta timeout_;
};
template <typename Event>
IOSTestEventWaiter<Event>::IOSTestEventWaiter(std::list<Event> expected_events,
base::TimeDelta timeout)
: expected_events_(std::move(expected_events)),
runloop_running_(false),
timeout_(timeout) {}
template <typename Event>
bool IOSTestEventWaiter<Event>::Wait() {
if (expected_events_.empty())
return true;
if (runloop_running_)
return false;
runloop_running_ = true;
bool result = base::test::ios::WaitUntilConditionOrTimeout(timeout_, ^{
return expected_events_.empty();
});
runloop_running_ = false;
return result;
}
template <typename Event>
bool IOSTestEventWaiter<Event>::OnEvent(Event event) {
if (expected_events_.empty() || expected_events_.front() != event)
return false;
expected_events_.pop_front();
return true;
}
}
#endif