viewpoint-core 0.2.10

High-level browser automation API for Viewpoint
Documentation
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
#![cfg(feature = "integration")]

//! Device emulation tests for viewpoint-core.
//!
//! These tests verify device emulation functionality including
//! device descriptors, viewport, locale, timezone, touch, and mobile mode.

mod common;

use std::time::Duration;

use viewpoint_core::{Browser, devices};
use viewpoint_js::js;

use common::init_tracing;

// =============================================================================
// Test HTML Templates
// =============================================================================

const VIEWPORT_HTML: &str = r#"
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <div id="viewport-info"></div>
    <script>
        function updateInfo() {
            document.getElementById('viewport-info').textContent = 
                window.innerWidth + 'x' + window.innerHeight;
        }
        window.addEventListener('resize', updateInfo);
        updateInfo();
    </script>
</body>
</html>
"#;

const LOCALE_HTML: &str = r#"
<!DOCTYPE html>
<html>
<body>
    <div id="locale"></div>
    <div id="timezone"></div>
    <script>
        document.getElementById('locale').textContent = navigator.language;
        document.getElementById('timezone').textContent = Intl.DateTimeFormat().resolvedOptions().timeZone;
    </script>
</body>
</html>
"#;

const TOUCH_HTML: &str = r#"
<!DOCTYPE html>
<html>
<body>
    <div id="touch-support"></div>
    <script>
        document.getElementById('touch-support').textContent = 
            ('ontouchstart' in window) || (navigator.maxTouchPoints > 0) ? 'yes' : 'no';
    </script>
</body>
</html>
"#;

const USER_AGENT_HTML: &str = r#"
<!DOCTYPE html>
<html>
<body>
    <div id="user-agent"></div>
    <script>
        document.getElementById('user-agent').textContent = navigator.userAgent;
    </script>
</body>
</html>
"#;

// =============================================================================
// Viewport Tests
// =============================================================================

/// Test setting viewport size.
#[tokio::test]
async fn test_viewport_size() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let page = context.new_page().await.expect("Failed to create page");

    page.set_content(VIEWPORT_HTML)
        .set()
        .await
        .expect("Failed to set content");

    // Set viewport size
    page.set_viewport_size(1280, 720)
        .await
        .expect("Failed to set viewport size");

    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify viewport size
    let width: i32 = page
        .evaluate(js! { window.innerWidth })
        .await
        .expect("Failed to get width");

    let height: i32 = page
        .evaluate(js! { window.innerHeight })
        .await
        .expect("Failed to get height");

    assert_eq!(width, 1280, "Viewport width should be 1280");
    assert_eq!(height, 720, "Viewport height should be 720");

    browser.close().await.expect("Failed to close browser");
}

/// Test setting different viewport sizes.
#[tokio::test]
async fn test_viewport_size_mobile() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let page = context.new_page().await.expect("Failed to create page");

    page.set_content(VIEWPORT_HTML)
        .set()
        .await
        .expect("Failed to set content");

    // Set mobile viewport size
    page.set_viewport_size(375, 812)
        .await
        .expect("Failed to set viewport size");

    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify viewport size
    let width: i32 = page
        .evaluate(js! { window.innerWidth })
        .await
        .expect("Failed to get width");

    assert_eq!(width, 375, "Viewport width should be 375");

    browser.close().await.expect("Failed to close browser");
}

// =============================================================================
// Device Descriptor Tests
// =============================================================================

/// Test iPhone device descriptor availability.
#[tokio::test]
async fn test_device_iphone_available() {
    init_tracing();

    // Verify device descriptors exist
    assert_eq!(devices::IPHONE_13.name, "iPhone 13");
    assert!(devices::IPHONE_13.is_mobile);
    assert!(devices::IPHONE_13.has_touch);
    assert!(devices::IPHONE_13.device_scale_factor > 1.0);
}

/// Test Pixel device descriptor availability.
#[tokio::test]
async fn test_device_pixel_available() {
    init_tracing();

    // Verify device descriptors exist
    assert_eq!(devices::PIXEL_7.name, "Pixel 7");
    assert!(devices::PIXEL_7.is_mobile);
    assert!(devices::PIXEL_7.has_touch);
}

