OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
jam
2012/09/17 17:47:53
are you sure you really want this class? this alre
lazyboy
2012/09/17 20:22:24
Albert, can you comment on this one.
I guess we do
| |
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 CONTENT_BROWSER_BROWSER_PLUGIN_TEST_TIMEOUT_TRACKER_H_ | |
6 #define CONTENT_BROWSER_BROWSER_PLUGIN_TEST_TIMEOUT_TRACKER_H_ | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/callback_forward.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/test/test_timeouts.h" | |
12 #include "content/public/test/test_utils.h" | |
13 | |
14 namespace content { | |
15 | |
16 // A class that tracks if |OnTimeout| callback has been called on it. | |
17 // | |
18 // This class also provides a utility method to run a MessageLoopRunner for | |
19 // a specific timeout (TestTimeouts::action_timeout()) time. | |
20 class TestTimeoutTracker { | |
21 public: | |
22 TestTimeoutTracker() : timed_out_(false) {} | |
23 virtual ~TestTimeoutTracker() {} | |
24 | |
25 void OnTimeout(const base::Closure& done) { | |
26 LOG(INFO) << "Timer timed out"; | |
27 timed_out_ = true; | |
28 done.Run(); | |
29 } | |
30 | |
31 bool timed_out() { return timed_out_; } | |
32 | |
33 // Runs |message_loop_runner| for TestTimeouts::action_timeout(). | |
34 static bool RunInActionTimeout(MessageLoopRunner* message_loop_runner) { | |
35 TestTimeoutTracker timeout_tracker; | |
36 // So the task cancels on function exit. | |
37 base::WeakPtrFactory<TestTimeoutTracker> cb_weak_factory(&timeout_tracker); | |
38 MessageLoop::current()->PostDelayedTask( | |
39 FROM_HERE, | |
40 base::Bind(&TestTimeoutTracker::OnTimeout, | |
41 cb_weak_factory.GetWeakPtr(), | |
42 message_loop_runner->QuitClosure()), | |
43 TestTimeouts::action_timeout()); | |
44 message_loop_runner->Run(); | |
45 return !timeout_tracker.timed_out(); | |
46 } | |
47 | |
48 private: | |
49 bool timed_out_; | |
50 }; | |
51 | |
52 } // namespace content | |
53 | |
54 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_TEST_TIMEOUT_TRACKER_H_ | |
OLD | NEW |