Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/NotificationTest.java

Issue 29344967: Issue 4031 - Implement tests for libadblockplus-android (Closed)
Left Patch Set: Updated tests, also moved to libadblockplus-android repo Created June 21, 2016, 10:51 a.m.
Right Patch Set: fix typo ".. file[s]" Created Sept. 15, 2016, 10:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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
(...skipping 13 matching lines...) Expand all
24 import org.adblockplus.libadblockplus.LazyWebRequest; 24 import org.adblockplus.libadblockplus.LazyWebRequest;
25 import org.adblockplus.libadblockplus.Notification; 25 import org.adblockplus.libadblockplus.Notification;
26 import org.adblockplus.libadblockplus.ServerResponse; 26 import org.adblockplus.libadblockplus.ServerResponse;
27 import org.adblockplus.libadblockplus.ShowNotificationCallback; 27 import org.adblockplus.libadblockplus.ShowNotificationCallback;
28 import org.adblockplus.libadblockplus.WebRequest; 28 import org.adblockplus.libadblockplus.WebRequest;
29 29
30 import org.junit.Test; 30 import org.junit.Test;
31 31
32 import java.util.List; 32 import java.util.List;
33 33
34 public class NotificationTest extends BaseJsTest { 34 public class NotificationTest extends BaseJsTest
35 {
35 36
36 protected FilterEngine filterEngine; 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 }
37 67
38 @Override 68 @Override
39 protected void setUp() throws Exception { 69 public void showNotificationCallback(Notification notification)
40 super.setUp(); 70 {
71 Log.d(TAG, this + " received [" + notification + "]");
72 retValue = notification;
73 }
74 }
41 75
42 jsEngine.setLogSystem(new LazyLogSystem()); 76 protected Notification peekNotification(String url) throws InterruptedExceptio n
43 jsEngine.setWebRequest(new LazyWebRequest()); 77 {
44 filterEngine = new FilterEngine(jsEngine); 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;
45 } 96 }
46 97
47 protected void addNotification(String notification) { 98 @Override
48 jsEngine.evaluate( 99 public ServerResponse httpGET(String url, List<HeaderEntry> headers)
49 "(function()\n" + 100 {
50 "{\n" + 101 if (url.indexOf("/notification.json") < 0)
51 "require('notification').Notification.addNotification(" + notificati on + ");\n" + 102 {
52 "})();"); 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;
53 } 111 }
112 }
54 113
55 private static final String TAG = "notification"; 114 @Test
115 public void testNoNotifications() throws InterruptedException
116 {
117 assertNull(peekNotification(""));
118 }
56 119
57 private class LocalShowNotificationCallback extends ShowNotificationCallback { 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 }
58 135
59 private Notification retValue; 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'] }");
60 142
61 public Notification getRetValue() { 143 Notification notification = peekNotification("");
62 return retValue; 144 assertNotNull(notification);
63 } 145 assertEquals(Notification.Type.CRITICAL, notification.getType());
64 146
65 @Override 147 notification = peekNotification("http://www.de");
66 public void showNotificationCallback(Notification notification) { 148 assertNotNull(notification);
67 Log.d(TAG, this + " received [" + notification + "]"); 149 assertEquals(Notification.Type.QUESTION, notification.getType());
68 retValue = notification;
69 }
70 }
71 150
72 protected Notification peekNotification(String url) throws InterruptedExcept ion { 151 notification = peekNotification("http://www.com");
73 Log.d(TAG, "Start peek"); 152 assertNotNull(notification);
153 assertEquals(Notification.Type.INFORMATION, notification.getType());
154 }
74 155
75 LocalShowNotificationCallback callback = new LocalShowNotificationCallba ck(); 156 @Test
76 Log.d(TAG, "set callback " + callback); 157 public void testMarkAsShown() throws InterruptedException
77 filterEngine.setShowNotificationCallback(callback); 158 {
78 filterEngine.showNextNotification(url); 159 addNotification("{ type: 'question' }");
79 filterEngine.removeShowNotificationCallback(); 160 assertNotNull(peekNotification(""));
80 Log.d(TAG, "removed callback");
81 return callback.getRetValue();
82 }
83 161
84 private class MockWebRequest extends WebRequest { 162 Notification notification = peekNotification("");
163 assertNotNull(notification);
85 164
86 public MockWebRequest(String notification) { 165 Thread.sleep(1000);
87 this.responseText = notification; 166 notification.markAsShown();
88 }
89 167
90 private String responseText; 168 assertNull(peekNotification(""));
91 169 }
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 } 170 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld