Remove --enable-hidpi-pdf-plugin flags, enable by default
[chromium-blink-merge.git] / base / message_pump_libevent_unittest.cc
blobb94b3823c808fd241aaa381677396fb115a654a5
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.
5 #include "base/message_pump_libevent.h"
7 #include <unistd.h>
9 #include "base/eintr_wrapper.h"
10 #include "base/message_loop.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #if defined(USE_SYSTEM_LIBEVENT)
15 #include <event.h>
16 #else
17 #include "third_party/libevent/event.h"
18 #endif
20 namespace base {
22 class MessagePumpLibeventTest : public testing::Test {
23 protected:
24 MessagePumpLibeventTest()
25 : ui_loop_(MessageLoop::TYPE_UI),
26 io_thread_("MessagePumpLibeventTestIOThread") {}
27 virtual ~MessagePumpLibeventTest() {}
29 virtual void SetUp() OVERRIDE {
30 Thread::Options options(MessageLoop::TYPE_IO, 0);
31 ASSERT_TRUE(io_thread_.StartWithOptions(options));
32 ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type());
33 int err = pipe(pipefds_);
34 ASSERT_EQ(0, err);
37 virtual void TearDown() OVERRIDE {
38 if (HANDLE_EINTR(close(pipefds_[0])) < 0)
39 PLOG(ERROR) << "close";
40 if (HANDLE_EINTR(close(pipefds_[1])) < 0)
41 PLOG(ERROR) << "close";
44 MessageLoop* ui_loop() { return &ui_loop_; }
45 MessageLoopForIO* io_loop() const {
46 return static_cast<MessageLoopForIO*>(io_thread_.message_loop());
49 void OnLibeventNotification(
50 MessagePumpLibevent* pump,
51 MessagePumpLibevent::FileDescriptorWatcher* controller) {
52 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller);
55 MessageLoop ui_loop_;
56 Thread io_thread_;
57 int pipefds_[2];
60 namespace {
62 // Concrete implementation of MessagePumpLibevent::Watcher that does
63 // nothing useful.
64 class StupidWatcher : public MessagePumpLibevent::Watcher {
65 public:
66 virtual ~StupidWatcher() {}
68 // base:MessagePumpLibevent::Watcher interface
69 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {}
70 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
73 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
75 // Test to make sure that we catch calling WatchFileDescriptor off of the
76 // wrong thread.
77 TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) {
78 MessagePumpLibevent::FileDescriptorWatcher watcher;
79 StupidWatcher delegate;
81 ASSERT_DEATH(io_loop()->WatchFileDescriptor(
82 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate),
83 "Check failed: "
84 "watch_file_descriptor_caller_checker_.CalledOnValidThread()");
87 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
89 class BaseWatcher : public MessagePumpLibevent::Watcher {
90 public:
91 BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller)
92 : controller_(controller) {
93 DCHECK(controller_);
95 virtual ~BaseWatcher() {}
97 // base:MessagePumpLibevent::Watcher interface
98 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {
99 NOTREACHED();
102 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
103 NOTREACHED();
106 protected:
107 MessagePumpLibevent::FileDescriptorWatcher* controller_;
110 class DeleteWatcher : public BaseWatcher {
111 public:
112 explicit DeleteWatcher(
113 MessagePumpLibevent::FileDescriptorWatcher* controller)
114 : BaseWatcher(controller) {}
116 virtual ~DeleteWatcher() {
117 DCHECK(!controller_);
120 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
121 DCHECK(controller_);
122 delete controller_;
123 controller_ = NULL;
127 TEST_F(MessagePumpLibeventTest, DeleteWatcher) {
128 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
129 MessagePumpLibevent::FileDescriptorWatcher* watcher =
130 new MessagePumpLibevent::FileDescriptorWatcher;
131 DeleteWatcher delegate(watcher);
132 pump->WatchFileDescriptor(pipefds_[1],
133 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate);
135 // Spoof a libevent notification.
136 OnLibeventNotification(pump, watcher);
139 class StopWatcher : public BaseWatcher {
140 public:
141 explicit StopWatcher(
142 MessagePumpLibevent::FileDescriptorWatcher* controller)
143 : BaseWatcher(controller) {}
145 virtual ~StopWatcher() {}
147 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
148 controller_->StopWatchingFileDescriptor();
152 TEST_F(MessagePumpLibeventTest, StopWatcher) {
153 scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
154 MessagePumpLibevent::FileDescriptorWatcher watcher;
155 StopWatcher delegate(&watcher);
156 pump->WatchFileDescriptor(pipefds_[1],
157 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate);
159 // Spoof a libevent notification.
160 OnLibeventNotification(pump, &watcher);
163 } // namespace
165 } // namespace base