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
use super::windows::*;
use crate::platforms::AccessibilityEngine;
use std::process;
use std::time::Instant;

#[test]
fn test_get_process_name_by_pid_current_process() {
    // Test with the current process PID
    let current_pid = process::id() as i32;
    let result = get_process_name_by_pid(current_pid);

    assert!(result.is_ok(), "Should be able to get current process name");
    let process_name = result.unwrap();

    // The process name should be a valid non-empty string
    assert!(!process_name.is_empty(), "Process name should not be empty");

    // Should not contain .exe extension
    assert!(
        !process_name.ends_with(".exe"),
        "Process name should not contain .exe extension"
    );
    assert!(
        !process_name.ends_with(".EXE"),
        "Process name should not contain .EXE extension"
    );

    // Should be a reasonable process name (alphanumeric, hyphens, underscores)
    assert!(
        process_name
            .chars()
            .all(|c| c.is_alphanumeric() || c == '-' || c == '_'),
        "Process name should contain only alphanumeric characters, hyphens, and underscores: {process_name}"
    );

    println!("Current process name: {process_name}");
}

#[test]
fn test_tree_building_performance_stress_test() {
    // This test is more intensive and can be used to identify performance bottlenecks
    let engine = match WindowsEngine::new(false, false) {
        Ok(engine) => engine,
        Err(_) => {
            println!("Cannot create WindowsEngine, skipping stress test");
            return;
        }
    };

    // Get all applications for a larger test
    let applications = match engine.get_applications() {
        Ok(apps) => apps,
        Err(_) => {
            println!("Cannot get applications, using root element");
            return;
        }
    };

    if applications.is_empty() {
        println!("No applications available, using root element for stress test");
        return;
    }

    // Use the first application with more elements allowed
    let app = &applications[0];

    println!(
        "Starting stress test with application: {:?}",
        app.attributes().name
    );

    let start_time = Instant::now();

    // Try to get a window tree first to see what we're dealing with
    let config = crate::platforms::TreeBuildConfig::default();
    match engine.get_window_tree(
        app.process_id().unwrap_or(0),
        app.attributes().name.as_deref(),
        config,
    ) {
        Ok(tree) => {
            let total_time = start_time.elapsed();

            // Count elements in the tree
            let element_count = count_tree_elements(&tree);
            let tree_depth = calculate_tree_depth(&tree);

            println!("=== Stress Test Results ===");
            println!("Tree building time: {total_time:?}");
            println!("Total elements in tree: {element_count}");
            println!("Tree depth: {tree_depth}");
            println!(
                "Elements per second: {:.2}",
                element_count as f64 / total_time.as_secs_f64()
            );

            // Performance assertions

            // Don't make the test too strict, but it shouldn't take forever
            if total_time > std::time::Duration::from_secs(30) {
                println!("Warning: Tree building took longer than expected: {total_time:?}");
            }
        }
        Err(e) => {
            println!("Tree building failed in stress test: {e}");
            // Don't fail the test, just log the issue
        }
    }
}

fn count_tree_elements(node: &crate::UINode) -> usize {
    1 + node.children.iter().map(count_tree_elements).sum::<usize>()
}

fn calculate_tree_depth(node: &crate::UINode) -> usize {
    if node.children.is_empty() {
        1
    } else {
        1 + node
            .children
            .iter()
            .map(calculate_tree_depth)
            .max()
            .unwrap_or(0)
    }
}

#[test]
fn test_get_process_name_by_pid_invalid_pid() {
    // Test with an invalid PID
    let result = get_process_name_by_pid(-1);
    assert!(result.is_err(), "Should fail for invalid PID");

    // Test with a PID that likely doesn't exist (very high number)
    let result = get_process_name_by_pid(999999);
    assert!(result.is_err(), "Should fail for non-existent PID");
}

#[test]
fn test_get_process_name_by_pid_system_process() {
    // Test with system processes that should exist
    let system_pids = vec![0, 4]; // System Idle Process and System

    for pid in system_pids {
        match get_process_name_by_pid(pid) {
            Ok(name) => {
                println!("System process {pid}: {name}");
                assert!(!name.is_empty(), "System process name should not be empty");
            }
            Err(e) => {
                println!("Could not get name for system process {pid}: {e}");
                // Don't fail the test as access might be restricted
            }
        }
    }
}

