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

Unified Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/NotificationTest.java

Issue 29344967: Issue 4031 - Implement tests for libadblockplus-android (Closed)
Patch Set: Created May 27, 2016, 1:38 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/NotificationTest.java
===================================================================
new file mode 100644
--- /dev/null
+++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/NotificationTest.java
@@ -0,0 +1,146 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.adblockplus.libadblockplus.tests;
+
+import org.adblockplus.libadblockplus.FilterEngine;
+import org.adblockplus.libadblockplus.HeaderEntry;
+import org.adblockplus.libadblockplus.LazyLogSystem;
+import org.adblockplus.libadblockplus.LazyWebRequest;
+import org.adblockplus.libadblockplus.Notification;
+import org.adblockplus.libadblockplus.ServerResponse;
+import org.adblockplus.libadblockplus.ShowNotificationCallback;
+import org.adblockplus.libadblockplus.WebRequest;
+
+import org.junit.Test;
+
+import java.util.List;
+
+public class NotificationTest extends BaseJsTest {
+
+ protected FilterEngine filterEngine;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ jsEngine.setLogSystem(new LazyLogSystem());
+ jsEngine.setWebRequest(new LazyWebRequest());
+ filterEngine = new FilterEngine(jsEngine);
+ }
+
+ protected void addNotification(String notification) {
+ jsEngine.evaluate(
+ "(function()\n" +
+ "{\n" +
+ "require('notification').Notification.addNotification(" + notification + ");\n" +
+ "})();");
+ }
+
+ private class LocalShowNotificationCallback extends ShowNotificationCallback {
+
+ private Notification retValue;
+
+ public Notification getRetValue() {
+ return retValue;
+ }
+
+ @Override
+ public void showNotificationCallback(Notification jsValue) {
+ retValue = jsValue;
+ }
+ }
+
+ protected Notification peekNotification(String url) {
+ LocalShowNotificationCallback callback = new LocalShowNotificationCallback();
+ filterEngine.setShowNotificationCallback(callback);
+ filterEngine.showNextNotification(url);
+ filterEngine.removeShowNotificationCallback();
+ return callback.getRetValue();
+ }
+
+ private class MockWebRequest extends WebRequest {
+
+ public MockWebRequest(String notification) {
+ this.responseText = notification;
+ }
+
+ private String responseText;
+
+ @Override
+ public ServerResponse httpGET(String url, List<HeaderEntry> headers) {
+ if (url.indexOf("/notification.json") < 0)
+ return new ServerResponse();
+
+ ServerResponse response = new ServerResponse();
+ response.setStatus(ServerResponse.NsStatus.OK);
+ response.setResponseStatus(200);
+ response.setResponse(responseText);
+ return response;
+ }
+ }
+
+ @Test
+ public void testNoNotifications() {
+ assertNull(peekNotification(""));
+ }
+
+ @Test
+ public void testAddNotification() {
+ addNotification(
+ "{\n" +
+ " type: 'critical',\n" +
+ " title: 'testTitle',\n" +
+ " message: 'testMessage',\n" +
+ "}");
+ Notification notification = peekNotification("");
+ assertNotNull(notification);
+ assertEquals(Notification.Type.CRITICAL, notification.getType());
+ assertEquals("testTitle", notification.getTitle());
+ assertEquals("testMessage", notification.getMessageString());
+ }
+
+ @Test
+ public void testFilterByUrl() {
+ addNotification("{ id:'no-filter', type:'critical' }");
+ addNotification("{ id:'www.com', type:'information', urlFilters:['||www.com$document'] }");
+ addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$document'] }");
+
+ Notification notification = peekNotification("");
+ assertNotNull(notification);
+ assertEquals(Notification.Type.CRITICAL, notification.getType());
+
+ notification = peekNotification("http://www.de");
+ assertNotNull(notification);
+ assertEquals(Notification.Type.QUESTION, notification.getType());
+
+ notification = peekNotification("http://www.com");
+ assertNotNull(notification);
+ assertEquals(Notification.Type.INFORMATION, notification.getType());
+ }
+
+ @Test
+ public void testMarkAsShown() throws InterruptedException {
+ addNotification("{ id: 'id', type: 'question' }");
+ assertNotNull(peekNotification(""));
+
+ Notification notification = peekNotification("");
+ assertNotNull(notification);
+ notification.markAsShown();
+ assertNull(peekNotification(""));
+ }
+}

Powered by Google App Engine
This is Rietveld