Add ICU message format support
[chromium-blink-merge.git] / content / browser / fileapi / timed_task_helper_unittest.cc
blob1bdc25d727a4e282aeb3178cd08a54563c519c62
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"
6 #include "base/bind.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;
17 namespace content {
19 namespace {
21 class Embedder {
22 public:
23 Embedder()
24 : timer_(base::ThreadTaskRunnerHandle::Get().get()),
25 timer_fired_(false) {}
27 void OnTimerFired() {
28 timer_fired_ = true;
31 TimedTaskHelper* timer() { return &timer_; }
32 bool timer_fired() const { return timer_fired_; }
34 private:
35 TimedTaskHelper timer_;
36 bool timer_fired_;
39 } // namespace
41 TEST(TimedTaskHelper, FireTimerWhenAlive) {
42 base::MessageLoop message_loop;
43 Embedder embedder;
45 ASSERT_FALSE(embedder.timer_fired());
46 ASSERT_FALSE(embedder.timer()->IsRunning());
48 embedder.timer()->Start(
49 FROM_HERE,
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.
68 Embedder embedder;
70 ASSERT_FALSE(embedder.timer_fired());
71 ASSERT_FALSE(embedder.timer()->IsRunning());
73 embedder.timer()->Start(
74 FROM_HERE,
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
82 // embedder is gone.
83 base::RunLoop().RunUntilIdle();
86 } // namespace content