#[test]
fn test_open_regular_application() {
    let engine = match WindowsEngine::new(false, false) {
        Ok(engine) => engine,
        Err(_) => {
            println!("Cannot create WindowsEngine, skipping application test");
            return;
        }
    };

    // Test with common Windows applications
    let test_apps = vec!["notepad", "calc", "mspaint"];

    for app_name in test_apps {
        println!("Testing application opening: {app_name}");

        match engine.open_application(app_name) {
            Ok(app_element) => {
                println!("Successfully opened {app_name}");
                let attrs = app_element.attributes();
                println!(
                    "App attributes - Role: {}, Name: {:?}",
                    attrs.role, attrs.name
                );

                // Basic validation
                assert!(!attrs.role.is_empty(), "Application should have a role");

                // Clean up - try to close the application
                let _ = app_element.press_key("Alt+F4");
            }
            Err(e) => {
                println!("Could not open {app_name}: {e} (this might be expected)");
            }
        }
    }
}

#[test]
#[ignore]
fn test_open_uwp_application() {
    let engine = match WindowsEngine::new(false, false) {
        Ok(engine) => engine,
        Err(_) => {
            println!("Cannot create WindowsEngine, skipping UWP test");
            return;
        }
    };

    // Test with common UWP applications
    let test_apps = vec!["Microsoft Store", "Settings", "Photos"];

    for app_name in test_apps {
        println!("Testing UWP application opening: {app_name}");

        match engine.open_application(app_name) {
            Ok(app_element) => {
                println!("Successfully opened UWP app {app_name}");
                let attrs = app_element.attributes();
                println!(
                    "UWP app attributes - Role: {}, Name: {:?}",
                    attrs.role, attrs.name
                );

                // Basic validation
                assert!(!attrs.role.is_empty(), "UWP application should have a role");

                // Clean up
                let _ = app_element.press_key("Alt+F4");
            }
            Err(e) => {
                println!("Could not open UWP app {app_name}: {e} (this might be expected)");
            }
        }
    }
}

#[test]
fn test_browser_title_matching() {
    // Test the extract_browser_info function
    let (is_browser, parts) = WindowsEngine::extract_browser_info(
        "MailTracker: Email tracker for Gmail - Chrome Web Store - Google Chrome",
    );

    assert!(is_browser, "Should detect as browser title");
    assert!(
        parts.len() >= 2,
        "Should split browser title into parts: {parts:?}"
    );

    // Should contain both the page title and the browser name
    let parts_str = parts.join(" ");
    assert!(
        parts_str.to_lowercase().contains("mailtracker"),
        "Should contain page title"
    );
    assert!(
        parts_str.to_lowercase().contains("chrome"),
        "Should contain browser name"
    );

    // Test similarity calculation
    let similarity = WindowsEngine::calculate_similarity(
        "Chrome Web Store - Google Chrome",
        "MailTracker: Email tracker for Gmail - Chrome Web Store - Google Chrome",
    );

    assert!(
        similarity > 0.3,
        "Should have reasonable similarity: {similarity}"
    );

    println!("Browser title parts: {parts:?}");
    println!("Similarity score: {similarity:.2}");
}

#[test]
fn test_browser_title_matching_edge_cases() {
    // Test various browser title formats
    let test_cases = vec![
        ("Tab Title - Google Chrome", true),
        ("Mozilla Firefox", true),
        ("Microsoft Edge", true),
        ("Some App - Not Application", false), // Changed to avoid "browser" word
        ("Chrome Web Store - Google Chrome", true),
        ("GitHub - Google Chrome", true),
        ("Random Window Title", false),
    ];

    for (title, expected_is_browser) in test_cases {
        let (is_browser, parts) = WindowsEngine::extract_browser_info(title);
        assert_eq!(
            is_browser, expected_is_browser,
            "Browser detection failed for: '{title}', expected: {expected_is_browser}, got: {is_browser}"
        );

        if is_browser {
            assert!(
                !parts.is_empty(),
                "Browser title should have parts: '{title}'"
            );
        }
    }
}

