Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
(...skipping 30 matching lines...) Expand all Loading... | |
44 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LogSystem)); | 44 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LogSystem)); |
45 filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); | 45 filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); |
46 } | 46 } |
47 }; | 47 }; |
48 | 48 |
49 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest; | 49 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest; |
50 typedef FilterEngineTestGeneric<VeryLazyFileSystem, LazyLogSystem> FilterEngin eTestNoData; | 50 typedef FilterEngineTestGeneric<VeryLazyFileSystem, LazyLogSystem> FilterEngin eTestNoData; |
51 | 51 |
52 struct MockFilterChangeCallback | 52 struct MockFilterChangeCallback |
53 { | 53 { |
54 MockFilterChangeCallback(int& timesCalled) : timesCalled(timesCalled) {} | 54 MockFilterChangeCallback(int& timesCalled) : timesCalled(timesCalled) {} |
Felix Dahlke
2014/09/16 09:37:19
It's somewhat unintuitive that the functor doesn't
Wladimir Palant
2014/12/09 21:44:41
Yes, that's a footgun. Should we at least have a c
Felix Dahlke
2015/05/29 23:06:40
It just occurred to me that a shared_ptr should be
| |
55 | 55 |
56 void operator()(const std::string&, const AdblockPlus::JsValuePtr) | 56 void operator()(const std::string&, const AdblockPlus::JsValuePtr) |
57 { | 57 { |
58 timesCalled++; | 58 timesCalled++; |
59 } | 59 } |
60 | 60 |
61 private: | 61 private: |
62 int& timesCalled; | 62 int& timesCalled; |
63 }; | 63 }; |
64 | |
65 class UpdaterTest : public ::testing::Test | |
66 { | |
67 protected: | |
68 class MockWebRequest : public AdblockPlus::WebRequest | |
69 { | |
70 public: | |
71 AdblockPlus::ServerResponse response; | |
72 | |
73 AdblockPlus::ServerResponse GET(const std::string& url, | |
74 const AdblockPlus::HeaderList& requestHeaders) const | |
75 { | |
76 return response; | |
77 } | |
78 }; | |
79 | |
80 MockWebRequest* mockWebRequest; | |
81 FilterEnginePtr filterEngine; | |
82 | |
83 void SetUp() | |
84 { | |
85 AdblockPlus::AppInfo appInfo; | |
86 appInfo.name = "test"; | |
87 appInfo.version = "1.0.1"; | |
88 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(appInfo); | |
89 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); | |
90 mockWebRequest = new MockWebRequest; | |
91 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(mockWebRequest)); | |
92 filterEngine = FilterEnginePtr(new AdblockPlus::FilterEngine(jsEngine)); | |
93 } | |
94 }; | |
95 | |
96 struct MockUpdateAvailableCallback | |
97 { | |
98 MockUpdateAvailableCallback(int& timesCalled) : timesCalled(timesCalled) {} | |
99 | |
100 void operator()(const std::string&) | |
101 { | |
102 timesCalled++; | |
103 } | |
104 | |
105 private: | |
106 // We currently cannot store timesCalled in the functor, see: | |
107 // https://issues.adblockplus.org/ticket/1378. | |
108 int& timesCalled; | |
109 }; | |
110 | |
111 // Workaround for https://issues.adblockplus.org/ticket/1397. | |
112 void NoOpUpdaterCallback(const std::string&) {} | |
64 } | 113 } |
65 | 114 |
66 TEST_F(FilterEngineTest, FilterCreation) | 115 TEST_F(FilterEngineTest, FilterCreation) |
67 { | 116 { |
68 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); | 117 AdblockPlus::FilterPtr filter1 = filterEngine->GetFilter("foo"); |
69 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); | 118 ASSERT_EQ(AdblockPlus::Filter::TYPE_BLOCKING, filter1->GetType()); |
70 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); | 119 AdblockPlus::FilterPtr filter2 = filterEngine->GetFilter("@@foo"); |
71 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType()); | 120 ASSERT_EQ(AdblockPlus::Filter::TYPE_EXCEPTION, filter2->GetType()); |
72 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo"); | 121 AdblockPlus::FilterPtr filter3 = filterEngine->GetFilter("example.com##foo"); |
73 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetType()); | 122 ASSERT_EQ(AdblockPlus::Filter::TYPE_ELEMHIDE, filter3->GetType()); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 { | 373 { |
325 ASSERT_TRUE(filterEngine->IsFirstRun()); | 374 ASSERT_TRUE(filterEngine->IsFirstRun()); |
326 } | 375 } |
327 | 376 |
328 TEST_F(FilterEngineTest, SetRemoveFilterChangeCallback) | 377 TEST_F(FilterEngineTest, SetRemoveFilterChangeCallback) |
329 { | 378 { |
330 int timesCalled = 0; | 379 int timesCalled = 0; |
331 MockFilterChangeCallback mockFilterChangeCallback(timesCalled); | 380 MockFilterChangeCallback mockFilterChangeCallback(timesCalled); |
332 | 381 |
333 filterEngine->SetFilterChangeCallback(mockFilterChangeCallback); | 382 filterEngine->SetFilterChangeCallback(mockFilterChangeCallback); |
334 filterEngine->GetSubscription("foo")->AddToList(); | 383 filterEngine->GetFilter("foo")->AddToList(); |
Felix Dahlke
2014/09/16 09:37:19
I tried this with fitlerEngine->GetFilter("foo")->
Wladimir Palant
2014/12/09 21:44:41
Not really, no. If the filter was added it should
Felix Dahlke
2015/05/29 23:06:40
Debugged this a bit. filter.added is actually not
| |
384 EXPECT_EQ(2, timesCalled); | |
385 | |
386 filterEngine->RemoveFilterChangeCallback(); | |
387 filterEngine->GetFilter("foo")->RemoveFromList(); | |
388 EXPECT_EQ(2, timesCalled); | |
389 } | |
390 | |
391 TEST_F(UpdaterTest, SetRemoveUpdateAvailableCallback) | |
392 { | |
393 mockWebRequest->response.status = 0; | |
394 mockWebRequest->response.responseStatus = 200; | |
395 mockWebRequest->response.responseText = "\ | |
396 {\ | |
397 \"test\": {\ | |
398 \"version\": \"1.0.2\",\ | |
399 \"url\": \"https://downloads.adblockplus.org/test-1.0.2.tar.gz?update\"\ | |
400 }\ | |
401 }"; | |
402 | |
403 int timesCalled = 0; | |
404 MockUpdateAvailableCallback mockUpdateAvailableCallback(timesCalled); | |
405 | |
406 filterEngine->SetUpdateAvailableCallback(mockUpdateAvailableCallback); | |
407 filterEngine->ForceUpdateCheck(&NoOpUpdaterCallback); | |
408 AdblockPlus::Sleep(100); | |
335 ASSERT_EQ(1, timesCalled); | 409 ASSERT_EQ(1, timesCalled); |
336 | 410 |
337 filterEngine->RemoveFilterChangeCallback(); | 411 filterEngine->RemoveUpdateAvailableCallback(); |
338 filterEngine->GetSubscription("foo")->RemoveFromList(); | 412 filterEngine->ForceUpdateCheck(&NoOpUpdaterCallback); |
413 AdblockPlus::Sleep(100); | |
339 ASSERT_EQ(1, timesCalled); | 414 ASSERT_EQ(1, timesCalled); |
340 } | 415 } |
LEFT | RIGHT |