// Copyright 2014 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/autofill/save_card_bubble_controller_impl.h" #include #include #include "base/json/json_reader.h" #include "base/macros.h" #include "base/strings/utf_string_conversions.h" #include "base/test/histogram_tester.h" #include "base/values.h" #include "chrome/browser/ui/autofill/save_card_bubble_view.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/test/base/browser_with_test_window_test.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h" #include "components/autofill/core/browser/autofill_metrics.h" #include "components/autofill/core/browser/credit_card.h" #include "content/public/browser/navigation_details.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" using base::Bucket; using testing::ElementsAre; namespace autofill { class TestSaveCardBubbleControllerImpl : public SaveCardBubbleControllerImpl { public: static void CreateForTesting(content::WebContents* web_contents) { web_contents->SetUserData( UserDataKey(), new TestSaveCardBubbleControllerImpl(web_contents)); } explicit TestSaveCardBubbleControllerImpl(content::WebContents* web_contents) : SaveCardBubbleControllerImpl(web_contents) {} void set_elapsed(base::TimeDelta elapsed) { elapsed_ = elapsed; } using SaveCardBubbleControllerImpl::DidNavigateMainFrame; protected: base::TimeDelta Elapsed() const override { return elapsed_; } private: base::TimeDelta elapsed_; }; class SaveCardBubbleControllerImplTest : public BrowserWithTestWindowTest { public: SaveCardBubbleControllerImplTest() {} void SetUp() override { BrowserWithTestWindowTest::SetUp(); AddTab(browser(), GURL("about:blank")); TestSaveCardBubbleControllerImpl::CreateForTesting( browser()->tab_strip_model()->GetActiveWebContents()); } BrowserWindow* CreateBrowserWindow() override { return new SaveCardBubbleTestBrowserWindow(); } void SetLegalMessage(const std::string& message_json) { scoped_ptr value(base::JSONReader::Read(message_json)); ASSERT_TRUE(value); base::DictionaryValue* dictionary; ASSERT_TRUE(value->GetAsDictionary(&dictionary)); scoped_ptr legal_message = dictionary->CreateDeepCopy(); controller()->ShowBubbleForUpload(CreditCard(), std::move(legal_message), base::Bind(&SaveCardCallback)); } void ShowLocalBubble() { controller()->ShowBubbleForLocalSave(CreditCard(), base::Bind(&SaveCardCallback)); } void ShowUploadBubble() { SetLegalMessage( "{" " \"line\" : [ {" " \"template\": \"This is the entire message.\"" " } ]" "}"); } void CloseAndReshowBubble() { controller()->OnBubbleClosed(); controller()->ReshowBubble(); } protected: TestSaveCardBubbleControllerImpl* controller() { return static_cast( TestSaveCardBubbleControllerImpl::FromWebContents( browser()->tab_strip_model()->GetActiveWebContents())); } private: class TestSaveCardBubbleView : public SaveCardBubbleView { void Hide() override{}; }; class SaveCardBubbleTestBrowserWindow : public TestBrowserWindow { public: SaveCardBubbleView* ShowSaveCreditCardBubble( content::WebContents* contents, SaveCardBubbleController* controller, bool user_gesture) override { if (!save_card_bubble_view_) save_card_bubble_view_.reset(new TestSaveCardBubbleView()); return save_card_bubble_view_.get(); } private: scoped_ptr save_card_bubble_view_; }; static void SaveCardCallback() {} DISALLOW_COPY_AND_ASSIGN(SaveCardBubbleControllerImplTest); }; TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_ShowBubble) { base::HistogramTester histogram_tester; ShowLocalBubble(); EXPECT_THAT( histogram_tester.GetAllSamples( "Autofill.SaveCreditCardPrompt.Local.FirstShow"), ElementsAre(Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1))); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_Reshows_ShowBubble) { ShowLocalBubble(); base::HistogramTester histogram_tester; CloseAndReshowBubble(); EXPECT_THAT( histogram_tester.GetAllSamples( "Autofill.SaveCreditCardPrompt.Local.Reshows"), ElementsAre(Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1))); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_FirstShow_ShowBubble) { base::HistogramTester histogram_tester; ShowUploadBubble(); EXPECT_THAT( histogram_tester.GetAllSamples( "Autofill.SaveCreditCardPrompt.Upload.FirstShow"), ElementsAre(Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1))); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_Reshows_ShowBubble) { ShowUploadBubble(); base::HistogramTester histogram_tester; CloseAndReshowBubble(); EXPECT_THAT( histogram_tester.GetAllSamples( "Autofill.SaveCreditCardPrompt.Upload.Reshows"), ElementsAre(Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1))); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_SaveButton) { ShowLocalBubble(); base::HistogramTester histogram_tester; controller()->OnSaveButton(); controller()->OnBubbleClosed(); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Local.FirstShow", AutofillMetrics::SAVE_CARD_PROMPT_END_ACCEPTED, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_Reshows_SaveButton) { ShowLocalBubble(); CloseAndReshowBubble(); base::HistogramTester histogram_tester; controller()->OnSaveButton(); controller()->OnBubbleClosed(); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Local.Reshows", AutofillMetrics::SAVE_CARD_PROMPT_END_ACCEPTED, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_CancelButton) { ShowLocalBubble(); base::HistogramTester histogram_tester; controller()->OnCancelButton(); controller()->OnBubbleClosed(); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Local.FirstShow", AutofillMetrics::SAVE_CARD_PROMPT_END_DENIED, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_Reshows_CancelButton) { ShowLocalBubble(); CloseAndReshowBubble(); base::HistogramTester histogram_tester; controller()->OnCancelButton(); controller()->OnBubbleClosed(); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Local.Reshows", AutofillMetrics::SAVE_CARD_PROMPT_END_DENIED, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_NavigateWhileShowing) { ShowLocalBubble(); base::HistogramTester histogram_tester; // Fake-navigate after bubble has been visible for a long time. controller()->set_elapsed(base::TimeDelta::FromMinutes(1)); controller()->DidNavigateMainFrame(content::LoadCommittedDetails(), content::FrameNavigateParams()); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Local.FirstShow", AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_SHOWING, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_Reshows_NavigateWhileShowing) { ShowLocalBubble(); CloseAndReshowBubble(); base::HistogramTester histogram_tester; // Fake-navigate after bubble has been visible for a long time. controller()->set_elapsed(base::TimeDelta::FromMinutes(1)); controller()->DidNavigateMainFrame(content::LoadCommittedDetails(), content::FrameNavigateParams()); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Local.Reshows", AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_SHOWING, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_NavigateWhileHidden) { ShowLocalBubble(); base::HistogramTester histogram_tester; controller()->OnBubbleClosed(); // Fake-navigate after bubble has been visible for a long time. controller()->set_elapsed(base::TimeDelta::FromMinutes(1)); controller()->DidNavigateMainFrame(content::LoadCommittedDetails(), content::FrameNavigateParams()); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Local.FirstShow", AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_HIDDEN, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_Reshows_NavigateWhileHidden) { ShowLocalBubble(); CloseAndReshowBubble(); base::HistogramTester histogram_tester; controller()->OnBubbleClosed(); // Fake-navigate after bubble has been visible for a long time. controller()->set_elapsed(base::TimeDelta::FromMinutes(1)); controller()->DidNavigateMainFrame(content::LoadCommittedDetails(), content::FrameNavigateParams()); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Local.Reshows", AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_HIDDEN, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_FirstShow_LearnMore) { ShowUploadBubble(); base::HistogramTester histogram_tester; controller()->OnLearnMoreClicked(); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Upload.FirstShow", AutofillMetrics::SAVE_CARD_PROMPT_DISMISS_CLICK_LEARN_MORE, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_Reshows_LearnMore) { ShowUploadBubble(); CloseAndReshowBubble(); base::HistogramTester histogram_tester; controller()->OnLearnMoreClicked(); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Upload.Reshows", AutofillMetrics::SAVE_CARD_PROMPT_DISMISS_CLICK_LEARN_MORE, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_FirstShow_LegalMessageLink) { ShowUploadBubble(); base::HistogramTester histogram_tester; controller()->OnLegalMessageLinkClicked(GURL("http://www.example.com")); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Upload.FirstShow", AutofillMetrics::SAVE_CARD_PROMPT_DISMISS_CLICK_LEGAL_MESSAGE, 1); } TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_Reshows_LegalMessageLink) { ShowUploadBubble(); CloseAndReshowBubble(); base::HistogramTester histogram_tester; controller()->OnLegalMessageLinkClicked(GURL("http://www.example.com")); histogram_tester.ExpectUniqueSample( "Autofill.SaveCreditCardPrompt.Upload.Reshows", AutofillMetrics::SAVE_CARD_PROMPT_DISMISS_CLICK_LEGAL_MESSAGE, 1); } // SAVE_CARD_PROMPT_END_INVALID_LEGAL_MESSAGE is only possible for // Upload.FirstShow. TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_FirstShow_InvalidLegalMessage) { base::HistogramTester histogram_tester; // Legal message is invalid because it's missing the url. SetLegalMessage( "{" " \"line\" : [ {" " \"template\": \"Panda {0}.\"," " \"template_parameter\": [ {" " \"display_text\": \"bear\"" " } ]" " } ]" "}"); EXPECT_THAT( histogram_tester.GetAllSamples( "Autofill.SaveCreditCardPrompt.Upload.FirstShow"), ElementsAre( Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1), Bucket(AutofillMetrics::SAVE_CARD_PROMPT_END_INVALID_LEGAL_MESSAGE, 1))); } } // namespace autofill