#[test]
fn test_similarity_calculation_edge_cases() {
    let test_cases = vec![
        ("identical", "identical", 1.0),
        ("Longer String", "Long", 0.3), // More realistic expected value
        ("Chrome Web Store", "MailTracker Chrome Web Store", 0.4), // More realistic
        ("completely different", "nothing similar", 0.0),
        ("", "empty test", 0.0),
        ("single", "", 0.0),
    ];

    for (text1, text2, min_expected) in test_cases {
        let similarity = WindowsEngine::calculate_similarity(text1, text2);

        if min_expected == 1.0 {
            assert_eq!(
                similarity, 1.0,
                "Identical strings should have similarity 1.0"
            );
        } else if min_expected == 0.0 {
            assert_eq!(
                similarity, 0.0,
                "Completely different strings should have similarity 0.0"
            );
        } else {
            assert!(
                similarity >= min_expected - 0.2 && similarity <= 1.0,
                "Similarity for '{text1}' vs '{text2}' should be around {min_expected}, got: {similarity:.2}"
            );
        }

        println!("'{text1}' vs '{text2}' = {similarity:.2}");
    }
}

#[test]
fn test_find_best_title_match_browser_scenario() {
    // Mock window data based on the actual log
    // Expected: "MailTracker: Email tracker for Gmail - Chrome Web Store - Google Chrome"
    // Available: "Chrome Web Store - Google Chrome"

    // We can't create actual UIElements for testing, but we can test our logic
    let target_title = "MailTracker: Email tracker for Gmail - Chrome Web Store - Google Chrome";
    let available_window_name = "Chrome Web Store - Google Chrome";

    // Test the individual components
    let (is_target_browser, target_parts) = WindowsEngine::extract_browser_info(target_title);
    let (is_window_browser, window_parts) =
        WindowsEngine::extract_browser_info(available_window_name);

    assert!(is_target_browser, "Target should be detected as browser");
    assert!(is_window_browser, "Window should be detected as browser");

    println!("Target parts: {target_parts:?}");
    println!("Window parts: {window_parts:?}");

    // Test similarity between parts
    let mut max_similarity = 0.0f64;
    for target_part in &target_parts {
        for window_part in &window_parts {
            let similarity = WindowsEngine::calculate_similarity(target_part, window_part);
            max_similarity = max_similarity.max(similarity);
            println!("'{target_part}' vs '{window_part}' = {similarity:.2}");
        }
    }

    // Should find a good match since both contain "Chrome Web Store - Google Chrome"
    assert!(
        max_similarity > 0.6,
        "Should find good similarity between browser titles, got: {max_similarity:.2}"
    );
}

#[test]
fn test_enhanced_error_messages() {
    // Test that browser error messages provide helpful suggestions
    let target_title = "MailTracker: Email tracker for Gmail - Chrome Web Store - Google Chrome";
    let available_windows = [
        "Taskbar".to_string(),
        "Chrome Web Store - Google Chrome".to_string(),
        "Firefox - Mozilla Firefox".to_string(),
        "Random Application".to_string(),
    ];

    let (is_target_browser, _) = WindowsEngine::extract_browser_info(target_title);
    assert!(is_target_browser, "Target should be browser");

    let browser_windows: Vec<&String> = available_windows
        .iter()
        .filter(|name| {
            let (is_browser, _) = WindowsEngine::extract_browser_info(name);
            is_browser
        })
        .collect();

    assert!(
        !browser_windows.is_empty(),
        "Should find browser windows in the list"
    );
    assert!(
        browser_windows.len() >= 2,
        "Should find multiple browser windows: {browser_windows:?}"
    );

    // Verify the specific windows we expect
    assert!(
        browser_windows.iter().any(|w| w.contains("Chrome")),
        "Should find Chrome window"
    );
    assert!(
        browser_windows.iter().any(|w| w.contains("Firefox")),
        "Should find Firefox window"
    );
}

