Index: chrome/browser/policy/host_blacklist_manager_unittest.cc |
diff --git a/chrome/browser/policy/host_blacklist_manager_unittest.cc b/chrome/browser/policy/host_blacklist_manager_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6c9ca895d806d1f70935ef5bc62df2a3a6de5f7b |
--- /dev/null |
+++ b/chrome/browser/policy/host_blacklist_manager_unittest.cc |
@@ -0,0 +1,125 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/policy/host_blacklist_manager.h" |
+ |
+#include "base/basictypes.h" |
+#include "base/message_loop.h" |
+#include "chrome/common/pref_names.h" |
+#include "chrome/test/base/testing_pref_service.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "content/browser/browser_thread.h" |
+#include "content/common/notification_service.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using ::testing::Invoke; |
+using ::testing::Mock; |
+ |
+namespace policy { |
+ |
+class TestingHostBlacklistManager : public HostBlacklistManager { |
+ public: |
+ explicit TestingHostBlacklistManager(Profile* profile) |
+ : HostBlacklistManager(profile) { |
+ dtor_called_ = false; |
+ } |
+ |
+ virtual ~TestingHostBlacklistManager() { |
+ dtor_called_ = true; |
+ } |
+ |
+ virtual void PostUpdateTask() OVERRIDE { |
+ // Post tasks without a delay during tests. |
+ MessageLoop::current()->PostTask(FROM_HERE, update_task_); |
+ } |
+ |
+ static bool DtorWasCalled() { |
+ return dtor_called_; |
+ } |
+ |
+ void UpdateNotMocked() { |
+ HostBlacklistManager::Update(); |
+ } |
+ |
+ MOCK_METHOD0(Update, void()); |
+ |
+ private: |
+ static bool dtor_called_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestingHostBlacklistManager); |
+}; |
+ |
+bool TestingHostBlacklistManager::dtor_called_; |
+ |
+class HostBlacklistManagerTest : public testing::Test { |
+ protected: |
+ HostBlacklistManagerTest() |
+ : ui_thread_(BrowserThread::UI, &loop_), |
+ file_thread_(BrowserThread::FILE, &loop_), |
+ io_thread_(BrowserThread::IO, &loop_) { |
+ } |
+ |
+ virtual void SetUp() OVERRIDE { |
+ notification_service_.reset(new NotificationService); |
+ profile_.reset(new TestingProfile); |
+ pref_service_ = profile_->GetTestingPrefService(); |
+ host_blacklist_manager_ = new TestingHostBlacklistManager(profile_.get()); |
+ host_blacklist_manager_->Initialize(); |
+ } |
+ |
+ void ExpectUpdate() { |
+ EXPECT_CALL(*host_blacklist_manager_, Update()) |
+ .WillOnce(Invoke(host_blacklist_manager_.get(), |
+ &TestingHostBlacklistManager::UpdateNotMocked)); |
+ } |
+ |
+ MessageLoop loop_; |
+ 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.
|
+ 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.
|
+ TestingPrefService* pref_service_; |
+ scoped_refptr<TestingHostBlacklistManager> host_blacklist_manager_; |
+ |
+ private: |
+ BrowserThread ui_thread_; |
+ BrowserThread file_thread_; |
+ BrowserThread io_thread_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(HostBlacklistManagerTest); |
+}; |
+ |
+TEST_F(HostBlacklistManagerTest, SingleUpdateForTwoPrefChanges) { |
+ ExpectUpdate(); |
+ |
+ ListValue* blacklist = new ListValue; |
+ blacklist->Append(new StringValue("*.google.com")); |
+ ListValue* whitelist = new ListValue; |
+ whitelist->Append(new StringValue("mail.google.com")); |
+ pref_service_->SetManagedPref(prefs::kHostBlacklist, blacklist); |
+ pref_service_->SetManagedPref(prefs::kHostBlacklist, whitelist); |
+ loop_.RunAllPending(); |
+ |
+ Mock::VerifyAndClearExpectations(host_blacklist_manager_.get()); |
+} |
+ |
+TEST_F(HostBlacklistManagerTest, ReleaseWithPendingTasks) { |
+ HostBlacklistManager* manager = host_blacklist_manager_.get(); |
+ EXPECT_TRUE(manager->HasOneRef()); |
+ ExpectUpdate(); |
+ |
+ ListValue* blacklist = new ListValue; |
+ blacklist->Append(new StringValue("*.google.com")); |
+ pref_service_->SetManagedPref(prefs::kHostBlacklist, blacklist); |
+ |
+ // The pref change notification made the HostBlacklistManager post a task. |
+ // Release the main ref now. |
+ EXPECT_FALSE(manager->HasOneRef()); // Should have 2 refs. |
+ host_blacklist_manager_ = NULL; |
+ EXPECT_TRUE(manager->HasOneRef()); |
+ EXPECT_FALSE(TestingHostBlacklistManager::DtorWasCalled()); |
+ loop_.RunAllPending(); |
+ EXPECT_TRUE(TestingHostBlacklistManager::DtorWasCalled()); |
+} |
+ |
+} // namespace policy |