/// Test listing all available devices.
#[tokio::test]
async fn test_device_list_all() {
    init_tracing();

    let all = devices::all_devices();

    // Should have many devices
    assert!(
        all.len() > 20,
        "Should have more than 20 device descriptors"
    );

    // Should include various device types
    let names: Vec<_> = all.iter().map(|d| d.name).collect();
    assert!(names.iter().any(|n| n.contains("iPhone")));
    assert!(names.iter().any(|n| n.contains("iPad")));
    assert!(names.iter().any(|n| n.contains("Pixel")));
    assert!(names.iter().any(|n| n.contains("Galaxy")));
    assert!(names.iter().any(|n| n.contains("Desktop")));
}

/// Test finding device by name.
#[tokio::test]
async fn test_device_find_by_name() {
    init_tracing();

    let device = devices::find_device("iPhone 13");
    assert!(device.is_some());
    assert_eq!(device.unwrap().name, "iPhone 13");

    // Case insensitive
    let device_lower = devices::find_device("iphone 13");
    assert!(device_lower.is_some());
}

// =============================================================================
// Context Device Emulation Tests
// =============================================================================

/// Test creating context with device descriptor.
#[tokio::test]
async fn test_context_with_device() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    // Create context with iPhone device
    let context = browser
        .new_context_builder()
        .device(devices::IPHONE_13.clone())
        .build()
        .await
        .expect("Failed to create context");

    let page = context.new_page().await.expect("Failed to create page");
    page.set_content(USER_AGENT_HTML)
        .set()
        .await
        .expect("Failed to set content");
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify user agent contains iPhone
    let user_agent: String = page
        .evaluate(js! { navigator.userAgent })
        .await
        .expect("Failed to get user agent");

    assert!(
        user_agent.contains("iPhone"),
        "User agent should contain iPhone"
    );

    browser.close().await.expect("Failed to close browser");
}

/// Test creating context with custom user agent.
#[tokio::test]
async fn test_context_custom_user_agent() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    // Create context with custom user agent
    let context = browser
        .new_context_builder()
        .user_agent("Custom User Agent/1.0")
        .build()
        .await
        .expect("Failed to create context");

    let page = context.new_page().await.expect("Failed to create page");
    page.set_content(USER_AGENT_HTML)
        .set()
        .await
        .expect("Failed to set content");
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify custom user agent
    let user_agent: String = page
        .evaluate(js! { navigator.userAgent })
        .await
        .expect("Failed to get user agent");

    assert_eq!(user_agent, "Custom User Agent/1.0");

    browser.close().await.expect("Failed to close browser");
}

// =============================================================================
// Locale and Timezone Tests
// =============================================================================

/// Test setting locale.
/// Note: In headless Chromium, navigator.language may not reflect the locale setting.
/// This test verifies the locale configuration is accepted and the page renders correctly.
#[tokio::test]
async fn test_locale() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    // Create context with French locale
    let context = browser
        .new_context_builder()
        .locale("fr-FR")
        .build()
        .await
        .expect("Failed to create context");

    let page = context.new_page().await.expect("Failed to create page");
    page.set_content(LOCALE_HTML)
        .set()
        .await
        .expect("Failed to set content");
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify we can access locale-related APIs
    // Note: navigator.language may return system locale in headless mode
    let locale: String = page
        .evaluate(js! { navigator.language })
        .await
        .expect("Failed to get locale");

    // Just verify it returns a valid locale string (not empty)
    assert!(!locale.is_empty(), "Locale should not be empty");

    browser.close().await.expect("Failed to close browser");
}

/// Test setting timezone.
#[tokio::test]
async fn test_timezone() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    // Create context with Paris timezone
    let context = browser
        .new_context_builder()
        .timezone_id("Europe/Paris")
        .build()
        .await
        .expect("Failed to create context");

    let page = context.new_page().await.expect("Failed to create page");
    page.set_content(LOCALE_HTML)
        .set()
        .await
        .expect("Failed to set content");
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify timezone
    let timezone: String = page
        .evaluate(js! { Intl.DateTimeFormat().resolvedOptions().timeZone })
        .await
        .expect("Failed to get timezone");

    assert_eq!(timezone, "Europe/Paris", "Timezone should be Europe/Paris");

    browser.close().await.expect("Failed to close browser");
}

