| 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 | 
|  | 37   protected FilterEngine filterEngine; | 
|  | 38 | 
|  | 39   @Override | 
|  | 40   protected void setUp() throws Exception | 
|  | 41   { | 
|  | 42     super.setUp(); | 
|  | 43 | 
|  | 44     jsEngine.setWebRequest(new LazyWebRequest()); | 
|  | 45     filterEngine = new FilterEngine(jsEngine); | 
|  | 46   } | 
|  | 47 | 
|  | 48   protected void addNotification(String notification) | 
|  | 49   { | 
|  | 50     jsEngine.evaluate( | 
|  | 51       "(function()\n" + | 
|  | 52       "{\n" + | 
|  | 53       "require('notification').Notification.addNotification(" + notification + "
     );\n" + | 
|  | 54       "})();"); | 
|  | 55   } | 
|  | 56 | 
|  | 57   private static final String TAG = "notification"; | 
|  | 58 | 
|  | 59   private class LocalShowNotificationCallback extends ShowNotificationCallback | 
|  | 60   { | 
|  | 61     private Notification retValue; | 
|  | 62 | 
|  | 63     public Notification getRetValue() | 
|  | 64     { | 
|  | 65       return retValue; | 
|  | 66     } | 
|  | 67 | 
|  | 68     @Override | 
|  | 69     public void showNotificationCallback(Notification notification) | 
|  | 70     { | 
|  | 71       Log.d(TAG, this + " received [" + notification + "]"); | 
|  | 72       retValue = notification; | 
|  | 73     } | 
|  | 74   } | 
|  | 75 | 
|  | 76   protected Notification peekNotification(String url) throws InterruptedExceptio
     n | 
|  | 77   { | 
|  | 78     Log.d(TAG, "Start peek"); | 
|  | 79 | 
|  | 80     LocalShowNotificationCallback callback = new LocalShowNotificationCallback()
     ; | 
|  | 81     Log.d(TAG, "set callback " + callback); | 
|  | 82     filterEngine.setShowNotificationCallback(callback); | 
|  | 83     filterEngine.showNextNotification(url); | 
|  | 84     filterEngine.removeShowNotificationCallback(); | 
|  | 85     Log.d(TAG, "removed callback"); | 
|  | 86     return callback.getRetValue(); | 
|  | 87   } | 
|  | 88 | 
|  | 89   private class MockWebRequest extends WebRequest | 
|  | 90   { | 
|  | 91     private String responseText; | 
|  | 92 | 
|  | 93     public MockWebRequest(String responseText) | 
|  | 94     { | 
|  | 95       this.responseText = responseText; | 
|  | 96     } | 
|  | 97 | 
|  | 98     @Override | 
|  | 99     public ServerResponse httpGET(String url, List<HeaderEntry> headers) | 
|  | 100     { | 
|  | 101       if (url.indexOf("/notification.json") < 0) | 
|  | 102       { | 
|  | 103         return new ServerResponse(); | 
|  | 104       } | 
|  | 105 | 
|  | 106       ServerResponse response = new ServerResponse(); | 
|  | 107       response.setStatus(ServerResponse.NsStatus.OK); | 
|  | 108       response.setResponseStatus(200); | 
|  | 109       response.setResponse(responseText); | 
|  | 110       return response; | 
|  | 111     } | 
|  | 112   } | 
|  | 113 | 
|  | 114   @Test | 
|  | 115   public void testNoNotifications() throws InterruptedException | 
|  | 116   { | 
|  | 117     assertNull(peekNotification("")); | 
|  | 118   } | 
|  | 119 | 
|  | 120   @Test | 
|  | 121   public void testAddNotification() throws InterruptedException | 
|  | 122   { | 
|  | 123     addNotification( | 
|  | 124       "{\n" + | 
|  | 125       "   type: 'critical',\n" + | 
|  | 126       "   title: 'testTitle',\n" + | 
|  | 127       "   message: 'testMessage',\n" + | 
|  | 128       "}"); | 
|  | 129     Notification notification = peekNotification(""); | 
|  | 130     assertNotNull(notification); | 
|  | 131     assertEquals(Notification.Type.CRITICAL, notification.getType()); | 
|  | 132     assertEquals("testTitle", notification.getTitle()); | 
|  | 133     assertEquals("testMessage", notification.getMessageString()); | 
|  | 134   } | 
|  | 135 | 
|  | 136   @Test | 
|  | 137   public void testFilterByUrl() throws InterruptedException | 
|  | 138   { | 
|  | 139     addNotification("{ id:'no-filter', type:'critical' }"); | 
|  | 140     addNotification("{ id:'www.com', type:'information', urlFilters:['||www.com$
     document'] }"); | 
|  | 141     addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$docum
     ent'] }"); | 
|  | 142 | 
|  | 143     Notification notification = peekNotification(""); | 
|  | 144     assertNotNull(notification); | 
|  | 145     assertEquals(Notification.Type.CRITICAL, notification.getType()); | 
|  | 146 | 
|  | 147     notification = peekNotification("http://www.de"); | 
|  | 148     assertNotNull(notification); | 
|  | 149     assertEquals(Notification.Type.QUESTION, notification.getType()); | 
|  | 150 | 
|  | 151     notification = peekNotification("http://www.com"); | 
|  | 152     assertNotNull(notification); | 
|  | 153     assertEquals(Notification.Type.INFORMATION, notification.getType()); | 
|  | 154   } | 
|  | 155 | 
|  | 156   @Test | 
|  | 157   public void testMarkAsShown() throws InterruptedException | 
|  | 158   { | 
|  | 159     addNotification("{ type: 'question' }"); | 
|  | 160     assertNotNull(peekNotification("")); | 
|  | 161 | 
|  | 162     Notification notification = peekNotification(""); | 
|  | 163     assertNotNull(notification); | 
|  | 164 | 
|  | 165     Thread.sleep(1000); | 
|  | 166     notification.markAsShown(); | 
|  | 167 | 
|  | 168     assertNull(peekNotification("")); | 
|  | 169   } | 
|  | 170 } | 
| OLD | NEW | 
|---|