Home | History | Annotate | Download | only in remoting
      1 // Copyright 2013 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 
      5 #ifndef CHROME_TEST_REMOTING_WAITER_H_
      6 #define CHROME_TEST_REMOTING_WAITER_H_
      7 
      8 #include "base/timer/timer.h"
      9 
     10 namespace content {
     11 class MessageLoopRunner;
     12 }
     13 
     14 namespace remoting {
     15 
     16 // Block the execution of the test code for the specified number of
     17 // milliseconds while keeping the message loop running. The browser instance
     18 // will be responsive during the wait and test actions initiated before
     19 // the wait will keep running.
     20 class TimeoutWaiter {
     21  public:
     22   explicit TimeoutWaiter(base::TimeDelta timeout);
     23   virtual ~TimeoutWaiter();
     24 
     25   // Returns true in case of success.
     26   // For TimeoutWaiter it should always be true.
     27   virtual bool Wait();
     28 
     29  protected:
     30   virtual void CancelWait();
     31 
     32  private:
     33   // Callback used to cancel the TimeoutWaiter::Wait.
     34   void CancelWaitCallback();
     35 
     36   base::OneShotTimer<TimeoutWaiter> timeout_timer_;
     37   base::TimeDelta timeout_;
     38   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
     39 
     40   DISALLOW_COPY_AND_ASSIGN(TimeoutWaiter);
     41 };
     42 
     43 // With a message loop running, keep calling callback in the specified
     44 // interval until true is returned.
     45 class ConditionalTimeoutWaiter : public TimeoutWaiter {
     46  public:
     47   typedef base::Callback<bool(void)> Predicate;
     48 
     49   ConditionalTimeoutWaiter(base::TimeDelta timeout,
     50                            base::TimeDelta interval,
     51                            const Predicate& callback);
     52   virtual ~ConditionalTimeoutWaiter();
     53 
     54   // Returns true if |callback_| returned true and false in case of timeout.
     55   virtual bool Wait() OVERRIDE;
     56 
     57  protected:
     58   virtual void CancelWait() OVERRIDE;
     59 
     60  private:
     61   // Callback used to cancel the ConditionalTimeoutWaiter::Wait.
     62   void CancelWaitCallback();
     63 
     64   base::TimeDelta interval_;
     65   Predicate callback_;
     66   base::RepeatingTimer<ConditionalTimeoutWaiter> condition_timer_;
     67   bool success_;
     68 
     69   DISALLOW_COPY_AND_ASSIGN(ConditionalTimeoutWaiter);
     70 };
     71 
     72 }  // namespace remoting
     73 
     74 #endif  // CHROME_TEST_REMOTING_WAITER_H_
     75