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

Side by Side Diff: chrome/common/worker_thread_ticker_unittest.cc

Issue 1815623004: Remove Hung plugin detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remove_windowed_plugins
Patch Set: fix compile Created 4 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
« no previous file with comments | « chrome/common/worker_thread_ticker.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/common/worker_thread_ticker.h"
6
7 #include "base/location.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/threading/platform_thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 class TestCallback : public WorkerThreadTicker::Callback {
17 public:
18 TestCallback() : counter_(0), message_loop_(base::MessageLoop::current()) {}
19
20 void OnTick() override {
21 counter_++;
22
23 // Finish the test faster.
24 message_loop_->task_runner()->PostTask(
25 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
26 }
27
28 int counter() const { return counter_; }
29
30 private:
31 int counter_;
32 base::MessageLoop* message_loop_;
33 };
34
35 class LongCallback : public WorkerThreadTicker::Callback {
36 public:
37 void OnTick() override {
38 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1500));
39 }
40 };
41
42 void RunMessageLoopForAWhile() {
43 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
44 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
45 base::TimeDelta::FromMilliseconds(500));
46 base::MessageLoop::current()->Run();
47 }
48
49 } // namespace
50
51 TEST(WorkerThreadTickerTest, Basic) {
52 base::MessageLoop loop;
53
54 TestCallback callback;
55 WorkerThreadTicker ticker(50);
56 EXPECT_FALSE(ticker.IsRunning());
57 EXPECT_TRUE(ticker.RegisterTickHandler(&callback));
58 EXPECT_TRUE(ticker.UnregisterTickHandler(&callback));
59 EXPECT_TRUE(ticker.Start());
60 EXPECT_FALSE(ticker.RegisterTickHandler(&callback));
61 EXPECT_FALSE(ticker.UnregisterTickHandler(&callback));
62 EXPECT_TRUE(ticker.IsRunning());
63 EXPECT_FALSE(ticker.Start()); // Can't start when it is running.
64 EXPECT_TRUE(ticker.Stop());
65 EXPECT_FALSE(ticker.IsRunning());
66 EXPECT_FALSE(ticker.Stop()); // Can't stop when it isn't running.
67 }
68
69 TEST(WorkerThreadTickerTest, Callback) {
70 base::MessageLoop loop;
71
72 TestCallback callback;
73 WorkerThreadTicker ticker(50);
74 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
75
76 ASSERT_TRUE(ticker.Start());
77 RunMessageLoopForAWhile();
78 EXPECT_TRUE(callback.counter() > 0);
79
80 ASSERT_TRUE(ticker.Stop());
81 const int counter_value = callback.counter();
82 RunMessageLoopForAWhile();
83 EXPECT_EQ(counter_value, callback.counter());
84 }
85
86 TEST(WorkerThreadTickerTest, OutOfScope) {
87 base::MessageLoop loop;
88
89 TestCallback callback;
90 {
91 WorkerThreadTicker ticker(50);
92 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
93
94 ASSERT_TRUE(ticker.Start());
95 RunMessageLoopForAWhile();
96 EXPECT_TRUE(callback.counter() > 0);
97 }
98 const int counter_value = callback.counter();
99 RunMessageLoopForAWhile();
100 EXPECT_EQ(counter_value, callback.counter());
101 }
102
103 TEST(WorkerThreadTickerTest, LongCallback) {
104 base::MessageLoop loop;
105
106 LongCallback callback;
107 WorkerThreadTicker ticker(50);
108 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
109
110 ASSERT_TRUE(ticker.Start());
111 RunMessageLoopForAWhile();
112 }
OLDNEW
« no previous file with comments | « chrome/common/worker_thread_ticker.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698