viewpoint-core 0.2.15

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
#![cfg(feature = "integration")]

//! Device emulation context tests for viewpoint-core.
//!
//! These tests verify device emulation at context level including
//! 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 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>
"#;

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>
"#;

// =============================================================================
// 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");
}