#!/bin/bash # reboot-test-v2.sh - Enhanced Reboot Testing with RecoveryManager set -e APP_PACKAGE="com.timesafari.dailynotification" APP_ACTIVITY=".MainActivity" echo "๐Ÿ”„ DailyNotification Reboot Test Suite v2.0" echo "==========================================" echo "Testing RecoveryManager-based reboot recovery" echo "โœ… Boot Receiver with RecoveryManager" echo "โœ… App Startup Recovery Coexistence" echo "โœ… Recovery Statistics Tracking" echo "โœ… Idempotent Recovery Operations" echo "" # Test results tracking declare -A test_results # Function to run test and track result run_test() { local test_name="$1" local test_function="$2" echo "๐Ÿงช Running: $test_name" echo "----------------------------------------" if $test_function; then echo "โœ… PASS: $test_name" test_results["$test_name"]="PASS" else echo "โŒ FAIL: $test_name" test_results["$test_name"]="FAIL" fi echo "" } # Function to wait for device wait_for_device() { echo "โณ Waiting for device to be ready..." adb wait-for-device sleep 15 # Additional wait for boot completion # Check if device is fully booted local boot_completed=$(adb shell getprop sys.boot_completed) if [ "$boot_completed" != "1" ]; then echo "โณ Device not fully booted, waiting more..." sleep 15 fi } # Function to schedule notification schedule_notification() { echo "๐Ÿ“… Scheduling test notification..." adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY sleep 3 echo "โš ๏ธ Manual step: Schedule notification in app" echo " - Tap 'Test Notification' button" echo " - Wait for 'Notification scheduled' message" read -p "Press Enter when notification is scheduled..." } # Function to check recovery logs check_recovery_logs() { local source="$1" echo "๐Ÿ” Checking recovery logs for: $source" if adb logcat -d | grep -q "Recovery requested from: $source"; then echo "โœ… Recovery triggered from: $source" return 0 else echo "โŒ Recovery not triggered from: $source" return 1 fi } # Function to check recovery cooldown check_recovery_cooldown() { echo "๐Ÿ” Checking recovery cooldown behavior..." local recovery_count=$(adb logcat -d | grep -c "Recovery requested from: APP_STARTUP") local cooldown_count=$(adb logcat -d | grep -c "Recovery performed recently.*skipping") if [ $recovery_count -eq 1 ] && [ $cooldown_count -gt 0 ]; then echo "โœ… Recovery cooldown working correctly" echo " - Recovery performed: $recovery_count times" echo " - Cooldown skips: $cooldown_count times" return 0 else echo "โŒ Recovery cooldown not working" echo " - Recovery performed: $recovery_count times" echo " - Cooldown skips: $cooldown_count times" return 1 fi } # Function to check scheduled alarms check_scheduled_alarms() { echo "โฐ Checking scheduled alarms..." if adb shell "dumpsys alarm | grep timesafari" | grep -q "RTC_WAKEUP"; then echo "โœ… Alarms scheduled correctly" return 0 else echo "โŒ No alarms found" return 1 fi } # Test 1: Boot Recovery with RecoveryManager test_boot_recovery() { echo "๐Ÿ”„ Testing Boot Recovery with RecoveryManager..." # Schedule notification schedule_notification # Verify initial scheduling if ! check_scheduled_alarms; then echo "โŒ Initial scheduling failed" return 1 fi # Clear logs before reboot adb logcat -c # Reboot device echo "๐Ÿ”„ Rebooting device..." adb reboot wait_for_device # Check boot recovery logs if ! check_recovery_logs "BOOT_COMPLETED"; then echo "โŒ Boot recovery not triggered" return 1 fi # Check alarms restored if ! check_scheduled_alarms; then echo "โŒ Alarms not restored after reboot" return 1 fi echo "โœ… Boot recovery working correctly" return 0 } # Test 2: App Startup Recovery Coexistence test_app_startup_coexistence() { echo "๐Ÿ”„ Testing App Startup Recovery Coexistence..." # Clear logs adb logcat -c # Launch app multiple times to test cooldown echo "๐Ÿš€ Testing app startup recovery cooldown..." adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY sleep 2 adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY sleep 2 adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY sleep 2 # Check recovery cooldown behavior if ! check_recovery_cooldown; then echo "โŒ Recovery cooldown not working" return 1 fi echo "โœ… App startup recovery coexistence working" return 0 } # Test 3: Package Replacement Recovery test_package_replacement() { echo "๐Ÿ”„ Testing Package Replacement Recovery..." # Schedule notification schedule_notification # Verify initial scheduling if ! check_scheduled_alarms; then echo "โŒ Initial scheduling failed" return 1 fi # Clear logs adb logcat -c # Update app (simulate package replacement) echo "๐Ÿ“ฆ Updating app (simulating package replacement)..." cd android && ./gradlew assembleDebug adb install -r app/build/outputs/apk/debug/app-debug.apk cd .. # Check package replacement recovery logs if ! check_recovery_logs "MY_PACKAGE_REPLACED"; then echo "โŒ Package replacement recovery not triggered" return 1 fi # Check alarms restored if ! check_scheduled_alarms; then echo "โŒ Alarms not restored after package replacement" return 1 fi echo "โœ… Package replacement recovery working" return 0 } # Test 4: Recovery Statistics test_recovery_statistics() { echo "๐Ÿ“Š Testing Recovery Statistics..." # Clear logs adb logcat -c # Perform multiple recovery operations echo "๐Ÿ”„ Performing multiple recovery operations..." # Launch app (triggers startup recovery) adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY sleep 2 # Launch app again (should be skipped due to cooldown) adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY sleep 2 # Check recovery statistics in logs local recovery_count=$(adb logcat -d | grep -c "Recovery requested from:") local cooldown_count=$(adb logcat -d | grep -c "Recovery performed recently.*skipping") if [ $recovery_count -gt 0 ] && [ $cooldown_count -gt 0 ]; then echo "โœ… Recovery statistics tracking working" echo " - Total recovery requests: $recovery_count" echo " - Cooldown skips: $cooldown_count" return 0 else echo "โŒ Recovery statistics not tracking correctly" return 1 fi } # Test 5: Recovery State Persistence test_recovery_state_persistence() { echo "๐Ÿ’พ Testing Recovery State Persistence..." # Clear logs adb logcat -c # Launch app to trigger recovery adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY sleep 2 # Check if recovery state is persisted local recovery_state=$(adb shell "run-as $APP_PACKAGE ls -la /data/data/$APP_PACKAGE/shared_prefs/ | grep recovery_state") if [ -n "$recovery_state" ]; then echo "โœ… Recovery state persisted" echo " - Recovery state file: $recovery_state" return 0 else echo "โŒ Recovery state not persisted" return 1 fi } # Test 6: Multiple Notifications Recovery test_multiple_notifications_recovery() { echo "๐Ÿ“ฑ Testing Multiple Notifications Recovery..." # Schedule multiple notifications echo "๐Ÿ“… Scheduling multiple notifications..." echo "โš ๏ธ Manual step: Schedule 3-4 notifications in app UI" echo " - Tap 'Test Notification' multiple times" echo " - Wait for each 'Notification scheduled' message" read -p "Press Enter when multiple notifications are scheduled..." # Count initial alarms local initial_alarms=$(adb shell "dumpsys alarm | grep timesafari | wc -l") echo "๐Ÿ“Š Initial alarms scheduled: $initial_alarms" if [ $initial_alarms -lt 2 ]; then echo "โŒ Not enough notifications scheduled for test" return 1 fi # Clear logs adb logcat -c # Reboot device echo "๐Ÿ”„ Rebooting device..." adb reboot wait_for_device # Check recovery logs if ! check_recovery_logs "BOOT_COMPLETED"; then echo "โŒ Boot recovery not triggered" return 1 fi # Count restored alarms local restored_alarms=$(adb shell "dumpsys alarm | grep timesafari | wc -l") echo "๐Ÿ“Š Restored alarms: $restored_alarms" if [ $restored_alarms -eq $initial_alarms ]; then echo "โœ… All notifications recovered correctly" return 0 else echo "โŒ Not all notifications recovered" echo " - Initial: $initial_alarms, Restored: $restored_alarms" return 1 fi } # Main test execution main() { echo "๐Ÿš€ Starting Reboot Test Suite v2.0..." echo "" # Run all tests run_test "Boot Recovery with RecoveryManager" test_boot_recovery run_test "App Startup Recovery Coexistence" test_app_startup_coexistence run_test "Package Replacement Recovery" test_package_replacement run_test "Recovery Statistics" test_recovery_statistics run_test "Recovery State Persistence" test_recovery_state_persistence run_test "Multiple Notifications Recovery" test_multiple_notifications_recovery # Print results summary echo "๐Ÿ“Š Reboot Test Results Summary" echo "=============================" local pass_count=0 local total_count=0 for test_name in "${!test_results[@]}"; do local result="${test_results[$test_name]}" echo "$test_name: $result" if [ "$result" = "PASS" ]; then ((pass_count++)) fi ((total_count++)) done echo "" echo "Overall: $pass_count/$total_count tests passed" if [ $pass_count -eq $total_count ]; then echo "๐ŸŽ‰ All reboot tests passed! RecoveryManager working correctly." echo "โฐ Wait for scheduled notifications to appear" exit 0 else echo "โŒ Some reboot tests failed. Check logs for details." exit 1 fi } # Run main function main