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 org.adblockplus.libadblockplus.FilterEngine; |
| 21 import org.adblockplus.libadblockplus.HeaderEntry; |
| 22 import org.adblockplus.libadblockplus.LazyLogSystem; |
| 23 import org.adblockplus.libadblockplus.LazyWebRequest; |
| 24 import org.adblockplus.libadblockplus.Notification; |
| 25 import org.adblockplus.libadblockplus.ServerResponse; |
| 26 import org.adblockplus.libadblockplus.ShowNotificationCallback; |
| 27 import org.adblockplus.libadblockplus.WebRequest; |
| 28 |
| 29 import org.junit.Test; |
| 30 |
| 31 import java.util.List; |
| 32 |
| 33 public class NotificationTest extends BaseJsTest { |
| 34 |
| 35 protected FilterEngine filterEngine; |
| 36 |
| 37 @Override |
| 38 protected void setUp() throws Exception { |
| 39 super.setUp(); |
| 40 |
| 41 jsEngine.setLogSystem(new LazyLogSystem()); |
| 42 jsEngine.setWebRequest(new LazyWebRequest()); |
| 43 filterEngine = new FilterEngine(jsEngine); |
| 44 } |
| 45 |
| 46 protected void addNotification(String notification) { |
| 47 jsEngine.evaluate( |
| 48 "(function()\n" + |
| 49 "{\n" + |
| 50 "require('notification').Notification.addNotification(" + notificati
on + ");\n" + |
| 51 "})();"); |
| 52 } |
| 53 |
| 54 private class LocalShowNotificationCallback extends ShowNotificationCallback
{ |
| 55 |
| 56 private Notification retValue; |
| 57 |
| 58 public Notification getRetValue() { |
| 59 return retValue; |
| 60 } |
| 61 |
| 62 @Override |
| 63 public void showNotificationCallback(Notification jsValue) { |
| 64 retValue = jsValue; |
| 65 } |
| 66 } |
| 67 |
| 68 protected Notification peekNotification(String url) { |
| 69 LocalShowNotificationCallback callback = new LocalShowNotificationCallba
ck(); |
| 70 filterEngine.setShowNotificationCallback(callback); |
| 71 filterEngine.showNextNotification(url); |
| 72 filterEngine.removeShowNotificationCallback(); |
| 73 return callback.getRetValue(); |
| 74 } |
| 75 |
| 76 private class MockWebRequest extends WebRequest { |
| 77 |
| 78 public MockWebRequest(String notification) { |
| 79 this.responseText = notification; |
| 80 } |
| 81 |
| 82 private String responseText; |
| 83 |
| 84 @Override |
| 85 public ServerResponse httpGET(String url, List<HeaderEntry> headers) { |
| 86 if (url.indexOf("/notification.json") < 0) |
| 87 return new ServerResponse(); |
| 88 |
| 89 ServerResponse response = new ServerResponse(); |
| 90 response.setStatus(ServerResponse.NsStatus.OK); |
| 91 response.setResponseStatus(200); |
| 92 response.setResponse(responseText); |
| 93 return response; |
| 94 } |
| 95 } |
| 96 |
| 97 @Test |
| 98 public void testNoNotifications() { |
| 99 assertNull(peekNotification("")); |
| 100 } |
| 101 |
| 102 @Test |
| 103 public void testAddNotification() { |
| 104 addNotification( |
| 105 "{\n" + |
| 106 " type: 'critical',\n" + |
| 107 " title: 'testTitle',\n" + |
| 108 " message: 'testMessage',\n" + |
| 109 "}"); |
| 110 Notification notification = peekNotification(""); |
| 111 assertNotNull(notification); |
| 112 assertEquals(Notification.Type.CRITICAL, notification.getType()); |
| 113 assertEquals("testTitle", notification.getTitle()); |
| 114 assertEquals("testMessage", notification.getMessageString()); |
| 115 } |
| 116 |
| 117 @Test |
| 118 public void testFilterByUrl() { |
| 119 addNotification("{ id:'no-filter', type:'critical' }"); |
| 120 addNotification("{ id:'www.com', type:'information', urlFilters:['||www.
com$document'] }"); |
| 121 addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$d
ocument'] }"); |
| 122 |
| 123 Notification notification = peekNotification(""); |
| 124 assertNotNull(notification); |
| 125 assertEquals(Notification.Type.CRITICAL, notification.getType()); |
| 126 |
| 127 notification = peekNotification("http://www.de"); |
| 128 assertNotNull(notification); |
| 129 assertEquals(Notification.Type.QUESTION, notification.getType()); |
| 130 |
| 131 notification = peekNotification("http://www.com"); |
| 132 assertNotNull(notification); |
| 133 assertEquals(Notification.Type.INFORMATION, notification.getType()); |
| 134 } |
| 135 |
| 136 @Test |
| 137 public void testMarkAsShown() throws InterruptedException { |
| 138 addNotification("{ id: 'id', type: 'question' }"); |
| 139 assertNotNull(peekNotification("")); |
| 140 |
| 141 Notification notification = peekNotification(""); |
| 142 assertNotNull(notification); |
| 143 notification.markAsShown(); |
| 144 assertNull(peekNotification("")); |
| 145 } |
| 146 } |
OLD | NEW |