Skip to content

chore(example): update example app #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class InstabugExampleMethodCallHandler : MethodChannel.MethodCallHandler {
}
SEND_NATIVE_FATAL_HANG -> {
Log.d(TAG, "Sending native fatal hang for 3000 ms")
sendANR()
sendFatalHang()
result.success(null)
}
SEND_ANR -> {
Log.d(TAG, "Sending android not responding 'ANR' hanging for 20000 ms")
sendFatalHang()
sendANR()
result.success(null)
}
SEND_OOM -> {
Expand Down Expand Up @@ -71,58 +71,41 @@ class InstabugExampleMethodCallHandler : MethodChannel.MethodCallHandler {
}

private fun sendANR() {
android.os.Handler(Looper.getMainLooper()).post {
try {
Thread.sleep(20000)
} catch (e: InterruptedException) {
throw RuntimeException(e)
}
}
}

private fun sendFatalHang() {
try {
Thread.sleep(3000)
} catch (e: InterruptedException) {
throw RuntimeException(e)
android.os.Handler(Looper.getMainLooper()).post {

try {
Thread.sleep(3000)
} catch (e: InterruptedException) {
throw RuntimeException(e)
}
}
}

private fun sendOOM() {
android.os.Handler(Looper.getMainLooper()).post {


oomCrash()
}
}

private fun oomCrash() {
Thread {
val stringList: MutableList<String> = ArrayList()
for (i in 0 until 1000000) {
stringList.add(getRandomString(10000))
}
}.start()
}

private fun getRandomString(length: Int): String {
val charset: MutableList<Char> = ArrayList()
var ch = 'a'
while (ch <= 'z') {
charset.add(ch)
ch++
val list = ArrayList<ByteArray>()
while (true) {
list.add(ByteArray(10 * 1024 * 1024)) // Allocate 10MB chunks
}
ch = 'A'
while (ch <= 'Z') {
charset.add(ch)
ch++
}
ch = '0'
while (ch <= '9') {
charset.add(ch)
ch++
}
val randomString = StringBuilder()
val random = java.util.Random()
for (i in 0 until length) {
val randomChar = charset[random.nextInt(charset.size)]
randomString.append(randomChar)
}
return randomString.toString()
}



}
28 changes: 16 additions & 12 deletions example/ios/Runner/InstabugExampleMethodCallHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,21 @@ + (BOOL)requiresMainQueueSetup
}

- (void)oomCrash {
dispatch_async(self.serialQueue, ^{
self.oomBelly = [NSMutableArray array];
[UIApplication.sharedApplication beginBackgroundTaskWithName:@"OOM Crash" expirationHandler:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *belly = [NSMutableArray array];
while (true) {
unsigned long dinnerLength = 1024 * 1024 * 10;
char *dinner = malloc(sizeof(char) * dinnerLength);
for (int i=0; i < dinnerLength; i++)
{
//write to each byte ensure that the memory pages are actually allocated
dinner[i] = '0';
// 20 MB chunks to speed up OOM
void *buffer = malloc(20 * 1024 * 1024);
if (buffer == NULL) {
NSLog(@"OOM: malloc failed");
break;
}
NSData *plate = [NSData dataWithBytesNoCopy:dinner length:dinnerLength freeWhenDone:YES];
[self.oomBelly addObject:plate];
memset(buffer, 1, 20 * 1024 * 1024);
NSData *data = [NSData dataWithBytesNoCopy:buffer length:20 * 1024 * 1024 freeWhenDone:YES];
[belly addObject:data];

// Optional: slight delay to avoid CPU spike
[NSThread sleepForTimeInterval:0.01];
}
});
}
Expand Down Expand Up @@ -108,7 +110,9 @@ - (void)sendNativeFatalCrash {
}

- (void)sendFatalHang {
[NSThread sleepForTimeInterval:3.0f];
dispatch_async(dispatch_get_main_queue(), ^{
sleep(20); // Block main thread for 20 seconds
});
}

- (void)sendOOM {
Expand Down
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import 'src/widget/section_title.dart';

part 'src/screens/crashes_page.dart';

part 'src/screens/bug_reporting.dart';

part 'src/screens/complex_page.dart';

part 'src/screens/apm_page.dart';
Expand Down
2 changes: 2 additions & 0 deletions example/lib/src/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ final appRoutes = {
"/": (BuildContext context) =>
const MyHomePage(title: 'Flutter Demo Home Pag'),
CrashesPage.screenName: (BuildContext context) => const CrashesPage(),
BugReportingPage.screenName: (BuildContext context) =>
const BugReportingPage(),
ComplexPage.screenName: (BuildContext context) => const ComplexPage(),
ApmPage.screenName: (BuildContext context) => const ApmPage(),
ScreenLoadingPage.screenName: (BuildContext context) =>
Expand Down
10 changes: 10 additions & 0 deletions example/lib/src/components/fatal_crashes_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,37 @@ class FatalCrashesContent extends StatelessWidget {
children: [
InstabugButton(
text: 'Throw Exception',
key: const Key('fatal_crash_exception'),
onPressed: () => throwUnhandledException(
Exception('This is a generic exception.')),
),
InstabugButton(
text: 'Throw StateError',
key: const Key('fatal_crash_state_exception'),
onPressed: () =>
throwUnhandledException(StateError('This is a StateError.')),
),
InstabugButton(
text: 'Throw ArgumentError',
key: const Key('fatal_crash_argument_exception'),
onPressed: () => throwUnhandledException(
ArgumentError('This is an ArgumentError.')),
),
InstabugButton(
text: 'Throw RangeError',
key: const Key('fatal_crash_range_exception'),
onPressed: () => throwUnhandledException(
RangeError.range(5, 0, 3, 'Index out of range')),
),
InstabugButton(
text: 'Throw FormatException',
key: const Key('fatal_crash_format_exception'),
onPressed: () =>
throwUnhandledException(UnsupportedError('Invalid format.')),
),
InstabugButton(
text: 'Throw NoSuchMethodError',
key: const Key('fatal_crash_no_such_method_error_exception'),
onPressed: () {
// This intentionally triggers a NoSuchMethodError
dynamic obj;
Expand All @@ -53,20 +59,24 @@ class FatalCrashesContent extends StatelessWidget {
),
const InstabugButton(
text: 'Throw Native Fatal Crash',
key: const Key('fatal_crash_native_exception'),
onPressed: InstabugFlutterExampleMethodChannel.sendNativeFatalCrash,
),
const InstabugButton(
text: 'Send Native Fatal Hang',
key: const Key('fatal_crash_native_hang'),
onPressed: InstabugFlutterExampleMethodChannel.sendNativeFatalHang,
),
Platform.isAndroid
? const InstabugButton(
text: 'Send Native ANR',
key: const Key('fatal_crash_anr'),
onPressed: InstabugFlutterExampleMethodChannel.sendAnr,
)
: const SizedBox.shrink(),
const InstabugButton(
text: 'Throw Unhandled Native OOM Exception',
key: const Key('fatal_crash_oom'),
onPressed: InstabugFlutterExampleMethodChannel.sendOom,
),
],
Expand Down
31 changes: 26 additions & 5 deletions example/lib/src/components/non_fatal_crashes_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,50 @@ class _NonFatalCrashesContentState extends State<NonFatalCrashesContent> {
children: [
InstabugButton(
text: 'Throw Exception',
key: const Key('non_fatal_exception'),
onPressed: () =>
throwHandledException(Exception('This is a generic exception.')),
),
InstabugButton(
text: 'Throw StateError',
key: const Key('non_fatal_state_exception'),
onPressed: () =>
throwHandledException(StateError('This is a StateError.')),
),
InstabugButton(
text: 'Throw ArgumentError',
key: const Key('non_fatal_argument_exception'),

onPressed: () =>
throwHandledException(ArgumentError('This is an ArgumentError.')),
),
InstabugButton(
text: 'Throw RangeError',
key: const Key('non_fatal_range_exception'),

onPressed: () => throwHandledException(
RangeError.range(5, 0, 3, 'Index out of range')),
),
InstabugButton(
text: 'Throw FormatException',
key: const Key('non_fatal_format_exception'),

onPressed: () =>
throwHandledException(UnsupportedError('Invalid format.')),
),
InstabugButton(
text: 'Throw NoSuchMethodError',
key: const Key('non_fatal_no_such_method_exception'),

onPressed: () {
dynamic obj;
throwHandledException(obj.methodThatDoesNotExist());
},
),
const InstabugButton(
text: 'Throw Handled Native Exception',
key: Key('non_fatal_native_exception'),

onPressed:
InstabugFlutterExampleMethodChannel.sendNativeNonFatalCrash,
),
Expand All @@ -86,6 +98,7 @@ class _NonFatalCrashesContentState extends State<NonFatalCrashesContent> {
Expanded(
child: InstabugTextField(
label: "Crash title",
key: const Key("non_fatal_crash_title_textfield"),
controller: crashNameController,
validator: (value) {
if (value?.trim().isNotEmpty == true) return null;
Expand All @@ -100,7 +113,9 @@ class _NonFatalCrashesContentState extends State<NonFatalCrashesContent> {
Expanded(
child: InstabugTextField(
label: "User Attribute key",
controller: crashUserAttributeKeyController,
key: const Key("non_fatal_user_attribute_key_textfield"),

controller: crashUserAttributeKeyController,
validator: (value) {
if (crashUserAttributeValueController.text.isNotEmpty) {
if (value?.trim().isNotEmpty == true) return null;
Expand All @@ -113,7 +128,9 @@ class _NonFatalCrashesContentState extends State<NonFatalCrashesContent> {
Expanded(
child: InstabugTextField(
label: "User Attribute Value",
controller: crashUserAttributeValueController,
key: const Key("non_fatal_user_attribute_value_textfield"),

controller: crashUserAttributeValueController,
validator: (value) {
if (crashUserAttributeKeyController.text.isNotEmpty) {
if (value?.trim().isNotEmpty == true) return null;
Expand All @@ -130,7 +147,9 @@ class _NonFatalCrashesContentState extends State<NonFatalCrashesContent> {
Expanded(
child: InstabugTextField(
label: "Fingerprint",
controller: crashfingerPrintController,
key: const Key("non_fatal_user_attribute_fingerprint_textfield"),

controller: crashfingerPrintController,
)),
],
),
Expand All @@ -141,6 +160,8 @@ class _NonFatalCrashesContentState extends State<NonFatalCrashesContent> {
Expanded(
flex: 5,
child: DropdownButtonHideUnderline(
key: const Key("non_fatal_crash_level_dropdown"),

child:
DropdownButtonFormField<NonFatalExceptionLevel>(
value: crashType,
Expand All @@ -161,7 +182,7 @@ class _NonFatalCrashesContentState extends State<NonFatalCrashesContent> {
],
),
),
SizedBox(
const SizedBox(
height: 8,
),
InstabugButton(
Expand Down Expand Up @@ -190,7 +211,7 @@ class _NonFatalCrashesContentState extends State<NonFatalCrashesContent> {
fingerprint: crashfingerPrintController.text,
level: crashType);
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Crash sent")));
.showSnackBar(const SnackBar(content: Text("Crash sent")));
crashNameController.text = '';
crashfingerPrintController.text = '';
crashUserAttributeValueController.text = '';
Expand Down
Loading