computeruse-rs 2.0.0

A Playwright-style SDK for automating desktop GUI applications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//! Functional verification tests for optimized Windows implementation
//!
//! This module contains tests to verify that the performance optimizations
//! in the Windows platform layer don't break existing functionality.

use crate::Desktop;

/// Initialize test environment
fn init_test_environment() -> Desktop {
    Desktop::new(false, false).expect("Failed to create Desktop for testing")
}

#[test]
#[ignore] // Run with: cargo test test_get_applications_functional -- --ignored --nocapture
fn test_get_applications_functional() {
    println!("\n🔍 Testing get_applications() functionality");

    let desktop = init_test_environment();

    // Test basic functionality
    match desktop.applications() {
        Ok(apps) => {
            println!("✅ Successfully retrieved {} applications", apps.len());

            // Verify we get some applications (should be at least 1 on any Windows system)
            assert!(!apps.is_empty(), "Should find at least one application");

            // Test that each application has basic properties
            for (i, app) in apps.iter().take(3).enumerate() {
                println!("  App {}: Testing basic properties...", i + 1);

                // Test that we can get the name without crashing
                match app.name() {
                    Some(name) => {
                        println!("    ✅ Name: '{name}'");
                        assert!(!name.is_empty(), "Application name should not be empty");
                    }
                    None => println!("    ⚠️  No name available"),
                }

                // Test that we can get the role without crashing
                let role = app.role();
                println!("    ✅ Role: '{role}'");
                assert!(!role.is_empty(), "Role should not be empty");

                // Test that we can get attributes without crashing
                let attrs = app.attributes();
                println!("    ✅ Attributes role: '{}'", attrs.role);

                // Test that we can get process ID without crashing
                match app.process_id() {
                    Ok(pid) => {
                        println!("    ✅ Process ID: {pid}");
                        assert!(pid > 0, "Process ID should be positive");
                    }
                    Err(e) => println!("    ⚠️  Could not get process ID: {e}"),
                }
            }
        }
        Err(e) => {
            panic!("❌ Failed to get applications: {e}");
        }
    }
}

#[test]
#[ignore]
fn test_get_application_by_name_functional() {
    println!("\n🔍 Testing get_application_by_name() functionality");

    let desktop = init_test_environment();

    // Test with a known Windows application that should always be available
    let test_apps = ["explorer", "dwm", "winlogon", "csrss"];
    let mut found_app = false;

    for app_name in &test_apps {
        println!("  Testing lookup for: '{app_name}'");

        match desktop.application(app_name) {
            Ok(app) => {
                println!("    ✅ Found application: '{app_name}'");
                found_app = true;

                // Verify the application is functional
                let role = app.role();
                println!("    ✅ Role: '{role}'");

                match app.process_id() {
                    Ok(pid) => {
                        println!("    ✅ Process ID: {pid}");
                        assert!(pid > 0, "Process ID should be positive");
                    }
                    Err(e) => println!("    ⚠️  Could not get process ID: {e}"),
                }

                // Test attributes access
                let attrs = app.attributes();
                println!("    ✅ Attributes accessible, role: '{}'", attrs.role);

                break; // Found one working app, that's enough
            }
            Err(e) => {
                println!("    ⚠️  Could not find '{app_name}': {e}");
            }
        }
    }

    assert!(
        found_app,
        "Should be able to find at least one system application"
    );

    // Test with non-existent application
    println!("  Testing lookup for non-existent app...");
    match desktop.application("nonexistent_app_12345") {
        Ok(_) => panic!("❌ Should not find non-existent application"),
        Err(e) => {
            println!("    ✅ Correctly failed to find non-existent app: {e}");
        }
    }
}

#[test]
#[ignore]
fn test_cached_attributes_functional() {
    println!("\n🔍 Testing cached attributes functionality");

    let desktop = init_test_environment();

    match desktop.applications() {
        Ok(apps) => {
            if let Some(app) = apps.first() {
                println!("  Testing attributes for first application...");

                // Test multiple attribute accesses to verify caching works
                for i in 1..=3 {
                    println!("    Access #{i}: Testing attributes...");

                    let attrs = app.attributes();
                    println!("      ✅ Role: '{}'", attrs.role);
                    assert!(!attrs.role.is_empty(), "Role should not be empty");

                    // Test that name is consistently accessible
                    if let Some(name) = &attrs.name {
                        println!("      ✅ Name: '{name}'");
                        assert!(!name.is_empty(), "Name should not be empty if present");
                    } else {
                        println!("      ⚠️  No name in attributes");
                    }

                    // Verify properties map is accessible
                    println!(
                        "      ✅ Properties map accessible with {} entries",
                        attrs.properties.len()
                    );
                }

                // Test that direct role access still works
                let direct_role = app.role();
                let attrs_role = app.attributes().role;
                println!("    ✅ Direct role: '{direct_role}', Attrs role: '{attrs_role}'");

                // They should be the same
                assert_eq!(
                    direct_role, attrs_role,
                    "Direct role and attributes role should match"
                );
            }
        }
        Err(e) => {
            panic!("❌ Failed to get applications for attribute testing: {e}");
        }
    }
}

