|
7 | 7 |
|
8 | 8 | "use strict";
|
9 | 9 |
|
| 10 | +ChromeUtils.defineESModuleGetters(this, { |
| 11 | + AmpMatchingStrategy: "resource://gre/modules/RustSuggest.sys.mjs", |
| 12 | + SuggestionProvider: "resource://gre/modules/RustSuggest.sys.mjs", |
| 13 | +}); |
| 14 | + |
10 | 15 | const SPONSORED_SEARCH_STRING = "amp";
|
11 | 16 | const NONSPONSORED_SEARCH_STRING = "wikipedia";
|
12 | 17 | const SPONSORED_AND_NONSPONSORED_SEARCH_STRING = "sponsored and non-sponsored";
|
@@ -1868,3 +1873,124 @@ add_task(async function ampTopPickCharThreshold_zero() {
|
1868 | 1873 |
|
1869 | 1874 | UrlbarPrefs.clear("quicksuggest.ampTopPickCharThreshold");
|
1870 | 1875 | });
|
| 1876 | + |
| 1877 | +// Tests `ampMatchingStrategy`. |
| 1878 | +add_task(async function ampMatchingStrategy() { |
| 1879 | + UrlbarPrefs.set("suggest.quicksuggest.nonsponsored", false); |
| 1880 | + UrlbarPrefs.set("suggest.quicksuggest.sponsored", true); |
| 1881 | + await QuickSuggestTestUtils.forceSync(); |
| 1882 | + |
| 1883 | + // Test each strategy in `AmpMatchingStrategy`. There are only a few. |
| 1884 | + for (let [key, value] of Object.entries(AmpMatchingStrategy)) { |
| 1885 | + await doAmpMatchingStrategyTest({ key, value }); |
| 1886 | + |
| 1887 | + // Reset back to the default strategy just to make sure that works. |
| 1888 | + await doAmpMatchingStrategyTest({ |
| 1889 | + key: "(default)", |
| 1890 | + value: 0, |
| 1891 | + }); |
| 1892 | + } |
| 1893 | + |
| 1894 | + // Test an invalid strategy integer value. The default strategy should |
| 1895 | + // actually be used. First we need to set a valid non-default strategy. |
| 1896 | + await doAmpMatchingStrategyTest({ |
| 1897 | + key: "FTS_AGAINST_TITLE", |
| 1898 | + value: AmpMatchingStrategy.FTS_AGAINST_TITLE, |
| 1899 | + }); |
| 1900 | + await doAmpMatchingStrategyTest({ |
| 1901 | + key: "(invalid)", |
| 1902 | + value: 99, |
| 1903 | + expectedStrategy: 0, |
| 1904 | + }); |
| 1905 | + |
| 1906 | + Services.prefs.clearUserPref( |
| 1907 | + "browser.urlbar.quicksuggest.ampMatchingStrategy" |
| 1908 | + ); |
| 1909 | + await QuickSuggestTestUtils.forceSync(); |
| 1910 | +}); |
| 1911 | + |
| 1912 | +async function doAmpMatchingStrategyTest({ |
| 1913 | + key, |
| 1914 | + value, |
| 1915 | + expectedStrategy = value, |
| 1916 | +}) { |
| 1917 | + info("Doing ampMatchingStrategy test: " + JSON.stringify({ key, value })); |
| 1918 | + |
| 1919 | + let sandbox = sinon.createSandbox(); |
| 1920 | + let ingestSpy = sandbox.spy(QuickSuggest.rustBackend._test_store, "ingest"); |
| 1921 | + |
| 1922 | + // Set the strategy. It should trigger ingest. (Assuming it's different from |
| 1923 | + // the current strategy. If it's not, ingest won't happen.) |
| 1924 | + Services.prefs.setIntPref( |
| 1925 | + "browser.urlbar.quicksuggest.ampMatchingStrategy", |
| 1926 | + value |
| 1927 | + ); |
| 1928 | + |
| 1929 | + let ingestCall = await TestUtils.waitForCondition(() => { |
| 1930 | + return ingestSpy.getCalls().find(call => { |
| 1931 | + let ingestConstraints = call.args[0]; |
| 1932 | + return ingestConstraints?.providers[0] == SuggestionProvider.AMP; |
| 1933 | + }); |
| 1934 | + }, "Waiting for ingest() to be called with Amp provider"); |
| 1935 | + |
| 1936 | + // Check the provider constraints in the ingest constraints. |
| 1937 | + let { providerConstraints } = ingestCall.args[0]; |
| 1938 | + if (!expectedStrategy) { |
| 1939 | + Assert.ok( |
| 1940 | + !providerConstraints, |
| 1941 | + "ingest() should not have been called with provider constraints" |
| 1942 | + ); |
| 1943 | + } else { |
| 1944 | + Assert.ok( |
| 1945 | + providerConstraints, |
| 1946 | + "ingest() should have been called with provider constraints" |
| 1947 | + ); |
| 1948 | + Assert.strictEqual( |
| 1949 | + providerConstraints.ampAlternativeMatching, |
| 1950 | + expectedStrategy, |
| 1951 | + "ampAlternativeMatching should have been set" |
| 1952 | + ); |
| 1953 | + } |
| 1954 | + |
| 1955 | + // Now do a query to make sure it also uses the correct provider constraints. |
| 1956 | + // No need to use `check_results()`. We only need to trigger a query, and |
| 1957 | + // checking the right results unnecessarily complicates things. |
| 1958 | + let querySpy = sandbox.spy( |
| 1959 | + QuickSuggest.rustBackend._test_store, |
| 1960 | + "queryWithMetrics" |
| 1961 | + ); |
| 1962 | + |
| 1963 | + let controller = UrlbarTestUtils.newMockController(); |
| 1964 | + await controller.startQuery( |
| 1965 | + createContext(SPONSORED_SEARCH_STRING, { |
| 1966 | + providers: [UrlbarProviderQuickSuggest.name], |
| 1967 | + isPrivate: false, |
| 1968 | + }) |
| 1969 | + ); |
| 1970 | + |
| 1971 | + let queryCalls = querySpy.getCalls(); |
| 1972 | + Assert.equal(queryCalls.length, 1, "query() should have been called once"); |
| 1973 | + |
| 1974 | + let query = queryCalls[0].args[0]; |
| 1975 | + Assert.ok(query, "query() should have been called with a query object"); |
| 1976 | + Assert.ok( |
| 1977 | + query.providerConstraints, |
| 1978 | + "query() should have been called with provider constraints" |
| 1979 | + ); |
| 1980 | + |
| 1981 | + if (!expectedStrategy) { |
| 1982 | + Assert.strictEqual( |
| 1983 | + query.providerConstraints.ampAlternativeMatching, |
| 1984 | + null, |
| 1985 | + "ampAlternativeMatching should not have been set on query provider constraints" |
| 1986 | + ); |
| 1987 | + } else { |
| 1988 | + Assert.strictEqual( |
| 1989 | + query.providerConstraints.ampAlternativeMatching, |
| 1990 | + expectedStrategy, |
| 1991 | + "ampAlternativeMatching should have been set on query provider constraints" |
| 1992 | + ); |
| 1993 | + } |
| 1994 | + |
| 1995 | + sandbox.restore(); |
| 1996 | +} |
0 commit comments