use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class DeviceInfoActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_info);
mNetworkStateHelper = NetworkStateHelper.getSharedInstance(this);
mNetworkStateHelper.addListener(this);
onNetworkStateUpdated(mNetworkStateHelper.isNetworkConnected());
Device log;
try {
log = DeviceInfoHelper.getDeviceInfo(getApplicationContext());
} catch (DeviceInfoHelper.DeviceInfoException e) {
Toast.makeText(getBaseContext(), R.string.error_device_info, Toast.LENGTH_LONG).show();
return;
}
final List<DeviceInfoDisplayModel> list = getDeviceInfoDisplayModelList(log);
@SuppressLint("HardwareIds") final String deviceId = Settings.Secure.getString(getApplication().getContentResolver(), Settings.Secure.ANDROID_ID);
list.add(new DeviceInfoDisplayModel() {
{
title = "Device ID";
value = deviceId;
}
});
ArrayAdapter<DeviceInfoDisplayModel> adapter = new ArrayAdapter<DeviceInfoDisplayModel>(this, android.R.layout.simple_list_item_2, android.R.id.text1, list) {
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = view.findViewById(android.R.id.text1);
TextView text2 = view.findViewById(android.R.id.text2);
text1.setText(list.get(position).title);
text2.setText(list.get(position).value);
return view;
}
};
((ListView) findViewById(R.id.device_info_list_view)).setAdapter(adapter);
}
use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method minidumpFilePathNull.
@Test
public void minidumpFilePathNull() throws Exception {
/* Set up mock for the crash. */
final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
DefaultLogSerializer defaultLogSerializer = mock(DefaultLogSerializer.class);
mock(ErrorAttachmentLog.class);
mockStatic(ErrorLogHelper.class);
mockStatic(ErrorAttachmentLog.class);
ErrorReport errorReport = new ErrorReport();
Device device = new Device();
device.setWrapperSdkName(WRAPPER_SDK_NAME_NDK);
errorReport.setDevice(device);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(errorReport);
whenNew(DefaultLogSerializer.class).withAnyArguments().thenReturn(defaultLogSerializer);
whenNew(com.microsoft.appcenter.crashes.ingestion.models.Exception.class).withAnyArguments().thenReturn(exception);
when(exception.getMinidumpFilePath()).thenReturn(null);
when(exception.getType()).thenReturn(MINIDUMP_FILE);
when(exception.getMessage()).thenReturn("Error message");
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class), mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(FileManager.read(any(File.class))).thenReturn("");
String jsonCrash = "{}";
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<ManagedErrorLog>() {
@Override
public ManagedErrorLog answer(InvocationOnMock invocation) {
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
when(log.getException()).thenReturn(exception);
return log;
}
});
when(logSerializer.serializeLog(any(Log.class))).thenReturn(jsonCrash);
when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
ErrorAttachmentLog errorAttachmentLog = mock(ErrorAttachmentLog.class);
whenNew(ErrorAttachmentLog.class).withAnyArguments().thenReturn(errorAttachmentLog);
/* Start crashes. */
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), mock(Channel.class), "secret-app-mock", null, true);
/*
* Verify that attachmentWithBinary doesn't get called if minidump is missing.
* This scenario used to crash before, so if the test succeeds that also tests the crash is fixed.
*/
verifyStatic(never());
attachmentWithBinary(new byte[] { anyByte() }, anyString(), anyString());
}
use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method minidumpStoredWithOldSDK.
@Test
public void minidumpStoredWithOldSDK() throws Exception {
/* Set up mock for the crash. */
final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
DefaultLogSerializer defaultLogSerializer = mock(DefaultLogSerializer.class);
mock(ErrorAttachmentLog.class);
mockStatic(ErrorLogHelper.class);
mockStatic(ErrorAttachmentLog.class);
ErrorReport errorReport = new ErrorReport();
Device device = new Device();
device.setWrapperSdkName(WRAPPER_SDK_NAME_NDK);
errorReport.setDevice(device);
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(errorReport);
whenNew(DefaultLogSerializer.class).withAnyArguments().thenReturn(defaultLogSerializer);
whenNew(com.microsoft.appcenter.crashes.ingestion.models.Exception.class).withAnyArguments().thenReturn(exception);
when(exception.getStackTrace()).thenReturn("some minidump");
when(exception.getType()).thenReturn(MINIDUMP_FILE);
when(exception.getMessage()).thenReturn("message");
/* This mocks we already processed minidump to convert to pending regular crash report as that would be the case if migrating data from older SDK. */
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
when(FileManager.read(any(File.class))).thenReturn("");
String jsonCrash = "{}";
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<ManagedErrorLog>() {
@Override
public ManagedErrorLog answer(InvocationOnMock invocation) {
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
when(log.getException()).thenReturn(exception);
return log;
}
});
when(logSerializer.serializeLog(any(Log.class))).thenReturn(jsonCrash);
when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
ErrorAttachmentLog errorAttachmentLog = mock(ErrorAttachmentLog.class);
whenNew(ErrorAttachmentLog.class).withAnyArguments().thenReturn(errorAttachmentLog);
/* Start crashes. */
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), mock(Channel.class), "secret-app-mock", null, true);
/* Verify that attachmentWithBinary does get sent. */
verifyStatic();
attachmentWithBinary(new byte[] { anyByte() }, anyString(), anyString());
/* Verify temporary field erased. */
verify(exception, times(1)).setStackTrace(null);
}
use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class ErrorLogHelperTest method throwJSONExceptionWhenGetMinidumpSubfolderWithDeviceInfo.
@Test
public void throwJSONExceptionWhenGetMinidumpSubfolderWithDeviceInfo() throws java.lang.Exception {
/* Prepare data. */
Device mockDevice = mock(Device.class);
mockStatic(DeviceInfoHelper.class);
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
Context mockContext = mock(Context.class);
File mockFile = mock(File.class);
whenNew(File.class).withAnyArguments().thenReturn(mockFile);
doThrow(new JSONException("crash", new java.lang.Exception())).when(mockDevice).write(any(JSONStringer.class));
/* Verify. */
ErrorLogHelper.getNewMinidumpSubfolderWithContextData(mockContext);
verify(mockFile).delete();
}
use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class ErrorLogHelperTest method createErrorLog.
@Test
public void createErrorLog() throws java.lang.Exception {
/* Dummy coverage of utils class. */
new ErrorLogHelper();
/* Mock base. */
Context mockContext = mock(Context.class);
when(Process.myPid()).thenReturn(123);
Date logTimestamp = new Date(1000L);
whenNew(Date.class).withNoArguments().thenReturn(logTimestamp);
whenNew(Date.class).withArguments(anyLong()).thenAnswer(new Answer<Date>() {
@Override
public Date answer(InvocationOnMock invocation) {
return new Date((Long) invocation.getArguments()[0]);
}
});
/* Mock device. */
Device mockDevice = mock(Device.class);
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
/* Mock process name. */
ActivityManager activityManager = mock(ActivityManager.class);
RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
runningAppProcessInfo.pid = 123;
runningAppProcessInfo.processName = "right.process";
when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
/* Mock architecture. */
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", 23);
TestUtils.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
/* Test. */
long launchTimeStamp = 2000;
ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new IOException(new TestCrashException())), java.lang.Thread.getAllStackTraces(), launchTimeStamp);
assertNotNull(errorLog);
assertNotNull(errorLog.getId());
assertEquals(logTimestamp, errorLog.getTimestamp());
assertEquals(mockDevice, errorLog.getDevice());
assertEquals(Integer.valueOf(123), errorLog.getProcessId());
assertEquals("right.process", errorLog.getProcessName());
assertNull(errorLog.getParentProcessId());
assertNull(errorLog.getParentProcessName());
assertEquals("armeabi-v7a", errorLog.getArchitecture());
assertEquals((Long) java.lang.Thread.currentThread().getId(), errorLog.getErrorThreadId());
assertEquals(java.lang.Thread.currentThread().getName(), errorLog.getErrorThreadName());
assertEquals(Boolean.TRUE, errorLog.getFatal());
assertEquals(launchTimeStamp, errorLog.getAppLaunchTimestamp().getTime());
/* Check first exception. */
Exception topException = errorLog.getException();
sanityCheck(topException);
assertEquals(RuntimeException.class.getName(), topException.getType());
assertNotNull(topException.getMessage());
assertNotNull(topException.getInnerExceptions());
assertEquals(1, topException.getInnerExceptions().size());
/* Check second exception. */
Exception middleException = topException.getInnerExceptions().get(0);
sanityCheck(middleException);
assertEquals(IOException.class.getName(), middleException.getType());
assertNotNull(middleException.getInnerExceptions());
assertEquals(1, middleException.getInnerExceptions().size());
/* Check third exception. */
Exception rootCauseException = middleException.getInnerExceptions().get(0);
sanityCheck(rootCauseException);
assertEquals(TestCrashException.class.getName(), rootCauseException.getType());
assertNotNull(rootCauseException.getMessage());
assertNull(rootCauseException.getInnerExceptions());
/* Check threads. */
assertNotNull(errorLog.getThreads());
assertEquals(java.lang.Thread.getAllStackTraces().size(), errorLog.getThreads().size());
for (Thread thread : errorLog.getThreads()) {
assertNotNull(thread);
assertTrue(thread.getId() > 0);
assertNotNull(thread.getName());
assertNotNull(thread.getFrames());
for (StackFrame frame : thread.getFrames()) {
assertNotNull(frame);
assertNotNull(frame.getClassName());
assertNotNull(frame.getMethodName());
}
}
}
Aggregations