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/policy/host_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 using ::testing::Invoke; | |
| 18 using ::testing::Mock; | |
| 19 | |
| 20 namespace policy { | |
| 21 | |
| 22 class TestingHostBlacklistManager : public HostBlacklistManager { | |
| 23 public: | |
| 24 explicit TestingHostBlacklistManager(Profile* profile) | |
| 25 : HostBlacklistManager(profile) { | |
| 26 dtor_called_ = false; | |
| 27 } | |
| 28 | |
| 29 virtual ~TestingHostBlacklistManager() { | |
| 30 dtor_called_ = true; | |
| 31 } | |
| 32 | |
| 33 virtual void PostUpdateTask() OVERRIDE { | |
| 34 // Post tasks without a delay during tests. | |
| 35 MessageLoop::current()->PostTask(FROM_HERE, update_task_); | |
| 36 } | |
| 37 | |
| 38 static bool DtorWasCalled() { | |
| 39 return dtor_called_; | |
| 40 } | |
| 41 | |
| 42 void UpdateNotMocked() { | |
| 43 HostBlacklistManager::Update(); | |
| 44 } | |
| 45 | |
| 46 MOCK_METHOD0(Update, void()); | |
| 47 | |
| 48 private: | |
| 49 static bool dtor_called_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(TestingHostBlacklistManager); | |
| 52 }; | |
| 53 | |
| 54 bool TestingHostBlacklistManager::dtor_called_; | |
| 55 | |
| 56 class HostBlacklistManagerTest : public testing::Test { | |
| 57 protected: | |
| 58 HostBlacklistManagerTest() | |
| 59 : ui_thread_(BrowserThread::UI, &loop_), | |
| 60 file_thread_(BrowserThread::FILE, &loop_), | |
| 61 io_thread_(BrowserThread::IO, &loop_) { | |
| 62 } | |
| 63 | |
| 64 virtual void SetUp() OVERRIDE { | |
| 65 notification_service_.reset(new NotificationService); | |
| 66 profile_.reset(new TestingProfile); | |
| 67 pref_service_ = profile_->GetTestingPrefService(); | |
| 68 host_blacklist_manager_ = new TestingHostBlacklistManager(profile_.get()); | |
| 69 host_blacklist_manager_->Initialize(); | |
| 70 } | |
| 71 | |
| 72 void ExpectUpdate() { | |
| 73 EXPECT_CALL(*host_blacklist_manager_, Update()) | |
| 74 .WillOnce(Invoke(host_blacklist_manager_.get(), | |
| 75 &TestingHostBlacklistManager::UpdateNotMocked)); | |
| 76 } | |
| 77 | |
| 78 MessageLoop loop_; | |
| 79 scoped_ptr<NotificationService> notification_service_; | |
|
Paweł Hajdan Jr.
2011/08/25 17:19:52
nit: Why scoped_ptr?
Joao da Silva
2011/08/26 09:28:30
No reason for it, removed.
| |
| 80 scoped_ptr<TestingProfile> profile_; | |
|
Paweł Hajdan Jr.
2011/08/25 17:19:52
nit: Why scoped_ptr?
Joao da Silva
2011/08/26 09:28:30
Same.
| |
| 81 TestingPrefService* pref_service_; | |
| 82 scoped_refptr<TestingHostBlacklistManager> host_blacklist_manager_; | |
| 83 | |
| 84 private: | |
| 85 BrowserThread ui_thread_; | |
| 86 BrowserThread file_thread_; | |
| 87 BrowserThread io_thread_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(HostBlacklistManagerTest); | |
| 90 }; | |
| 91 | |
| 92 TEST_F(HostBlacklistManagerTest, SingleUpdateForTwoPrefChanges) { | |
| 93 ExpectUpdate(); | |
| 94 | |
| 95 ListValue* blacklist = new ListValue; | |
| 96 blacklist->Append(new StringValue("*.google.com")); | |
| 97 ListValue* whitelist = new ListValue; | |
| 98 whitelist->Append(new StringValue("mail.google.com")); | |
| 99 pref_service_->SetManagedPref(prefs::kHostBlacklist, blacklist); | |
| 100 pref_service_->SetManagedPref(prefs::kHostBlacklist, whitelist); | |
| 101 loop_.RunAllPending(); | |
| 102 | |
| 103 Mock::VerifyAndClearExpectations(host_blacklist_manager_.get()); | |
| 104 } | |
| 105 | |
| 106 TEST_F(HostBlacklistManagerTest, ReleaseWithPendingTasks) { | |
| 107 HostBlacklistManager* manager = host_blacklist_manager_.get(); | |
| 108 EXPECT_TRUE(manager->HasOneRef()); | |
| 109 ExpectUpdate(); | |
| 110 | |
| 111 ListValue* blacklist = new ListValue; | |
| 112 blacklist->Append(new StringValue("*.google.com")); | |
| 113 pref_service_->SetManagedPref(prefs::kHostBlacklist, blacklist); | |
| 114 | |
| 115 // The pref change notification made the HostBlacklistManager post a task. | |
| 116 // Release the main ref now. | |
| 117 EXPECT_FALSE(manager->HasOneRef()); // Should have 2 refs. | |
| 118 host_blacklist_manager_ = NULL; | |
| 119 EXPECT_TRUE(manager->HasOneRef()); | |
| 120 EXPECT_FALSE(TestingHostBlacklistManager::DtorWasCalled()); | |
| 121 loop_.RunAllPending(); | |
| 122 EXPECT_TRUE(TestingHostBlacklistManager::DtorWasCalled()); | |
| 123 } | |
| 124 | |
| 125 } // namespace policy | |
| OLD | NEW |