OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.libadblockplus.tests; |
| 19 |
| 20 import android.util.Log; |
| 21 import org.adblockplus.libadblockplus.FilterEngine; |
| 22 import org.adblockplus.libadblockplus.HeaderEntry; |
| 23 import org.adblockplus.libadblockplus.LazyLogSystem; |
| 24 import org.adblockplus.libadblockplus.LazyWebRequest; |
| 25 import org.adblockplus.libadblockplus.Notification; |
| 26 import org.adblockplus.libadblockplus.ServerResponse; |
| 27 import org.adblockplus.libadblockplus.ShowNotificationCallback; |
| 28 import org.adblockplus.libadblockplus.WebRequest; |
| 29 |
| 30 import org.junit.Test; |
| 31 |
| 32 import java.util.List; |
| 33 |
| 34 public class NotificationTest extends BaseJsTest { |
| 35 |
| 36 protected FilterEngine filterEngine; |
| 37 |
| 38 @Override |
| 39 protected void setUp() throws Exception { |
| 40 super.setUp(); |
| 41 |
| 42 jsEngine.setLogSystem(new LazyLogSystem()); |
| 43 jsEngine.setWebRequest(new LazyWebRequest()); |
| 44 filterEngine = new FilterEngine(jsEngine); |
| 45 } |
| 46 |
| 47 protected void addNotification(String notification) { |
| 48 jsEngine.evaluate( |
| 49 "(function()\n" + |
| 50 "{\n" + |
| 51 "require('notification').Notification.addNotification(" + notificati
on + ");\n" + |
| 52 "})();"); |
| 53 } |
| 54 |
| 55 private static final String TAG = "notification"; |
| 56 |
| 57 private class LocalShowNotificationCallback extends ShowNotificationCallback
{ |
| 58 |
| 59 private Notification retValue; |
| 60 |
| 61 public Notification getRetValue() { |
| 62 return retValue; |
| 63 } |
| 64 |
| 65 @Override |
| 66 public void showNotificationCallback(Notification notification) { |
| 67 Log.d(TAG, this + " received [" + notification + "]"); |
| 68 retValue = notification; |
| 69 } |
| 70 } |
| 71 |
| 72 protected Notification peekNotification(String url) throws InterruptedExcept
ion { |
| 73 Log.d(TAG, "Start peek"); |
| 74 |
| 75 LocalShowNotificationCallback callback = new LocalShowNotificationCallba
ck(); |
| 76 Log.d(TAG, "set callback " + callback); |
| 77 filterEngine.setShowNotificationCallback(callback); |
| 78 filterEngine.showNextNotification(url); |
| 79 filterEngine.removeShowNotificationCallback(); |
| 80 Log.d(TAG, "removed callback"); |
| 81 return callback.getRetValue(); |
| 82 } |
| 83 |
| 84 private class MockWebRequest extends WebRequest { |
| 85 |
| 86 public MockWebRequest(String notification) { |
| 87 this.responseText = notification; |
| 88 } |
| 89 |
| 90 private String responseText; |
| 91 |
| 92 @Override |
| 93 public ServerResponse httpGET(String url, List<HeaderEntry> headers) { |
| 94 if (url.indexOf("/notification.json") < 0) |
| 95 return new ServerResponse(); |
| 96 |
| 97 ServerResponse response = new ServerResponse(); |
| 98 response.setStatus(ServerResponse.NsStatus.OK); |
| 99 response.setResponseStatus(200); |
| 100 response.setResponse(responseText); |
| 101 return response; |
| 102 } |
| 103 } |
| 104 |
| 105 @Test |
| 106 public void testNoNotifications() throws InterruptedException { |
| 107 assertNull(peekNotification("")); |
| 108 } |
| 109 |
| 110 @Test |
| 111 public void testAddNotification() throws InterruptedException { |
| 112 addNotification( |
| 113 "{\n" + |
| 114 " type: 'critical',\n" + |
| 115 " title: 'testTitle',\n" + |
| 116 " message: 'testMessage',\n" + |
| 117 "}"); |
| 118 Notification notification = peekNotification(""); |
| 119 assertNotNull(notification); |
| 120 assertEquals(Notification.Type.CRITICAL, notification.getType()); |
| 121 assertEquals("testTitle", notification.getTitle()); |
| 122 assertEquals("testMessage", notification.getMessageString()); |
| 123 } |
| 124 |
| 125 @Test |
| 126 public void testFilterByUrl() throws InterruptedException { |
| 127 addNotification("{ id:'no-filter', type:'critical' }"); |
| 128 addNotification("{ id:'www.com', type:'information', urlFilters:['||www.
com$document'] }"); |
| 129 addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$d
ocument'] }"); |
| 130 |
| 131 Notification notification = peekNotification(""); |
| 132 assertNotNull(notification); |
| 133 assertEquals(Notification.Type.CRITICAL, notification.getType()); |
| 134 |
| 135 notification = peekNotification("http://www.de"); |
| 136 assertNotNull(notification); |
| 137 assertEquals(Notification.Type.QUESTION, notification.getType()); |
| 138 |
| 139 notification = peekNotification("http://www.com"); |
| 140 assertNotNull(notification); |
| 141 assertEquals(Notification.Type.INFORMATION, notification.getType()); |
| 142 } |
| 143 |
| 144 @Test |
| 145 public void testMarkAsShown() throws InterruptedException { |
| 146 addNotification("{ type: 'question' }"); |
| 147 assertNotNull(peekNotification("")); |
| 148 |
| 149 Notification notification = peekNotification(""); |
| 150 assertNotNull(notification); |
| 151 |
| 152 Thread.sleep(1000); |
| 153 notification.markAsShown(); |
| 154 |
| 155 assertNull(peekNotification("")); |
| 156 } |
| 157 } |
OLD | NEW |