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

#import <Foundation/Foundation.h>

#import <memory>

#import "base/path_service.h"
#import "base/strings/sys_string_conversions.h"
#import "components/autofill/core/browser/country_type.h"
#import "components/autofill/core/browser/form_parsing/determine_regex_types.h"
#import "components/autofill/core/browser/form_structure.h"
#import "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#import "components/autofill/core/common/language_code.h"
#import "ios/web_view/internal/autofill/cwv_autofill_form_internal.h"
#import "ios/web_view/test/test_with_locale_and_resources.h"
#import "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#import "ui/base/resource/resource_bundle.h"

namespace ios_web_view {

using CWVAutofillFormTest = TestWithLocaleAndResources;

// Tests CWVAutofillForm initialization.
TEST_F(CWVAutofillFormTest, Initialization) {
  // Load resources that are generated by the WebView build.
  // The resources contain the configuration for the PatternProvider.
  base::FilePath pak_path;
  base::PathService::Get(base::DIR_ASSETS, &pak_path);
  pak_path = pak_path.AppendASCII("web_view_resources.pak");
  ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
      pak_path, ui::kScaleFactorNone);

  autofill::test::AutofillUnitTestEnvironment autofill_test_environment_;
  autofill::FormData form_data = autofill::test::CreateTestAddressFormData();
  std::unique_ptr<autofill::FormStructure> form_structure =
      std::make_unique<autofill::FormStructure>(form_data);
  const autofill::RegexPredictions regex_predictions = DetermineRegexTypes(
      autofill::GeoIpCountryCode(""), autofill::LanguageCode(""),
      form_structure->ToFormData(), nullptr, /*ignore_small_forms=*/true);
  regex_predictions.ApplyTo(form_structure->fields());
  form_structure->RationalizeAndAssignSections(
      autofill::GeoIpCountryCode(""), autofill::LanguageCode(""), nullptr);
  CWVAutofillForm* form =
      [[CWVAutofillForm alloc] initWithFormStructure:*form_structure];
  EXPECT_NSEQ(base::SysUTF16ToNSString(form_data.name()), form.name);
  EXPECT_TRUE(form.type & CWVAutofillFormTypeAddresses);
  EXPECT_FALSE(form.type & CWVAutofillFormTypeCreditCards);
  EXPECT_FALSE(form.type & CWVAutofillFormTypePasswords);
}

}  // namespace ios_web_view
