Index: chrome/android/javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableTest.java |
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionCallableTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableTest.java |
similarity index 28% |
rename from chrome/android/javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionCallableTest.java |
rename to chrome/android/javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableTest.java |
index 9de518819a9722d7405b4d9f6e58205adb8c3dc3..cd851953dff3bf5634e50b2c5a3716357c21a30d 100644 |
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionCallableTest.java |
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableTest.java |
@@ -9,6 +9,7 @@ import android.content.Context; |
import android.content.Intent; |
import android.support.test.filters.MediumTest; |
+import org.chromium.base.StreamUtil; |
import org.chromium.base.test.util.AdvancedMockContext; |
import org.chromium.components.minidump_uploader.CrashFileManager; |
import org.chromium.components.minidump_uploader.CrashTestCase; |
@@ -16,15 +17,16 @@ import org.chromium.components.minidump_uploader.CrashTestCase; |
import java.io.BufferedReader; |
import java.io.File; |
import java.io.FileReader; |
+import java.io.FileWriter; |
import java.io.IOException; |
import java.util.ArrayList; |
import java.util.List; |
import java.util.concurrent.atomic.AtomicInteger; |
/** |
- * Unittests for {@link LogcatExtractionCallable}. |
+ * Unittests for {@link LogcatExtractionRunnable}. |
*/ |
-public class LogcatExtractionCallableTest extends CrashTestCase { |
+public class LogcatExtractionRunnableTest extends CrashTestCase { |
private File mCrashDir; |
@Override |
@@ -34,65 +36,76 @@ public class LogcatExtractionCallableTest extends CrashTestCase { |
} |
@MediumTest |
- public void testExtractToFile() { |
- final AtomicInteger numServiceStarts = new AtomicInteger(0); |
- final String logContent = "some random log content"; |
- final Intent testIntent = new Intent(); |
- File dmp1 = new File(mCrashDir, "test1.dmp"); |
- File dmp2 = new File(mCrashDir, "test2.dmp"); |
- File dmp3 = new File(mCrashDir, "test3.dmp"); |
- final String[] dumps = new String[] {dmp1.getName(), dmp2.getName(), dmp3.getName()}; |
+ public void testSimpleExtraction() { |
+ // Set up the test. |
+ final List<String> logcat = new ArrayList<String>(); |
+ logcat.add("some random log content"); |
+ logcat.add("some more deterministic log content"); |
+ final File minidump = new File(mCrashDir, "test.dmp"); |
+ final String boundary = "boundary"; |
+ final String minidumpContents = "important minidump contents"; |
+ FileWriter writer = null; |
+ try { |
+ writer = new FileWriter(minidump); |
+ writer.write(boundary + "\n"); |
+ writer.write(minidumpContents + "\n"); |
+ } catch (IOException e) { |
+ fail(e.toString()); |
+ } finally { |
+ StreamUtil.closeQuietly(writer); |
+ } |
+ |
+ // The testContext is responsible for verifying that the correct intent is fired after the |
+ // logcat is extracted. |
+ final AtomicInteger numServiceStarts = new AtomicInteger(0); |
Context testContext = new AdvancedMockContext(getInstrumentation().getTargetContext()) { |
@Override |
- public ComponentName startService(Intent intentToCheck) { |
- assertTrue("We should have no more than 3 intents created", |
- numServiceStarts.incrementAndGet() <= dumps.length); |
- Intent redirectIntent = intentToCheck.getParcelableExtra("redirect_intent"); |
- |
- // Only the very last one will have a non-null redirect_intent. |
- if (numServiceStarts.get() == dumps.length) { |
- // And it should be == to the one we pass into the constructor. |
- assertSame(redirectIntent, testIntent); |
- } else { |
- assertNull(redirectIntent); |
- } |
- return new ComponentName( |
- getPackageName(), LogcatExtractionCallable.class.getName()); |
+ public ComponentName startService(Intent intent) { |
+ assertEquals(1, numServiceStarts.incrementAndGet()); |
+ assertEquals(MinidumpUploadService.class.getName(), |
+ intent.getComponent().getClassName()); |
+ assertEquals(MinidumpUploadService.ACTION_UPLOAD, intent.getAction()); |
+ assertEquals(new File(mCrashDir, "test.dmp.try0").getAbsolutePath(), |
+ intent.getStringExtra(MinidumpUploadService.FILE_TO_UPLOAD_KEY)); |
+ return super.startService(intent); |
} |
}; |
- LogcatExtractionCallable callable = |
- new LogcatExtractionCallable(testContext, dumps, testIntent) { |
- @Override |
- protected List<String> getLogcat() throws IOException, InterruptedException { |
- List<String> result = new ArrayList<String>(); |
- result.add(logContent); |
- return result; |
- } |
- }; |
- |
- callable.call(); |
- |
- assertFileContent("test1.logcat", logContent); |
- assertFileContent("test2.logcat", logContent); |
- assertFileContent("test3.logcat", logContent); |
- } |
+ // Extract the logcat! |
+ LogcatExtractionRunnable runnable = new LogcatExtractionRunnable(testContext, minidump) { |
+ @Override |
+ protected List<String> getLogcat() { |
+ return logcat; |
+ } |
+ }; |
+ runnable.run(); |
- private void assertFileContent(String fileName, String content) { |
+ // Verify the file contents. |
+ BufferedReader input = null; |
try { |
- File logfile = new File(mCrashDir, fileName); |
- assertTrue("Logfile does not exist!", logfile.exists()); |
- BufferedReader input = new BufferedReader(new FileReader(logfile)); |
- StringBuilder sb = new StringBuilder(); |
- String line = null; |
- while ((line = input.readLine()) != null) { |
- sb.append(line); |
+ File logfile = new File(mCrashDir, "test.dmp.try0"); |
+ assertTrue("Logfile should exist!", logfile.exists()); |
+ |
+ input = new BufferedReader(new FileReader(logfile)); |
+ assertEquals("The first line should be the boundary line.", boundary, input.readLine()); |
+ assertEquals("The second line should be the content dispoistion.", |
+ MinidumpLogcatPrepender.LOGCAT_CONTENT_DISPOSITION, input.readLine()); |
+ assertEquals("The third line should be the content type.", |
+ MinidumpLogcatPrepender.LOGCAT_CONTENT_TYPE, input.readLine()); |
+ assertEquals("The fourth line should be blank, for padding.", "", input.readLine()); |
+ for (String expected : logcat) { |
+ assertEquals(expected, input.readLine()); |
} |
- input.close(); |
- assertEquals("Content not matched!", content, sb.toString()); |
- } catch (Exception e) { |
+ assertEquals("The logcat should be followed by the boundary line.", boundary, |
+ input.readLine()); |
+ assertEquals( |
+ "The minidump contents should follow.", minidumpContents, input.readLine()); |
+ assertNull("There should be nothing else in the file", input.readLine()); |
+ } catch (IOException e) { |
fail(e.toString()); |
+ } finally { |
+ StreamUtil.closeQuietly(input); |
} |
} |
} |