#[test]
#[ignore]
fn test_pid_lookup_caching_functional() {
    println!("\n🔍 Testing PID lookup caching functionality");

    let desktop = init_test_environment();

    // Test with known system processes
    let test_processes = ["explorer", "dwm", "winlogon"];

    for process_name in &test_processes {
        println!("  Testing PID lookup for: '{process_name}'");

        // First lookup - should populate cache
        let start_time = std::time::Instant::now();
        match desktop.application(process_name) {
            Ok(app) => {
                let first_duration = start_time.elapsed();
                println!("    ✅ First lookup took: {first_duration:?}");

                if let Ok(pid1) = app.process_id() {
                    println!("    ✅ Found PID: {pid1}");

                    // Second lookup - should use cache
                    let start_time2 = std::time::Instant::now();
                    match desktop.application(process_name) {
                        Ok(app2) => {
                            let second_duration = start_time2.elapsed();
                            println!("    ✅ Second lookup took: {second_duration:?}");

                            if let Ok(pid2) = app2.process_id() {
                                // PIDs should be the same (unless process restarted)
                                println!("    ✅ PIDs: {pid1} vs {pid2}");

                                // Cache should make second lookup faster (usually)
                                if second_duration < first_duration {
                                    println!(
                                        "    ✅ Cache optimization detected (faster second lookup)"
                                    );
                                } else {
                                    println!(
                                        "    ⚠️  Second lookup not faster, but that's still okay"
                                    );
                                }
                            }
                        }
                        Err(e) => println!("    ⚠️  Second lookup failed: {e}"),
                    }
                }
                break; // Found one working process, that's enough
            }
            Err(e) => {
                println!("    ⚠️  Could not find '{process_name}': {e}");
            }
        }
    }
}

#[test]
#[ignore]
fn test_application_children_access() {
    println!("\n🔍 Testing application children access");

    let desktop = init_test_environment();

    match desktop.applications() {
        Ok(apps) => {
            // Find an application with children (try a few)
            for app in apps.iter().take(5) {
                let app_name = app.name().unwrap_or_else(|| "Unknown".to_string());
                println!("  Testing children access for: '{app_name}'");

                match app.children() {
                    Ok(children) => {
                        println!("    ✅ Found {} children", children.len());

                        if !children.is_empty() {
                            // Test first child's attributes
                            let child = &children[0];
                            let child_attrs = child.attributes();
                            println!("    ✅ First child role: '{}'", child_attrs.role);

                            // Test that we can get child's role without crashing
                            let child_role = child.role();
                            println!("    ✅ First child direct role: '{child_role}'");

                            break; // Found an app with children, that's enough
                        }
                    }
                    Err(e) => {
                        println!("    ⚠️  Could not get children: {e}");
                    }
                }
            }
        }
        Err(e) => {
            panic!("❌ Failed to get applications for children testing: {e}");
        }
    }
}

#[test]
#[ignore]
fn test_element_bounds_and_properties() {
    println!("\n🔍 Testing element bounds and properties access");

    let desktop = init_test_environment();

    match desktop.applications() {
        Ok(apps) => {
            if let Some(app) = apps.first() {
                let app_name = app.name().unwrap_or_else(|| "Unknown".to_string());
                println!("  Testing bounds for: '{app_name}'");

                // Test bounds access
                match app.bounds() {
                    Ok((x, y, width, height)) => {
                        println!("    ✅ Bounds: ({x}, {y}, {width}, {height})");
                        assert!(width >= 0.0, "Width should be non-negative");
                        assert!(height >= 0.0, "Height should be non-negative");
                    }
                    Err(e) => {
                        println!("    ⚠️  Could not get bounds: {e}");
                    }
                }

                // Test enabled state
                match app.is_enabled() {
                    Ok(enabled) => {
                        println!("    ✅ Is enabled: {enabled}");
                    }
                    Err(e) => {
                        println!("    ⚠️  Could not check enabled state: {e}");
                    }
                }

                // Test visible state
                match app.is_visible() {
                    Ok(visible) => {
                        println!("    ✅ Is visible: {visible}");
                    }
                    Err(e) => {
                        println!("    ⚠️  Could not check visible state: {e}");
                    }
                }
            }
        }
        Err(e) => {
            panic!("❌ Failed to get applications for bounds testing: {e}");
        }
    }
}

