1 // Copyright 2014 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.
5 #include "base/basictypes.h"
7 #include "base/location.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/run_loop.h"
11 #include "base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "webkit/browser/fileapi/timed_task_helper.h"
15 using storage::TimedTaskHelper
;
24 : timer_(base::MessageLoopProxy::current().get()), timer_fired_(false) {}
30 TimedTaskHelper
* timer() { return &timer_
; }
31 bool timer_fired() const { return timer_fired_
; }
34 TimedTaskHelper timer_
;
40 TEST(TimedTaskHelper
, FireTimerWhenAlive
) {
41 base::MessageLoop message_loop
;
44 ASSERT_FALSE(embedder
.timer_fired());
45 ASSERT_FALSE(embedder
.timer()->IsRunning());
47 embedder
.timer()->Start(
49 base::TimeDelta::FromSeconds(0),
50 base::Bind(&Embedder::OnTimerFired
, base::Unretained(&embedder
)));
52 ASSERT_TRUE(embedder
.timer()->IsRunning());
53 embedder
.timer()->Reset();
54 ASSERT_TRUE(embedder
.timer()->IsRunning());
56 base::RunLoop().RunUntilIdle();
58 ASSERT_TRUE(embedder
.timer_fired());
61 TEST(TimedTaskHelper
, FireTimerWhenAlreadyDeleted
) {
62 base::MessageLoop message_loop
;
64 // Run message loop after embedder is already deleted to make sure callback
65 // doesn't cause a crash for use after free.
69 ASSERT_FALSE(embedder
.timer_fired());
70 ASSERT_FALSE(embedder
.timer()->IsRunning());
72 embedder
.timer()->Start(
74 base::TimeDelta::FromSeconds(0),
75 base::Bind(&Embedder::OnTimerFired
, base::Unretained(&embedder
)));
77 ASSERT_TRUE(embedder
.timer()->IsRunning());
80 // At this point the callback is still in the message queue but
82 base::RunLoop().RunUntilIdle();
85 } // namespace content