LEFT | RIGHT |
(no file at all) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present 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; |
| 19 |
| 20 import android.content.Context; |
| 21 import android.os.SystemClock; |
| 22 import android.test.InstrumentationTestCase; |
| 23 |
| 24 import org.adblockplus.libadblockplus.AppInfo; |
| 25 import org.adblockplus.libadblockplus.FilterEngine; |
| 26 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; |
| 27 import org.adblockplus.libadblockplus.JsEngine; |
| 28 import org.adblockplus.libadblockplus.LazyLogSystem; |
| 29 import org.adblockplus.libadblockplus.LogSystem; |
| 30 import org.adblockplus.libadblockplus.Platform; |
| 31 import org.adblockplus.libadblockplus.ThrowingWebRequest; |
| 32 import org.adblockplus.libadblockplus.WebRequest; |
| 33 |
| 34 import java.io.File; |
| 35 |
| 36 public class BaseTest extends InstrumentationTestCase |
| 37 { |
| 38 protected Platform platform; |
| 39 protected JsEngine jsEngine; |
| 40 protected FilterEngine filterEngine; |
| 41 private final SetupInfo setupInfo; |
| 42 |
| 43 public BaseTest() |
| 44 { |
| 45 super(); |
| 46 setupInfo = new SetupInfo(); |
| 47 } |
| 48 |
| 49 @Override |
| 50 protected void setUp() throws Exception |
| 51 { |
| 52 deleteFiles(); |
| 53 super.setUp(); |
| 54 } |
| 55 |
| 56 private static class SetupInfo |
| 57 { |
| 58 public AppInfo appInfo; |
| 59 public LogSystem logSystem; |
| 60 public WebRequest webRequest; |
| 61 public String basePath; |
| 62 public IsAllowedConnectionCallback isAllowedConnectionCallback; |
| 63 |
| 64 public SetupInfo() |
| 65 { |
| 66 appInfo = AppInfo.builder().build(); |
| 67 logSystem = new LazyLogSystem(); |
| 68 webRequest = new ThrowingWebRequest(); |
| 69 } |
| 70 |
| 71 public void updateBasePath(Context context) |
| 72 { |
| 73 if (basePath == null) |
| 74 { |
| 75 basePath = context.getFilesDir().getAbsolutePath(); |
| 76 } |
| 77 } |
| 78 } |
| 79 |
| 80 protected void setAppInfo(AppInfo appInfo) |
| 81 { |
| 82 setupInfo.appInfo = appInfo; |
| 83 } |
| 84 |
| 85 protected void setLogSystem(LogSystem logSystem) |
| 86 { |
| 87 setupInfo.logSystem = logSystem; |
| 88 } |
| 89 |
| 90 protected void setWebRequest(WebRequest webRequest) |
| 91 { |
| 92 setupInfo.webRequest = webRequest; |
| 93 } |
| 94 |
| 95 protected void setBasePath(String basePath) |
| 96 { |
| 97 setupInfo.basePath = basePath; |
| 98 } |
| 99 |
| 100 protected void setIsAllowedConnectionCallback(IsAllowedConnectionCallback call
back) |
| 101 { |
| 102 setupInfo.isAllowedConnectionCallback = callback; |
| 103 } |
| 104 |
| 105 protected void deleteFiles() |
| 106 { |
| 107 for (File f : getContext().getFilesDir().listFiles()) |
| 108 { |
| 109 f.delete(); |
| 110 } |
| 111 } |
| 112 |
| 113 protected void setupPlatform() |
| 114 { |
| 115 if (platform == null) |
| 116 { |
| 117 setupInfo.updateBasePath(getContext()); |
| 118 platform = new Platform(setupInfo.logSystem, setupInfo.webRequest, setupIn
fo.basePath); |
| 119 } |
| 120 } |
| 121 |
| 122 protected void setupJsEngine() |
| 123 { |
| 124 setupPlatform(); |
| 125 if (jsEngine == null) |
| 126 { |
| 127 platform.setUpJsEngine(setupInfo.appInfo); |
| 128 jsEngine = platform.getJsEngine(); |
| 129 } |
| 130 } |
| 131 |
| 132 protected void setupFilterEngine() |
| 133 { |
| 134 setupJsEngine(); |
| 135 if (filterEngine == null) |
| 136 { |
| 137 if (setupInfo.isAllowedConnectionCallback != null) |
| 138 { |
| 139 platform.setUpFilterEngine(setupInfo.isAllowedConnectionCallback); |
| 140 } |
| 141 |
| 142 filterEngine = platform.getFilterEngine(); |
| 143 |
| 144 if (filterEngine.isFirstRun()) |
| 145 { |
| 146 // Wait until stuff got persisted ... |
| 147 File patterns = new File(getContext().getFilesDir(), "patterns.ini"); |
| 148 while (!patterns.exists()) |
| 149 { |
| 150 SystemClock.sleep(50); |
| 151 } |
| 152 } |
| 153 } |
| 154 } |
| 155 |
| 156 protected void disposeEngines() |
| 157 { |
| 158 filterEngine = null; |
| 159 jsEngine = null; |
| 160 if (platform != null) |
| 161 { |
| 162 platform.dispose(); |
| 163 platform = null; |
| 164 } |
| 165 } |
| 166 |
| 167 protected Context getContext() |
| 168 { |
| 169 return getInstrumentation().getTargetContext(); |
| 170 } |
| 171 |
| 172 @Override |
| 173 protected void tearDown() throws Exception |
| 174 { |
| 175 super.tearDown(); |
| 176 disposeEngines(); |
| 177 } |
| 178 } |
LEFT | RIGHT |