| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 * |
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 package org.adblockplus.libadblockplus.tests; | 18 package org.adblockplus.libadblockplus.tests; |
| 19 | 19 |
| 20 import android.util.Log; | |
| 21 import org.adblockplus.libadblockplus.FilterEngine; | 20 import org.adblockplus.libadblockplus.FilterEngine; |
| 22 import org.adblockplus.libadblockplus.HeaderEntry; | |
| 23 import org.adblockplus.libadblockplus.LazyLogSystem; | |
| 24 import org.adblockplus.libadblockplus.LazyWebRequest; | 21 import org.adblockplus.libadblockplus.LazyWebRequest; |
| 25 import org.adblockplus.libadblockplus.Notification; | 22 import org.adblockplus.libadblockplus.Notification; |
| 26 import org.adblockplus.libadblockplus.ServerResponse; | |
| 27 import org.adblockplus.libadblockplus.ShowNotificationCallback; | 23 import org.adblockplus.libadblockplus.ShowNotificationCallback; |
| 28 import org.adblockplus.libadblockplus.WebRequest; | |
| 29 | 24 |
| 30 import org.junit.Test; | 25 import org.junit.Test; |
| 31 | 26 |
| 32 import java.util.List; | |
| 33 | |
| 34 public class NotificationTest extends BaseJsTest { | 27 public class NotificationTest extends BaseJsTest { |
| 35 | 28 |
| 36 protected FilterEngine filterEngine; | 29 protected FilterEngine filterEngine; |
| 37 | 30 |
| 38 @Override | 31 @Override |
| 39 protected void setUp() throws Exception { | 32 protected void setUp() throws Exception { |
| 40 super.setUp(); | 33 super.setUp(); |
| 41 | 34 |
| 42 jsEngine.setWebRequest(new LazyWebRequest()); | 35 jsEngine.setWebRequest(new LazyWebRequest()); |
| 43 filterEngine = new FilterEngine(jsEngine); | 36 filterEngine = new FilterEngine(jsEngine); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 59 private Notification retValue; | 52 private Notification retValue; |
| 60 | 53 |
| 61 public Notification getRetValue() | 54 public Notification getRetValue() |
| 62 { | 55 { |
| 63 return retValue; | 56 return retValue; |
| 64 } | 57 } |
| 65 | 58 |
| 66 @Override | 59 @Override |
| 67 public void showNotificationCallback(Notification notification) | 60 public void showNotificationCallback(Notification notification) |
| 68 { | 61 { |
| 69 Log.d(TAG, this + " received [" + notification + "]"); | |
| 70 retValue = notification; | 62 retValue = notification; |
| 71 } | 63 } |
| 72 } | 64 } |
| 73 | 65 |
| 74 protected Notification peekNotification(String url) throws InterruptedExcept
ion | 66 protected Notification peekNotification(String url) throws InterruptedExcept
ion |
| 75 { | 67 { |
| 76 Log.d(TAG, "Start peek"); | |
| 77 | |
| 78 LocalShowNotificationCallback callback = new LocalShowNotificationCallba
ck(); | 68 LocalShowNotificationCallback callback = new LocalShowNotificationCallba
ck(); |
| 79 Log.d(TAG, "set callback " + callback); | |
| 80 filterEngine.setShowNotificationCallback(callback); | 69 filterEngine.setShowNotificationCallback(callback); |
| 81 filterEngine.showNextNotification(url); | 70 filterEngine.showNextNotification(url); |
| 82 filterEngine.removeShowNotificationCallback(); | 71 filterEngine.removeShowNotificationCallback(); |
| 83 Log.d(TAG, "removed callback"); | |
| 84 return callback.getRetValue(); | 72 return callback.getRetValue(); |
| 85 } | 73 } |
| 86 | 74 |
| 87 private class MockWebRequest extends WebRequest | |
| 88 { | |
| 89 private String responseText; | |
| 90 | |
| 91 public MockWebRequest(String responseText) | |
| 92 { | |
| 93 this.responseText = responseText; | |
| 94 } | |
| 95 | |
| 96 @Override | |
| 97 public ServerResponse httpGET(String url, List<HeaderEntry> headers) | |
| 98 { | |
| 99 if (url.indexOf("/notification.json") < 0) | |
| 100 return new ServerResponse(); | |
| 101 | |
| 102 ServerResponse response = new ServerResponse(); | |
| 103 response.setStatus(ServerResponse.NsStatus.OK); | |
| 104 response.setResponseStatus(200); | |
| 105 response.setResponse(responseText); | |
| 106 return response; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 @Test | 75 @Test |
| 111 public void testNoNotifications() throws InterruptedException | 76 public void testNoNotifications() throws InterruptedException |
| 112 { | 77 { |
| 113 assertNull(peekNotification("")); | 78 assertNull(peekNotification("")); |
| 114 } | 79 } |
| 115 | 80 |
| 116 @Test | 81 @Test |
| 117 public void testAddNotification() throws InterruptedException | 82 public void testAddNotification() throws InterruptedException |
| 118 { | 83 { |
| 119 addNotification( | 84 addNotification( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 145 assertEquals(Notification.Type.QUESTION, notification.getType()); | 110 assertEquals(Notification.Type.QUESTION, notification.getType()); |
| 146 | 111 |
| 147 notification = peekNotification("http://www.com"); | 112 notification = peekNotification("http://www.com"); |
| 148 assertNotNull(notification); | 113 assertNotNull(notification); |
| 149 assertEquals(Notification.Type.INFORMATION, notification.getType()); | 114 assertEquals(Notification.Type.INFORMATION, notification.getType()); |
| 150 } | 115 } |
| 151 | 116 |
| 152 @Test | 117 @Test |
| 153 public void testMarkAsShown() throws InterruptedException | 118 public void testMarkAsShown() throws InterruptedException |
| 154 { | 119 { |
| 155 addNotification("{ type: 'question' }"); | 120 addNotification("{ id: 'id', type: 'information' }"); |
| 156 assertNotNull(peekNotification("")); | 121 assertNotNull(peekNotification("")); |
| 157 | 122 |
| 158 Notification notification = peekNotification(""); | 123 Notification notification = peekNotification(""); |
| 159 assertNotNull(notification); | 124 assertNotNull(notification); |
| 160 | 125 |
| 161 Thread.sleep(1000); | 126 Thread.sleep(1000); |
| 162 notification.markAsShown(); | 127 notification.markAsShown(); |
| 163 | 128 |
| 164 assertNull(peekNotification("")); | 129 assertNull(peekNotification("")); |
| 165 } | 130 } |
| 166 } | 131 } |
| OLD | NEW |