#[test]
#[ignore]
fn test_focused_element_access() {
    println!("\n🔍 Testing focused element access");

    let desktop = init_test_environment();

    match desktop.focused_element() {
        Ok(focused) => {
            println!("  ✅ Found focused element");

            // Test that we can access focused element properties
            let role = focused.role();
            println!("    ✅ Focused element role: '{role}'");

            let attrs = focused.attributes();
            println!("    ✅ Focused element attributes role: '{}'", attrs.role);

            if let Some(name) = attrs.name {
                println!("    ✅ Focused element name: '{name}'");
            }

            // Test process ID access
            match focused.process_id() {
                Ok(pid) => {
                    println!("    ✅ Focused element PID: {pid}");
                    assert!(pid > 0, "Process ID should be positive");
                }
                Err(e) => {
                    println!("    ⚠️  Could not get focused element PID: {e}");
                }
            }
        }
        Err(e) => {
            println!("    ⚠️  Could not get focused element: {e}");
        }
    }
}

#[test]
#[ignore]
fn test_error_handling_robustness() {
    println!("\n🔍 Testing error handling robustness");

    let desktop = init_test_environment();

    // Test with various invalid inputs
    let invalid_names = [
        "",
        "   ",
        "invalid/name",
        "app\0with\0nulls",
        "very_long_name_that_definitely_does_not_exist_in_any_system_12345",
    ];

    for invalid_name in &invalid_names {
        println!("  Testing with invalid name: '{invalid_name}'");

        match desktop.application(invalid_name) {
            Ok(_) => {
                println!("    ⚠️  Unexpectedly found application for invalid name");
            }
            Err(e) => {
                println!("    ✅ Correctly handled error: {e}");
                // Should be a proper error, not a crash
            }
        }
    }

    // Test that the desktop is still functional after error cases
    println!("  Verifying desktop still functional after error cases...");
    match desktop.applications() {
        Ok(apps) => {
            println!("    ✅ Desktop still functional, found {} apps", apps.len());
        }
        Err(e) => {
            panic!("    ❌ Desktop became non-functional after error cases: {e}");
        }
    }
}

#[test]
#[ignore]
fn test_comprehensive_functional_verification() {
    println!("\n📊 Comprehensive Functional Verification Test Suite");
    println!("=====================================================");
    println!("Running all functional verification tests...\n");

    test_get_applications_functional();
    println!("\n{}\n", "=".repeat(50));

    test_get_application_by_name_functional();
    println!("\n{}\n", "=".repeat(50));

    test_cached_attributes_functional();
    println!("\n{}\n", "=".repeat(50));

    test_pid_lookup_caching_functional();
    println!("\n{}\n", "=".repeat(50));

    test_application_children_access();
    println!("\n{}\n", "=".repeat(50));

    test_element_bounds_and_properties();
    println!("\n{}\n", "=".repeat(50));

    test_focused_element_access();
    println!("\n{}\n", "=".repeat(50));

    test_window_activation();
    println!("\n{}\n", "=".repeat(50));

    test_error_handling_robustness();

    println!("\n✅ All functional verification tests completed!");
    println!("\n📝 Summary:");
    println!("- Application enumeration works correctly");
    println!("- Application lookup by name functions properly");
    println!("- Cached attributes provide consistent results");
    println!("- PID lookup caching improves performance without breaking functionality");
    println!("- Element property access remains stable");
    println!("- Error handling is robust and doesn't crash the system");
    println!("\n💡 Individual tests can be run with:");
    println!("  cargo test test_get_applications_functional -- --ignored --nocapture");
    println!("  cargo test test_get_application_by_name_functional -- --ignored --nocapture");
    println!("  cargo test test_comprehensive_functional_verification -- --ignored --nocapture");
}

