summaryrefslogtreecommitdiffstats
path: root/chrome/common/worker_thread_ticker_unittest.cc
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-14 17:23:19 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-14 17:23:19 +0000
commitd195f60a9b147fc2593167b639976d0e1012dab6 (patch)
tree5107b9251fe237cb66a87952b6d7742711f7108a /chrome/common/worker_thread_ticker_unittest.cc
parent36f1290ca4b444a7960e8528cc5b8c9b23d53e4d (diff)
downloadchromium_src-d195f60a9b147fc2593167b639976d0e1012dab6.zip
chromium_src-d195f60a9b147fc2593167b639976d0e1012dab6.tar.gz
chromium_src-d195f60a9b147fc2593167b639976d0e1012dab6.tar.bz2
Porting in chrome/common/
Review URL: http://codereview.chromium.org/17604 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8020 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/worker_thread_ticker_unittest.cc')
-rw-r--r--chrome/common/worker_thread_ticker_unittest.cc109
1 files changed, 109 insertions, 0 deletions
diff --git a/chrome/common/worker_thread_ticker_unittest.cc b/chrome/common/worker_thread_ticker_unittest.cc
new file mode 100644
index 0000000..c6d4db2
--- /dev/null
+++ b/chrome/common/worker_thread_ticker_unittest.cc
@@ -0,0 +1,109 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/common/worker_thread_ticker.h"
+
+#include "base/logging.h"
+#include "base/message_loop.h"
+#include "base/platform_thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+class TestCallback : public WorkerThreadTicker::Callback {
+ public:
+ TestCallback() : counter_(0), message_loop_(MessageLoop::current()) {
+ }
+
+ virtual void OnTick() {
+ counter_++;
+
+ // Finish the test faster.
+ message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+ }
+
+ int counter() const { return counter_; }
+
+ private:
+ int counter_;
+ MessageLoop* message_loop_;
+};
+
+class LongCallback : public WorkerThreadTicker::Callback {
+ public:
+ virtual void OnTick() {
+ PlatformThread::Sleep(1500);
+ }
+};
+
+void RunMessageLoopForAWhile() {
+ MessageLoop::current()->PostDelayedTask(FROM_HERE,
+ new MessageLoop::QuitTask(), 500);
+ MessageLoop::current()->Run();
+}
+
+} // namespace
+
+TEST(WorkerThreadTickerTest, Basic) {
+ MessageLoop loop;
+
+ WorkerThreadTicker ticker(50);
+ TestCallback callback;
+ EXPECT_FALSE(ticker.IsRunning());
+ EXPECT_TRUE(ticker.RegisterTickHandler(&callback));
+ EXPECT_TRUE(ticker.UnregisterTickHandler(&callback));
+ EXPECT_TRUE(ticker.Start());
+ EXPECT_FALSE(ticker.RegisterTickHandler(&callback));
+ EXPECT_FALSE(ticker.UnregisterTickHandler(&callback));
+ EXPECT_TRUE(ticker.IsRunning());
+ EXPECT_FALSE(ticker.Start()); // Can't start when it is running.
+ EXPECT_TRUE(ticker.Stop());
+ EXPECT_FALSE(ticker.IsRunning());
+ EXPECT_FALSE(ticker.Stop()); // Can't stop when it isn't running.
+}
+
+TEST(WorkerThreadTickerTest, Callback) {
+ MessageLoop loop;
+
+ WorkerThreadTicker ticker(50);
+ TestCallback callback;
+ ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
+
+ ASSERT_TRUE(ticker.Start());
+ RunMessageLoopForAWhile();
+ EXPECT_TRUE(callback.counter() > 0);
+
+ ASSERT_TRUE(ticker.Stop());
+ const int counter_value = callback.counter();
+ RunMessageLoopForAWhile();
+ EXPECT_EQ(counter_value, callback.counter());
+}
+
+TEST(WorkerThreadTickerTest, OutOfScope) {
+ MessageLoop loop;
+
+ TestCallback callback;
+ {
+ WorkerThreadTicker ticker(50);
+ ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
+
+ ASSERT_TRUE(ticker.Start());
+ RunMessageLoopForAWhile();
+ EXPECT_TRUE(callback.counter() > 0);
+ }
+ const int counter_value = callback.counter();
+ RunMessageLoopForAWhile();
+ EXPECT_EQ(counter_value, callback.counter());
+}
+
+TEST(WorkerThreadTickerTest, LongCallback) {
+ MessageLoop loop;
+
+ WorkerThreadTicker ticker(50);
+ LongCallback callback;
+ ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
+
+ ASSERT_TRUE(ticker.Start());
+ RunMessageLoopForAWhile();
+}