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 12 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 @@ -78,60 +78,43 @@ 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()
}



private fun setFullscreen(enabled: Boolean) {
try {

Expand Down
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
17 changes: 16 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'dart:convert';
import 'dart:math' as math;

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:instabug_flutter/instabug_flutter.dart';
import 'package:instabug_flutter_example/src/components/apm_switch.dart';
import 'package:instabug_flutter_example/src/screens/callback/callback_handler_provider.dart';
import 'package:instabug_flutter_example/src/screens/callback/callback_page.dart';
import 'package:instabug_flutter_example/src/utils/widget_ext.dart';
import 'package:instabug_http_client/instabug_http_client.dart';
import 'package:instabug_flutter_example/src/app_routes.dart';
import 'package:instabug_flutter_example/src/widget/nested_view.dart';
import 'package:provider/provider.dart';

import 'src/native/instabug_flutter_example_method_channel.dart';
import 'src/widget/instabug_button.dart';
Expand All @@ -20,6 +26,10 @@ import 'src/widget/section_title.dart';

part 'src/screens/crashes_page.dart';

part 'src/screens/bug_reporting.dart';

part 'src/screens/session_replay_page.dart';

part 'src/screens/complex_page.dart';

part 'src/screens/apm_page.dart';
Expand Down Expand Up @@ -55,7 +65,12 @@ void main() {
Zone.current.handleUncaughtError(details.exception, details.stack!);
};

runApp(const MyApp());
runApp(
ChangeNotifierProvider(
create: (_) => CallbackHandlersProvider(),
child: const MyApp(),
),
);
},
CrashReporting.reportCrash,
);
Expand Down
8 changes: 8 additions & 0 deletions example/lib/src/app_routes.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/widgets.dart' show BuildContext;
import 'package:instabug_flutter_example/main.dart';
import 'package:instabug_flutter_example/src/screens/callback/callback_page.dart';