#[test]
fn test_unified_tree_api_with_config() {
    let engine = match WindowsEngine::new(false, false) {
        Ok(engine) => engine,
        Err(_) => {
            println!("Cannot create WindowsEngine, skipping unified API test");
            return;
        }
    };

    // Open Calculator for testing
    let app = match engine.open_application("calc") {
        Ok(app) => app,
        Err(e) => {
            println!("Cannot open Calculator: {e}, skipping test");
            return;
        }
    };

    let pid = app.process_id().unwrap_or(0);
    let title_string = app.attributes().name;
    let title = title_string.as_deref();

    // Test with Fast property loading mode
    let fast_config = crate::platforms::TreeBuildConfig {
        property_mode: crate::platforms::PropertyLoadingMode::Fast,
        timeout_per_operation_ms: Some(50),
        yield_every_n_elements: Some(50),
        batch_size: Some(50),
        max_depth: None,
        include_all_bounds: false,
        ui_settle_delay_ms: None,
        format_output: false,
        show_overlay: false,
        overlay_display_mode: None,
        from_selector: None,
    };

    let start_fast = std::time::Instant::now();
    let fast_result = engine.get_window_tree(pid, title, fast_config);
    let fast_duration = start_fast.elapsed();

    // Test with Full property loading mode
    let full_config = crate::platforms::TreeBuildConfig {
        property_mode: crate::platforms::PropertyLoadingMode::Complete,
        timeout_per_operation_ms: Some(100),
        yield_every_n_elements: Some(25),
        batch_size: Some(25),
        max_depth: None,
        include_all_bounds: false,
        ui_settle_delay_ms: None,
        format_output: false,
        show_overlay: false,
        overlay_display_mode: None,
        from_selector: None,
    };

    let start_full = std::time::Instant::now();
    let full_result = engine.get_window_tree(pid, title, full_config);
    let full_duration = start_full.elapsed();

    // Test with default config
    let default_config = crate::platforms::TreeBuildConfig::default();
    let start_default = std::time::Instant::now();
    let default_result = engine.get_window_tree(pid, title, default_config);
    let default_duration = start_default.elapsed();

    // Verify all configurations work
    assert!(
        fast_result.is_ok(),
        "Fast config should work: {:?}",
        fast_result.err()
    );
    assert!(
        full_result.is_ok(),
        "Full config should work: {:?}",
        full_result.err()
    );
    assert!(
        default_result.is_ok(),
        "Default config should work: {:?}",
        default_result.err()
    );

    // Fast mode should generally be faster than full mode
    println!("Fast mode: {fast_duration:?}");
    println!("Full mode: {full_duration:?}");
    println!("Default mode: {default_duration:?}");

    // Count elements to ensure we get reasonable results
    if let (Ok(fast_tree), Ok(full_tree), Ok(default_tree)) =
        (&fast_result, &full_result, &default_result)
    {
        let fast_count = count_tree_elements(fast_tree);
        let full_count = count_tree_elements(full_tree);
        let default_count = count_tree_elements(default_tree);

        println!("Fast mode elements: {fast_count}");
        println!("Full mode elements: {full_count}");
        println!("Default mode elements: {default_count}");

        // All modes should find the same number of elements
        assert_eq!(
            fast_count, full_count,
            "Fast and full modes should find same number of elements"
        );
        assert_eq!(
            fast_count, default_count,
            "Fast and default modes should find same number of elements"
        );
        assert!(fast_count > 0, "Should find at least some elements");
    }

    // Clean up
    let _ = app.close();
}

#[test]
#[ignore] // does not work in ci cd it seems
fn test_window_transparency() {
    let engine = WindowsEngine::new(false, true).unwrap();
    let notepad = engine.open_application("notepad.exe").unwrap();

    // Fade out effect
    for percentage in (0..=100).rev().step_by(5) {
        notepad.set_transparency(percentage as u8).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(20));
    }

    // Fade in effect
    for percentage in (0..=100).step_by(5) {
        notepad.set_transparency(percentage as u8).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(20));
    }

    // Final fade to fully opaque
    notepad.set_transparency(100).unwrap();

    // Clean up
    notepad.close().unwrap();
}