/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Test Google search plugin URLs
 */

"use strict";

let expectedEngine = {
  name: "Google",
  alias: null,
  description: "Google Search",
  searchForm: "https://www.google.com/search?q=&ie=utf-8&oe=utf-8",
  hidden: false,
  wrappedJSObject: {
    queryCharset: "UTF-8",
    "_iconURL": "data:image/x-icon;base64,[encoded data, 7240 characters]",
    _urls: [
      {
        type: "application/x-suggestions+json",
        method: "GET",
        template: "https://www.google.com/complete/search?client=firefox&q={searchTerms}",
        params: "",
      },
      {
        type: "text/html",
        method: "GET",
        template: "https://www.google.com/search",
        params: [
          {
            "name": "q",
            "value": "{searchTerms}",
            "purpose": undefined,
          },
          {
            "name": "ie",
            "value": "utf-8",
            "purpose": undefined,
          },
          {
            "name": "oe",
            "value": "utf-8",
            "purpose": undefined,
          },
        ],
        mozparams: {
        },
      },
    ],
  },
};

function test() {
  let engine = Services.search.getEngineByName("Google");
  ok(engine, "Found Google search engine");

  let countryCode = Services.prefs.getCharPref("browser.search.countryCode");
  let code = "";
  switch (countryCode) {
    case "US":
      code = "firefox-b-1";
      break;
    case "DE":
      code = "firefox-b";
      break;
    case "RU":
      // Covered by test but doesn't use a code
      break;
  }

  let base = "https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8";
  // Keyword uses a slightly different code
  let keywordBase = base;
  if (code) {
    let suffix = `&client=${code}`;
    base += suffix;
    keywordBase += `${suffix}-ab`;
    expectedEngine.searchForm += suffix;
    let urlParams = expectedEngine.wrappedJSObject._urls[1].params;
    urlParams.push({
      name: "client",
      value: `${code}-ab`,
      purpose: "keyword",
    });
    urlParams.push({
      name: "client",
      value: code,
      purpose: "searchbar",
    });
  }

  let url;

  // Test search URLs (including purposes).
  let purposes = ["", "contextmenu", "searchbar", "homepage", "newtab"];
  for (let purpose of purposes) {
    url = engine.getSubmission("foo", null, purpose).uri.spec;
    is(url, base, `Check ${purpose} search URL for 'foo'`);
  }
  url = engine.getSubmission("foo", null, "keyword").uri.spec;
  is(url, keywordBase, "Check keyword search URL for 'foo'");

  // Check search suggestion URL.
  url = engine.getSubmission("foo", "application/x-suggestions+json").uri.spec;
  is(url, "https://www.google.com/complete/search?client=firefox&q=foo", "Check search suggestion URL for 'foo'");

  // Check result parsing and alternate domains.
  let alternateBase = base.replace("www.google.com", "www.google.fr");
  is(Services.search.parseSubmissionURL(base).terms, "foo",
     "Check result parsing");
  is(Services.search.parseSubmissionURL(alternateBase).terms, "foo",
     "Check alternate domain");

  // Check all other engine properties.
  isSubObjectOf(expectedEngine, engine, "Google");
}