#[test]
#[ignore]
fn test_window_activation() {
    use std::thread::sleep;
    use std::time::Duration;

    println!("\n🪟 Testing window activation functionality");
    let desktop = Desktop::new(false, false).unwrap();

    // Get current applications
    let apps = match desktop.applications() {
        Ok(apps) => {
            println!("  ✅ Found {} applications", apps.len());
            apps
        }
        Err(e) => {
            println!("  ❌ Failed to get applications: {e}");
            return;
        }
    };

    if apps.is_empty() {
        println!("  ⚠️  No applications found to test activation");
        return;
    }

    // Test activation on multiple applications
    for (i, app) in apps.iter().take(3).enumerate() {
        let app_name = app.name().unwrap_or_else(|| format!("App_{i}"));
        println!("\n  🎯 Testing activation for application: '{app_name}'");

        // Test 1: activate_window method
        println!("    📋 Testing activate_window() method...");
        match app.activate_window() {
            Ok(()) => {
                println!("      ✅ activate_window() reported success");

                // Verify activation by checking if the app became the focused element
                sleep(Duration::from_millis(500));
                match desktop.focused_element() {
                    Ok(focused) => {
                        if let (Ok(focused_pid), Ok(app_pid)) =
                            (focused.process_id(), app.process_id())
                        {
                            if focused_pid == app_pid {
                                println!(
                                    "      ✅ Verification: Application is now focused (PID match)"
                                );
                            } else {
                                println!(
                                    "      ⚠️  Verification: Different app is focused (PID: {focused_pid} vs {app_pid})"
                                );
                            }
                        } else {
                            println!("      ⚠️  Verification: Could not get PIDs for comparison");
                        }
                    }
                    Err(e) => {
                        println!("      ⚠️  Verification: Could not get focused element: {e}");
                    }
                }
            }
            Err(e) => {
                println!("      ❌ activate_window() failed: {e}");
            }
        }

        // Test 2: focus method as alternative
        println!("    📋 Testing focus() method...");
        match app.focus() {
            Ok(()) => {
                println!("      ✅ focus() reported success");

                // Verify focus
                sleep(Duration::from_millis(500));
                match desktop.focused_element() {
                    Ok(focused) => {
                        if let (Ok(focused_pid), Ok(app_pid)) =
                            (focused.process_id(), app.process_id())
                        {
                            if focused_pid == app_pid {
                                println!(
                                    "      ✅ Verification: Application is now focused (PID match)"
                                );
                            } else {
                                println!(
                                    "      ⚠️  Verification: Different app is focused (PID: {focused_pid} vs {app_pid})"
                                );
                            }
                        } else {
                            println!("      ⚠️  Verification: Could not get PIDs for comparison");
                        }
                    }
                    Err(e) => {
                        println!("      ⚠️  Verification: Could not get focused element: {e}");
                    }
                }
            }
            Err(e) => {
                println!("      ❌ focus() failed: {e}");
            }
        }

        // Test 3: Check if the element is keyboard focusable
        println!("    📋 Testing keyboard focusability...");
        match app.is_keyboard_focusable() {
            Ok(focusable) => {
                println!("      ✅ is_keyboard_focusable(): {focusable}");
                if !focusable {
                    println!(
                        "      ⚠️  Element is not keyboard focusable - this may explain activation issues"
                    );
                }
            }
            Err(e) => {
                println!("      ❌ is_keyboard_focusable() failed: {e}");
            }
        }

        // Test 4: Get window for this app and try to activate that
        println!("    📋 Testing window activation via window() method...");
        match app.window() {
            Ok(Some(window)) => {
                println!("      ✅ Found window for application");
                match window.activate_window() {
                    Ok(()) => {
                        println!("      ✅ Window activate_window() reported success");

                        // Verify activation
                        sleep(Duration::from_millis(500));
                        match desktop.focused_element() {
                            Ok(focused) => {
                                if let (Ok(focused_pid), Ok(window_pid)) =
                                    (focused.process_id(), window.process_id())
                                {
                                    if focused_pid == window_pid {
                                        println!(
                                            "      ✅ Verification: Window is now focused (PID match)"
                                        );
                                    } else {
                                        println!(
                                            "      ⚠️  Verification: Different app is focused (PID: {focused_pid} vs {window_pid})"
                                        );
                                    }
                                } else {
                                    println!(
                                        "      ⚠️  Verification: Could not get PIDs for comparison"
                                    );
                                }
                            }
                            Err(e) => {
                                println!(
                                    "      ⚠️  Verification: Could not get focused element: {e}"
                                );
                            }
                        }
                    }
                    Err(e) => {
                        println!("      ❌ Window activate_window() failed: {e}");
                    }
                }
            }
            Ok(None) => {
                println!("      ⚠️  No window found for application");
            }
            Err(e) => {
                println!("      ❌ Failed to get window: {e}");
            }
        }

        println!("    ⏱️  Pausing between tests...");
        sleep(Duration::from_millis(1000));
    }

    // Test 5: Test with Notepad specifically (if available)
    println!("\n  🗒️  Testing Notepad specifically...");
    match desktop.application("notepad") {
        Ok(notepad) => {
            println!("    ✅ Found Notepad application");

            // Test various activation methods on Notepad
            println!("    📋 Testing Notepad activation methods...");

            // Method 1: Direct activation
            match notepad.activate_window() {
                Ok(()) => println!("      ✅ Notepad activate_window() succeeded"),
                Err(e) => println!("      ❌ Notepad activate_window() failed: {e}"),
            }

            sleep(Duration::from_millis(500));

            // Method 2: Focus
            match notepad.focus() {
                Ok(()) => println!("      ✅ Notepad focus() succeeded"),
                Err(e) => println!("      ❌ Notepad focus() failed: {e}"),
            }

            sleep(Duration::from_millis(500));

            // Method 3: Click to activate
            match notepad.click() {
                Ok(_) => println!("      ✅ Notepad click() succeeded"),
                Err(e) => println!("      ❌ Notepad click() failed: {e}"),
            }
        }
        Err(_) => {
            println!("    ⚠️  Notepad not found - this is fine for testing");
        }
    }
}

