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

Unified Diff: components/autofill/core/browser/assist_manager_unittest.cc

Issue 2026353002: [Autofill] Credit Card Assist Infobar (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tests Created 4 years, 5 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/autofill/core/browser/assist_manager_unittest.cc
diff --git a/components/autofill/core/browser/assist_manager_unittest.cc b/components/autofill/core/browser/assist_manager_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..be438993391dc201d57213805094454bd4b25a72
--- /dev/null
+++ b/components/autofill/core/browser/assist_manager_unittest.cc
@@ -0,0 +1,165 @@
+// Copyright 2016 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/autofill/core/browser/assist_manager.h"
+
+#include <memory>
+
+#include "base/callback.h"
+#include "base/feature_list.h"
+#include "base/message_loop/message_loop.h"
+#include "base/strings/utf_string_conversions.h"
+#include "components/autofill/core/browser/autofill_driver.h"
+#include "components/autofill/core/browser/autofill_experiments.h"
+#include "components/autofill/core/browser/autofill_manager.h"
+#include "components/autofill/core/browser/autofill_test_utils.h"
+#include "components/autofill/core/browser/credit_card.h"
+#include "components/autofill/core/browser/form_structure.h"
+#include "components/autofill/core/browser/test_autofill_client.h"
+#include "components/autofill/core/browser/test_autofill_driver.h"
+#include "components/autofill/core/common/autofill_constants.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::_;
+
+namespace autofill {
+namespace {
+
+class MockAutofillManager : public AutofillManager {
+ public:
+ MockAutofillManager(TestAutofillDriver* driver, TestAutofillClient* client)
+ // Force to use the constructor designated for unit test, but we don't
+ // really need personal_data in this test so we pass a NULL pointer.
+ : AutofillManager(driver, client, NULL) {}
+ virtual ~MockAutofillManager() {}
+
+ MOCK_METHOD5(FillCreditCardForm,
+ void(int query_id,
+ const FormData& form,
+ const FormFieldData& field,
+ const CreditCard& credit_card,
+ const base::string16& cvc));
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockAutofillManager);
+};
+}
please use gerrit instead 2016/07/27 17:02:26 } // namespace
Mathieu 2016/07/27 21:35:53 Done.
+
+class AssistManagerTest : public testing::Test {
+ protected:
+ void SetUp() override {
please use gerrit instead 2016/07/27 17:02:26 Gtests work like this: running CanShowCreditCardA
Mathieu 2016/07/27 21:35:52 Done.
+ autofill_driver_.reset(new testing::NiceMock<TestAutofillDriver>());
+ autofill_manager_.reset(
+ new MockAutofillManager(autofill_driver_.get(), &autofill_client_));
+ assist_manager_.reset(new AssistManager(autofill_manager_.get()));
+ }
+
+ void TearDown() override {
please use gerrit instead 2016/07/27 17:02:26 Redundant, because every test case destroys Assist
Mathieu 2016/07/27 21:35:53 Done.
+ autofill_manager_.reset();
+ autofill_driver_.reset();
+ assist_manager_.reset();
+ }
+
+ void EnableAutofillCreditCardAssist() {
+ base::FeatureList::ClearInstanceForTesting();
+ std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
+ feature_list->InitializeFromCommandLine(kAutofillCreditCardAssist.name,
+ std::string());
+ base::FeatureList::SetInstance(std::move(feature_list));
+ }
+
+ // Returns an initialized FormStructure with credit card form data. To be
+ // owned by the caller.
+ std::unique_ptr<FormStructure> CreateValidCreditCardForm() {
+ std::unique_ptr<FormStructure> form_structure;
+ FormData form;
+
+ FormFieldData field;
+ field.form_control_type = "text";
+
+ field.label = base::ASCIIToUTF16("Name on Card");
+ field.name = base::ASCIIToUTF16("name_on_card");
+ form.fields.push_back(field);
+
+ field.label = base::ASCIIToUTF16("Card Number");
+ field.name = base::ASCIIToUTF16("card_number");
+ form.fields.push_back(field);
+
+ field.label = base::ASCIIToUTF16("Exp Month");
+ field.name = base::ASCIIToUTF16("ccmonth");
+ form.fields.push_back(field);
+
+ field.label = base::ASCIIToUTF16("Exp Year");
+ field.name = base::ASCIIToUTF16("ccyear");
+ form.fields.push_back(field);
+
+ field.label = base::ASCIIToUTF16("Verification");
+ field.name = base::ASCIIToUTF16("verification");
+ form.fields.push_back(field);
+
+ form_structure.reset(new FormStructure(form));
+ form_structure->DetermineHeuristicTypes();
+
+ return std::move(form_structure);
+ }
+
+ TestAutofillClient autofill_client_;
+ std::unique_ptr<testing::NiceMock<TestAutofillDriver>> autofill_driver_;
+ std::unique_ptr<MockAutofillManager> autofill_manager_;
+ std::unique_ptr<AssistManager> assist_manager_;
+
+ base::MessageLoop message_loop_;
+};
+
+MATCHER_P(CreditCardMatches, guid, "") {
+ return arg.guid() == guid;
+}
+
+// If the feature is turned off, CanShowCreditCardAssist() always returns
+// false.
+TEST_F(AssistManagerTest, CanShowCreditCardAssist_FeatureOff) {
+ std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm();
+
+ std::vector<FormStructure*> form_structures{form_structure.get()};
+ EXPECT_FALSE(assist_manager_->CanShowCreditCardAssist(form_structures));
+}
+
+// Tests that with the feature enabled and proper input,
+// CanShowCreditCardAssist() behaves as expected.
+TEST_F(AssistManagerTest, CanShowCreditCardAssist_FeatureOn) {
+ EnableAutofillCreditCardAssist();
+ std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm();
+
+ std::vector<FormStructure*> form_structures;
+ EXPECT_FALSE(assist_manager_->CanShowCreditCardAssist(form_structures));
+
+ // With valid input, the function extracts the credit card form properly.
+ form_structures.push_back(form_structure.get());
+ EXPECT_TRUE(assist_manager_->CanShowCreditCardAssist(form_structures));
+}
+
+TEST_F(AssistManagerTest, ShowAssistForCreditCard_ValidCard) {
+ EnableAutofillCreditCardAssist();
+ std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm();
+
+ // Will extract the credit card form data.
+ std::vector<FormStructure*> form_structures{form_structure.get()};
+ EXPECT_TRUE(assist_manager_->CanShowCreditCardAssist(form_structures));
+
+ // Create a valid card for the assist.
+ CreditCard card;
+ test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999");
+
+ // FillCreditCardForm ends up being called after user has accepted the
+ // prompt.
+ EXPECT_CALL(
+ *autofill_manager_,
+ FillCreditCardForm(kNoQueryId, _, _, CreditCardMatches(card.guid()),
+ /* empty cvc */ base::string16()));
+
+ assist_manager_->ShowAssistForCreditCard(card);
+}
+
+} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698