Skip to content

Commit b8e31d1

Browse files
committed
only use saved mode if enableAi is on
1 parent f12ddd9 commit b8e31d1

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

special-pages/pages/new-tab/app/omnibar/components/OmnibarConsumer.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useTabState } from '../../tabs/TabsProvider.js';
1212
/**
1313
* @typedef {import('../strings.json')} Strings
1414
* @typedef {import('../../../types/new-tab.js').OmnibarConfig} OmnibarConfig
15+
* @typedef {import('../../../types/new-tab.js').OmnibarMode} Mode
1516
*/
1617

1718
/**
@@ -43,7 +44,19 @@ export function OmnibarConsumer() {
4344
function OmnibarReadyState({ config, tabId }) {
4445
const { enableAi = true, showAiSetting = true, mode: defaultMode } = config;
4546
const { setEnableAi, setMode } = useContext(OmnibarContext);
46-
const mode = useModeWithLocalPersistence(tabId, defaultMode);
47+
const modeForCurrentTab = useModeWithLocalPersistence(tabId, defaultMode);
48+
49+
/**
50+
* Respect the current tab's mode only if 'enableAi' is on.
51+
* Otherwise always search
52+
*
53+
* @type {Mode}
54+
*/
55+
const mode = (() => {
56+
if (enableAi) return modeForCurrentTab;
57+
return /** @type {const} */ ('search');
58+
})();
59+
4760
return (
4861
<>
4962
{showAiSetting && <AiSetting enableAi={enableAi} setEnableAi={setEnableAi} />}

special-pages/pages/new-tab/app/omnibar/integration-tests/omnibar.page.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,50 @@ export class OmnibarPage {
186186
* @returns {Promise<void>}
187187
*/
188188
async didSwitchToTab(tabId, tabIds) {
189-
await test.step('simulate tab change event', async () => {
189+
await test.step(`simulate tab change event, to: ${tabId} `, async () => {
190190
await this.ntp.mocks.simulateSubscriptionMessage(sub('tabs_onDataUpdate'), tabs({ tabId, tabIds }));
191191
});
192192
}
193193

194+
/**
195+
* @param {object} props
196+
* @param {Mode} props.mode
197+
* @returns {Promise<void>}
198+
*/
199+
switchMode({ mode }) {
200+
switch (mode) {
201+
case 'ai': {
202+
return this.aiTab().click();
203+
}
204+
case 'search': {
205+
return this.searchTab().click();
206+
}
207+
}
208+
}
209+
210+
/**
211+
* @param {object} props
212+
* @param {Mode} props.mode
213+
* @param {string} props.value
214+
*/
215+
async expectValue({ mode, value }) {
216+
switch (mode) {
217+
case 'ai': {
218+
return await expect(this.chatInput()).toHaveValue(value);
219+
}
220+
case 'search': {
221+
return await expect(this.searchInput()).toHaveValue(value);
222+
}
223+
}
224+
}
225+
194226
/**
195227
* @param {object} props
196228
* @param {Mode} props.mode
197229
* @param {string} props.value
198230
* @returns {Promise<void>}
199231
*/
200-
input({ mode, value }) {
232+
types({ mode, value }) {
201233
switch (mode) {
202234
case 'ai': {
203235
return this.chatInput().fill(value);

special-pages/pages/new-tab/app/omnibar/integration-tests/omnibar.persistence.spec.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { test } from '@playwright/test';
22
import { NewtabPage } from '../../../integration-tests/new-tab.page.js';
33
import { OmnibarPage } from './omnibar.page.js';
4+
import { CustomizerPage } from '../../customizer/integration-tests/customizer.page.js';
45

56
test.describe('omnibar widget persistence', () => {
67
test('remembers input across tabs', async ({ page }, workerInfo) => {
@@ -11,14 +12,14 @@ test.describe('omnibar widget persistence', () => {
1112
await omnibar.ready();
1213

1314
// first fill
14-
await omnibar.input({ mode: 'search', value: 'shoes' });
15+
await omnibar.types({ mode: 'search', value: 'shoes' });
1516

1617
// switch
1718
await omnibar.didSwitchToTab('02', ['01', '02']);
1819
await omnibar.expectInputValue('');
1920

2021
// second fill
21-
await omnibar.input({ mode: 'search', value: 'dresses' });
22+
await omnibar.types({ mode: 'search', value: 'dresses' });
2223

2324
// back to first
2425
await omnibar.didSwitchToTab('01', ['01', '02']);
@@ -36,7 +37,7 @@ test.describe('omnibar widget persistence', () => {
3637
await omnibar.ready();
3738

3839
// first fill
39-
await omnibar.input({ mode: 'search', value: 'shoes' });
40+
await omnibar.types({ mode: 'search', value: 'shoes' });
4041
await page.getByRole('tab', { name: 'Duck.ai' }).click();
4142

4243
// new tab, should be opened with duck.ai input still visible
@@ -47,4 +48,29 @@ test.describe('omnibar widget persistence', () => {
4748
await omnibar.didSwitchToTab('01', ['01', '02']);
4849
await omnibar.expectChatValue('shoes');
4950
});
51+
test('adjusts mode of other tabs when duck.ai is disabled', async ({ page }, workerInfo) => {
52+
const ntp = NewtabPage.create(page, workerInfo);
53+
const omnibar = new OmnibarPage(ntp);
54+
const customizer = new CustomizerPage(ntp);
55+
await ntp.reducedMotion();
56+
await ntp.openPage({ additional: { omnibar: true, tabs: true, 'tabs.debug': true } });
57+
await omnibar.ready();
58+
59+
// first tab, switch to ai mode
60+
await omnibar.switchMode({ mode: 'ai' });
61+
62+
// switch to second tab, should be empty but still on duck.ai
63+
await omnibar.didSwitchToTab('02', ['01', '02']);
64+
await omnibar.expectValue({ value: '', mode: 'ai' });
65+
await omnibar.types({ value: 'shoes', mode: 'ai' });
66+
67+
// now turn duck.ai off, 'shoes' should remain, but on search mode
68+
await customizer.opensCustomizer();
69+
await omnibar.toggleDuckAiButton().uncheck();
70+
await omnibar.expectValue({ value: 'shoes', mode: 'search' });
71+
72+
// back to first tab, should be empty + search
73+
await omnibar.didSwitchToTab('01', ['01', '02']);
74+
await omnibar.expectValue({ value: '', mode: 'search' });
75+
});
5076
});

0 commit comments

Comments
 (0)