Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: content/browser/fileapi/timed_task_helper_unittest.cc

Issue 2775383003: Move some fileapi tests next to the files they cover. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include <memory>
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/run_loop.h"
10 #include "base/threading/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"
14
15 using storage::TimedTaskHelper;
16
17 namespace content {
18
19 namespace {
20
21 class Embedder {
22 public:
23 Embedder()
24 : timer_(base::ThreadTaskRunnerHandle::Get().get()),
25 timer_fired_(false) {}
26
27 void OnTimerFired() {
28 timer_fired_ = true;
29 }
30
31 TimedTaskHelper* timer() { return &timer_; }
32 bool timer_fired() const { return timer_fired_; }
33
34 private:
35 TimedTaskHelper timer_;
36 bool timer_fired_;
37 };
38
39 } // namespace
40
41 TEST(TimedTaskHelper, FireTimerWhenAlive) {
42 base::MessageLoop message_loop;
43 Embedder embedder;
44
45 ASSERT_FALSE(embedder.timer_fired());
46 ASSERT_FALSE(embedder.timer()->IsRunning());
47
48 embedder.timer()->Start(
49 FROM_HERE,
50 base::TimeDelta::FromSeconds(0),
51 base::Bind(&Embedder::OnTimerFired, base::Unretained(&embedder)));
52
53 ASSERT_TRUE(embedder.timer()->IsRunning());
54 embedder.timer()->Reset();
55 ASSERT_TRUE(embedder.timer()->IsRunning());
56
57 base::RunLoop().RunUntilIdle();
58
59 ASSERT_TRUE(embedder.timer_fired());
60 }
61
62 TEST(TimedTaskHelper, FireTimerWhenAlreadyDeleted) {
63 base::MessageLoop message_loop;
64
65 // Run message loop after embedder is already deleted to make sure callback
66 // doesn't cause a crash for use after free.
67 {
68 Embedder embedder;
69
70 ASSERT_FALSE(embedder.timer_fired());
71 ASSERT_FALSE(embedder.timer()->IsRunning());
72
73 embedder.timer()->Start(
74 FROM_HERE,
75 base::TimeDelta::FromSeconds(0),
76 base::Bind(&Embedder::OnTimerFired, base::Unretained(&embedder)));
77
78 ASSERT_TRUE(embedder.timer()->IsRunning());
79 }
80
81 // At this point the callback is still in the message queue but
82 // embedder is gone.
83 base::RunLoop().RunUntilIdle();
84 }
85
86 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/fileapi/sandbox_prioritized_origin_database_unittest.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698