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