diff options
-rw-r--r-- | chrome/browser/sync/net/network_change_notifier_io_thread.cc | 24 | ||||
-rw-r--r-- | chrome/browser/sync/net/network_change_notifier_io_thread.h | 38 | ||||
-rw-r--r-- | chrome/browser/sync/net/network_change_notifier_io_thread_unittest.cc | 80 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 22 |
5 files changed, 162 insertions, 4 deletions
diff --git a/chrome/browser/sync/net/network_change_notifier_io_thread.cc b/chrome/browser/sync/net/network_change_notifier_io_thread.cc new file mode 100644 index 0000000..c2cd2b4 --- /dev/null +++ b/chrome/browser/sync/net/network_change_notifier_io_thread.cc @@ -0,0 +1,24 @@ +// Copyright (c) 2010 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/browser/sync/net/network_change_notifier_io_thread.h" + +#include "base/logging.h" +#include "chrome/browser/io_thread.h" + +NetworkChangeNotifierIOThread::NetworkChangeNotifierIOThread( + IOThread* io_thread) : io_thread_(io_thread) { + DCHECK(io_thread_); +} + +NetworkChangeNotifierIOThread::~NetworkChangeNotifierIOThread() {} + +MessageLoop* NetworkChangeNotifierIOThread::GetMessageLoop() const { + return io_thread_->message_loop(); +} + +net::NetworkChangeNotifier* +NetworkChangeNotifierIOThread::GetNetworkChangeNotifier() const { + return io_thread_->globals()->network_change_notifier.get(); +} diff --git a/chrome/browser/sync/net/network_change_notifier_io_thread.h b/chrome/browser/sync/net/network_change_notifier_io_thread.h new file mode 100644 index 0000000..c15156a --- /dev/null +++ b/chrome/browser/sync/net/network_change_notifier_io_thread.h @@ -0,0 +1,38 @@ +// Copyright (c) 2010 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. + +#ifndef CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_IO_THREAD_H_ +#define CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_IO_THREAD_H_ + +// This is a simple NetworkChangeNotifierThread wrapper around an +// IOThread and its NetworkChangeNotifier. + +#include "base/basictypes.h" +#include "chrome/common/net/network_change_notifier_thread.h" + +class IOThread; +class MessageLoop; + +class NetworkChangeNotifierIOThread + : public chrome_common_net::NetworkChangeNotifierThread { + public: + // Does not take ownership of |io_thread|. This instance must live + // no longer than |io_thread|. + explicit NetworkChangeNotifierIOThread(IOThread* io_thread); + + virtual ~NetworkChangeNotifierIOThread(); + + // chrome_common_net::NetworkChangeNotifierThread implementation. + + virtual MessageLoop* GetMessageLoop() const; + + virtual net::NetworkChangeNotifier* GetNetworkChangeNotifier() const; + + private: + IOThread* const io_thread_; + + DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierIOThread); +}; + +#endif // CHROME_BROWSER_SYNC_NET_NETWORK_CHANGE_NOTIFIER_IO_THREAD_H_ diff --git a/chrome/browser/sync/net/network_change_notifier_io_thread_unittest.cc b/chrome/browser/sync/net/network_change_notifier_io_thread_unittest.cc new file mode 100644 index 0000000..e2b4e91 --- /dev/null +++ b/chrome/browser/sync/net/network_change_notifier_io_thread_unittest.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2010 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/browser/sync/net/network_change_notifier_io_thread.h" + +#include "base/basictypes.h" +#include "base/message_loop.h" +#include "base/scoped_ptr.h" +#include "base/task.h" +#include "chrome/browser/io_thread.h" +#include "chrome/common/net/thread_blocker.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class NetworkChangeNotifierIOThreadTest : public testing::Test { + protected: + NetworkChangeNotifierIOThreadTest() {} + + virtual ~NetworkChangeNotifierIOThreadTest() {} + + virtual void SetUp() { + // We need to set the message loop type explicitly because + // IOThread doesn't do it for us and the Linux + // NetworkChangeNotifier expects it. + base::Thread::Options options; + options.message_loop_type = MessageLoop::TYPE_IO; + EXPECT_TRUE(io_thread_.StartWithOptions(options)); + thread_blocker_.reset(new chrome_common_net::ThreadBlocker(&io_thread_)); + thread_blocker_->Block(); + network_change_notifier_io_thread_.reset( + new NetworkChangeNotifierIOThread(&io_thread_)); + } + + virtual void TearDown() { + network_change_notifier_io_thread_.reset(); + // Nothing should be posted on |io_thread_| at this point; + // otherwise, it may try to access + // network_change_notifier_io_thread_, which now NULL + thread_blocker_->Unblock(); + thread_blocker_.reset(); + io_thread_.Stop(); + } + + IOThread io_thread_; + scoped_ptr<chrome_common_net::ThreadBlocker> thread_blocker_; + scoped_ptr<NetworkChangeNotifierIOThread> + network_change_notifier_io_thread_; + + private: + DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierIOThreadTest); +}; + +void CompareNetworkChangeNotifiers( + IOThread* io_thread, + NetworkChangeNotifierIOThread* network_change_notifier_io_thread) { + EXPECT_EQ(network_change_notifier_io_thread->GetMessageLoop(), + MessageLoop::current()); + EXPECT_EQ(io_thread->globals()->network_change_notifier.get(), + network_change_notifier_io_thread->GetNetworkChangeNotifier()); +} + +TEST_F(NetworkChangeNotifierIOThreadTest, Basic) { + EXPECT_EQ(io_thread_.message_loop(), + network_change_notifier_io_thread_->GetMessageLoop()); + ASSERT_TRUE( + io_thread_.PostTask(ChromeThread::IO, + FROM_HERE, + NewRunnableFunction( + &CompareNetworkChangeNotifiers, + &io_thread_, + network_change_notifier_io_thread_.get()))); + // Pump the thread to make sure the task we just posted is run + // before this test ends. + thread_blocker_->Unblock(); + thread_blocker_->Block(); +} + +} // namespace diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 9def079..25df2af 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2056,6 +2056,8 @@ 'browser/sync/glue/typed_url_model_associator.h', 'browser/sync/glue/ui_model_worker.cc', 'browser/sync/glue/ui_model_worker.h', + 'browser/sync/net/network_change_notifier_io_thread.cc', + 'browser/sync/net/network_change_notifier_io_thread.h', 'browser/sync/notification_method.h', 'browser/sync/notification_method.cc', 'browser/sync/profile_sync_service.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index c2792ae..e22f0d5 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -510,6 +510,7 @@ 'chrome_resources', 'chrome_strings', 'common', + 'common_net_test_support', 'debugger', 'renderer', 'test_support_unit', @@ -920,6 +921,7 @@ 'browser/sync/glue/theme_util_unittest.cc', 'browser/sync/glue/typed_url_model_associator_unittest.cc', 'browser/sync/glue/ui_model_worker_unittest.cc', + 'browser/sync/net/network_change_notifier_io_thread_unittest.cc', 'browser/sync/profile_sync_factory_impl_unittest.cc', 'browser/sync/profile_sync_factory_mock.cc', 'browser/sync/profile_sync_factory_mock.h', @@ -1722,6 +1724,21 @@ ], # conditions }, { + 'target_name': 'common_net_test_support', + 'type': '<(library)', + 'sources': [ + 'common/net/fake_network_change_notifier_thread.cc', + 'common/net/fake_network_change_notifier_thread.h', + 'common/net/thread_blocker.cc', + 'common/net/thread_blocker.h', + ], + 'dependencies': [ + 'common_net', + '../base/base.gyp:base', + '../net/net.gyp:net_base', + ], + }, + { # TODO(akalin): Add this to all.gyp. 'target_name': 'common_net_unit_tests', 'type': 'executable', @@ -1730,20 +1747,17 @@ '../base/test/run_all_unittests.cc', '../base/test/test_suite.h', 'common/net/fake_network_change_notifier_thread_unittest.cc', - 'common/net/fake_network_change_notifier_thread.cc', - 'common/net/fake_network_change_notifier_thread.h', 'common/net/mock_network_change_observer.h', 'common/net/network_change_notifier_proxy_unittest.cc', 'common/net/network_change_observer_proxy_unittest.cc', 'common/net/thread_blocker_unittest.cc', - 'common/net/thread_blocker.cc', - 'common/net/thread_blocker.h', ], 'include_dirs': [ '..', ], 'dependencies': [ 'common_net', + 'common_net_test_support', '../build/temp_gyp/googleurl.gyp:googleurl', '../testing/gmock.gyp:gmock', '../testing/gtest.gyp:gtest', |