Commit 27412245 authored by Zufeng Wang's avatar Zufeng Wang Committed by Commit Bot

Help app: Implement clearSearchIndex and make mock app use LSS

This change lets the inner frame of the help app clear the search index.

Make the mock app add test data to the local search service similar to
the chrome branded build.

Bug: b/172018145
Change-Id: Ief94524c63b70a969500e94ece1b9e1aad811c15
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2513806
Commit-Queue: Zufeng Wang <zufeng@google.com>
Reviewed-by: default avatarRachel Carpenter <carpenterr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823807}
parent 23a792c8
......@@ -109,7 +109,7 @@ guestMessagePipe.registerHandler(Message.CLEAR_SEARCH_INDEX, async () => {
if (!(await isLssEnabled)) {
return;
}
// TODO(b/166047521): Clear the index when that method is available.
return indexRemote.clearIndex();
});
guestMessagePipe.registerHandler(
......
......@@ -20,12 +20,35 @@ class ShowoffApp extends HTMLElement {
/** @override */
setDelegate(delegate) {
this.delegate = delegate;
// Note: This is intended to mimic how the real app initializes the search
// index once on startup. But the real app does this in firstUpdated, not
// setDelegate.
this.initSearchIndex();
}
/** @override */
getDelegate() {
return /** @type {!helpApp.ClientApiDelegate} */ (this.delegate);
}
/**
* Mimics the way the real app initializes the search index. Adds one fake
* search result.
*/
async initSearchIndex() {
await this.delegate.clearSearchIndex();
await this.delegate.addOrUpdateSearchIndex([
{
id: 'mock-app-test-id',
title: 'Get help with Chrome',
body: 'Test body',
mainCategoryName: 'Help',
locale: 'en-US',
}
]);
this.toggleAttribute('loaded', true);
}
}
window.customElements.define('showoff-app', ShowoffApp);
......
......@@ -33,9 +33,7 @@ const DELEGATE = {
Message.ADD_OR_UPDATE_SEARCH_INDEX, data);
},
async clearSearchIndex() {
// TODO(b/166047521): Send the message when clear search index has been
// implemented. the index when that method is available.
return;
await parentMessagePipe.sendMessage(Message.CLEAR_SEARCH_INDEX);
},
/**
* @override
......
......@@ -9,11 +9,32 @@ GUEST_TEST('GuestHasLang', () => {
assertEquals(document.documentElement.lang, 'en-US');
});
// Test that search works when called from the guest frame, and it works for
// searchable items with and without subheadings.
GUEST_TEST('GuestCanSearchWithHeadings', async () => {
/**
* Waits for the app's initial index update to complete. This prevents it from
* interfering with test code. After the update completes, there will be at
* least one search result for the query "Chrome".
*
* Returns the app's client API delegate.
*/
async function waitForInitialIndexUpdate() {
const delegate = /** @type {!helpApp.ClientApi} */ (
document.querySelector('showoff-app')).getDelegate();
document.querySelector('showoff-app')).getDelegate();
for (let numTries = 0; numTries < 50; numTries++) {
// 'Chrome' appears in the mock app's fake search results, and should appear
// in the real app's search results.
const response = await delegate.findInSearchIndex('Chrome');
if (response && response.results && response.results.length > 0) break;
await new Promise(resolve => {setTimeout(resolve, 50)});
}
return delegate;
}
// Test that search (add, find) works when called from the guest frame,
// and it works for searchable items with and without subheadings.
GUEST_TEST('GuestCanSearchWithHeadings', async () => {
const delegate = await waitForInitialIndexUpdate();
await delegate.addOrUpdateSearchIndex([{
// Title match. No subheadings.
id: 'test-id-1',
......@@ -45,9 +66,11 @@ GUEST_TEST('GuestCanSearchWithHeadings', async () => {
// Keep polling until the index finishes updating or too much time has passed.
/** @type {?helpApp.FindResponse} */
let response = null;
for (let numTries = 0; numTries < 100; numTries++) {
for (let numTries = 0; numTries < 50; numTries++) {
// This search query was chosen because it is unlikely to show any search
// results for the real app's data.
response = await delegate.findInSearchIndex('verycomplicatedsearchtoken');
if (response && response.results) break;
if (response && response.results && response.results.length > 0) break;
await new Promise(resolve => {setTimeout(resolve, 50)});
}
......@@ -74,3 +97,15 @@ GUEST_TEST('GuestCanSearchWithHeadings', async () => {
},
]);
});
// Test that the guest frame can clear the search index.
GUEST_TEST('GuestCanClearSearchIndex', async () => {
const delegate = await waitForInitialIndexUpdate();
// Clear resolves after the index finishes clearing, so we don't need to try
// finding multiple times.
await delegate.clearSearchIndex();
const res = await delegate.findInSearchIndex('Chrome');
assertDeepEquals(res, {results: null});
});
......@@ -90,7 +90,12 @@ TEST_F('HelpAppUIBrowserTest', 'GuestHasLang', async () => {
testDone();
});
TEST_F('HelpAppUIBrowserTest', 'GuestCanSearchWithSubheadings', async () => {
TEST_F('HelpAppUIBrowserTest', 'GuestCanSearchWithHeadings', async () => {
await runTestInGuest('GuestCanSearchWithHeadings');
testDone();
});
TEST_F('HelpAppUIBrowserTest', 'GuestCanClearSearchIndex', async () => {
await runTestInGuest('GuestCanClearSearchIndex');
testDone();
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment