Chromium Code Reviews| Index: components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java |
| diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java |
| index cb47233dea4eebdaaa4e6d1fb7f6af3841346f68..6857e97016704a4ef1d13abda6dcc7da5fd3b422 100644 |
| --- a/components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java |
| +++ b/components/cronet/android/test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java |
| @@ -4,16 +4,18 @@ |
| package org.chromium.net; |
| -import android.support.test.filters.SmallTest; |
| +import android.support.test.filters.MediumTest; |
| import org.json.JSONObject; |
| +import org.chromium.base.Log; |
| import org.chromium.base.PathUtils; |
| import org.chromium.base.test.util.Feature; |
| -import java.io.BufferedReader; |
| import java.io.File; |
| -import java.io.FileReader; |
| +import java.io.FileInputStream; |
| +import java.io.FileNotFoundException; |
| +import java.io.IOException; |
| /** |
| * Tests for experimental options. |
| @@ -42,7 +44,7 @@ public class ExperimentalOptionsTest extends CronetTestBase { |
| super.tearDown(); |
| } |
| - @SmallTest |
| + @MediumTest |
| @Feature({"Cronet"}) |
| @OnlyRunNativeCronet |
| // Tests that NetLog writes effective experimental options to NetLog. |
| @@ -66,27 +68,19 @@ public class ExperimentalOptionsTest extends CronetTestBase { |
| assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| assertEquals("GET", callback.mResponseAsString); |
| mTestFramework.mCronetEngine.stopNetLog(); |
| - assertTrue(logfile.exists()); |
| - assertTrue(logfile.length() != 0); |
| - BufferedReader logReader = new BufferedReader(new FileReader(logfile)); |
| - boolean validFile = false; |
| - try { |
| - String logLine; |
| - while ((logLine = logReader.readLine()) != null) { |
| - if (logLine.contains("HostResolverRules")) { |
| - validFile = true; |
| - break; |
| - } |
| - } |
| - } finally { |
| - logReader.close(); |
| + boolean contains = false; |
| + for (int i = 0; i < 5; i++) { |
|
kapishnikov
2017/04/18 18:50:00
This loop is a good candidate to be refactored int
xunjieli
2017/04/19 19:02:52
Done. Good idea!
|
| + contains = fileContainsString(logfile, "HostResolverRules"); |
| + if (contains) break; |
| + Log.i(TAG, "Still waiting for netlog file....."); |
| + Thread.sleep(100); |
| } |
| - assertTrue(validFile); |
| assertTrue(logfile.delete()); |
| - assertTrue(!logfile.exists()); |
| + assertFalse(logfile.exists()); |
| + assertTrue("File content doesn't match", contains); |
|
kapishnikov
2017/04/18 18:50:00
Can we add the file name and the searched string t
xunjieli
2017/04/19 19:02:52
Done.
|
| } |
| - @SmallTest |
| + @MediumTest |
| @Feature({"Cronet"}) |
| @OnlyRunNativeCronet |
| public void testSetSSLKeyLogFile() throws Exception { |
| @@ -107,23 +101,35 @@ public class ExperimentalOptionsTest extends CronetTestBase { |
| assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| assertEquals("GET", callback.mResponseAsString); |
| - assertTrue(file.exists()); |
| - assertTrue(file.length() != 0); |
| - BufferedReader logReader = new BufferedReader(new FileReader(file)); |
| - boolean validFile = false; |
| + boolean contains = false; |
| + for (int i = 0; i < 5; i++) { |
| + contains = fileContainsString(file, "CLIENT_RANDOM"); |
| + if (contains) break; |
| + Log.i(TAG, "Still waiting for ssl key log file....."); |
| + Thread.sleep(100); |
| + } |
| + assertTrue(file.delete()); |
| + assertFalse(file.exists()); |
| + assertTrue("File content doesn't match", contains); |
| + } |
| + |
| + // Returns whether a file contains a particular string. |
| + private boolean fileContainsString(File file, String content) throws IOException { |
| + FileInputStream fileInputStream = null; |
| try { |
| - String logLine; |
| - while ((logLine = logReader.readLine()) != null) { |
| - if (logLine.contains("CLIENT_RANDOM")) { |
| - validFile = true; |
| - break; |
| - } |
| - } |
| + fileInputStream = new FileInputStream(file); |
| + byte[] data = new byte[(int) file.length()]; |
| + fileInputStream.read(data); |
| + return new String(data, "UTF-8").contains(content); |
| + } catch (FileNotFoundException e) { |
| + // Ignored this exception since the file will only be created when updates are |
| + // flushed to the disk. |
| + Log.i(TAG, "file not found"); |
| } finally { |
| - logReader.close(); |
| + if (fileInputStream != null) { |
| + fileInputStream.close(); |
| + } |
| } |
| - assertTrue(validFile); |
| - assertTrue(file.delete()); |
| - assertTrue(!file.exists()); |
| + return false; |
| } |
| } |