Skip to content

Commit fd2a265

Browse files
committed
Add support for AdBlock
Overall design: AdBlockManager (QObject) - One per browser with a property saying if adblock is enabled. - Owns all of the subscriptions AdBlockSubscription (QObject) - Constructed from an abp: url - Owns all of the rules for this subscription - Downloads rules when needed. AdBlockRule - Has a bool networkMatch(const QString &) function. - Various properties such as if enabled, is a css rule etc. AdBlockNetwork (applies network rules) - QNetworkAccessManager uses this to see if a connection should be blocked. AdBlockPage (applies css rules) - WebPage uses this to hide elements. AdBlockModel/AdBlockDialog - Sits on top of AdBlockManager and presents a dialog to the user so they can add rules, subscriptions etc. AdBlockScheme - Handle the abp url scheme to subscribe to a new adblock subscriptions. Misc: - Added right click action to block
1 parent 6ed449b commit fd2a265

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3902
-0
lines changed

autotests/adblock/adblock.pro

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
TEMPLATE = subdirs
2+
SUBDIRS = \
3+
adblockmanager \
4+
adblocknetwork \
5+
adblockpage \
6+
adblockrule \
7+
adblocksubscription
8+
9+
CONFIG += ordered
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
adblockmanager
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
TEMPLATE = app
2+
TARGET =
3+
DEPENDPATH += .
4+
INCLUDEPATH += .
5+
6+
include(../../autotests.pri)
7+
8+
# Input
9+
SOURCES += tst_adblockmanager.cpp
10+
HEADERS +=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/**
2+
* Copyright (c) 2009, Benjamin C. Meyer <[email protected]>
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
13+
* may be used to endorse or promote products derived from this software
14+
* without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26+
* SUCH DAMAGE.
27+
*/
28+
29+
#include <qtest.h>
30+
#include <qsignalspy.h>
31+
#include <qtry.h>
32+
33+
#include "adblockdialog.h"
34+
#include "adblockmanager.h"
35+
#include "adblocksubscription.h"
36+
37+
#include <qdebug.h>
38+
39+
class tst_AdBlockManager : public QObject
40+
{
41+
Q_OBJECT
42+
43+
public slots:
44+
void initTestCase();
45+
void cleanupTestCase();
46+
void init();
47+
void cleanup();
48+
49+
private slots:
50+
void adblockmanager_data();
51+
void adblockmanager();
52+
53+
void addSubscription();
54+
void customRules();
55+
void isEnabled_data();
56+
void isEnabled();
57+
void load();
58+
void removeSubscription();
59+
void showDialog();
60+
void rulesChanged();
61+
};
62+
63+
// Subclass that exposes the protected functions.
64+
class SubAdBlockManager : public AdBlockManager
65+
{
66+
public:
67+
~SubAdBlockManager() {
68+
QList<AdBlockSubscription*> list = subscriptions();
69+
foreach (AdBlockSubscription *s, list)
70+
removeSubscription(s);
71+
setEnabled(false);
72+
}
73+
74+
void call_rulesChanged()
75+
{ return SubAdBlockManager::rulesChanged(); }
76+
};
77+
78+
// This will be called before the first test function is executed.
79+
// It is only called once.
80+
void tst_AdBlockManager::initTestCase()
81+
{
82+
}
83+
84+
// This will be called after the last test function is executed.
85+
// It is only called once.
86+
void tst_AdBlockManager::cleanupTestCase()
87+
{
88+
}
89+
90+
// This will be called before each test function is executed.
91+
void tst_AdBlockManager::init()
92+
{
93+
}
94+
95+
// This will be called after every test function.
96+
void tst_AdBlockManager::cleanup()
97+
{
98+
}
99+
100+
void tst_AdBlockManager::adblockmanager_data()
101+
{
102+
}
103+
104+
void tst_AdBlockManager::adblockmanager()
105+
{
106+
SubAdBlockManager manager;
107+
manager.addSubscription((AdBlockSubscription*)0);
108+
QVERIFY(manager.customRules());
109+
QVERIFY(manager.instance() != (AdBlockManager*)0);
110+
QCOMPARE(manager.isEnabled(), false);
111+
manager.load();
112+
QVERIFY(manager.network());
113+
QVERIFY(manager.page());
114+
manager.removeSubscription((AdBlockSubscription*)0);
115+
manager.setEnabled(false);
116+
QVERIFY(manager.showDialog());
117+
QList<AdBlockSubscription*> list;
118+
list.append(manager.customRules());
119+
QCOMPARE(manager.subscriptions(), list);
120+
}
121+
122+
// public void addSubscription(AdBlockSubscription *subscription)
123+
void tst_AdBlockManager::addSubscription()
124+
{
125+
SubAdBlockManager manager;
126+
127+
QList<AdBlockSubscription*> list = manager.subscriptions();
128+
129+
QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));
130+
131+
AdBlockSubscription *subscription = new AdBlockSubscription(QUrl(), &manager);
132+
manager.addSubscription(subscription);
133+
QCOMPARE(manager.subscriptions(), (list += subscription));
134+
135+
QCOMPARE(spy0.count(), 1);
136+
}
137+
138+
// public AdBlockSubscription *customRules()
139+
void tst_AdBlockManager::customRules()
140+
{
141+
SubAdBlockManager manager;
142+
QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));
143+
144+
AdBlockSubscription *subscription = manager.customRules();
145+
QVERIFY(subscription);
146+
QVERIFY(!subscription->title().isEmpty());
147+
QVERIFY(subscription->allRules().isEmpty());
148+
149+
QCOMPARE(spy0.count(), 1);
150+
151+
subscription = manager.customRules();
152+
QCOMPARE(spy0.count(), 1);
153+
}
154+
155+
void tst_AdBlockManager::isEnabled_data()
156+
{
157+
QTest::addColumn<bool>("isEnabled");
158+
QTest::newRow("true") << true;
159+
QTest::newRow("false") << false;
160+
}
161+
162+
// public bool isEnabled() const
163+
void tst_AdBlockManager::isEnabled()
164+
{
165+
QFETCH(bool, isEnabled);
166+
167+
SubAdBlockManager manager;
168+
169+
QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));
170+
171+
bool before = manager.isEnabled();
172+
173+
manager.setEnabled(isEnabled);
174+
manager.setEnabled(isEnabled);
175+
QCOMPARE(manager.isEnabled(), isEnabled);
176+
177+
QCOMPARE(spy0.count(), before == isEnabled ? 0 : 1);
178+
}
179+
180+
// public void load()
181+
void tst_AdBlockManager::load()
182+
{
183+
SubAdBlockManager manager;
184+
185+
QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));
186+
187+
manager.load();
188+
189+
QCOMPARE(spy0.count(), 0);
190+
}
191+
192+
// public void removeSubscription(AdBlockSubscription *subscription)
193+
void tst_AdBlockManager::removeSubscription()
194+
{
195+
SubAdBlockManager manager;
196+
197+
QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));
198+
199+
QList<AdBlockSubscription*> list = manager.subscriptions();
200+
AdBlockSubscription *subscription = new AdBlockSubscription(QUrl(), &manager);
201+
manager.addSubscription(subscription);
202+
manager.removeSubscription(subscription);
203+
QCOMPARE(manager.subscriptions(), list);
204+
205+
QCOMPARE(spy0.count(), 2);
206+
}
207+
208+
209+
// public AdBlockDialog *showDialog()
210+
void tst_AdBlockManager::showDialog()
211+
{
212+
SubAdBlockManager manager;
213+
214+
QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));
215+
216+
AdBlockDialog *dialog = manager.showDialog();
217+
QVERIFY(dialog);
218+
QTRY_VERIFY(dialog->isVisible());
219+
}
220+
221+
void tst_AdBlockManager::rulesChanged()
222+
{
223+
SubAdBlockManager manager;
224+
225+
QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));
226+
227+
228+
AdBlockSubscription *subscription = new AdBlockSubscription(QUrl(), &manager);
229+
manager.addSubscription(subscription);
230+
subscription->setEnabled(true);
231+
subscription->addRule(AdBlockRule());
232+
233+
QCOMPARE(spy0.count(), 3);
234+
}
235+
236+
QTEST_MAIN(tst_AdBlockManager)
237+
#include "tst_adblockmanager.moc"
238+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
adblocknetwork
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
TEMPLATE = app
2+
TARGET =
3+
DEPENDPATH += .
4+
INCLUDEPATH += .
5+
6+
include(../../autotests.pri)
7+
8+
# Input
9+
SOURCES += tst_adblocknetwork.cpp
10+
HEADERS +=

0 commit comments

Comments
 (0)