Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(451)

Unified Diff: components/payments/core/address_normalizer_unittest.cc

Issue 2829903004: Reland: Normalize shipping address for merchant on Desktop. (Closed)
Patch Set: Make android code use the new impl Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/payments/core/address_normalizer_unittest.cc
diff --git a/components/payments/core/address_normalizer_unittest.cc b/components/payments/core/address_normalizer_unittest.cc
deleted file mode 100644
index da9760b3ef28e450083520abd7a64560587193c0..0000000000000000000000000000000000000000
--- a/components/payments/core/address_normalizer_unittest.cc
+++ /dev/null
@@ -1,247 +0,0 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "components/payments/core/address_normalizer.h"
-
-#include <utility>
-
-#include "base/run_loop.h"
-#include "base/strings/utf_string_conversions.h"
-#include "base/test/scoped_task_scheduler.h"
-#include "components/autofill/core/browser/autofill_profile.h"
-#include "testing/gtest/include/gtest/gtest.h"
-#include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_storage.h"
-#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
-#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
-#include "third_party/libaddressinput/src/cpp/test/testdata_source.h"
-
-namespace payments {
-namespace {
-
-using ::autofill::AutofillProfile;
-using ::i18n::addressinput::NullStorage;
-using ::i18n::addressinput::Source;
-using ::i18n::addressinput::Storage;
-using ::i18n::addressinput::TestdataSource;
-
-const char kLocale[] = "US";
-
-// The requester of normalization for this test.
-class NormalizationDelegate : public AddressNormalizer::Delegate {
- public:
- NormalizationDelegate()
- : normalized_called_(false), not_normalized_called_(false) {}
-
- ~NormalizationDelegate() override {}
-
- void OnAddressNormalized(const autofill::AutofillProfile& profile) override {
- normalized_called_ = true;
- profile_ = profile;
- }
-
- void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override {
- not_normalized_called_ = true;
- profile_ = profile;
- }
-
- bool normalized_called() const { return normalized_called_; }
-
- bool not_normalized_called() const { return not_normalized_called_; }
-
- const AutofillProfile& profile() const { return profile_; }
-
- private:
- bool normalized_called_;
- bool not_normalized_called_;
- AutofillProfile profile_;
-
- DISALLOW_COPY_AND_ASSIGN(NormalizationDelegate);
-};
-
-// Used to load region rules for this test.
-class ChromiumTestdataSource : public TestdataSource {
- public:
- ChromiumTestdataSource() : TestdataSource(true) {}
-
- ~ChromiumTestdataSource() override {}
-
- // For this test, only load the rules for the kLocale.
- void Get(const std::string& key, const Callback& data_ready) const override {
- data_ready(
- true, key,
- new std::string("{\"data/US\": "
- "{\"id\":\"data/US\",\"key\":\"US\",\"name\":\"UNITED "
- "STATES\",\"lang\":\"en\",\"languages\":\"en\"}}"));
- }
-
- private:
- DISALLOW_COPY_AND_ASSIGN(ChromiumTestdataSource);
-};
-
-// A test subclass of the AddressNormalizer. Used to simulate rules not being
-// loaded.
-class TestAddressNormalizer : public AddressNormalizer {
- public:
- TestAddressNormalizer(std::unique_ptr<::i18n::addressinput::Source> source,
- std::unique_ptr<::i18n::addressinput::Storage> storage)
- : AddressNormalizer(std::move(source), std::move(storage)),
- should_load_rules_(true) {}
-
- ~TestAddressNormalizer() override {}
-
- void ShouldLoadRules(bool should_load_rules) {
- should_load_rules_ = should_load_rules;
- }
-
- void LoadRulesForRegion(const std::string& region_code) override {
- if (should_load_rules_) {
- AddressNormalizer::LoadRulesForRegion(region_code);
- }
- }
-
- private:
- bool should_load_rules_;
-
- DISALLOW_COPY_AND_ASSIGN(TestAddressNormalizer);
-};
-
-} // namespace
-
-class AddressNormalizerTest : public testing::Test {
- protected:
- AddressNormalizerTest()
- : normalizer_(new TestAddressNormalizer(
- std::unique_ptr<Source>(new ChromiumTestdataSource),
- std::unique_ptr<Storage>(new NullStorage))) {}
-
- ~AddressNormalizerTest() override {}
-
- const std::unique_ptr<TestAddressNormalizer> normalizer_;
-
- base::test::ScopedTaskScheduler scoped_task_scheduler_;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(AddressNormalizerTest);
-};
-
-// Tests that rules are not loaded by default.
-TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_NotLoaded) {
- EXPECT_FALSE(normalizer_->AreRulesLoadedForRegion(kLocale));
-}
-
-// Tests that the rules are loaded correctly.
-TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_Loaded) {
- normalizer_->LoadRulesForRegion(kLocale);
- EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale));
-}
-
-// Tests that if the rules are loaded before the normalization is started, the
-// normalized profile will be returned to the delegate synchronously.
-TEST_F(AddressNormalizerTest, StartNormalization_RulesLoaded) {
- NormalizationDelegate delegate;
- AutofillProfile profile;
-
- // Load the rules.
- normalizer_->LoadRulesForRegion(kLocale);
- EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale));
-
- // Start the normalization.
- normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
-
- // Since the rules are already loaded, the address should be normalized
- // synchronously.
- EXPECT_TRUE(delegate.normalized_called());
- EXPECT_FALSE(delegate.not_normalized_called());
-}
-
-// Tests that if the rules are not loaded before the normalization and cannot be
-// loaded after, the address will not be normalized and the delegate will be
-// notified.
-TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillNotLoad) {
- NormalizationDelegate delegate;
- AutofillProfile profile;
-
- // Make sure the rules will not be loaded in the StartAddressNormalization
- // call.
- normalizer_->ShouldLoadRules(false);
-
- // Start the normalization.
- normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
-
- // Let the timeout execute.
- base::RunLoop().RunUntilIdle();
-
- // Since the rules are never loaded and the timeout is 0, the delegate should
- // get notified that the address could not be normalized.
- EXPECT_FALSE(delegate.normalized_called());
- EXPECT_TRUE(delegate.not_normalized_called());
-}
-
-// Tests that if the rules are not loaded before the call to
-// StartAddressNormalization, they will be loaded in the call.
-TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillLoad) {
- NormalizationDelegate delegate;
- AutofillProfile profile;
-
- // Start the normalization.
- normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
-
- // Even if the rules are not loaded before the call to
- // StartAddressNormalization, they should get loaded in the call. Since our
- // test source is synchronous, the normalization will happen synchronously
- // too.
- EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale));
- EXPECT_TRUE(delegate.normalized_called());
- EXPECT_FALSE(delegate.not_normalized_called());
-}
-
-// Tests that the phone number is formatted when the address is normalized.
-TEST_F(AddressNormalizerTest, FormatPhone_AddressNormalized) {
- NormalizationDelegate delegate;
- AutofillProfile profile;
- profile.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
- base::UTF8ToUTF16("(515) 123-1234"));
-
- // Load the rules.
- normalizer_->LoadRulesForRegion(kLocale);
- EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale));
-
- // Start the normalization.
- normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
-
- // Make sure the address was normalized.
- EXPECT_TRUE(delegate.normalized_called());
-
- // Expect that the phone number was formatted.
- EXPECT_EQ("+15151231234", base::UTF16ToUTF8(delegate.profile().GetRawInfo(
- autofill::PHONE_HOME_WHOLE_NUMBER)));
-}
-
-// Tests that the phone number is formatted even when the address is not
-// normalized.
-TEST_F(AddressNormalizerTest, FormatPhone_AddressNotNormalized) {
- NormalizationDelegate delegate;
- AutofillProfile profile;
- profile.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
- base::UTF8ToUTF16("515-123-1234"));
-
- // Make sure the rules will not be loaded in the StartAddressNormalization
- // call.
- normalizer_->ShouldLoadRules(false);
-
- // Start the normalization.
- normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
-
- // Let the timeout execute.
- base::RunLoop().RunUntilIdle();
-
- // Make sure the address was not normalized.
- EXPECT_TRUE(delegate.not_normalized_called());
-
- // Expect that the phone number was formatted.
- EXPECT_EQ("+15151231234", base::UTF16ToUTF8(delegate.profile().GetRawInfo(
- autofill::PHONE_HOME_WHOLE_NUMBER)));
-}
-
-} // namespace payments
« no previous file with comments | « components/payments/core/address_normalizer_impl_unittest.cc ('k') | components/payments/core/payment_request_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698