| Index: base/message_pump_io_ios_unittest.cc
|
| diff --git a/base/message_pump_libevent_unittest.cc b/base/message_pump_io_ios_unittest.cc
|
| similarity index 46%
|
| copy from base/message_pump_libevent_unittest.cc
|
| copy to base/message_pump_io_ios_unittest.cc
|
| index 94245cec8a91eddbc43fddbd3ddbf8a8ea3a8d0b..ddf740f24693e52b6925e72431653fba16303a2f 100644
|
| --- a/base/message_pump_libevent_unittest.cc
|
| +++ b/base/message_pump_io_ios_unittest.cc
|
| @@ -1,8 +1,8 @@
|
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Copyright 2012 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 "base/message_pump_libevent.h"
|
| +#include "base/message_pump_io_ios.h"
|
|
|
| #include <unistd.h>
|
|
|
| @@ -11,27 +11,23 @@
|
| #include "base/threading/thread.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| -#if defined(USE_SYSTEM_LIBEVENT)
|
| -#include <event.h>
|
| -#else
|
| -#include "third_party/libevent/event.h"
|
| -#endif
|
| -
|
| namespace base {
|
|
|
| -class MessagePumpLibeventTest : public testing::Test {
|
| +class MessagePumpIOSForIOTest : public testing::Test {
|
| protected:
|
| - MessagePumpLibeventTest()
|
| + MessagePumpIOSForIOTest()
|
| : ui_loop_(MessageLoop::TYPE_UI),
|
| - io_thread_("MessagePumpLibeventTestIOThread") {}
|
| - virtual ~MessagePumpLibeventTest() {}
|
| + io_thread_("MessagePumpIOSForIOTestIOThread") {}
|
| + virtual ~MessagePumpIOSForIOTest() {}
|
|
|
| virtual void SetUp() OVERRIDE {
|
| Thread::Options options(MessageLoop::TYPE_IO, 0);
|
| ASSERT_TRUE(io_thread_.StartWithOptions(options));
|
| ASSERT_EQ(MessageLoop::TYPE_IO, io_thread_.message_loop()->type());
|
| - int err = pipe(pipefds_);
|
| - ASSERT_EQ(0, err);
|
| + int ret = pipe(pipefds_);
|
| + ASSERT_EQ(0, ret);
|
| + ret = pipe(alternate_pipefds_);
|
| + ASSERT_EQ(0, ret);
|
| }
|
|
|
| virtual void TearDown() OVERRIDE {
|
| @@ -46,26 +42,31 @@ class MessagePumpLibeventTest : public testing::Test {
|
| return static_cast<MessageLoopForIO*>(io_thread_.message_loop());
|
| }
|
|
|
| - void OnLibeventNotification(
|
| - MessagePumpLibevent* pump,
|
| - MessagePumpLibevent::FileDescriptorWatcher* controller) {
|
| - pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller);
|
| + void HandleFdIOEvent(MessageLoopForIO::FileDescriptorWatcher* watcher) {
|
| + MessagePumpIOSForIO::HandleFdIOEvent(watcher->fdref_,
|
| + kCFFileDescriptorReadCallBack | kCFFileDescriptorWriteCallBack,
|
| + watcher);
|
| }
|
|
|
| + int pipefds_[2];
|
| + int alternate_pipefds_[2];
|
| +
|
| + private:
|
| MessageLoop ui_loop_;
|
| Thread io_thread_;
|
| - int pipefds_[2];
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIOTest);
|
| };
|
|
|
| namespace {
|
|
|
| -// Concrete implementation of MessagePumpLibevent::Watcher that does
|
| +// Concrete implementation of MessagePumpIOSForIO::Watcher that does
|
| // nothing useful.
|
| -class StupidWatcher : public MessagePumpLibevent::Watcher {
|
| +class StupidWatcher : public MessagePumpIOSForIO::Watcher {
|
| public:
|
| virtual ~StupidWatcher() {}
|
|
|
| - // base:MessagePumpLibevent::Watcher interface
|
| + // base:MessagePumpIOSForIO::Watcher interface
|
| virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {}
|
| virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
|
| };
|
| @@ -73,12 +74,12 @@ class StupidWatcher : public MessagePumpLibevent::Watcher {
|
| #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
|
|
|
| // Test to make sure that we catch calling WatchFileDescriptor off of the
|
| -// wrong thread.
|
| -TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) {
|
| - MessagePumpLibevent::FileDescriptorWatcher watcher;
|
| +// wrong thread.
|
| +TEST_F(MessagePumpIOSForIOTest, TestWatchingFromBadThread) {
|
| + MessagePumpIOSForIO::FileDescriptorWatcher watcher;
|
| StupidWatcher delegate;
|
|
|
| - ASSERT_DEATH(io_loop()->WatchFileDescriptor(
|
| + ASSERT_DEBUG(io_loop()->WatchFileDescriptor(
|
| STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate),
|
| "Check failed: "
|
| "watch_file_descriptor_caller_checker_.CalledOnValidThread()");
|
| @@ -86,15 +87,15 @@ TEST_F(MessagePumpLibeventTest, TestWatchingFromBadThread) {
|
|
|
| #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
|
|
|
| -class BaseWatcher : public MessagePumpLibevent::Watcher {
|
| +class BaseWatcher : public MessagePumpIOSForIO::Watcher {
|
| public:
|
| - BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller)
|
| + BaseWatcher(MessagePumpIOSForIO::FileDescriptorWatcher* controller)
|
| : controller_(controller) {
|
| DCHECK(controller_);
|
| }
|
| virtual ~BaseWatcher() {}
|
|
|
| - // base:MessagePumpLibevent::Watcher interface
|
| + // MessagePumpIOSForIO::Watcher interface
|
| virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {
|
| NOTREACHED();
|
| }
|
| @@ -104,13 +105,13 @@ class BaseWatcher : public MessagePumpLibevent::Watcher {
|
| }
|
|
|
| protected:
|
| - MessagePumpLibevent::FileDescriptorWatcher* controller_;
|
| + MessagePumpIOSForIO::FileDescriptorWatcher* controller_;
|
| };
|
|
|
| class DeleteWatcher : public BaseWatcher {
|
| public:
|
| explicit DeleteWatcher(
|
| - MessagePumpLibevent::FileDescriptorWatcher* controller)
|
| + MessagePumpIOSForIO::FileDescriptorWatcher* controller)
|
| : BaseWatcher(controller) {}
|
|
|
| virtual ~DeleteWatcher() {
|
| @@ -124,40 +125,62 @@ class DeleteWatcher : public BaseWatcher {
|
| }
|
| };
|
|
|
| -TEST_F(MessagePumpLibeventTest, DeleteWatcher) {
|
| - scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
|
| - MessagePumpLibevent::FileDescriptorWatcher* watcher =
|
| - new MessagePumpLibevent::FileDescriptorWatcher;
|
| +TEST_F(MessagePumpIOSForIOTest, DeleteWatcher) {
|
| + scoped_refptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO);
|
| + MessagePumpIOSForIO::FileDescriptorWatcher* watcher =
|
| + new MessagePumpIOSForIO::FileDescriptorWatcher;
|
| DeleteWatcher delegate(watcher);
|
| pump->WatchFileDescriptor(pipefds_[1],
|
| - false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate);
|
| + false, MessagePumpIOSForIO::WATCH_READ_WRITE, watcher, &delegate);
|
|
|
| - // Spoof a libevent notification.
|
| - OnLibeventNotification(pump, watcher);
|
| + // Spoof a callback.
|
| + HandleFdIOEvent(watcher);
|
| }
|
|
|
| class StopWatcher : public BaseWatcher {
|
| public:
|
| - explicit StopWatcher(
|
| - MessagePumpLibevent::FileDescriptorWatcher* controller)
|
| - : BaseWatcher(controller) {}
|
| + StopWatcher(MessagePumpIOSForIO::FileDescriptorWatcher* controller,
|
| + MessagePumpIOSForIO* pump,
|
| + int fd_to_start_watching = -1)
|
| + : BaseWatcher(controller),
|
| + pump_(pump),
|
| + fd_to_start_watching_(fd_to_start_watching) {}
|
|
|
| virtual ~StopWatcher() {}
|
|
|
| virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {
|
| controller_->StopWatchingFileDescriptor();
|
| + if (fd_to_start_watching_ >= 0) {
|
| + pump_->WatchFileDescriptor(fd_to_start_watching_,
|
| + false, MessagePumpIOSForIO::WATCH_READ_WRITE, controller_, this);
|
| + }
|
| }
|
| +
|
| + private:
|
| + MessagePumpIOSForIO* pump_;
|
| + int fd_to_start_watching_;
|
| };
|
|
|
| -TEST_F(MessagePumpLibeventTest, StopWatcher) {
|
| - scoped_refptr<MessagePumpLibevent> pump(new MessagePumpLibevent);
|
| - MessagePumpLibevent::FileDescriptorWatcher watcher;
|
| - StopWatcher delegate(&watcher);
|
| +TEST_F(MessagePumpIOSForIOTest, StopWatcher) {
|
| + scoped_refptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO);
|
| + MessagePumpIOSForIO::FileDescriptorWatcher watcher;
|
| + StopWatcher delegate(&watcher, pump);
|
| + pump->WatchFileDescriptor(pipefds_[1],
|
| + false, MessagePumpIOSForIO::WATCH_READ_WRITE, &watcher, &delegate);
|
| +
|
| + // Spoof a callback.
|
| + HandleFdIOEvent(&watcher);
|
| +}
|
| +
|
| +TEST_F(MessagePumpIOSForIOTest, StopWatcherAndWatchSomethingElse) {
|
| + scoped_refptr<MessagePumpIOSForIO> pump(new MessagePumpIOSForIO);
|
| + MessagePumpIOSForIO::FileDescriptorWatcher watcher;
|
| + StopWatcher delegate(&watcher, pump, alternate_pipefds_[1]);
|
| pump->WatchFileDescriptor(pipefds_[1],
|
| - false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate);
|
| + false, MessagePumpIOSForIO::WATCH_READ_WRITE, &watcher, &delegate);
|
|
|
| - // Spoof a libevent notification.
|
| - OnLibeventNotification(pump, &watcher);
|
| + // Spoof a callback.
|
| + HandleFdIOEvent(&watcher);
|
| }
|
|
|
| } // namespace
|
|
|