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

Side by Side Diff: test/FilterEngine.cpp

Issue 10260028: Refactor tests, use fixtures and avoid duplication (Closed)
Patch Set: Created April 19, 2013, 4:11 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/FileSystemJsObject.cpp ('k') | test/GlobalJsObject.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include <iostream> 1 #include "BaseJsTest.h"
2 #include <AdblockPlus.h>
3 #include <gtest/gtest.h>
4 2
5 class DummyFileSystem : public AdblockPlus::FileSystem 3 namespace
6 { 4 {
7 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const 5 typedef std::tr1::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
6
7 class FilterEngineTest : public BaseJsTest
8 { 8 {
9 throw std::runtime_error("Not implemented"); 9 protected:
10 } 10 FilterEnginePtr filterEngine;
11 11
12 void Write(const std::string& path, 12 void SetUp()
13 std::tr1::shared_ptr<std::ostream> content) 13 {
14 { 14 BaseJsTest::SetUp();
15 throw std::runtime_error("Not implemented"); 15 // TODO: Don't use the default ErrorCallback/WebRequest
16 } 16 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new AdblockPlus:: DefaultErrorCallback));
17 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new AdblockPlus::Defaul tWebRequest));
18 filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine));
19 }
20 };
21 }
17 22
18 void Move(const std::string& fromPath, const std::string& toPath) 23 TEST_F(FilterEngineTest, FilterCreation)
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 TEST(FilterEngineTest, FilterCreation)
35 { 24 {
36 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 25 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo");
37 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
38 AdblockPlus::FilterEngine filterEngine(jsEngine);
39
40 AdblockPlus::FilterPtr filter1 = filterEngine.GetFilter("foo");
41 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetProperty("type", -1) ); 26 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetProperty("type", -1) );
42 AdblockPlus::FilterPtr filter2 = filterEngine.GetFilter("@@foo"); 27 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo");
43 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetProperty("type", -1 )); 28 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetProperty("type", -1 ));
44 AdblockPlus::FilterPtr filter3 = filterEngine.GetFilter("example.com##foo"); 29 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo");
45 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetProperty("type", -1) ); 30 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetProperty("type", -1) );
46 AdblockPlus::FilterPtr filter4 = filterEngine.GetFilter("example.com#@#foo"); 31 AdblockPlus::FilterPtr filter4 = filterEngine->GetFilter("example.com#@#foo");
47 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE_EXCEPTION, filter4->GetProperty(" type", -1)); 32 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE_EXCEPTION, filter4->GetProperty(" type", -1));
48 AdblockPlus::FilterPtr filter5 = filterEngine.GetFilter(" foo "); 33 AdblockPlus::FilterPtr filter5 = filterEngine->GetFilter(" foo ");
49 ASSERT_EQ(*filter1, *filter5); 34 ASSERT_EQ(*filter1, *filter5);
50 } 35 }
51 36
52 TEST(FilterEngineTest, FilterProperties) 37 TEST_F(FilterEngineTest, FilterProperties)
53 { 38 {
54 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 39 AdblockPlus::FilterPtr filter = filterEngine->GetFilter("foo");
55 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
56 AdblockPlus::FilterEngine filterEngine(jsEngine);
57 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo");
58 40
59 ASSERT_EQ("x", filter->GetProperty("stringFoo", "x")); 41 ASSERT_EQ("x", filter->GetProperty("stringFoo", "x"));
60 ASSERT_EQ(42, filter->GetProperty("intFoo", 42)); 42 ASSERT_EQ(42, filter->GetProperty("intFoo", 42));
61 ASSERT_FALSE(filter->GetProperty("boolFoo", false)); 43 ASSERT_FALSE(filter->GetProperty("boolFoo", false));
62 44
63 filter->SetProperty("stringFoo", "y"); 45 filter->SetProperty("stringFoo", "y");
64 filter->SetProperty("intFoo", 24); 46 filter->SetProperty("intFoo", 24);
65 filter->SetProperty("boolFoo", true); 47 filter->SetProperty("boolFoo", true);
66 ASSERT_EQ("y", filter->GetProperty("stringFoo", "x")); 48 ASSERT_EQ("y", filter->GetProperty("stringFoo", "x"));
67 ASSERT_EQ(24, filter->GetProperty("intFoo", 42)); 49 ASSERT_EQ(24, filter->GetProperty("intFoo", 42));
68 ASSERT_TRUE(filter->GetProperty("boolFoo", false)); 50 ASSERT_TRUE(filter->GetProperty("boolFoo", false));
69 } 51 }
70 52
71 TEST(FilterEngineTest, AddRemoveFilters) 53 TEST_F(FilterEngineTest, AddRemoveFilters)
72 { 54 {
73 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 55 ASSERT_EQ(0u, filterEngine->GetListedFilters().size());
74 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 56 AdblockPlus::FilterPtr filter = filterEngine->GetFilter("foo");
75 AdblockPlus::FilterEngine filterEngine(jsEngine); 57 ASSERT_EQ(0u, filterEngine->GetListedFilters().size());
76 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
77 AdblockPlus::FilterPtr filter = filterEngine.GetFilter("foo");
78 ASSERT_EQ(0u, filterEngine.GetListedFilters().size());
79 filter->AddToList(); 58 filter->AddToList();
80 ASSERT_EQ(1u, filterEngine.GetListedFilters().size()); 59 ASSERT_EQ(1u, filterEngine->GetListedFilters().size());
81 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]); 60 ASSERT_EQ(*filter, *filterEngine->GetListedFilters()[0]);
82 filter->AddToList(); 61 filter->AddToList();
83 ASSERT_EQ(1u, filterEngine.GetListedFilters().size()); 62 ASSERT_EQ(1u, filterEngine->GetListedFilters().size());
84 ASSERT_EQ(*filter, *filterEngine.GetListedFilters()[0]); 63 ASSERT_EQ(*filter, *filterEngine->GetListedFilters()[0]);
85 filter->RemoveFromList(); 64 filter->RemoveFromList();
86 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 65 ASSERT_EQ(0u, filterEngine->GetListedFilters().size());
87 filter->RemoveFromList(); 66 filter->RemoveFromList();
88 ASSERT_EQ(0u, filterEngine.GetListedFilters().size()); 67 ASSERT_EQ(0u, filterEngine->GetListedFilters().size());
89 } 68 }
90 69
91 TEST(FilterEngineTest, SubscriptionProperties) 70 TEST_F(FilterEngineTest, SubscriptionProperties)
92 { 71 {
93 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 72 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription("foo ");
94 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
95 AdblockPlus::FilterEngine filterEngine(jsEngine);
96 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
97 73
98 ASSERT_EQ("x", subscription->GetProperty("stringFoo", "x")); 74 ASSERT_EQ("x", subscription->GetProperty("stringFoo", "x"));
99 ASSERT_EQ(42, subscription->GetProperty("intFoo", 42)); 75 ASSERT_EQ(42, subscription->GetProperty("intFoo", 42));
100 ASSERT_FALSE(subscription->GetProperty("boolFoo", false)); 76 ASSERT_FALSE(subscription->GetProperty("boolFoo", false));
101 77
102 subscription->SetProperty("stringFoo", "y"); 78 subscription->SetProperty("stringFoo", "y");
103 subscription->SetProperty("intFoo", 24); 79 subscription->SetProperty("intFoo", 24);
104 subscription->SetProperty("boolFoo", true); 80 subscription->SetProperty("boolFoo", true);
105 ASSERT_EQ("y", subscription->GetProperty("stringFoo", "x")); 81 ASSERT_EQ("y", subscription->GetProperty("stringFoo", "x"));
106 ASSERT_EQ(24, subscription->GetProperty("intFoo", 42)); 82 ASSERT_EQ(24, subscription->GetProperty("intFoo", 42));
107 ASSERT_TRUE(subscription->GetProperty("boolFoo", false)); 83 ASSERT_TRUE(subscription->GetProperty("boolFoo", false));
108 } 84 }
109 85
110 TEST(FilterEngineTest, AddRemoveSubscriptions) 86 TEST_F(FilterEngineTest, AddRemoveSubscriptions)
111 { 87 {
112 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 88 ASSERT_EQ(0u, filterEngine->GetListedSubscriptions().size());
113 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 89 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription("foo ");
114 AdblockPlus::FilterEngine filterEngine(jsEngine); 90 ASSERT_EQ(0u, filterEngine->GetListedSubscriptions().size());
115 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
116 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
117 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size());
118 subscription->AddToList(); 91 subscription->AddToList();
119 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size()); 92 ASSERT_EQ(1u, filterEngine->GetListedSubscriptions().size());
120 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]); 93 ASSERT_EQ(*subscription, *filterEngine->GetListedSubscriptions()[0]);
121 subscription->AddToList(); 94 subscription->AddToList();
122 ASSERT_EQ(1u, filterEngine.GetListedSubscriptions().size()); 95 ASSERT_EQ(1u, filterEngine->GetListedSubscriptions().size());
123 ASSERT_EQ(*subscription, *filterEngine.GetListedSubscriptions()[0]); 96 ASSERT_EQ(*subscription, *filterEngine->GetListedSubscriptions()[0]);
124 subscription->RemoveFromList(); 97 subscription->RemoveFromList();
125 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 98 ASSERT_EQ(0u, filterEngine->GetListedSubscriptions().size());
126 subscription->RemoveFromList(); 99 subscription->RemoveFromList();
127 ASSERT_EQ(0u, filterEngine.GetListedSubscriptions().size()); 100 ASSERT_EQ(0u, filterEngine->GetListedSubscriptions().size());
128 } 101 }
129 102
130 TEST(FilterEngineTest, SubscriptionUpdates) 103 TEST_F(FilterEngineTest, SubscriptionUpdates)
131 { 104 {
132 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 105 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription("foo ");
133 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem()));
134 AdblockPlus::FilterEngine filterEngine(jsEngine);
135 AdblockPlus::SubscriptionPtr subscription = filterEngine.GetSubscription("foo" );
136 ASSERT_FALSE(subscription->IsUpdating()); 106 ASSERT_FALSE(subscription->IsUpdating());
137
138 subscription->UpdateFilters(); 107 subscription->UpdateFilters();
139 } 108 }
140 109
141 TEST(FilterEngineTest, Matches) 110 TEST_F(FilterEngineTest, Matches)
142 { 111 {
143 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 112 filterEngine->GetFilter("adbanner.gif")->AddToList();
144 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new DummyFileSystem())); 113 filterEngine->GetFilter("@@notbanner.gif")->AddToList();
145 AdblockPlus::FilterEngine filterEngine(jsEngine);
146 114
147 filterEngine.GetFilter("adbanner.gif")->AddToList(); 115 AdblockPlus::FilterPtr match1 = filterEngine->Matches("http://example.org/foob ar.gif", "IMAGE", "");
148 filterEngine.GetFilter("@@notbanner.gif")->AddToList();
149
150 AdblockPlus::FilterPtr match1 = filterEngine.Matches("http://example.org/fooba r.gif", "IMAGE", "");
151 ASSERT_FALSE(match1); 116 ASSERT_FALSE(match1);
152 117
153 AdblockPlus::FilterPtr match2 = filterEngine.Matches("http://example.org/adban ner.gif", "IMAGE", ""); 118 AdblockPlus::FilterPtr match2 = filterEngine->Matches("http://example.org/adba nner.gif", "IMAGE", "");
154 ASSERT_TRUE(match2); 119 ASSERT_TRUE(match2);
155 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match2->GetProperty("type", -1)) ; 120 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, match2->GetProperty("type", -1)) ;
156 121
157 AdblockPlus::FilterPtr match3 = filterEngine.Matches("http://example.org/notba nner.gif", "IMAGE", ""); 122 AdblockPlus::FilterPtr match3 = filterEngine->Matches("http://example.org/notb anner.gif", "IMAGE", "");
158 ASSERT_TRUE(match3); 123 ASSERT_TRUE(match3);
159 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match3->GetProperty("type", -1) ); 124 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match3->GetProperty("type", -1) );
160 125
161 AdblockPlus::FilterPtr match4 = filterEngine.Matches("http://example.org/notba nner.gif", "IMAGE", ""); 126 AdblockPlus::FilterPtr match4 = filterEngine->Matches("http://example.org/notb anner.gif", "IMAGE", "");
162 ASSERT_TRUE(match4); 127 ASSERT_TRUE(match4);
163 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match4->GetProperty("type", -1) ); 128 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, match4->GetProperty("type", -1) );
164 } 129 }
OLDNEW
« no previous file with comments | « test/FileSystemJsObject.cpp ('k') | test/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld