OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/garbled_text_service.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "chrome/browser/garbled_text_test_helper.h" |
| 9 #include "chrome/browser/prefs/pref_member.h" |
| 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "content/test/test_browser_thread.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 void OnCompleted(bool* completed) { |
| 20 *completed = true; |
| 21 } |
| 22 |
| 23 } // anonymous namespace |
| 24 |
| 25 class GarbledTextServiceTest : public ::testing::Test { |
| 26 public: |
| 27 GarbledTextServiceTest() |
| 28 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 29 io_thread_(content::BrowserThread::IO, &message_loop_) {} |
| 30 |
| 31 typedef GarbledTextURLTracker::Blacklist Blacklist; |
| 32 |
| 33 virtual void SetUp() OVERRIDE { |
| 34 test_helper_.reset(new GarbledTextTestHelper(profile())); |
| 35 } |
| 36 |
| 37 virtual void TearDown() OVERRIDE { |
| 38 message_loop_.RunAllPending(); |
| 39 } |
| 40 |
| 41 protected: |
| 42 Profile* profile() { |
| 43 return &profile_; |
| 44 } |
| 45 |
| 46 GarbledTextService* service() { |
| 47 return test_helper_->service(); |
| 48 } |
| 49 |
| 50 GarbledTextURLTracker* tracker() { |
| 51 return test_helper_->tracker(); |
| 52 } |
| 53 |
| 54 const Blacklist& blacklist() { |
| 55 return tracker()->blacklist_; |
| 56 } |
| 57 |
| 58 void UpdateBlacklist(const GarbledTextURLTracker::GarbledURLs& urls) { |
| 59 bool completed = false; |
| 60 content::BrowserThread::PostTask( |
| 61 content::BrowserThread::UI, |
| 62 FROM_HERE, |
| 63 base::Bind(&GarbledTextService::UpdateBlacklist, |
| 64 service()->AsWeakPtr(), urls, |
| 65 base::Bind(&OnCompleted, &completed))); |
| 66 message_loop_.RunAllPending(); |
| 67 EXPECT_TRUE(completed); |
| 68 } |
| 69 |
| 70 GarbledTextTestHelper* test_helper() { |
| 71 return test_helper_.get(); |
| 72 } |
| 73 |
| 74 private: |
| 75 MessageLoop message_loop_; |
| 76 content::TestBrowserThread ui_thread_; |
| 77 content::TestBrowserThread io_thread_; |
| 78 |
| 79 TestingProfile profile_; |
| 80 scoped_ptr<GarbledTextTestHelper> test_helper_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(GarbledTextServiceTest); |
| 83 }; |
| 84 |
| 85 TEST_F(GarbledTextServiceTest, LoadAndUpdateTest) { |
| 86 const std::string kHost1("foo.example.com"); |
| 87 const std::string kHost2("bar.example.com"); |
| 88 |
| 89 { |
| 90 PrefService* prefs = profile()->GetPrefs(); |
| 91 EXPECT_TRUE(prefs); |
| 92 GarbledTextServiceFactory::GetInstance()->RegisterUserPrefs(prefs); |
| 93 ListPrefUpdate update(prefs, GarbledTextService::kPrefGarbledTextBlacklist); |
| 94 ListValue* blacklist = update.Get(); |
| 95 EXPECT_TRUE(blacklist); |
| 96 |
| 97 blacklist->Append(base::Value::CreateStringValue("foo.example.com")); |
| 98 } |
| 99 |
| 100 test_helper()->Initialize(); |
| 101 |
| 102 EXPECT_EQ(1u, blacklist().size()); |
| 103 EXPECT_TRUE(blacklist().find(kHost1) != blacklist().end()); |
| 104 |
| 105 GURL kGarbledURLs[] = { |
| 106 GURL("http://foo.example.com/"), |
| 107 GURL("http://bar.example.com/"), |
| 108 }; |
| 109 GarbledTextURLTracker::GarbledURLs urls( |
| 110 kGarbledURLs, kGarbledURLs + arraysize(kGarbledURLs)); |
| 111 |
| 112 test_helper()->enabled()->SetValue(false); |
| 113 UpdateBlacklist(urls); |
| 114 EXPECT_EQ(1u, blacklist().size()); |
| 115 EXPECT_TRUE(blacklist().find(kHost1) != blacklist().end()); |
| 116 |
| 117 test_helper()->enabled()->SetValue(true); |
| 118 UpdateBlacklist(urls); |
| 119 EXPECT_EQ(2u, blacklist().size()); |
| 120 EXPECT_TRUE(blacklist().find(kHost1) != blacklist().end()); |
| 121 EXPECT_TRUE(blacklist().find(kHost2) != blacklist().end()); |
| 122 |
| 123 PrefService* prefs = profile()->GetPrefs(); |
| 124 EXPECT_TRUE(prefs); |
| 125 const ListValue* pref_blacklist = prefs->GetList( |
| 126 GarbledTextService::kPrefGarbledTextBlacklist); |
| 127 EXPECT_TRUE(pref_blacklist); |
| 128 EXPECT_EQ(2u, pref_blacklist->GetSize()); |
| 129 |
| 130 std::string entry; |
| 131 EXPECT_TRUE(pref_blacklist->GetString(0, &entry)); |
| 132 EXPECT_EQ("foo.example.com", entry); |
| 133 |
| 134 entry.clear(); |
| 135 EXPECT_TRUE(pref_blacklist->GetString(1, &entry)); |
| 136 EXPECT_EQ("bar.example.com", entry); |
| 137 } |
OLD | NEW |