Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/FilterEngineStubs.cpp

Issue 10198022: Pass application data into libadblockplus (Closed)
Left Patch Set: Created April 12, 2013, 1:23 p.m.
Right Patch Set: Merged upstream, addressed issues, renamed AppInfo fields Created April 17, 2013, 2:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 #include <iostream>
1 #include <AdblockPlus.h> 2 #include <AdblockPlus.h>
2 #include <gtest/gtest.h> 3 #include <gtest/gtest.h>
3 4
5 class DummyFileSystem : public AdblockPlus::FileSystem
6 {
7 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const
8 {
9 throw std::runtime_error("Not implemented");
10 }
11
12 void Write(const std::string& path,
13 std::tr1::shared_ptr<std::ostream> content)
14 {
15 throw std::runtime_error("Not implemented");
16 }
17
18 void Move(const std::string& fromPath, const std::string& toPath)
19 {
20 throw std::runtime_error("Not implemented");
21 }
22
23 void Remove(const std::string& path)
24 {
25 throw std::runtime_error("Not implemented");
26 }
27
28 StatResult Stat(const std::string& path) const
29 {
30 throw std::runtime_error("Not implemented");
31 }
32 };
33
34 class DummyErrorCallback : public AdblockPlus::ErrorCallback
35 {
36 public:
37 void operator()(const std::string& message)
38 {
39 std::cout << message << std::endl;
40 }
41 };
42
4 TEST(FilterEngineStubsTest, FilterCreation) 43 TEST(FilterEngineStubsTest, FilterCreation)
5 { 44 {
6 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 45 DummyFileSystem fileSystem;
46 DummyErrorCallback errorCallback;
47 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
48 &errorCallback);
7 AdblockPlus::FilterEngine filterEngine(jsEngine); 49 AdblockPlus::FilterEngine filterEngine(jsEngine);
8 50
9 AdblockPlus::FilterPtr filter1 = filterEngine.GetFilter("foo"); 51 AdblockPlus::FilterPtr filter1 = filterEngine.GetFilter("foo");
10 ASSERT_EQ(filter1->GetProperty("type", -1), AdblockPlus::Filter::TYPE_BLOCKING ); 52 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetProperty("type", -1) );
11 AdblockPlus::FilterPtr filter2 = filterEngine.GetFilter("@@foo"); 53 AdblockPlus::FilterPtr filter2 = filterEngine.GetFilter("@@foo");
12 ASSERT_EQ(filter2->GetProperty("type", -1), AdblockPlus::Filter::TYPE_EXCEPTIO N); 54 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetProperty("type", -1 ));
13 AdblockPlus::FilterPtr filter3 = filterEngine.GetFilter("example.com##foo"); 55 AdblockPlus::FilterPtr filter3 = filterEngine.GetFilter("example.com##foo");
14 ASSERT_EQ(filter3->GetProperty("type", -1), AdblockPlus::Filter::TYPE_ELEMHIDE ); 56 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetProperty("type", -1) );
15 AdblockPlus::FilterPtr filter4 = filterEngine.GetFilter("example.com#@#foo"); 57 AdblockPlus::FilterPtr filter4 = filterEngine.GetFilter("example.com#@#foo");
16 ASSERT_EQ(filter4->GetProperty("type", -1), AdblockPlus::Filter::TYPE_ELEMHIDE _EXCEPTION); 58 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE_EXCEPTION, filter4->GetProperty(" type", -1));
17 AdblockPlus::FilterPtr filter5 = filterEngine.GetFilter(" foo "); 59 AdblockPlus::FilterPtr filter5 = filterEngine.GetFilter(" foo ");
18 ASSERT_EQ(filter5, filter1); 60 ASSERT_EQ(*filter1, *filter5);
19 } 61 }
20 62
21 TEST(FilterEngineStubsTest, FilterProperties) 63 TEST(FilterEngineStubsTest, FilterProperties)
22 { 64 {
23 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 65 DummyFileSystem fileSystem;
66 DummyErrorCallback errorCallback;
67 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
68 &errorCallback);
24 AdblockPlus::FilterEngine filterEngine(jsEngine); 69 AdblockPlus::FilterEngine filterEngine(jsEngine);
25 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo"); 70 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo");
26 71
27 ASSERT_EQ(filter->GetProperty("stringFoo", "x"), "x"); 72 ASSERT_EQ("x", filter->GetProperty("stringFoo", "x"));
28 ASSERT_EQ(filter->GetProperty("intFoo", 42), 42); 73 ASSERT_EQ(42, filter->GetProperty("intFoo", 42));
29 ASSERT_EQ(filter->GetProperty("boolFoo", false), false); 74 ASSERT_FALSE(filter->GetProperty("boolFoo", false));
30 75
31 filter->SetProperty("stringFoo", "y"); 76 filter->SetProperty("stringFoo", "y");
32 filter->SetProperty("intFoo", 24); 77 filter->SetProperty("intFoo", 24);
33 filter->SetProperty("boolFoo", true); 78 filter->SetProperty("boolFoo", true);
34 ASSERT_EQ(filter->GetProperty("stringFoo", "x"), "y"); 79 ASSERT_EQ("y", filter->GetProperty("stringFoo", "x"));
35 ASSERT_EQ(filter->GetProperty("intFoo", 42), 24); 80 ASSERT_EQ(24, filter->GetProperty("intFoo", 42));
36 ASSERT_EQ(filter->GetProperty("boolFoo", false), true); 81 ASSERT_TRUE(filter->GetProperty("boolFoo", false));
37 } 82 }
38 83
39 TEST(FilterEngineStubsTest, AddRemoveFilters) 84 TEST(FilterEngineStubsTest, AddRemoveFilters)
40 { 85 {
41 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 86 DummyFileSystem fileSystem;
87 DummyErrorCallback errorCallback;
88 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
89 &errorCallback);
42 AdblockPlus::FilterEngine filterEngine(jsEngine); 90 AdblockPlus::FilterEngine filterEngine(jsEngine);
43 ASSERT_EQ(filterEngine.GetListedFilters().size(), 0u); 91 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
44 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo"); 92 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo");
45 ASSERT_EQ(filterEngine.GetListedFilters().size(), 0u); 93 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
46 filter->AddToList(); 94 filter->AddToList();
47 ASSERT_EQ(filterEngine.GetListedFilters().size(), 1u); 95 ASSERT_EQ(1u, filterEngine.GetListedFilters().size());
48 ASSERT_EQ(filterEngine.GetListedFilters()[0], filter); 96 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]);
49 filter->AddToList(); 97 filter->AddToList();
50 ASSERT_EQ(filterEngine.GetListedFilters().size(), 1u); 98 ASSERT_EQ(1u, filterEngine.GetListedFilters().size());
51 ASSERT_EQ(filterEngine.GetListedFilters()[0], filter); 99 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]);
52 filter->RemoveFromList(); 100 filter->RemoveFromList();
53 ASSERT_EQ(filterEngine.GetListedFilters().size(), 0u); 101 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
54 filter->RemoveFromList(); 102 filter->RemoveFromList();
55 ASSERT_EQ(filterEngine.GetListedFilters().size(), 0u); 103 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
56 } 104 }
57 105
58 TEST(FilterEngineStubsTest, SubscriptionProperties) 106 TEST(FilterEngineStubsTest, SubscriptionProperties)
59 { 107 {
60 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 108 DummyFileSystem fileSystem;
109 DummyErrorCallback errorCallback;
110 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
111 &errorCallback);
61 AdblockPlus::FilterEngine filterEngine(jsEngine); 112 AdblockPlus::FilterEngine filterEngine(jsEngine);
62 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" ); 113 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
63 114
64 ASSERT_EQ(subscription->GetProperty("stringFoo", "x"), "x"); 115 ASSERT_EQ("x", subscription->GetProperty("stringFoo", "x"));
65 ASSERT_EQ(subscription->GetProperty("intFoo", 42), 42); 116 ASSERT_EQ(42, subscription->GetProperty("intFoo", 42));
66 ASSERT_EQ(subscription->GetProperty("boolFoo", false), false); 117 ASSERT_FALSE(subscription->GetProperty("boolFoo", false));
67 118
68 subscription->SetProperty("stringFoo", "y"); 119 subscription->SetProperty("stringFoo", "y");
69 subscription->SetProperty("intFoo", 24); 120 subscription->SetProperty("intFoo", 24);
70 subscription->SetProperty("boolFoo", true); 121 subscription->SetProperty("boolFoo", true);
71 ASSERT_EQ(subscription->GetProperty("stringFoo", "x"), "y"); 122 ASSERT_EQ("y", subscription->GetProperty("stringFoo", "x"));
72 ASSERT_EQ(subscription->GetProperty("intFoo", 42), 24); 123 ASSERT_EQ(24, subscription->GetProperty("intFoo", 42));
73 ASSERT_EQ(subscription->GetProperty("boolFoo", false), true); 124 ASSERT_TRUE(subscription->GetProperty("boolFoo", false));
74 } 125 }
75 126
76 TEST(FilterEngineStubsTest, AddRemoveSubscriptions) 127 TEST(FilterEngineStubsTest, AddRemoveSubscriptions)
77 { 128 {
78 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 129 DummyFileSystem fileSystem;
130 DummyErrorCallback errorCallback;
131 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
132 &errorCallback);
79 AdblockPlus::FilterEngine filterEngine(jsEngine); 133 AdblockPlus::FilterEngine filterEngine(jsEngine);
80 ASSERT_EQ(filterEngine.GetListedSubscriptions().size(), 0u); 134 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
81 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" ); 135 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
82 ASSERT_EQ(filterEngine.GetListedSubscriptions().size(), 0u); 136 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
83 subscription->AddToList(); 137 subscription->AddToList();
84 ASSERT_EQ(filterEngine.GetListedSubscriptions().size(), 1u); 138 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size());
85 ASSERT_EQ(filterEngine.GetListedSubscriptions()[0], subscription); 139 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]);
86 subscription->AddToList(); 140 subscription->AddToList();
87 ASSERT_EQ(filterEngine.GetListedSubscriptions().size(), 1u); 141 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size());
88 ASSERT_EQ(filterEngine.GetListedSubscriptions()[0], subscription); 142 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]);
89 subscription->RemoveFromList(); 143 subscription->RemoveFromList();
90 ASSERT_EQ(filterEngine.GetListedSubscriptions().size(), 0u); 144 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
91 subscription->RemoveFromList(); 145 subscription->RemoveFromList();
92 ASSERT_EQ(filterEngine.GetListedSubscriptions().size(), 0u); 146 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
147 }
148
149 TEST(FilterEngineStubsTest, SubscriptionUpdates)
150 {
151 DummyFileSystem fileSystem;
152 DummyErrorCallback errorCallback;
153 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
154 &errorCallback);
155 AdblockPlus::FilterEngine filterEngine(jsEngine);
156 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
157 ASSERT_FALSE(subscription->IsUpdating());
158
159 //TODO: This currently crashes due to errors reported on a different thread
160 //subscription->UpdateFilters();
93 } 161 }
94 162
95 TEST(FilterEngineStubsTest, Matches) 163 TEST(FilterEngineStubsTest, Matches)
96 { 164 {
97 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, 0); 165 DummyFileSystem fileSystem;
166 DummyErrorCallback errorCallback;
167 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
168 &errorCallback);
98 AdblockPlus::FilterEngine filterEngine(jsEngine); 169 AdblockPlus::FilterEngine filterEngine(jsEngine);
99 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
100 subscription->AddToList();
101 170
102 AdblockPlus::FilterPtr match1 = filterEngine.Matches("http://example.org", "", ""); 171 filterEngine.GetFilter("adbanner.gif")->AddToList();
172 filterEngine.GetFilter("@@notbanner.gif")->AddToList();
173
174 AdblockPlus::FilterPtr match1 = filterEngine.Matches("http://example.org/fooba r.gif", "IMAGE", "");
103 ASSERT_FALSE(match1); 175 ASSERT_FALSE(match1);
104 176
105 AdblockPlus::FilterPtr match2 = filterEngine.Matches("http://example.org/adban ner.gif", "", ""); 177 AdblockPlus::FilterPtr match2 = filterEngine.Matches("http://example.org/adban ner.gif", "IMAGE", "");
106 ASSERT_TRUE(match2); 178 ASSERT_TRUE(match2);
107 ASSERT_EQ(match2->GetProperty("type", -1), AdblockPlus::Filter::TYPE_BLOCKING) ; 179 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match2->GetProperty("type", -1)) ;
108 180
109 AdblockPlus::FilterPtr match3 = filterEngine.Matches("http://example.org/notba nner.gif", "", ""); 181 AdblockPlus::FilterPtr match3 = filterEngine.Matches("http://example.org/notba nner.gif", "IMAGE", "");
110 ASSERT_TRUE(match3); 182 ASSERT_TRUE(match3);
111 ASSERT_EQ(match3->GetProperty("type", -1), AdblockPlus::Filter::TYPE_EXCEPTION ); 183 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match3->GetProperty("type", -1) );
112 184
113 AdblockPlus::FilterPtr match4 = filterEngine.Matches("http://example.org/notba nner.gif", "", ""); 185 AdblockPlus::FilterPtr match4 = filterEngine.Matches("http://example.org/notba nner.gif", "IMAGE", "");
114 ASSERT_TRUE(match4); 186 ASSERT_TRUE(match4);
115 ASSERT_EQ(match4->GetProperty("type", -1), AdblockPlus::Filter::TYPE_EXCEPTION ); 187 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match4->GetProperty("type", -1) );
116 } 188 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld