forked from mirrors/gecko-dev
		
	 8ca1546092
			
		
	
	
		8ca1546092
		
	
	
	
	
		
			
			`QuickSuggestTestUtils.ensureQuickSuggestInit()` was written before Merino, so it assumes the suggestion objects passed in are remote settings suggestions. This revision modifies it so Merino and remote settings suggestions can both be passed in. That makes it a little nicer for tests that need to test Merino suggestions in particular, like navigational suggestions, dynamic Wikipedia, etc. Another motivation for this change is that it makes it clear which type of suggestion is being passed to `ensureQuickSuggestInit()`. Unfortunately Merino suggestion objects are slightly different from remote settings result objects (`block_id` vs. `id` for example), which are both different from UrlbarResult objects, and it can be confusing when reading tests. Since "result" is the name of remote settings objects used internally in the remote settings client, I've used that term here, and I've updated all callers to use it instead of "suggestion". This also makes `MerinoTestUtils` and `QuickSuggestTestUtils` singletons. Otherwise the new `MerinoTestUtils` instance used inside `QuickSuggestTestUtils` isn't the same as the one used in the test that calls into `QuickSuggestTestUtils`, which is very confusing. This made me realize it's a good idea for these test utils objects to be singletons. Finally I removed `is_top_pick` handling from the remote settings client and remote settings suggestions, since the related test is now using Merino. I also removed `_test_is_best_match` since only one test was using it and it's not necessary. Depends on D166019 Differential Revision: https://phabricator.services.mozilla.com/D166050
		
			
				
	
	
		
			201 lines
		
	
	
	
		
			5.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			201 lines
		
	
	
	
		
			5.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /* Any copyright is dedicated to the Public Domain.
 | |
|  * http://creativecommons.org/publicdomain/zero/1.0/ */
 | |
| 
 | |
| /*
 | |
|   Tests the showSearchTerms option on the about:preferences#search page.
 | |
| */
 | |
| 
 | |
| "use strict";
 | |
| 
 | |
| XPCOMUtils.defineLazyGetter(this, "QuickSuggestTestUtils", () => {
 | |
|   const { QuickSuggestTestUtils: module } = ChromeUtils.importESModule(
 | |
|     "resource://testing-common/QuickSuggestTestUtils.sys.mjs"
 | |
|   );
 | |
|   module.init(this);
 | |
|   return module;
 | |
| });
 | |
| 
 | |
| const GROUP_ID = "searchbarGroup";
 | |
| const CHECKBOX_ID = "searchShowSearchTermCheckbox";
 | |
| const PREF_SEARCHTERMS = "browser.urlbar.showSearchTerms.enabled";
 | |
| const PREF_FEATUREGATE = "browser.urlbar.showSearchTerms.featureGate";
 | |
| 
 | |
| /*
 | |
|   If Nimbus experiment is enabled, check option visibility.
 | |
| */
 | |
| add_task(async function showSearchTermsVisibility_experiment_beforeOpen() {
 | |
|   await SpecialPowers.pushPrefEnv({
 | |
|     set: [[PREF_FEATUREGATE, false]],
 | |
|   });
 | |
|   await QuickSuggestTestUtils.withExperiment({
 | |
|     valueOverrides: {
 | |
|       showSearchTermsFeatureGate: true,
 | |
|     },
 | |
|     callback: async () => {
 | |
|       await openPreferencesViaOpenPreferencesAPI("search", {
 | |
|         leaveOpen: true,
 | |
|       });
 | |
|       let doc = gBrowser.selectedBrowser.contentDocument;
 | |
|       let container = doc.getElementById(CHECKBOX_ID);
 | |
|       Assert.ok(
 | |
|         BrowserTestUtils.is_visible(container),
 | |
|         "The option box is visible"
 | |
|       );
 | |
|       gBrowser.removeCurrentTab();
 | |
|     },
 | |
|   });
 | |
|   await SpecialPowers.popPrefEnv();
 | |
| });
 | |
| 
 | |
| /*
 | |
|   If Nimbus experiment is not enabled initially but eventually enabled,
 | |
|   check option visibility on Preferences page.
 | |
| */
 | |
| add_task(async function showSearchTermsVisibility_experiment_afterOpen() {
 | |
|   await SpecialPowers.pushPrefEnv({
 | |
|     set: [[PREF_FEATUREGATE, false]],
 | |
|   });
 | |
|   await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
 | |
|   let doc = gBrowser.selectedBrowser.contentDocument;
 | |
|   let container = doc.getElementById(CHECKBOX_ID);
 | |
|   Assert.ok(
 | |
|     BrowserTestUtils.is_hidden(container),
 | |
|     "The option box is initially hidden."
 | |
|   );
 | |
| 
 | |
|   // Install experiment.
 | |
|   await QuickSuggestTestUtils.withExperiment({
 | |
|     valueOverrides: {
 | |
|       showSearchTermsFeatureGate: true,
 | |
|     },
 | |
|     callback: async () => {
 | |
|       Assert.ok(
 | |
|         BrowserTestUtils.is_visible(container),
 | |
|         "The option box is visible"
 | |
|       );
 | |
|     },
 | |
|   });
 | |
| 
 | |
|   Assert.ok(
 | |
|     BrowserTestUtils.is_hidden(container),
 | |
|     "The option box is hidden again after the experiment is uninstalled."
 | |
|   );
 | |
| 
 | |
|   gBrowser.removeCurrentTab();
 | |
|   await SpecialPowers.popPrefEnv();
 | |
| });
 | |