#[test]
#[ignore] // Run with: cargo test test_close_functionality -- --ignored --nocapture
fn test_close_functionality() {
    use std::thread::sleep;
    use std::time::Duration;

    println!("\n🔍 Testing close() functionality with and without focus");

    let desktop = init_test_environment();

    // Test 1: Close when window is focused
    println!("\n📋 Test 1: Close when window is focused");

    // Open Notepad for testing
    match desktop.open_application("notepad") {
        Ok(notepad) => {
            println!("  ✅ Opened Notepad successfully");
            sleep(Duration::from_millis(1000));

            // Make sure the window is focused
            match notepad.focus() {
                Ok(()) => println!("  ✅ Focused Notepad window"),
                Err(e) => println!("  ❌ Failed to focus Notepad: {e}"),
            }
            sleep(Duration::from_millis(500));

            // Verify it's focused
            match notepad.is_focused() {
                Ok(is_focused) => println!("  📊 Window is focused: {is_focused}"),
                Err(e) => println!("  ❌ Failed to check focus state: {e}"),
            }

            // Attempt to close while focused
            match notepad.close() {
                Ok(()) => println!("  ✅ Successfully closed focused window"),
                Err(e) => println!("  ❌ Failed to close focused window: {e}"),
            }
        }
        Err(e) => println!("  ❌ Failed to open Notepad: {e}"),
    }

    sleep(Duration::from_millis(2000));

    // Test 2: Close when window is NOT focused
    println!("\n📋 Test 2: Close when window is NOT focused");

    // Open Notepad again
    match desktop.open_application("notepad") {
        Ok(notepad) => {
            println!("  ✅ Opened Notepad successfully");
            sleep(Duration::from_millis(1000));

            // Make sure we have another application to focus on
            match desktop.open_application("calc") {
                Ok(calc) => {
                    println!("  ✅ Opened Calculator to steal focus");
                    sleep(Duration::from_millis(1000));

                    // Focus on Calculator (so Notepad is not focused)
                    match calc.focus() {
                        Ok(()) => println!("  ✅ Focused Calculator window"),
                        Err(e) => println!("  ❌ Failed to focus Calculator: {e}"),
                    }
                    sleep(Duration::from_millis(500));

                    // Verify Notepad is NOT focused
                    match notepad.is_focused() {
                        Ok(is_focused) => {
                            println!("  📊 Notepad is focused: {is_focused}");
                            if is_focused {
                                println!("  ⚠️  Expected Notepad to NOT be focused, but it is");
                            }
                        }
                        Err(e) => println!("  ❌ Failed to check Notepad focus state: {e}"),
                    }

                    // Attempt to close Notepad while it's NOT focused
                    match notepad.close() {
                        Ok(()) => println!("  ✅ Successfully closed unfocused window"),
                        Err(e) => println!("  ❌ Failed to close unfocused window: {e}"),
                    }

                    // Clean up Calculator
                    match calc.close() {
                        Ok(()) => println!("  ✅ Cleaned up Calculator"),
                        Err(e) => println!("  ❌ Failed to cleanup Calculator: {e}"),
                    }
                }
                Err(e) => {
                    println!("  ❌ Failed to open Calculator: {e}");
                    // Still try to close Notepad
                    match notepad.close() {
                        Ok(()) => println!("  ✅ Closed Notepad (focus state unknown)"),
                        Err(e) => println!("  ❌ Failed to close Notepad: {e}"),
                    }
                }
            }
        }
        Err(e) => println!("  ❌ Failed to open Notepad: {e}"),
    }

    sleep(Duration::from_millis(2000));

    // Test 3: Test close mechanism details with Chrome
    println!("\n📋 Test 3: Test close mechanisms with Chrome");

    match desktop.open_application("chrome") {
        Ok(chrome) => {
            println!("  ✅ Opened Chrome successfully");
            sleep(Duration::from_millis(2000));

            // Test focused close
            match chrome.focus() {
                Ok(()) => println!("  ✅ Focused Chrome window"),
                Err(e) => println!("  ❌ Failed to focus Chrome: {e}"),
            }
            sleep(Duration::from_millis(500));

            match chrome.close() {
                Ok(()) => println!("  ✅ Successfully closed Chrome"),
                Err(e) => println!("  ❌ Failed to close Chrome: {e}"),
            }
        }
        Err(e) => println!("  ❌ Failed to open Chrome: {e}"),
    }

    sleep(Duration::from_millis(2000));

    // Test 4: Multiple close attempts (edge case)
    println!("\n📋 Test 4: Multiple close attempts");

    match desktop.open_application("notepad") {
        Ok(notepad) => {
            println!("  ✅ Opened Notepad for multiple close test");
            sleep(Duration::from_millis(1000));

            // First close attempt
            match notepad.close() {
                Ok(()) => println!("  ✅ First close attempt succeeded"),
                Err(e) => println!("  ❌ First close attempt failed: {e}"),
            }

            sleep(Duration::from_millis(1000));

            // Second close attempt (should fail gracefully)
            match notepad.close() {
                Ok(()) => println!("  ⚠️  Second close attempt succeeded (unexpected)"),
                Err(e) => println!("  ✅ Second close attempt failed as expected: {e}"),
            }
        }
        Err(e) => println!("  ❌ Failed to open Notepad: {e}"),
    }

    println!("\n🏁 Close functionality tests completed");
}

