Index: chrome/browser/net/http_server_properties_manager_unittest.cc |
=================================================================== |
--- chrome/browser/net/http_server_properties_manager_unittest.cc (revision 0) |
+++ chrome/browser/net/http_server_properties_manager_unittest.cc (revision 0) |
@@ -0,0 +1,186 @@ |
+// Copyright (c) 2011 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/net/http_server_properties_manager.h" |
+ |
+#include <ostream> |
willchan no longer on Chromium
2011/10/04 05:55:02
What's this for? Do you really need it?
ramant (doing other things)
2011/10/06 06:17:52
Done.
|
+ |
+#include "base/basictypes.h" |
+#include "base/message_loop.h" |
+#include "chrome/common/pref_names.h" |
+#include "chrome/test/base/testing_pref_service.h" |
+#include "content/browser/browser_thread.h" |
+#include "content/common/notification_service.h" |
+#include "googleurl/src/gurl.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace chrome_browser_net { |
+ |
+namespace { |
+ |
+using ::testing::_; |
+using ::testing::Invoke; |
+using ::testing::Mock; |
+ |
+class TestingHttpServerPropertiesManager : public HttpServerPropertiesManager { |
+ public: |
+ explicit TestingHttpServerPropertiesManager(PrefService* pref_service) |
+ : HttpServerPropertiesManager(pref_service) { |
+ InitializeOnIOThread(); |
+ } |
+ |
+ virtual ~TestingHttpServerPropertiesManager() { |
+ } |
+ |
+ // Make this method public for testing. |
+ using HttpServerPropertiesManager::ScheduleUpdateOnUI; |
+ using HttpServerPropertiesManager::ScheduleUpdateOnIO; |
+ |
+ // Post tasks without a delay during tests. |
+ virtual void PostUpdateTaskOnUI(Task* task) OVERRIDE { |
+ MessageLoop::current()->PostTask(FROM_HERE, task); |
+ } |
+ |
+ // Makes a direct call to UpdateCacheFromPrefsOnIO during tests. |
+ void UpdateCacheFromPrefsOnIO() { |
+ StringVector* spdy_servers = new StringVector; |
+ spdy_servers->push_back("www.google.com:443"); |
+ HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO(spdy_servers, true); |
+ } |
+ |
+ void UpdateNotMocked() { |
+ HttpServerPropertiesManager::UpdateCacheFromPrefs(); |
+ } |
+ |
+ MOCK_METHOD0(UpdateCacheFromPrefs, void()); |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager); |
+}; |
+ |
+class HttpServerPropertiesManagerTest : public testing::Test { |
+ protected: |
+ HttpServerPropertiesManagerTest() |
+ : ui_thread_(BrowserThread::UI, &loop_), |
+ file_thread_(BrowserThread::FILE, &loop_), |
+ io_thread_(BrowserThread::IO, &loop_) { |
+ } |
+ |
+ virtual void SetUp() OVERRIDE { |
+ pref_service_.RegisterListPref(prefs::kSpdyServers); |
+ http_server_props_manager_.reset( |
+ new TestingHttpServerPropertiesManager(&pref_service_)); |
+ loop_.RunAllPending(); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ if (http_server_props_manager_.get()) |
+ http_server_props_manager_->ShutdownOnUIThread(); |
+ loop_.RunAllPending(); |
+ // Delete |http_server_props_manager_| while |io_thread_| is mapping IO to |
+ // |loop_|. |
+ http_server_props_manager_.reset(); |
+ } |
+ |
+ void ExpectUpdate() { |
+ EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefs()) |
+ .WillOnce(Invoke(http_server_props_manager_.get(), |
+ &TestingHttpServerPropertiesManager::UpdateNotMocked)); |
+ } |
+ |
+ MessageLoop loop_; |
+ TestingPrefService pref_service_; |
+ scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_; |
+ |
+ private: |
+ BrowserThread ui_thread_; |
+ BrowserThread file_thread_; |
+ BrowserThread io_thread_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest); |
+}; |
+ |
+TEST_F(HttpServerPropertiesManagerTest, SingleUpdateForTwoPrefChanges) { |
+ ExpectUpdate(); |
+ |
+ ListValue* http_server_props = new ListValue; |
+ http_server_props->Append(new StringValue("www.google.com:443")); |
+ pref_service_.SetManagedPref(prefs::kSpdyServers, http_server_props); |
+ loop_.RunAllPending(); |
+ |
+ Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
+} |
+ |
+TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask0) { |
+ // Post an update task to the UI thread. |
+ http_server_props_manager_->ScheduleUpdateOnUI(); |
+ // Shutdown comes before the task is executed. |
+ http_server_props_manager_->ShutdownOnUIThread(); |
+ http_server_props_manager_.reset(); |
+ // Run the task after shutdown and deletion. |
+ loop_.RunAllPending(); |
+} |
+ |
+TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask1) { |
+ EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefs()).Times(0); |
+ // Post an update task. |
+ http_server_props_manager_->ScheduleUpdateOnUI(); |
+ // Shutdown comes before the task is executed. |
+ http_server_props_manager_->ShutdownOnUIThread(); |
+ // Run the task after shutdown, but before deletion. |
+ loop_.RunAllPending(); |
+ Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
+ http_server_props_manager_.reset(); |
+ loop_.RunAllPending(); |
+} |
+ |
+TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask2) { |
+ // Update posts a BuildHttpServerPropetiesTask to the FILE thread. |
+ http_server_props_manager_->UpdateNotMocked(); |
+ // Shutdown comes before the task is executed. |
+ http_server_props_manager_->ShutdownOnUIThread(); |
+ // Run the task after shutdown, but before deletion. |
+ loop_.RunAllPending(); |
+ Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
+ http_server_props_manager_.reset(); |
+ loop_.RunAllPending(); |
+} |
+ |
+TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask3) { |
+ // This posts a task to the FILE thread. |
+ http_server_props_manager_->UpdateCacheFromPrefsOnIO(); |
+ // But shutdown happens before it is done. |
+ http_server_props_manager_->ShutdownOnUIThread(); |
+ http_server_props_manager_.reset(); |
+ loop_.RunAllPending(); |
+ |
+ Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
+} |
+ |
+TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask4) { |
+ // This posts a task to the FILE thread. |
+ http_server_props_manager_->UpdateCacheFromPrefsOnIO(); |
+ // But shutdown happens before it is done. |
+ http_server_props_manager_->ShutdownOnUIThread(); |
+ // This time, shutdown on UI is done but the object is still alive. |
+ loop_.RunAllPending(); |
+ http_server_props_manager_.reset(); |
+ loop_.RunAllPending(); |
+ |
+ Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
+} |
+ |
+TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) { |
+ net::HostPortPair host_port_pair("mail.google.com", 443); |
+ EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(host_port_pair)); |
+ http_server_props_manager_->SetSupportsSpdy(host_port_pair, true); |
+ EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(host_port_pair)); |
+ http_server_props_manager_->SetSupportsSpdy(host_port_pair, false); |
+ EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(host_port_pair)); |
+} |
+ |
+} // namespace |
+ |
+} // namespace policy |