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/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/time/time.h"
12 #include "storage/browser/fileapi/timed_task_helper.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using storage::TimedTaskHelper
;
24 : timer_(base::ThreadTaskRunnerHandle::Get().get()),
25 timer_fired_(false) {}
31 TimedTaskHelper
* timer() { return &timer_
; }
32 bool timer_fired() const { return timer_fired_
; }
35 TimedTaskHelper timer_
;
41 TEST(TimedTaskHelper
, FireTimerWhenAlive
) {
42 base::MessageLoop message_loop
;
45 ASSERT_FALSE(embedder
.timer_fired());
46 ASSERT_FALSE(embedder
.timer()->IsRunning());
48 embedder
.timer()->Start(
50 base::TimeDelta::FromSeconds(0),
51 base::Bind(&Embedder::OnTimerFired
, base::Unretained(&embedder
)));
53 ASSERT_TRUE(embedder
.timer()->IsRunning());
54 embedder
.timer()->Reset();
55 ASSERT_TRUE(embedder
.timer()->IsRunning());
57 base::RunLoop().RunUntilIdle();
59 ASSERT_TRUE(embedder
.timer_fired());
62 TEST(TimedTaskHelper
, FireTimerWhenAlreadyDeleted
) {
63 base::MessageLoop message_loop
;
65 // Run message loop after embedder is already deleted to make sure callback
66 // doesn't cause a crash for use after free.
70 ASSERT_FALSE(embedder
.timer_fired());
71 ASSERT_FALSE(embedder
.timer()->IsRunning());
73 embedder
.timer()->Start(
75 base::TimeDelta::FromSeconds(0),
76 base::Bind(&Embedder::OnTimerFired
, base::Unretained(&embedder
)));
78 ASSERT_TRUE(embedder
.timer()->IsRunning());
81 // At this point the callback is still in the message queue but
83 base::RunLoop().RunUntilIdle();
86 } // namespace content