createContext.https.any.js (2514B)
1 // META: title=validation tests for WebNN API createContext() 2 // META: global=window,worker 3 // META: variant=?cpu 4 // META: variant=?gpu 5 // META: variant=?npu 6 // META: script=../resources/utils_validation.js 7 8 'use strict'; 9 10 promise_test(async t => { 11 const ml_context_options = {}; 12 await navigator.ml.createContext(ml_context_options); 13 }, 'Create context with default MLContextOptions.'); 14 15 promise_test(async t => { 16 const ml_context_options = { deviceType: 'cpu' }; 17 await navigator.ml.createContext(ml_context_options); 18 }, 'Create context with device type: cpu.'); 19 20 promise_test(async t => { 21 const ml_context_options = { deviceType: 'xpu' }; 22 const promise = navigator.ml.createContext(ml_context_options); 23 promise_rejects_js(t, TypeError, promise); 24 }, 'Throw if deviceType is not a valid enum value of type MLDeviceType when creating the context.'); 25 26 promise_test(async t => { 27 const ml_context_options = { powerPreference: 'default' }; 28 await navigator.ml.createContext(ml_context_options); 29 }, 'Create context with power preference: default.'); 30 31 promise_test(async t => { 32 const ml_context_options = { powerPreference: 'high-performance' }; 33 await navigator.ml.createContext(ml_context_options); 34 }, 'Create context with power preference: high-performance.'); 35 36 promise_test(async t => { 37 const ml_context_options = { powerPreference: 'low-power' }; 38 await navigator.ml.createContext(ml_context_options); 39 }, 'Create context with power preference: low-power.'); 40 41 promise_test(async t => { 42 const ml_context_options = { powerPreference: 'auto' }; 43 const promise = navigator.ml.createContext(ml_context_options); 44 promise_rejects_js(t, TypeError, promise); 45 }, 'Throw if powerPreference is not a valid enum value of type MLPowerPreference when creating the context.'); 46 47 promise_test(async t => { 48 const ml_context_options = { deviceType: 'cpu', powerPreference: 'high-performance' }; 49 await navigator.ml.createContext(ml_context_options); 50 }, '[createContext] Test creating context with deviceType=cpu, powerPreference=high-performance.'); 51 52 promise_test(async t => { 53 // Skip the test if WebGPU or an adapter/device is not available. 54 if (!navigator.gpu) { 55 return; 56 } 57 const adapter = await navigator.gpu.requestAdapter(); 58 if (!adapter) { 59 return; 60 } 61 const device = await adapter.requestDevice(); 62 if (!device) { 63 return; 64 } 65 66 await navigator.ml.createContext(device); 67 }, 'Create context with GPUDevice.');