| 
 | |
| /*
 | |
|   Check using the checkbox modifies the preference.
 | |
| */
 | |
| add_task(async function showSearchTerms_checkbox() {
 | |
|   // Enable the feature.
 | |
|   await SpecialPowers.pushPrefEnv({
 | |
|     set: [[PREF_FEATUREGATE, true]],
 | |
|   });
 | |
|   await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
 | |
|   let doc = gBrowser.selectedBrowser.contentDocument;
 | |
|   doc.getElementById(GROUP_ID).scrollIntoView();
 | |
| 
 | |
|   let option = doc.getElementById(CHECKBOX_ID);
 | |
| 
 | |
|   // Evaluate checkbox pref is true.
 | |
|   Assert.ok(option.checked, "Option box should be checked.");
 | |
| 
 | |
|   // Evaluate checkbox when pref is false.
 | |
|   await SpecialPowers.pushPrefEnv({
 | |
|     set: [[PREF_SEARCHTERMS, false]],
 | |
|   });
 | |
|   Assert.ok(!option.checked, "Option box should not be checked.");
 | |
|   await SpecialPowers.popPrefEnv();
 | |
| 
 | |
|   // Evaluate pref when checkbox is un-checked.
 | |
|   await BrowserTestUtils.synthesizeMouseAtCenter(
 | |
|     "#" + CHECKBOX_ID,
 | |
|     {},
 | |
|     gBrowser.selectedBrowser
 | |
|   );
 | |
|   Assert.equal(
 | |
|     Services.prefs.getBoolPref(PREF_SEARCHTERMS),
 | |
|     false,
 | |
|     "Preference should be false if un-checked."
 | |
|   );
 | |
| 
 | |
|   // Evaluate pref when checkbox is checked.
 | |
|   await BrowserTestUtils.synthesizeMouseAtCenter(
 | |
|     "#" + CHECKBOX_ID,
 | |
|     {},
 | |
|     gBrowser.selectedBrowser
 | |
|   );
 | |
|   Assert.equal(
 | |
|     Services.prefs.getBoolPref(PREF_SEARCHTERMS),
 | |
|     true,
 | |
|     "Preference should be true if checked."
 | |
|   );
 | |
| 
 | |
|   // Clean-up.
 | |
|   Services.prefs.clearUserPref(PREF_SEARCHTERMS);
 | |
|   gBrowser.removeCurrentTab();
 | |
|   await SpecialPowers.popPrefEnv();
 | |
| });
 | |
| 
 | |
| /*
 | |
|   When loading the search preferences panel, the
 | |
|   showSearchTerms checkbox should be disabled if
 | |
|   the search bar is enabled.
 | |
| */
 | |
| add_task(async function showSearchTerms_and_searchBar_preference_load() {
 | |
|   // Enable the feature.
 | |
|   await SpecialPowers.pushPrefEnv({
 | |
|     set: [
 | |
|       [PREF_FEATUREGATE, true],
 | |
|       ["browser.search.widget.inNavBar", true],
 | |
|     ],
 | |
|   });
 | |
| 
 | |
|   await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
 | |
|   let doc = gBrowser.selectedBrowser.contentDocument;
 | |
| 
 | |
|   let checkbox = doc.getElementById(CHECKBOX_ID);
 | |
|   Assert.ok(
 | |
|     checkbox.disabled,
 | |
|     "showSearchTerms checkbox should be disabled when search bar is enabled."
 | |
|   );
 | |
| 
 | |
|   // Clean-up.
 | |
|   gBrowser.removeCurrentTab();
 | |
|   await SpecialPowers.popPrefEnv();
 | |
| });
 | |
| 
 | |
| /*
 | |
|   If the search bar is enabled while the search
 | |
|   preferences panel is open, the showSearchTerms
 | |
|   checkbox should not be clickable.
 | |
| */
 | |
| add_task(async function showSearchTerms_and_searchBar_preference_change() {
 | |
|   // Enable the feature.
 | |
|   await SpecialPowers.pushPrefEnv({
 | |
|     set: [[PREF_FEATUREGATE, true]],
 | |
|   });
 | |
| 
 | |
|   await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
 | |
|   let doc = gBrowser.selectedBrowser.contentDocument;
 | |
| 
 | |
|   let checkbox = doc.getElementById(CHECKBOX_ID);
 | |
|   Assert.ok(!checkbox.disabled, "showSearchTerms checkbox should be enabled.");
 | |
| 
 | |
|   await SpecialPowers.pushPrefEnv({
 | |
|     set: [["browser.search.widget.inNavBar", true]],
 | |
|   });
 | |
|   Assert.ok(
 | |
|     checkbox.disabled,
 | |
|     "showSearchTerms checkbox should be disabled when search bar is enabled."
 | |
|   );
 | |
| 
 | |
|   // Clean-up.
 | |
|   await SpecialPowers.popPrefEnv();
 | |
|   Assert.ok(!checkbox.disabled, "showSearchTerms checkbox should be enabled.");
 | |
| 
 | |
|   gBrowser.removeCurrentTab();
 | |
|   await SpecialPowers.popPrefEnv();
 | |
| });
 |