| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.crash; | 5 package org.chromium.chrome.browser.crash; |
| 6 | 6 |
| 7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
| 8 import android.app.job.JobInfo; | 8 import android.app.job.JobInfo; |
| 9 import android.app.job.JobScheduler; | 9 import android.app.job.JobScheduler; |
| 10 import android.content.ComponentName; | 10 import android.content.ComponentName; |
| 11 import android.content.Context; | 11 import android.content.Context; |
| 12 import android.content.Intent; | 12 import android.content.Intent; |
| 13 import android.os.Build; | 13 import android.os.Build; |
| 14 import android.support.test.filters.MediumTest; | 14 import android.support.test.filters.MediumTest; |
| 15 | 15 |
| 16 import org.chromium.base.StreamUtil; | 16 import org.chromium.base.StreamUtil; |
| 17 import org.chromium.base.test.util.AdvancedMockContext; | 17 import org.chromium.base.test.util.AdvancedMockContext; |
| 18 import org.chromium.chrome.browser.ChromeFeatureList; | |
| 19 import org.chromium.components.background_task_scheduler.TaskIds; | 18 import org.chromium.components.background_task_scheduler.TaskIds; |
| 20 import org.chromium.components.minidump_uploader.CrashFileManager; | 19 import org.chromium.components.minidump_uploader.CrashFileManager; |
| 21 import org.chromium.components.minidump_uploader.CrashTestCase; | 20 import org.chromium.components.minidump_uploader.CrashTestCase; |
| 22 | 21 |
| 23 import java.io.BufferedReader; | 22 import java.io.BufferedReader; |
| 24 import java.io.File; | 23 import java.io.File; |
| 25 import java.io.FileReader; | 24 import java.io.FileReader; |
| 26 import java.io.FileWriter; | 25 import java.io.FileWriter; |
| 27 import java.io.IOException; | 26 import java.io.IOException; |
| 28 import java.util.Arrays; | 27 import java.util.Arrays; |
| 29 import java.util.HashMap; | |
| 30 import java.util.List; | 28 import java.util.List; |
| 31 import java.util.Map; | |
| 32 | 29 |
| 33 /** | 30 /** |
| 34 * Unittests for {@link LogcatExtractionRunnable}. | 31 * Unittests for {@link LogcatExtractionRunnable}. |
| 35 */ | 32 */ |
| 36 public class LogcatExtractionRunnableTest extends CrashTestCase { | 33 public class LogcatExtractionRunnableTest extends CrashTestCase { |
| 37 private File mCrashDir; | 34 private File mCrashDir; |
| 38 | 35 |
| 39 private static final String BOUNDARY = "boundary"; | 36 private static final String BOUNDARY = "boundary"; |
| 40 private static final String MINIDUMP_CONTENTS = "important minidump contents
"; | 37 private static final String MINIDUMP_CONTENTS = "important minidump contents
"; |
| 41 private static final List<String> LOGCAT = | 38 private static final List<String> LOGCAT = |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // Responsible for verifying that the correct intent is fired after the logc
at is extracted. | 81 // Responsible for verifying that the correct intent is fired after the logc
at is extracted. |
| 85 private class TestContext extends AdvancedMockContext { | 82 private class TestContext extends AdvancedMockContext { |
| 86 int mNumServiceStarts; | 83 int mNumServiceStarts; |
| 87 | 84 |
| 88 TestContext(Context realContext) { | 85 TestContext(Context realContext) { |
| 89 super(realContext); | 86 super(realContext); |
| 90 } | 87 } |
| 91 | 88 |
| 92 @Override | 89 @Override |
| 93 public ComponentName startService(Intent intent) { | 90 public ComponentName startService(Intent intent) { |
| 94 assertFalse("Should only start a service directly when the job sched
uler is disabled.", | |
| 95 ChromeFeatureList.isEnabled( | |
| 96 ChromeFeatureList.UPLOAD_CRASH_REPORTS_USING_JOB_SCH
EDULER)); | |
| 97 ++mNumServiceStarts; | 91 ++mNumServiceStarts; |
| 98 assertEquals(1, mNumServiceStarts); | 92 assertEquals(1, mNumServiceStarts); |
| 99 assertEquals( | 93 assertEquals( |
| 100 MinidumpUploadService.class.getName(), intent.getComponent()
.getClassName()); | 94 MinidumpUploadService.class.getName(), intent.getComponent()
.getClassName()); |
| 101 assertEquals(MinidumpUploadService.ACTION_UPLOAD, intent.getAction()
); | 95 assertEquals(MinidumpUploadService.ACTION_UPLOAD, intent.getAction()
); |
| 102 assertEquals(new File(mCrashDir, "test.dmp.try0").getAbsolutePath(), | 96 assertEquals(new File(mCrashDir, "test.dmp.try0").getAbsolutePath(), |
| 103 intent.getStringExtra(MinidumpUploadService.FILE_TO_UPLOAD_K
EY)); | 97 intent.getStringExtra(MinidumpUploadService.FILE_TO_UPLOAD_K
EY)); |
| 104 return super.startService(intent); | 98 return super.startService(intent); |
| 105 } | 99 } |
| 106 | 100 |
| 107 @Override | 101 @Override |
| 108 public Object getSystemService(String name) { | 102 public Object getSystemService(String name) { |
| 109 if (Context.JOB_SCHEDULER_SERVICE.equals(name)) { | 103 if (Context.JOB_SCHEDULER_SERVICE.equals(name)) { |
| 110 assertTrue("Should only access the JobScheduler when it is enabl
ed.", | |
| 111 ChromeFeatureList.isEnabled( | |
| 112 ChromeFeatureList.UPLOAD_CRASH_REPORTS_USING_JOB
_SCHEDULER)); | |
| 113 return new TestJobScheduler(); | 104 return new TestJobScheduler(); |
| 114 } | 105 } |
| 115 | 106 |
| 116 return super.getSystemService(name); | 107 return super.getSystemService(name); |
| 117 } | 108 } |
| 118 }; | 109 }; |
| 119 | 110 |
| 120 @Override | 111 @Override |
| 121 protected void setUp() throws Exception { | 112 protected void setUp() throws Exception { |
| 122 super.setUp(); | 113 super.setUp(); |
| 123 mCrashDir = new CrashFileManager(mCacheDir).getCrashDirectory(); | 114 mCrashDir = new CrashFileManager(mCacheDir).getCrashDirectory(); |
| 124 } | 115 } |
| 125 | 116 |
| 126 @Override | |
| 127 protected void tearDown() throws Exception { | |
| 128 ChromeFeatureList.setTestFeatures(null); | |
| 129 super.tearDown(); | |
| 130 } | |
| 131 | |
| 132 /** | |
| 133 * Sets whether to upload minidumps using the JobScheduler API. Minidumps ca
n either be uploaded | |
| 134 * via a JobScheduler, or via a direct Intent service. | |
| 135 * @param enable Whether to enable the JobScheduler API. | |
| 136 */ | |
| 137 private void setJobSchedulerEnabled(boolean enable) { | |
| 138 Map<String, Boolean> features = new HashMap<>(); | |
| 139 features.put(ChromeFeatureList.UPLOAD_CRASH_REPORTS_USING_JOB_SCHEDULER,
enable); | |
| 140 ChromeFeatureList.setTestFeatures(features); | |
| 141 } | |
| 142 | |
| 143 /** | 117 /** |
| 144 * Creates a simple fake minidump file for testing. | 118 * Creates a simple fake minidump file for testing. |
| 145 * @param filename The name of the file to create. | 119 * @param filename The name of the file to create. |
| 146 */ | 120 */ |
| 147 private File createMinidump(String filename) throws IOException { | 121 private File createMinidump(String filename) throws IOException { |
| 148 File minidump = new File(mCrashDir, filename); | 122 File minidump = new File(mCrashDir, filename); |
| 149 FileWriter writer = null; | 123 FileWriter writer = null; |
| 150 try { | 124 try { |
| 151 writer = new FileWriter(minidump); | 125 writer = new FileWriter(minidump); |
| 152 writer.write(BOUNDARY + "\n"); | 126 writer.write(BOUNDARY + "\n"); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 183 assertEquals( | 157 assertEquals( |
| 184 "The minidump contents should follow.", MINIDUMP_CONTENTS, i
nput.readLine()); | 158 "The minidump contents should follow.", MINIDUMP_CONTENTS, i
nput.readLine()); |
| 185 assertNull("There should be nothing else in the file", input.readLin
e()); | 159 assertNull("There should be nothing else in the file", input.readLin
e()); |
| 186 } finally { | 160 } finally { |
| 187 StreamUtil.closeQuietly(input); | 161 StreamUtil.closeQuietly(input); |
| 188 } | 162 } |
| 189 } | 163 } |
| 190 | 164 |
| 191 @MediumTest | 165 @MediumTest |
| 192 public void testSimpleExtraction_SansJobScheduler() throws IOException { | 166 public void testSimpleExtraction_SansJobScheduler() throws IOException { |
| 193 setJobSchedulerEnabled(false); | 167 // The JobScheduler API is used as of Android M+. |
| 168 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) return; |
| 169 |
| 194 final File minidump = createMinidump("test.dmp"); | 170 final File minidump = createMinidump("test.dmp"); |
| 195 Context testContext = new TestContext(getInstrumentation().getTargetCont
ext()); | 171 Context testContext = new TestContext(getInstrumentation().getTargetCont
ext()); |
| 196 | 172 |
| 197 LogcatExtractionRunnable runnable = new TestLogcatExtractionRunnable(tes
tContext, minidump); | 173 LogcatExtractionRunnable runnable = new TestLogcatExtractionRunnable(tes
tContext, minidump); |
| 198 runnable.run(); | 174 runnable.run(); |
| 199 | 175 |
| 200 verifyMinidumpWithLogcat("test.dmp.try0"); | 176 verifyMinidumpWithLogcat("test.dmp.try0"); |
| 201 } | 177 } |
| 202 | 178 |
| 203 @MediumTest | 179 @MediumTest |
| 204 public void testSimpleExtraction_WithJobScheduler() throws IOException { | 180 public void testSimpleExtraction_WithJobScheduler() throws IOException { |
| 205 // The JobScheduler API is only available as of Android M. | 181 // The JobScheduler API is only available as of Android M. |
| 206 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return; | 182 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return; |
| 207 | 183 |
| 208 setJobSchedulerEnabled(true); | |
| 209 final File minidump = createMinidump("test.dmp"); | 184 final File minidump = createMinidump("test.dmp"); |
| 210 Context testContext = new TestContext(getInstrumentation().getTargetCont
ext()); | 185 Context testContext = new TestContext(getInstrumentation().getTargetCont
ext()); |
| 211 | 186 |
| 212 LogcatExtractionRunnable runnable = new TestLogcatExtractionRunnable(tes
tContext, minidump); | 187 LogcatExtractionRunnable runnable = new TestLogcatExtractionRunnable(tes
tContext, minidump); |
| 213 runnable.run(); | 188 runnable.run(); |
| 214 | 189 |
| 215 verifyMinidumpWithLogcat("test.dmp.try0"); | 190 verifyMinidumpWithLogcat("test.dmp.try0"); |
| 216 } | 191 } |
| 217 } | 192 } |
| OLD | NEW |