#[test]
#[ignore] // Run with: cargo test test_close_button_functionality -- --ignored --nocapture
fn test_close_button_functionality() {
    use std::thread::sleep;
    use std::time::Duration;

    println!("\n🔍 Testing close button vs window close functionality");

    let desktop = init_test_environment();

    // Test close button clicking vs window close
    match desktop.open_application("notepad") {
        Ok(notepad) => {
            println!("  ✅ Opened Notepad for close button test");
            sleep(Duration::from_millis(1000));

            // Get the window tree to find the close button
            match notepad.children() {
                Ok(children) => {
                    println!("  📊 Found {} children in Notepad window", children.len());

                    // Look for close button
                    let mut close_button_found = false;
                    for child in &children {
                        if let Some(name) = child.name() {
                            if name.to_lowercase().contains("close") {
                                println!("  🔍 Found potential close button: '{name}'");
                                close_button_found = true;

                                // Test clicking the close button instead of using window.close()
                                match child.click() {
                                    Ok(_) => println!("  ✅ Successfully clicked close button"),
                                    Err(e) => println!("  ❌ Failed to click close button: {e}"),
                                }
                                break;
                            }
                        }
                    }

                    if !close_button_found {
                        println!("  ⚠️  No close button found, falling back to window.close()");
                        match notepad.close() {
                            Ok(()) => println!("  ✅ Successfully closed using window.close()"),
                            Err(e) => println!("  ❌ Failed to close using window.close(): {e}"),
                        }
                    }
                }
                Err(e) => {
                    println!("  ❌ Failed to get children: {e}");
                    // Fallback to normal close
                    match notepad.close() {
                        Ok(()) => println!("  ✅ Fallback close succeeded"),
                        Err(e) => println!("  ❌ Fallback close failed: {e}"),
                    }
                }
            }
        }
        Err(e) => println!("  ❌ Failed to open Notepad: {e}"),
    }

    println!("\n🏁 Close button functionality tests completed");
}