• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(new Statement() {
28             @Override
29             public void evaluate() throws Throwable {
30                 ruleSetUp();
31                 base.evaluate();
32                 ruleTearDown();
33             }
34         }, desc);
35     }
36 
37     @Override
38     public void initCronetEngine() {
39         super.initCronetEngine();
40         assertNativeEngine(mCronetEngine);
41         startNetLog();
42     }
43 
44     private void ruleSetUp() throws Exception {
45         ContextUtils.initApplicationContext(ApplicationProvider.getApplicationContext());
46         PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
47         mTestSupport.loadTestNativeLibrary();
48     }
49 
50     private void ruleTearDown() throws Exception {
51         stopAndSaveNetLog();
52     }
53 
54     private void startNetLog() {
55         if (mCronetEngine != null) {
56             mCronetEngine.startNetLogToFile(
57                     PathUtils.getDataDirectory() + "/" + LOGFILE_NAME, false);
58         }
59     }
60 
61     private void stopAndSaveNetLog() {
62         if (mCronetEngine == null) return;
63         mCronetEngine.stopNetLog();
64         File netLogFile = new File(PathUtils.getDataDirectory(), LOGFILE_NAME);
65         if (!netLogFile.exists()) return;
66         mTestSupport.processNetLog(ApplicationProvider.getApplicationContext(), netLogFile);
67     }
68 }
69