final appRoutes = {
/// ["/"] route name should only be used with [onGenerateRoute:] when no
Expand All @@ -9,7 +10,14 @@ final appRoutes = {
"/": (BuildContext context) =>
const MyHomePage(title: 'Flutter Demo Home Pag'),
CrashesPage.screenName: (BuildContext context) => const CrashesPage(),
BugReportingPage.screenName: (BuildContext context) =>
const BugReportingPage(),
CallbackScreen.screenName: (BuildContext context) => const CallbackScreen(),
ComplexPage.screenName: (BuildContext context) => const ComplexPage(),
SessionReplayPage.screenName: (BuildContext context) =>
const SessionReplayPage(),
TopTabBarScreen.route: (BuildContext context) => const TopTabBarScreen(),

ApmPage.screenName: (BuildContext context) => const ApmPage(),
ScreenLoadingPage.screenName: (BuildContext context) =>
const ScreenLoadingPage(),
Expand Down
28 changes: 24 additions & 4 deletions example/lib/src/components/fatal_crashes_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,74 @@ class FatalCrashesContent extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: [
InstabugButton(
symanticLabel: 'fatal_crash_exception',
text: 'Throw Exception',
key: const Key('fatal_crash_exception'),
onPressed: () => throwUnhandledException(
Exception('This is a generic exception.')),
),
InstabugButton(
symanticLabel: 'fatal_crash_state_exception',
text: 'Throw StateError',
key: const Key('fatal_crash_state_exception'),
onPressed: () =>
throwUnhandledException(StateError('This is a StateError.')),
),
InstabugButton(
symanticLabel: 'fatal_crash_argument_exception',
text: 'Throw ArgumentError',
key: const Key('fatal_crash_argument_exception'),
onPressed: () => throwUnhandledException(
ArgumentError('This is an ArgumentError.')),
),
InstabugButton(
symanticLabel: 'fatal_crash_range_exception',
text: 'Throw RangeError',
key: const Key('fatal_crash_range_exception'),
onPressed: () => throwUnhandledException(
RangeError.range(5, 0, 3, 'Index out of range')),
),
InstabugButton(
symanticLabel: 'fatal_crash_format_exception',
text: 'Throw FormatException',
key: const Key('fatal_crash_format_exception'),
onPressed: () =>
throwUnhandledException(UnsupportedError('Invalid format.')),
),
InstabugButton(
symanticLabel: 'fatal_crash_no_such_method_error_exception',
text: 'Throw NoSuchMethodError',
key: const Key('fatal_crash_no_such_method_error_exception'),
onPressed: () {
// This intentionally triggers a NoSuchMethodError
dynamic obj;
throwUnhandledException(obj.methodThatDoesNotExist());
},
),
const InstabugButton(
InstabugButton(
symanticLabel: 'fatal_crash_native_exception',
text: 'Throw Native Fatal Crash',
key: const Key('fatal_crash_native_exception'),
onPressed: InstabugFlutterExampleMethodChannel.sendNativeFatalCrash,
),
const InstabugButton(
InstabugButton(
symanticLabel: 'fatal_crash_native_hang',
text: 'Send Native Fatal Hang',
key: const Key('fatal_crash_native_hang'),
onPressed: InstabugFlutterExampleMethodChannel.sendNativeFatalHang,
),
Platform.isAndroid
? const InstabugButton(
? InstabugButton(
symanticLabel: 'fatal_crash_anr',
text: 'Send Native ANR',
key: const Key('fatal_crash_anr'),
onPressed: InstabugFlutterExampleMethodChannel.sendAnr,
)
: const SizedBox.shrink(),
const InstabugButton(
InstabugButton(
symanticLabel: 'fatal_crash_oom',
text: 'Throw Unhandled Native OOM Exception',
key: const Key('fatal_crash_oom'),
onPressed: InstabugFlutterExampleMethodChannel.sendOom,
),
],
Expand Down
7 changes: 7 additions & 0 deletions example/lib/src/components/flows_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class _FlowsContentState extends State<FlowsContent> {
children: [
InstabugTextField(
label: 'Flow name',
symanticLabel: 'flow_name_input',
labelStyle: textTheme.labelMedium,
controller: flowNameController,
),
Expand All @@ -33,6 +34,7 @@ class _FlowsContentState extends State<FlowsContent> {
flex: 5,
child: InstabugButton.smallFontSize(
text: 'Start Flow',
symanticLabel: 'start_flow',
onPressed: () => _startFlow(flowNameController.text),
margin: const EdgeInsetsDirectional.only(
start: 20.0,
Expand All @@ -44,6 +46,7 @@ class _FlowsContentState extends State<FlowsContent> {
flex: 5,
child: InstabugButton.smallFontSize(
text: 'Start flow With Delay',
symanticLabel: 'start_flow_with_delay',
onPressed: () => _startFlow(
flowNameController.text,
delayInMilliseconds: 5000,
Expand All @@ -62,6 +65,7 @@ class _FlowsContentState extends State<FlowsContent> {
flex: 5,
child: InstabugTextField(
label: 'Flow Key Attribute',
symanticLabel: 'flow_key_input',
controller: flowKeyAttributeController,
labelStyle: textTheme.labelMedium,
margin: const EdgeInsetsDirectional.only(
Expand All @@ -74,6 +78,7 @@ class _FlowsContentState extends State<FlowsContent> {
flex: 5,
child: InstabugTextField(
label: 'Flow Value Attribute',
symanticLabel: 'flow_value_input',
labelStyle: textTheme.labelMedium,
controller: flowValueAttributeController,
margin: const EdgeInsetsDirectional.only(
Expand All @@ -89,6 +94,7 @@ class _FlowsContentState extends State<FlowsContent> {
),
InstabugButton(
text: 'Set Flow Attribute',
symanticLabel: 'set_flow_attribute',
onPressed: () => _setFlowAttribute(
flowNameController.text,
flowKeyAttribute: flowKeyAttributeController.text,
Expand All @@ -97,6 +103,7 @@ class _FlowsContentState extends State<FlowsContent> {
),
InstabugButton(
text: 'End Flow',
symanticLabel: 'end_flow',
onPressed: () => _endFlow(flowNameController.text),
),
],
Expand Down
Loading