Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1050)

Unified Diff: chrome/browser/policy/host_blacklist_manager_unittest.cc

Issue 7747018: Introduced the URLBlacklistManager, and wired it to various places. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reviewed, rebased Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..776f1b7bd0b807376709379c81c9002c41966fde
--- /dev/null
+++ b/chrome/browser/policy/host_blacklist_manager_unittest.cc
@@ -0,0 +1,123 @@
+// 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 {
willchan no longer on Chromium 2011/08/26 11:40:21 Can you put everything inside here within an anony
Joao da Silva 2011/08/29 11:24:25 Done.
+
+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 {
+ pref_service_ = profile_.GetTestingPrefService();
+ host_blacklist_manager_ = new TestingHostBlacklistManager(&profile_);
+ host_blacklist_manager_->Initialize();
+ }
+
+ void ExpectUpdate() {
+ EXPECT_CALL(*host_blacklist_manager_, Update())
+ .WillOnce(Invoke(host_blacklist_manager_.get(),
+ &TestingHostBlacklistManager::UpdateNotMocked));
+ }
+
+ MessageLoop loop_;
+ NotificationService notification_service_;
+ TestingProfile profile_;
+ 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

Powered by Google App Engine
This is Rietveld 408576698