OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/browser/net/http_server_properties_manager.h" |
| 6 |
| 7 #include <ostream> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/testing_pref_service.h" |
| 13 #include "content/browser/browser_thread.h" |
| 14 #include "content/common/notification_service.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chrome_browser_net { |
| 20 |
| 21 namespace { |
| 22 |
| 23 using ::testing::_; |
| 24 using ::testing::Invoke; |
| 25 using ::testing::Mock; |
| 26 |
| 27 class TestingHttpServerPropertiesManager : public HttpServerPropertiesManager { |
| 28 public: |
| 29 explicit TestingHttpServerPropertiesManager(PrefService* pref_service) |
| 30 : HttpServerPropertiesManager(pref_service) { |
| 31 } |
| 32 |
| 33 virtual ~TestingHttpServerPropertiesManager() { |
| 34 } |
| 35 |
| 36 // Make this method public for testing. |
| 37 using HttpServerPropertiesManager::ScheduleUpdateOnUI; |
| 38 using HttpServerPropertiesManager::ScheduleUpdateOnIO; |
| 39 |
| 40 // Post tasks without a delay during tests. |
| 41 virtual void PostUpdateTaskOnUI(Task* task) OVERRIDE { |
| 42 MessageLoop::current()->PostTask(FROM_HERE, task); |
| 43 } |
| 44 |
| 45 // Makes a direct call to UpdateCacheFromPrefsOnIO during tests. |
| 46 void UpdateCacheFromPrefsOnIO() { |
| 47 StringVector* spdy_servers = new StringVector; |
| 48 spdy_servers->push_back("www.google.com:443"); |
| 49 HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO(spdy_servers, true); |
| 50 } |
| 51 |
| 52 void UpdateNotMocked() { |
| 53 HttpServerPropertiesManager::UpdateCacheFromPrefs(); |
| 54 } |
| 55 |
| 56 MOCK_METHOD0(UpdateCacheFromPrefs, void()); |
| 57 |
| 58 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager); |
| 60 }; |
| 61 |
| 62 class HttpServerPropertiesManagerTest : public testing::Test { |
| 63 protected: |
| 64 HttpServerPropertiesManagerTest() |
| 65 : ui_thread_(BrowserThread::UI, &loop_), |
| 66 file_thread_(BrowserThread::FILE, &loop_), |
| 67 io_thread_(BrowserThread::IO, &loop_) { |
| 68 } |
| 69 |
| 70 virtual void SetUp() OVERRIDE { |
| 71 pref_service_.RegisterListPref(prefs::kSpdyServers); |
| 72 http_server_props_manager_.reset( |
| 73 new TestingHttpServerPropertiesManager(&pref_service_)); |
| 74 loop_.RunAllPending(); |
| 75 } |
| 76 |
| 77 virtual void TearDown() OVERRIDE { |
| 78 if (http_server_props_manager_.get()) |
| 79 http_server_props_manager_->ShutdownOnUIThread(); |
| 80 loop_.RunAllPending(); |
| 81 // Delete |http_server_props_manager_| while |io_thread_| is mapping IO to |
| 82 // |loop_|. |
| 83 http_server_props_manager_.reset(); |
| 84 } |
| 85 |
| 86 void ExpectUpdate() { |
| 87 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefs()) |
| 88 .WillOnce(Invoke(http_server_props_manager_.get(), |
| 89 &TestingHttpServerPropertiesManager::UpdateNotMocked)); |
| 90 } |
| 91 |
| 92 MessageLoop loop_; |
| 93 TestingPrefService pref_service_; |
| 94 scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_; |
| 95 |
| 96 private: |
| 97 BrowserThread ui_thread_; |
| 98 BrowserThread file_thread_; |
| 99 BrowserThread io_thread_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest); |
| 102 }; |
| 103 |
| 104 TEST_F(HttpServerPropertiesManagerTest, SingleUpdateForTwoPrefChanges) { |
| 105 ExpectUpdate(); |
| 106 |
| 107 ListValue* http_server_props = new ListValue; |
| 108 http_server_props->Append(new StringValue("www.google.com:443")); |
| 109 pref_service_.SetManagedPref(prefs::kSpdyServers, http_server_props); |
| 110 loop_.RunAllPending(); |
| 111 |
| 112 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 113 } |
| 114 |
| 115 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask0) { |
| 116 // Post an update task to the UI thread. |
| 117 http_server_props_manager_->ScheduleUpdateOnUI(); |
| 118 // Shutdown comes before the task is executed. |
| 119 http_server_props_manager_->ShutdownOnUIThread(); |
| 120 http_server_props_manager_.reset(); |
| 121 // Run the task after shutdown and deletion. |
| 122 loop_.RunAllPending(); |
| 123 } |
| 124 |
| 125 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask1) { |
| 126 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefs()).Times(0); |
| 127 // Post an update task. |
| 128 http_server_props_manager_->ScheduleUpdateOnUI(); |
| 129 // Shutdown comes before the task is executed. |
| 130 http_server_props_manager_->ShutdownOnUIThread(); |
| 131 // Run the task after shutdown, but before deletion. |
| 132 loop_.RunAllPending(); |
| 133 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 134 http_server_props_manager_.reset(); |
| 135 loop_.RunAllPending(); |
| 136 } |
| 137 |
| 138 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask2) { |
| 139 // Update posts a BuildHttpServerPropetiesTask to the FILE thread. |
| 140 http_server_props_manager_->UpdateNotMocked(); |
| 141 // Shutdown comes before the task is executed. |
| 142 http_server_props_manager_->ShutdownOnUIThread(); |
| 143 // Run the task after shutdown, but before deletion. |
| 144 loop_.RunAllPending(); |
| 145 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 146 http_server_props_manager_.reset(); |
| 147 loop_.RunAllPending(); |
| 148 } |
| 149 |
| 150 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask3) { |
| 151 // This posts a task to the FILE thread. |
| 152 http_server_props_manager_->UpdateCacheFromPrefsOnIO(); |
| 153 // But shutdown happens before it is done. |
| 154 http_server_props_manager_->ShutdownOnUIThread(); |
| 155 http_server_props_manager_.reset(); |
| 156 loop_.RunAllPending(); |
| 157 |
| 158 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 159 } |
| 160 |
| 161 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingTask4) { |
| 162 // This posts a task to the FILE thread. |
| 163 http_server_props_manager_->UpdateCacheFromPrefsOnIO(); |
| 164 // But shutdown happens before it is done. |
| 165 http_server_props_manager_->ShutdownOnUIThread(); |
| 166 // This time, shutdown on UI is done but the object is still alive. |
| 167 loop_.RunAllPending(); |
| 168 http_server_props_manager_.reset(); |
| 169 loop_.RunAllPending(); |
| 170 |
| 171 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 172 } |
| 173 |
| 174 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) { |
| 175 net::HostPortPair host_port_pair("mail.google.com", 443); |
| 176 EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(host_port_pair)); |
| 177 http_server_props_manager_->SetSupportsSpdy(host_port_pair, true); |
| 178 EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(host_port_pair)); |
| 179 http_server_props_manager_->SetSupportsSpdy(host_port_pair, false); |
| 180 EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(host_port_pair)); |
| 181 } |
| 182 |
| 183 } // namespace |
| 184 |
| 185 } // namespace policy |
OLD | NEW |