| 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-present eyeo GmbH | 3  * Copyright (C) 2006-present 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 org.adblockplus.libadblockplus.LazyWebRequest; | 20 import android.os.SystemClock; | 
|  | 21 | 
|  | 22 import org.adblockplus.libadblockplus.BaseFilterEngineTest; | 
| 21 import org.adblockplus.libadblockplus.Notification; | 23 import org.adblockplus.libadblockplus.Notification; | 
| 22 import org.adblockplus.libadblockplus.ShowNotificationCallback; | 24 import org.adblockplus.libadblockplus.ShowNotificationCallback; | 
| 23 import org.adblockplus.libadblockplus.WebRequest; |  | 
| 24 | 25 | 
| 25 import org.junit.Test; | 26 import org.junit.Test; | 
| 26 | 27 | 
| 27 public class NotificationTest extends BaseFilterEngineTest | 28 public class NotificationTest extends BaseFilterEngineTest | 
| 28 { | 29 { | 
| 29   @Override | 30   private static final int NOTIFICATION_WAIT_DELAY_MS = 1000; | 
| 30   protected WebRequest createWebRequest() |  | 
| 31   { |  | 
| 32     return new LazyWebRequest(); |  | 
| 33   } |  | 
| 34 | 31 | 
| 35   protected void addNotification(String notification) | 32   protected void addNotification(String notification) | 
| 36   { | 33   { | 
| 37     platform.getJsEngine().evaluate( | 34     jsEngine.evaluate( | 
| 38       "(function()\n" + | 35       "(function()\n" + | 
| 39       "{\n" + | 36       "{\n" + | 
| 40       "require('notification').Notification.addNotification(" + notification + "
     );\n" + | 37       "require('notification').Notification.addNotification(" + notification + "
     );\n" + | 
| 41       "})();"); | 38       "})();"); | 
| 42   } | 39   } | 
| 43 | 40 | 
| 44   private class LocalShowNotificationCallback extends ShowNotificationCallback | 41   private class LocalShowNotificationCallback extends ShowNotificationCallback | 
| 45   { | 42   { | 
| 46     private Notification retValue; | 43     private Notification retValue; | 
| 47 | 44 | 
| 48     public Notification getRetValue() | 45     public Notification getRetValue() | 
| 49     { | 46     { | 
| 50       return retValue; | 47       return retValue; | 
| 51     } | 48     } | 
| 52 | 49 | 
| 53     @Override | 50     @Override | 
| 54     public void showNotificationCallback(Notification notification) | 51     public void showNotificationCallback(Notification notification) | 
| 55     { | 52     { | 
| 56       retValue = notification; | 53       retValue = notification; | 
| 57     } | 54     } | 
| 58   } | 55   } | 
| 59 | 56 | 
| 60   protected Notification peekNotification(String url) throws InterruptedExceptio
     n | 57   protected Notification peekNotification(String url) | 
| 61   { | 58   { | 
| 62     LocalShowNotificationCallback callback = new LocalShowNotificationCallback()
     ; | 59     LocalShowNotificationCallback callback = new LocalShowNotificationCallback()
     ; | 
| 63     filterEngine.setShowNotificationCallback(callback); | 60     filterEngine.setShowNotificationCallback(callback); | 
| 64     filterEngine.showNextNotification(url); | 61     filterEngine.showNextNotification(url); | 
| 65     filterEngine.removeShowNotificationCallback(); | 62     filterEngine.removeShowNotificationCallback(); | 
| 66     return callback.getRetValue(); | 63     return callback.getRetValue(); | 
| 67   } | 64   } | 
| 68 | 65 | 
| 69   @Test | 66   @Test | 
| 70   public void testNoNotifications() throws InterruptedException | 67   public void testNoNotifications() throws InterruptedException | 
| 71   { | 68   { | 
| 72     assertNull(peekNotification("")); | 69     assertNull(peekNotification("")); | 
| 73   } | 70   } | 
| 74 | 71 | 
| 75   @Test | 72   @Test | 
| 76   public void testAddNotification() throws InterruptedException | 73   public void testAddNotification() | 
| 77   { | 74   { | 
| 78     addNotification( | 75     addNotification( | 
| 79       "{\n" + | 76       "{\n" + | 
| 80       "   type: 'critical',\n" + | 77       "   type: 'critical',\n" + | 
| 81       "   title: 'testTitle',\n" + | 78       "   title: 'testTitle',\n" + | 
| 82       "   message: 'testMessage',\n" + | 79       "   message: 'testMessage',\n" + | 
| 83       "}"); | 80       "}"); | 
| 84     Notification notification = peekNotification(""); | 81     Notification notification = peekNotification(""); | 
| 85     assertNotNull(notification); | 82     assertNotNull(notification); | 
| 86     assertEquals(Notification.Type.CRITICAL, notification.getType()); | 83     assertEquals(Notification.Type.CRITICAL, notification.getType()); | 
| 87     assertEquals("testTitle", notification.getTitle()); | 84     assertEquals("testTitle", notification.getTitle()); | 
| 88     assertEquals("testMessage", notification.getMessageString()); | 85     assertEquals("testMessage", notification.getMessageString()); | 
| 89   } | 86   } | 
| 90 | 87 | 
| 91   @Test | 88   @Test | 
| 92   public void testFilterByUrl() throws InterruptedException | 89   public void testFilterByUrl() | 
| 93   { | 90   { | 
| 94     addNotification("{ id:'no-filter', type:'critical' }"); | 91     addNotification("{ id:'no-filter', type:'critical' }"); | 
| 95     addNotification("{ id:'www.com', type:'information', urlFilters:['||www.com$
     document'] }"); | 92     addNotification("{ id:'www.com', type:'information', urlFilters:['||www.com$
     document'] }"); | 
| 96     addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$docum
     ent'] }"); | 93     addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$docum
     ent'] }"); | 
| 97 | 94 | 
| 98     Notification notification = peekNotification(""); | 95     Notification notification = peekNotification(""); | 
| 99     assertNotNull(notification); | 96     assertNotNull(notification); | 
| 100     assertEquals(Notification.Type.CRITICAL, notification.getType()); | 97     assertEquals(Notification.Type.CRITICAL, notification.getType()); | 
| 101 | 98 | 
| 102     notification = peekNotification("http://www.de"); | 99     notification = peekNotification("http://www.de"); | 
| 103     assertNotNull(notification); | 100     assertNotNull(notification); | 
| 104     assertEquals(Notification.Type.QUESTION, notification.getType()); | 101     assertEquals(Notification.Type.QUESTION, notification.getType()); | 
| 105 | 102 | 
| 106     notification = peekNotification("http://www.com"); | 103     notification = peekNotification("http://www.com"); | 
| 107     assertNotNull(notification); | 104     assertNotNull(notification); | 
| 108     assertEquals(Notification.Type.INFORMATION, notification.getType()); | 105     assertEquals(Notification.Type.INFORMATION, notification.getType()); | 
| 109   } | 106   } | 
| 110 | 107 | 
| 111   @Test | 108   @Test | 
| 112   public void testMarkAsShown() throws InterruptedException | 109   public void testMarkAsShown() | 
| 113   { | 110   { | 
| 114     addNotification("{ id: 'id', type: 'information' }"); | 111     addNotification("{ id: 'id', type: 'information' }"); | 
| 115     assertNotNull(peekNotification("")); | 112     assertNotNull(peekNotification("")); | 
| 116 | 113 | 
| 117     Notification notification = peekNotification(""); | 114     Notification notification = peekNotification(""); | 
| 118     assertNotNull(notification); | 115     assertNotNull(notification); | 
| 119 | 116 | 
| 120     Thread.sleep(1000); | 117     SystemClock.sleep(NOTIFICATION_WAIT_DELAY_MS); | 
| 121     notification.markAsShown(); | 118     notification.markAsShown(); | 
| 122 | 119 | 
| 123     assertNull(peekNotification("")); | 120     assertNull(peekNotification("")); | 
| 124   } | 121   } | 
| 125 } | 122 } | 
| OLD | NEW | 
|---|