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 "chrome/test/base/testing_profile.h" |
| 12 #include "content/browser/browser_thread.h" |
| 13 #include "content/common/notification_service.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 using ::testing::_; |
| 20 using ::testing::Invoke; |
| 21 using ::testing::Mock; |
| 22 |
| 23 class TestingURLBlacklistManager : public policy::URLBlacklistManager { |
| 24 public: |
| 25 explicit TestingURLBlacklistManager(Profile* profile) |
| 26 : URLBlacklistManager(profile) { |
| 27 } |
| 28 |
| 29 virtual ~TestingURLBlacklistManager() { |
| 30 } |
| 31 |
| 32 // Make this method public for testing. |
| 33 using URLBlacklistManager::ScheduleUpdate; |
| 34 |
| 35 virtual void PostUpdateTask(Task* task) OVERRIDE { |
| 36 // Post tasks without a delay during tests. |
| 37 MessageLoop::current()->PostTask(FROM_HERE, task); |
| 38 } |
| 39 |
| 40 MOCK_METHOD0(Update, void()); |
| 41 MOCK_METHOD1(SetBlacklist, void(policy::Blacklist*)); |
| 42 |
| 43 void UpdateNotMocked() { |
| 44 URLBlacklistManager::Update(); |
| 45 } |
| 46 |
| 47 private: |
| 48 DISALLOW_COPY_AND_ASSIGN(TestingURLBlacklistManager); |
| 49 }; |
| 50 |
| 51 class URLBlacklistManagerTest : public testing::Test { |
| 52 protected: |
| 53 URLBlacklistManagerTest() |
| 54 : ui_thread_(BrowserThread::UI, &loop_), |
| 55 file_thread_(BrowserThread::FILE, &loop_), |
| 56 io_thread_(BrowserThread::IO, &loop_) { |
| 57 } |
| 58 |
| 59 virtual void SetUp() OVERRIDE { |
| 60 pref_service_ = profile_.GetTestingPrefService(); |
| 61 url_blacklist_manager_.reset(new TestingURLBlacklistManager(&profile_)); |
| 62 url_blacklist_manager_->InitializeOnIOThread(); |
| 63 loop_.RunAllPending(); |
| 64 } |
| 65 |
| 66 virtual void TearDown() OVERRIDE { |
| 67 if (url_blacklist_manager_.get()) |
| 68 url_blacklist_manager_->ShutdownOnUIThread(); |
| 69 loop_.RunAllPending(); |
| 70 } |
| 71 |
| 72 void ExpectUpdate() { |
| 73 EXPECT_CALL(*url_blacklist_manager_, Update()) |
| 74 .WillOnce(Invoke(url_blacklist_manager_.get(), |
| 75 &TestingURLBlacklistManager::UpdateNotMocked)); |
| 76 } |
| 77 |
| 78 MessageLoop loop_; |
| 79 TestingProfile profile_; |
| 80 TestingPrefService* pref_service_; |
| 81 scoped_ptr<TestingURLBlacklistManager> url_blacklist_manager_; |
| 82 |
| 83 private: |
| 84 BrowserThread ui_thread_; |
| 85 BrowserThread file_thread_; |
| 86 BrowserThread io_thread_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManagerTest); |
| 89 }; |
| 90 |
| 91 TEST_F(URLBlacklistManagerTest, SingleUpdateForTwoPrefChanges) { |
| 92 ExpectUpdate(); |
| 93 |
| 94 ListValue* blacklist = new ListValue; |
| 95 blacklist->Append(new StringValue("*.google.com")); |
| 96 ListValue* whitelist = new ListValue; |
| 97 whitelist->Append(new StringValue("mail.google.com")); |
| 98 pref_service_->SetManagedPref(prefs::kUrlBlacklist, blacklist); |
| 99 pref_service_->SetManagedPref(prefs::kUrlBlacklist, whitelist); |
| 100 loop_.RunAllPending(); |
| 101 |
| 102 Mock::VerifyAndClearExpectations(url_blacklist_manager_.get()); |
| 103 } |
| 104 |
| 105 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask0) { |
| 106 // Post an update task to the UI thread. |
| 107 url_blacklist_manager_->ScheduleUpdate(); |
| 108 // Shutdown comes before the task is executed. |
| 109 url_blacklist_manager_->ShutdownOnUIThread(); |
| 110 url_blacklist_manager_.reset(); |
| 111 // Run the task after shutdown and deletion. |
| 112 loop_.RunAllPending(); |
| 113 } |
| 114 |
| 115 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask1) { |
| 116 EXPECT_CALL(*url_blacklist_manager_, Update()).Times(0); |
| 117 // Post an update task. |
| 118 url_blacklist_manager_->ScheduleUpdate(); |
| 119 // Shutdown comes before the task is executed. |
| 120 url_blacklist_manager_->ShutdownOnUIThread(); |
| 121 // Run the task after shutdown, but before deletion. |
| 122 loop_.RunAllPending(); |
| 123 Mock::VerifyAndClearExpectations(url_blacklist_manager_.get()); |
| 124 url_blacklist_manager_.reset(); |
| 125 loop_.RunAllPending(); |
| 126 } |
| 127 |
| 128 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask2) { |
| 129 // Update posts a BuildBlacklistTask to the FILE thread. |
| 130 url_blacklist_manager_->UpdateNotMocked(); |
| 131 // Shutdown comes before the task is executed. |
| 132 url_blacklist_manager_->ShutdownOnUIThread(); |
| 133 url_blacklist_manager_.reset(); |
| 134 // Run the task after shutdown and deletion. |
| 135 loop_.RunAllPending(); |
| 136 } |
| 137 |
| 138 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask3) { |
| 139 EXPECT_CALL(*url_blacklist_manager_, SetBlacklist(_)).Times(0); |
| 140 // Update posts a BuildBlacklistTask to the FILE thread. |
| 141 url_blacklist_manager_->UpdateNotMocked(); |
| 142 // Shutdown comes before the task is executed. |
| 143 url_blacklist_manager_->ShutdownOnUIThread(); |
| 144 // Run the task after shutdown, but before deletion. |
| 145 loop_.RunAllPending(); |
| 146 Mock::VerifyAndClearExpectations(url_blacklist_manager_.get()); |
| 147 url_blacklist_manager_.reset(); |
| 148 loop_.RunAllPending(); |
| 149 } |
| 150 |
| 151 } // namespace |
OLD | NEW |