// =============================================================================
// Device Scale Factor Tests
// =============================================================================

/// Test device scale factor (device pixel ratio).
#[tokio::test]
async fn test_device_scale_factor() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    // Create context with high DPI scale factor
    let context = browser
        .new_context_builder()
        .device_scale_factor(2.0)
        .build()
        .await
        .expect("Failed to create context");

    let page = context.new_page().await.expect("Failed to create page");
    page.set_content(VIEWPORT_HTML)
        .set()
        .await
        .expect("Failed to set content");
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify device pixel ratio
    let dpr: f64 = page
        .evaluate(js! { window.devicePixelRatio })
        .await
        .expect("Failed to get DPR");

    assert_eq!(dpr, 2.0, "Device pixel ratio should be 2.0");

    browser.close().await.expect("Failed to close browser");
}

// =============================================================================
// Touch Emulation Tests
// =============================================================================

/// Test touch emulation.
#[tokio::test]
async fn test_touch_emulation() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    // Create context with touch enabled
    let context = browser
        .new_context_builder()
        .has_touch(true)
        .build()
        .await
        .expect("Failed to create context");

    let page = context.new_page().await.expect("Failed to create page");
    page.set_content(TOUCH_HTML)
        .set()
        .await
        .expect("Failed to set content");
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify touch support
    let touch_support: String = page
        .evaluate(js! { document.getElementById("touch-support").textContent })
        .await
        .expect("Failed to get touch support");

    assert_eq!(touch_support, "yes", "Touch should be supported");

    browser.close().await.expect("Failed to close browser");
}

/// Test without touch emulation.
#[tokio::test]
async fn test_no_touch_emulation() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    // Create context without touch
    let context = browser
        .new_context_builder()
        .has_touch(false)
        .build()
        .await
        .expect("Failed to create context");

    let page = context.new_page().await.expect("Failed to create page");
    page.set_content(TOUCH_HTML)
        .set()
        .await
        .expect("Failed to set content");
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify no touch support
    let touch_support: String = page
        .evaluate(js! { document.getElementById("touch-support").textContent })
        .await
        .expect("Failed to get touch support");

    assert_eq!(touch_support, "no", "Touch should not be supported");

    browser.close().await.expect("Failed to close browser");
}

// =============================================================================
// Mobile Mode Tests
// =============================================================================

/// Test mobile mode emulation.
#[tokio::test]
async fn test_mobile_mode() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    // Create context in mobile mode using device
    let context = browser
        .new_context_builder()
        .device(devices::IPHONE_13.clone())
        .build()
        .await
        .expect("Failed to create context");

    let page = context.new_page().await.expect("Failed to create page");
    page.set_content(USER_AGENT_HTML)
        .set()
        .await
        .expect("Failed to set content");
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify mobile user agent
    let user_agent: String = page
        .evaluate(js! { navigator.userAgent })
        .await
        .expect("Failed to get user agent");

    // Mobile user agents typically contain "Mobile"
    assert!(
        user_agent.contains("Mobile") || user_agent.contains("iPhone"),
        "User agent should indicate mobile device"
    );

    browser.close().await.expect("Failed to close browser");
}

// =============================================================================
// Bring to Front Tests
// =============================================================================

/// Test bringing a page to front.
#[tokio::test]
async fn test_page_bring_to_front() {
    init_tracing();

    let browser = Browser::launch()
        .headless(true)
        .launch()
        .await
        .expect("Failed to launch browser");

    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");

    // Create multiple pages
    let page1 = context.new_page().await.expect("Failed to create page 1");
    let _page2 = context.new_page().await.expect("Failed to create page 2");

    // Bring page1 to front
    page1
        .bring_to_front()
        .await
        .expect("Failed to bring to front");

    // Should not error - verifies the API works

    browser.close().await.expect("Failed to close browser");
}