Automatic update from web-platform-tests WebMCP: Remove navigator.modelContext This CL is a follow-up to https://crrev.com/c/7867800. It removes navigator.modelContext in favor of document.modelContext. R=khushalsagar Bug: 489045948 Change-Id: I1a4636afc7b0585412ccc3fabe9369148cd9c3cd Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8062992 Reviewed-by: Khushal Sagar <khushalsagar@chromium.org> Auto-Submit: Dominic Farolino <dom@chromium.org> Commit-Queue: Khushal Sagar <khushalsagar@chromium.org> Cr-Commit-Position: refs/heads/main@{#1659793} -- wpt-commits: 546a309f0e765b577b4f1b8ee60208e5004045ed wpt-pr: 61186
130 lines
4.4 KiB
HTML
130 lines
4.4 KiB
HTML
<!DOCTYPE html>
|
|
<meta charset="utf-8">
|
|
<title>WebMCP: toolchange event and getTools() update on form attribute mutation</title>
|
|
<link rel="author" href="mailto:dom@chromium.org">
|
|
<script src="/resources/testharness.js"></script>
|
|
<script src="/resources/testharnessreport.js"></script>
|
|
<script src="../resources/helpers.js"></script>
|
|
<body>
|
|
|
|
<form id="f1" toolname="my_tool" tooltitle="My Title" tooldescription="desc">
|
|
<input id="input1" type="text" name="input_name">
|
|
</form>
|
|
|
|
<script>
|
|
function waitForToolChange() {
|
|
return new Promise(resolve => {
|
|
document.modelContext.addEventListener('toolchange', resolve, { once: true });
|
|
});
|
|
}
|
|
|
|
promise_test(async t => {
|
|
const form = document.getElementById('f1');
|
|
|
|
// Wait for initial registration to complete.
|
|
const tool = await waitForTool('my_tool');
|
|
assert_equals(tool.name, 'my_tool');
|
|
assert_equals(tool.title, 'My Title');
|
|
assert_equals(tool.description, 'desc');
|
|
|
|
// Test updating tooltitle attribute
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.setAttribute('tooltitle', 'New Title');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 1, "Still one tool");
|
|
assert_equals(tools[0].title, 'New Title', "Title is updated");
|
|
}
|
|
|
|
// Test updating tooldescription attribute
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.setAttribute('tooldescription', 'New Description');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 1);
|
|
assert_equals(tools[0].description, 'New Description', "Description is updated");
|
|
}
|
|
|
|
// Test updating toolname attribute
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.setAttribute('toolname', 'new_tool_name');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 1);
|
|
assert_equals(tools[0].name, 'new_tool_name', "Name is updated");
|
|
}
|
|
|
|
// Test removing tooltitle attribute (should fall back to empty string)
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.removeAttribute('tooltitle');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 1);
|
|
assert_equals(tools[0].title, '', "Title should fall back to empty string");
|
|
}
|
|
|
|
// Test removing tooldescription attribute (should unregister the tool since it's required)
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.removeAttribute('tooldescription');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 0, "Tool should be unregistered when description is removed");
|
|
}
|
|
|
|
// Restore the description attribute to re-register the tool
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.setAttribute('tooldescription', 'Restored Description');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 1);
|
|
assert_equals(tools[0].name, 'new_tool_name');
|
|
assert_equals(tools[0].description, 'Restored Description');
|
|
}
|
|
|
|
// Test adding toolautosubmit attribute
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.setAttribute('toolautosubmit', '');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 1, "Tool still registered after adding toolautosubmit");
|
|
assert_equals(tools[0].name, 'new_tool_name', "Tool name is still the same");
|
|
}
|
|
|
|
// Test removing toolautosubmit attribute
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.removeAttribute('toolautosubmit');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 1, "Tool still registered after removing toolautosubmit");
|
|
assert_equals(tools[0].name, 'new_tool_name', "Tool name is still the same");
|
|
}
|
|
|
|
// Test removing toolname attribute (should unregister the tool since it's required)
|
|
{
|
|
const changePromise = waitForToolChange();
|
|
form.removeAttribute('toolname');
|
|
await changePromise;
|
|
|
|
let tools = await document.modelContext.getTools();
|
|
assert_equals(tools.length, 0, "Tool should be unregistered when name is removed");
|
|
}
|
|
}, "Test that mutating or removing form attributes (toolname, tooldescription, tooltitle, toolautosubmit) fires toolchange and updates getTools() correctly");
|
|
</script>
|
|
</body>
|