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 { | |
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.
| |
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 pref_service_ = profile_.GetTestingPrefService(); | |
66 host_blacklist_manager_ = new TestingHostBlacklistManager(&profile_); | |
67 host_blacklist_manager_->Initialize(); | |
68 } | |
69 | |
70 void ExpectUpdate() { | |
71 EXPECT_CALL(*host_blacklist_manager_, Update()) | |
72 .WillOnce(Invoke(host_blacklist_manager_.get(), | |
73 &TestingHostBlacklistManager::UpdateNotMocked)); | |
74 } | |
75 | |
76 MessageLoop loop_; | |
77 NotificationService notification_service_; | |
78 TestingProfile profile_; | |
79 TestingPrefService* pref_service_; | |
80 scoped_refptr<TestingHostBlacklistManager> host_blacklist_manager_; | |
81 | |
82 private: | |
83 BrowserThread ui_thread_; | |
84 BrowserThread file_thread_; | |
85 BrowserThread io_thread_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(HostBlacklistManagerTest); | |
88 }; | |
89 | |
90 TEST_F(HostBlacklistManagerTest, SingleUpdateForTwoPrefChanges) { | |
91 ExpectUpdate(); | |
92 | |
93 ListValue* blacklist = new ListValue; | |
94 blacklist->Append(new StringValue("*.google.com")); | |
95 ListValue* whitelist = new ListValue; | |
96 whitelist->Append(new StringValue("mail.google.com")); | |
97 pref_service_->SetManagedPref(prefs::kHostBlacklist, blacklist); | |
98 pref_service_->SetManagedPref(prefs::kHostBlacklist, whitelist); | |
99 loop_.RunAllPending(); | |
100 | |
101 Mock::VerifyAndClearExpectations(host_blacklist_manager_.get()); | |
102 } | |
103 | |
104 TEST_F(HostBlacklistManagerTest, ReleaseWithPendingTasks) { | |
105 HostBlacklistManager* manager = host_blacklist_manager_.get(); | |
106 EXPECT_TRUE(manager->HasOneRef()); | |
107 ExpectUpdate(); | |
108 | |
109 ListValue* blacklist = new ListValue; | |
110 blacklist->Append(new StringValue("*.google.com")); | |
111 pref_service_->SetManagedPref(prefs::kHostBlacklist, blacklist); | |
112 | |
113 // The pref change notification made the HostBlacklistManager post a task. | |
114 // Release the main ref now. | |
115 EXPECT_FALSE(manager->HasOneRef()); // Should have 2 refs. | |
116 host_blacklist_manager_ = NULL; | |
117 EXPECT_TRUE(manager->HasOneRef()); | |
118 EXPECT_FALSE(TestingHostBlacklistManager::DtorWasCalled()); | |
119 loop_.RunAllPending(); | |
120 EXPECT_TRUE(TestingHostBlacklistManager::DtorWasCalled()); | |
121 } | |
122 | |
123 } // namespace policy | |
OLD | NEW |