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