1 // Copyright 2016 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.net.smoke; 6 7 import androidx.test.core.app.ApplicationProvider; 8 9 import org.junit.runner.Description; 10 import org.junit.runners.model.Statement; 11 12 import org.chromium.base.ContextUtils; 13 import org.chromium.base.PathUtils; 14 15 import java.io.File; 16 17 /** 18 * Test base class for testing native Engine implementation. This class can import classes from the 19 * org.chromium.base package. 20 */ 21 public class NativeCronetTestRule extends CronetSmokeTestRule { 22 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "cronet_test"; 23 private static final String LOGFILE_NAME = "cronet-netlog.json"; 24 25 @Override apply(final Statement base, Description desc)26 public Statement apply(final Statement base, Description desc) { 27 return super.apply( 28 new Statement() { 29 @Override 30 public void evaluate() throws Throwable { 31 ruleSetUp(); 32 base.evaluate(); 33 ruleTearDown(); 34 } 35 }, 36 desc); 37 } 38 39 @Override 40 public void initCronetEngine() { 41 super.initCronetEngine(); 42 assertNativeEngine(mCronetEngine); 43 startNetLog(); 44 } 45 46 private void ruleSetUp() throws Exception { 47 ContextUtils.initApplicationContext(ApplicationProvider.getApplicationContext()); 48 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); 49 getTestSupport().loadTestNativeLibrary(); 50 } 51 52 private void ruleTearDown() throws Exception { 53 stopAndSaveNetLog(); 54 } 55 56 private void startNetLog() { 57 if (mCronetEngine != null) { 58 mCronetEngine.startNetLogToFile( 59 PathUtils.getDataDirectory() + "/" + LOGFILE_NAME, false); 60 } 61 } 62 63 private void stopAndSaveNetLog() { 64 if (mCronetEngine == null) return; 65 mCronetEngine.stopNetLog(); 66 File netLogFile = new File(PathUtils.getDataDirectory(), LOGFILE_NAME); 67 if (!netLogFile.exists()) return; 68 getTestSupport().processNetLog(ApplicationProvider.getApplicationContext(), netLogFile); 69 } 70 71 @Override 72 protected TestSupport initTestSupport() { 73 return new ChromiumNativeTestSupport(); 74 } 75 } 76