chromium/components/search_engines/enterprise/site_search_policy_handler_unittest.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "components/search_engines/enterprise/site_search_policy_handler.h"

#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/policy/core/browser/configuration_policy_pref_store.h"
#include "components/policy/core/browser/configuration_policy_pref_store_test.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/search_engines/default_search_manager.h"
#include "components/search_engines/enterprise/enterprise_site_search_manager.h"
#include "components/search_engines/template_url_data.h"
#include "components/strings/grit/components_strings.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

AllOf;
ElementsAre;

namespace policy {

namespace {

// Represents field values for SiteSearchSettings policy, used for generating
// policy value entries. Fields set as nullptr will not be added to the
// entry dictionary.
struct TestProvider {};

// Used for tests that require a list of valid providers.
TestProvider kValidTestProviders[] =;

// Used for tests that require providers with missing required fields. Missing
// fields for test are represented as nullptr.
TestProvider kMissingRequiredFieldsTestProviders[] =;

// Used for tests that require providers with empty required fields.
TestProvider kEmptyFieldTestProviders[] =;

// Used for tests that require a provider with unknown field.
TestProvider kUnknownFieldTestProviders[] =;

// Used for tests that require a list of providers with a duplicated shortcut,
// but at least one valid entry.
TestProvider kShortcutNotUniqueTestProviders[] =;

// Used for tests that require a list of providers with a duplicated shortcut
// and no valid entry.
TestProvider kNoUniqueShortcutTestProviders[] =;

// Used for tests that require a provider shortcut containing a space
// and no valid entry.
TestProvider kShortcutWithSpacesTestProviders[] =;

// Used for tests that require a provider shortcut that stars with @.
TestProvider kShortcutStartsWithAtTestProviders[] =;

// Used for tests that require a provider with invalid non-empty URLs.
TestProvider kInvalidUrlTestProviders[] =;

constexpr char kDSPKeyword[] =;

// Used for tests that require a provider with invalid non-empty URLs.
TestProvider kShortcutSameAsDSPKeywordTestProviders[] =;

// Used for tests that require a provider with non-HTTPS URL.
TestProvider kNonHttpsUrlTestProviders[] =;

// Used for tests that require a list of featured providers.
TestProvider kTestProvidersWithFeaturedEntries[] =;

// Creates a simple list item for the site search policy.
base::Value::Dict GenerateSiteSearchPolicyEntry(const std::string& name,
                                                const std::string& shortcut,
                                                const std::string& url,
                                                bool featured_by_policy) {}

base::Value::Dict GenerateSiteSearchPolicyEntry(TestProvider test_case) {}

// Accepts a dictionary that has a string field `field_name` with value
// `expected_value`.
MATCHER_P2(HasStringField,
           field_name,
           expected_value,
           base::StringPrintf("%s string field `%s` with value `%s`",
                              negation ? "does not contain" : "contains",
                              field_name,
                              expected_value.c_str())) {}

// Accepts a dictionary that has a boolean field `field_name` with value
// `expected_value`.
MATCHER_P2(HasBooleanField,
           field_name,
           expected_value,
           base::StringPrintf("%s boolean field `%s` with value `%d`",
                              negation ? "does not contain" : "contains",
                              field_name,
                              expected_value)) {}

// Accepts a dictionary that has a double field `field_name` with non-zero
// value.
MATCHER_P2(HasIntegerField,
           field_name,
           expected_value,
           base::StringPrintf("%s integer field `%s` with value `%d`",
                              negation ? "does not contain" : "contains",
                              field_name,
                              expected_value)) {}

// Accepts a dictionary that has a double field `field_name` with non-zero
// value.
MATCHER_P(HasDoubleField,
          field_name,
          base::StringPrintf("%s double field `%s` with non-zero value",
                             negation ? "does not contain" : "contains",
                             field_name)) {}

// Returns a matcher that accepts entries for the pref corresponding to the
// site search policy. Field values are obtained from |test_case|.
testing::Matcher<const base::Value&> IsSiteSearchEntry(TestProvider test_case,
                                                       bool featured) {}

testing::Matcher<const base::Value&> IsNonFeaturedSiteSearchEntry(
    TestProvider test_case) {}

testing::Matcher<const base::Value&> IsFeaturedSiteSearchEntry(
    TestProvider test_case) {}

MATCHER_P(HasValidationError,
          expected_str,
          base::StringPrintf("%s error message `%s` for `SiteSearchSettings`",
                             negation ? "does not contain" : "contains",
                             base::UTF16ToUTF8(expected_str).c_str())) {}

}  // namespace

TEST(SiteSearchPolicyHandlerTest, FeatureDisabled) {}

TEST(SiteSearchPolicyHandlerTest, PolicyNotSet) {}

TEST(SiteSearchPolicyHandlerTest, ValidSiteSearchEntries) {}

TEST(SiteSearchPolicyHandlerTest, InvalidFormat) {}

TEST(SiteSearchPolicyHandlerTest, TooManySiteSearchEntries) {}

TEST(SiteSearchPolicyHandlerTest, TooManyFeaturedSiteSearchEntries) {}

TEST(SiteSearchPolicyHandlerTest, MissingRequiredField) {}

TEST(SiteSearchPolicyHandlerTest, ShortcutNotUnique) {}

TEST(SiteSearchPolicyHandlerTest, NoUniqueShortcut) {}

TEST(SiteSearchPolicyHandlerTest, EmptyRequiredField) {}

TEST(SiteSearchPolicyHandlerTest, UnknownField) {}

TEST(SiteSearchPolicyHandlerTest, ShortcutWithSpace) {}

TEST(SiteSearchPolicyHandlerTest, ShortcutStartsWithAt) {}

TEST(SiteSearchPolicyHandlerTest, InvalidUrl) {}

TEST(SiteSearchPolicyHandlerTest, ShortcutSameAsDSPKeyword_DSPEnabledNotSet) {}

TEST(SiteSearchPolicyHandlerTest, ShortcutSameAsDSPKeyword_DSPDisabled) {}

TEST(SiteSearchPolicyHandlerTest, ShortcutSameAsDSPKeyword_DSPEnabled) {}

TEST(SiteSearchPolicyHandlerTest, NonHttpsUrl) {}

TEST(SiteSearchPolicyHandlerTest, NoValidEntry) {}

TEST(SiteSearchPolicyHandlerTest, FeaturedSiteSearchEntries) {}

}  // namespace policy