Commit b908f689 authored by estade's avatar estade Committed by Commit bot

Add a regression test for the CardUnmaskPrompt.

BUG=none

Review URL: https://codereview.chromium.org/1020013003

Cr-Commit-Position: refs/heads/master@{#321634}
parent d60174a4
......@@ -7,6 +7,10 @@
#include "base/strings/string16.h"
namespace base {
class TimeDelta;
}
namespace content {
class WebContents;
}
......@@ -32,6 +36,7 @@ class CardUnmaskPromptController {
virtual bool InputCvcIsValid(const base::string16& input_text) const = 0;
virtual bool InputExpirationIsValid(const base::string16& month,
const base::string16& year) const = 0;
virtual base::TimeDelta GetSuccessMessageDuration() const = 0;
};
} // namespace autofill
......
......@@ -36,10 +36,6 @@ CardUnmaskPromptControllerImpl::~CardUnmaskPromptControllerImpl() {
card_unmask_view_->ControllerGone();
}
CardUnmaskPromptView* CardUnmaskPromptControllerImpl::CreateAndShowView() {
return CardUnmaskPromptView::CreateAndShow(this);
}
void CardUnmaskPromptControllerImpl::ShowPrompt(
const CreditCard& card,
base::WeakPtr<CardUnmaskDelegate> delegate) {
......@@ -261,6 +257,15 @@ bool CardUnmaskPromptControllerImpl::InputExpirationIsValid(
return month_value >= now.month;
}
base::TimeDelta CardUnmaskPromptControllerImpl::GetSuccessMessageDuration()
const {
return base::TimeDelta::FromMilliseconds(500);
}
CardUnmaskPromptView* CardUnmaskPromptControllerImpl::CreateAndShowView() {
return CardUnmaskPromptView::CreateAndShow(this);
}
void CardUnmaskPromptControllerImpl::LoadRiskFingerprint() {
LoadRiskData(
0, web_contents_,
......
......@@ -43,19 +43,20 @@ class CardUnmaskPromptControllerImpl : public CardUnmaskPromptController {
bool InputCvcIsValid(const base::string16& input_text) const override;
bool InputExpirationIsValid(const base::string16& month,
const base::string16& year) const override;
base::TimeDelta GetSuccessMessageDuration() const override;
protected:
// Virtual so tests can suppress it.
virtual CardUnmaskPromptView* CreateAndShowView();
virtual void LoadRiskFingerprint();
virtual void OnDidLoadRiskFingerprint(const std::string& risk_data);
// Protected so tests can call it.
void OnDidLoadRiskFingerprint(const std::string& risk_data);
// Exposed for testing.
CardUnmaskPromptView* view() { return card_unmask_view_; }
private:
bool AllowsRetry(AutofillClient::GetRealPanResult result);
void LogOnCloseEvents();
......
......@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_UI_AUTOFILL_CARD_UNMASK_PROMPT_VIEW_H_
#define CHROME_BROWSER_UI_AUTOFILL_CARD_UNMASK_PROMPT_VIEW_H_
#include "base/strings/string16.h"
namespace autofill {
class CardUnmaskPromptController;
......
......@@ -4,7 +4,9 @@
#include "base/guid.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/autofill/card_unmask_prompt_controller_impl.h"
#include "chrome/browser/ui/autofill/card_unmask_prompt_view_tester.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
......@@ -24,14 +26,20 @@ class TestCardUnmaskDelegate : public CardUnmaskDelegate {
virtual ~TestCardUnmaskDelegate() {}
// CardUnmaskDelegate implementation.
void OnUnmaskResponse(const UnmaskResponse& response) override {}
void OnUnmaskResponse(const UnmaskResponse& response) override {
response_ = response;
}
void OnUnmaskPromptClosed() override {}
base::WeakPtr<TestCardUnmaskDelegate> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
UnmaskResponse response() { return response_; }
private:
UnmaskResponse response_;
base::WeakPtrFactory<TestCardUnmaskDelegate> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskDelegate);
......@@ -47,24 +55,20 @@ class TestCardUnmaskPromptController : public CardUnmaskPromptControllerImpl {
weak_factory_(this) {}
// CardUnmaskPromptControllerImpl implementation.
void OnDidLoadRiskFingerprint(const std::string& risk_data) override {
CardUnmaskPromptControllerImpl::OnDidLoadRiskFingerprint(risk_data);
// Call Quit() from here rather than from OnUnmaskDialogClosed().
// FingerprintDataLoader starts several async tasks that take a while to
// complete. If Quit() is called before FingerprintDataLoader is all done
// then LeakTracker will detect that some resources have not been freed
// and cause the browser test to fail. This is not a real leak though -
// normally the async tasks would complete and encounter weak callbacks.
runner_->Quit();
base::TimeDelta GetSuccessMessageDuration() const override {
return base::TimeDelta::FromMilliseconds(10);
}
void RunMessageLoop() { runner_->Run(); }
void LoadRiskFingerprint() override {
OnDidLoadRiskFingerprint(std::string("risk_data"));
}
base::WeakPtr<TestCardUnmaskPromptController> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
using CardUnmaskPromptControllerImpl::view;
private:
scoped_refptr<content::MessageLoopRunner> runner_;
base::WeakPtrFactory<TestCardUnmaskPromptController> weak_factory_;
......@@ -88,10 +92,11 @@ class CardUnmaskPromptViewBrowserTest : public InProcessBrowserTest {
TestCardUnmaskPromptController* controller() { return controller_.get(); }
TestCardUnmaskDelegate* delegate() { return delegate_.get(); }
private:
protected:
// This member must outlive the controller.
scoped_refptr<content::MessageLoopRunner> runner_;
private:
scoped_ptr<TestCardUnmaskPromptController> controller_;
scoped_ptr<TestCardUnmaskDelegate> delegate_;
......@@ -99,14 +104,34 @@ class CardUnmaskPromptViewBrowserTest : public InProcessBrowserTest {
};
IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest, DisplayUI) {
CreditCard credit_card(CreditCard::MASKED_SERVER_CARD, "a123");
test::SetCreditCardInfo(&credit_card, "Bonnie Parker",
"2109" /* Mastercard */, "12", "2012");
credit_card.SetTypeForMaskedCard(kMasterCard);
controller()->ShowPrompt(test::GetMaskedServerCard(),
delegate()->GetWeakPtr());
}
controller()->ShowPrompt(credit_card, delegate()->GetWeakPtr());
controller()->RunMessageLoop();
// TODO(bondd): bring up on Mac.
#if !defined(OS_MACOSX)
// Makes sure the user can close the dialog while the verification success
// message is showing.
IN_PROC_BROWSER_TEST_F(CardUnmaskPromptViewBrowserTest,
EarlyCloseAfterSuccess) {
controller()->ShowPrompt(test::GetMaskedServerCard(),
delegate()->GetWeakPtr());
controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"),
base::ASCIIToUTF16("10"),
base::ASCIIToUTF16("19"), false);
EXPECT_EQ(base::ASCIIToUTF16("123"), delegate()->response().cvc);
controller()->OnVerificationResult(AutofillClient::SUCCESS);
// Simulate the user clicking [x] before the "Success!" message disappears.
CardUnmaskPromptViewTester::For(controller()->view())->Close();
// Wait a little while; there should be no crash.
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE, base::Bind(&content::MessageLoopRunner::Quit,
base::Unretained(runner_.get())),
2 * controller()->GetSuccessMessageDuration());
runner_->Run();
}
#endif
} // namespace
......
// Copyright 2015 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.
#ifndef CHROME_BROWSER_UI_AUTOFILL_CARD_UNMASK_PROMPT_VIEW_TESTER_H_
#define CHROME_BROWSER_UI_AUTOFILL_CARD_UNMASK_PROMPT_VIEW_TESTER_H_
#include "base/memory/scoped_ptr.h"
namespace content {
class WebContents;
}
namespace autofill {
class CardUnmaskPromptView;
// Functionality that helps to test an AutofillCardUnmaskPromptView.
class CardUnmaskPromptViewTester {
public:
// Gets a AutofillCardUnmaskPromptViewTester for |view|.
static scoped_ptr<CardUnmaskPromptViewTester> For(CardUnmaskPromptView* view);
virtual ~CardUnmaskPromptViewTester() {}
virtual void Close() = 0;
};
} // namespace autofill
#endif // CHROME_BROWSER_UI_AUTOFILL_CARD_UNMASK_PROMPT_VIEW_TESTER_H_
// Copyright 2015 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 "chrome/browser/ui/views/autofill/card_unmask_prompt_view_tester_views.h"
#include "chrome/browser/ui/views/autofill/card_unmask_prompt_views.h"
namespace autofill {
// static
scoped_ptr<CardUnmaskPromptViewTester> CardUnmaskPromptViewTester::For(
CardUnmaskPromptView* view) {
return scoped_ptr<CardUnmaskPromptViewTester>(
new CardUnmaskPromptViewTesterViews(
static_cast<CardUnmaskPromptViews*>(view)));
}
// Class that facilitates testing.
CardUnmaskPromptViewTesterViews::CardUnmaskPromptViewTesterViews(
CardUnmaskPromptViews* view)
: view_(view) {
}
CardUnmaskPromptViewTesterViews::~CardUnmaskPromptViewTesterViews() {
}
void CardUnmaskPromptViewTesterViews::Close() {
view_->ClosePrompt();
}
} // namespace autofill
// Copyright 2015 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.
#ifndef CHROME_BROWSER_UI_VIEWS_AUTOFILL_CARD_UNMASK_PROMPT_VIEW_TESTER_VIEWS_H_
#define CHROME_BROWSER_UI_VIEWS_AUTOFILL_CARD_UNMASK_PROMPT_VIEW_TESTER_VIEWS_H_
#include "chrome/browser/ui/autofill/card_unmask_prompt_view_tester.h"
namespace autofill {
class CardUnmaskPromptViews;
// Class that facilitates testing a CardUnmaskPromptViews.
class CardUnmaskPromptViewTesterViews : public CardUnmaskPromptViewTester {
public:
explicit CardUnmaskPromptViewTesterViews(CardUnmaskPromptViews* view);
~CardUnmaskPromptViewTesterViews() override;
void Close() override;
private:
CardUnmaskPromptViews* view_;
DISALLOW_COPY_AND_ASSIGN(CardUnmaskPromptViewTesterViews);
};
} // namespace autofill
#endif // CHROME_BROWSER_UI_VIEWS_AUTOFILL_CARD_UNMASK_PROMPT_VIEW_TESTER_VIEWS_H_
// Copyright 2015 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.
#ifndef CHROME_BROWSER_UI_VIEWS_AUTOFILL_CARD_UNMASK_PROMPT_VIEWS_H_
#define CHROME_BROWSER_UI_VIEWS_AUTOFILL_CARD_UNMASK_PROMPT_VIEWS_H_
#include "chrome/browser/ui/autofill/autofill_dialog_models.h"
#include "chrome/browser/ui/autofill/card_unmask_prompt_view.h"
#include "ui/views/controls/combobox/combobox_listener.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/window/dialog_delegate.h"
namespace views {
class Label;
class Checkbox;
}
namespace autofill {
class DecoratedTextfield;
class CardUnmaskPromptViews : public CardUnmaskPromptView,
views::ComboboxListener,
views::DialogDelegateView,
views::TextfieldController {
public:
explicit CardUnmaskPromptViews(CardUnmaskPromptController* controller);
~CardUnmaskPromptViews() override;
void Show();
// CardUnmaskPromptView
void ControllerGone() override;
void DisableAndWaitForVerification() override;
void GotVerificationResult(const base::string16& error_message,
bool allow_retry) override;
// views::DialogDelegateView
View* GetContentsView() override;
views::View* CreateFootnoteView() override;
// views::View
gfx::Size GetPreferredSize() const override;
void Layout() override;
int GetHeightForWidth(int width) const override;
void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
ui::ModalType GetModalType() const override;
base::string16 GetWindowTitle() const override;
void DeleteDelegate() override;
int GetDialogButtons() const override;
base::string16 GetDialogButtonLabel(ui::DialogButton button) const override;
bool ShouldDefaultButtonBeBlue() const override;
bool IsDialogButtonEnabled(ui::DialogButton button) const override;
views::View* GetInitiallyFocusedView() override;
bool Cancel() override;
bool Accept() override;
// views::TextfieldController
void ContentsChanged(views::Textfield* sender,
const base::string16& new_contents) override;
// views::ComboboxListener
void OnPerformAction(views::Combobox* combobox) override;
private:
friend class CardUnmaskPromptViewTesterViews;
void InitIfNecessary();
void SetRetriableErrorMessage(const base::string16& message);
bool ExpirationDateIsValid() const;
void SetInputsEnabled(bool enabled);
void ClosePrompt();
CardUnmaskPromptController* controller_;
views::View* main_contents_;
// The error label for permanent errors (where the user can't retry).
views::Label* permanent_error_label_;
DecoratedTextfield* cvc_input_;
// These will be null when expiration date is not required.
views::Combobox* month_input_;
views::Combobox* year_input_;
MonthComboboxModel month_combobox_model_;
YearComboboxModel year_combobox_model_;
// The error label for most errors, which lives beneath the inputs.
views::Label* error_label_;
views::Checkbox* storage_checkbox_;
views::View* progress_overlay_;
views::Label* progress_label_;
base::WeakPtrFactory<CardUnmaskPromptViews> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(CardUnmaskPromptViews);
};
} // namespace autofill
#endif // CHROME_BROWSER_UI_VIEWS_AUTOFILL_CARD_UNMASK_PROMPT_VIEWS_H_
......@@ -1941,6 +1941,7 @@
'browser/ui/views/autofill/autofill_popup_view_views.cc',
'browser/ui/views/autofill/autofill_popup_view_views.h',
'browser/ui/views/autofill/card_unmask_prompt_views.cc',
'browser/ui/views/autofill/card_unmask_prompt_views.h',
'browser/ui/views/autofill/decorated_textfield.cc',
'browser/ui/views/autofill/decorated_textfield.h',
'browser/ui/views/autofill/expanding_textfield.cc',
......
......@@ -429,6 +429,8 @@
'browser/ui/ash/volume_controller_browsertest_chromeos.cc',
'browser/ui/autofill/autofill_dialog_controller_browsertest.cc',
'browser/ui/autofill/autofill_dialog_view_tester.h',
'browser/ui/autofill/card_unmask_prompt_view_browsertest.cc',
'browser/ui/autofill/card_unmask_view_tester.h',
'browser/ui/autofill/mock_address_validator.cc',
'browser/ui/autofill/mock_address_validator.h',
'browser/ui/autofill/password_generation_popup_view_browsertest.cc',
......@@ -596,6 +598,8 @@
'browser/ui/views/autofill/autofill_dialog_view_tester_views.cc',
'browser/ui/views/autofill/autofill_dialog_view_tester_views.h',
'browser/ui/views/autofill/autofill_popup_base_view_browsertest.cc',
'browser/ui/views/autofill/card_unmask_prompt_view_tester_views.cc',
'browser/ui/views/autofill/card_unmask_prompt_view_tester_views.h',
'browser/ui/views/autofill/password_generation_popup_view_tester_views.cc',
'browser/ui/views/autofill/password_generation_popup_view_tester_views.h',
'browser/ui/views/collected_cookies_views_browsertest.cc',
......@@ -2270,8 +2274,6 @@
'browser/media_galleries/fileapi/iphoto_data_provider_browsertest.cc',
'browser/renderer_host/chrome_render_widget_host_view_mac_history_swiper_browsertest.mm',
'browser/spellchecker/spellcheck_message_filter_mac_browsertest.cc',
# TODO(bondd): Make this test work on other platforms.
'browser/ui/autofill/card_unmask_prompt_view_browsertest.cc',
],
'sources!': [
# TODO(groby): This test depends on hunspell and we cannot run it on
......
......@@ -209,6 +209,14 @@ CreditCard GetVerifiedCreditCard2() {
return credit_card;
}
CreditCard GetMaskedServerCard() {
CreditCard credit_card(CreditCard::MASKED_SERVER_CARD, "a123");
test::SetCreditCardInfo(&credit_card, "Bonnie Parker",
"2109" /* Mastercard */, "12", "2012");
credit_card.SetTypeForMaskedCard(kMasterCard);
return credit_card;
}
void SetProfileInfo(AutofillProfile* profile,
const char* first_name, const char* middle_name,
const char* last_name, const char* email, const char* company,
......
......@@ -69,6 +69,9 @@ CreditCard GetVerifiedCreditCard();
// Returns a verified credit card full of dummy info, different to the above.
CreditCard GetVerifiedCreditCard2();
// Returns a masked server card full of dummy info.
CreditCard GetMaskedServerCard();
// A unit testing utility that is common to a number of the Autofill unit
// tests. |SetProfileInfo| provides a quick way to populate a profile with
// c-strings.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment