Simplify TabInteractionRecorderAndroid test.
This CL removes some unnecessary classes and makes the lifetime of
Autofill objects more natural: In production code, the driver always
owns the manager. Finally, it removes calls to the TaskEnvironment to
run until idle, since the TestAutofill* classes already take care
of waiting themselves.
Bug: 1007974
Change-Id: I4ea4731f33e1c3284e406d2935b256c3a06be1ba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5058815
Reviewed-by: Peter Conn <peconn@chromium.org>
Commit-Queue: Jan Keitel <jkeitel@google.com>
Cr-Commit-Position: refs/heads/main@{#1229356}
diff --git a/chrome/browser/android/customtabs/tab_interaction_recorder_android_unittest.cc b/chrome/browser/android/customtabs/tab_interaction_recorder_android_unittest.cc
index 74f331d..2fc0d393 100644
--- a/chrome/browser/android/customtabs/tab_interaction_recorder_android_unittest.cc
+++ b/chrome/browser/android/customtabs/tab_interaction_recorder_android_unittest.cc
@@ -10,13 +10,8 @@
#include "base/functional/callback_helpers.h"
#include "base/test/mock_callback.h"
-#include "base/test/scoped_feature_list.h"
-#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "chrome/browser/ui/tab_helpers.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
-#include "components/autofill/content/browser/content_autofill_driver.h"
-#include "components/autofill/content/browser/content_autofill_driver_factory_test_api.h"
-#include "components/autofill/content/browser/content_autofill_driver_test_api.h"
#include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/test_autofill_client.h"
@@ -25,8 +20,6 @@
#include "components/autofill/core/common/autofill_tick_clock.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/unique_ids.h"
-#include "content/public/browser/browser_task_traits.h"
-#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/navigation_simulator.h"
@@ -40,56 +33,27 @@
using ::autofill::AutofillManager;
using ::autofill::FormData;
+using ::autofill::TestAutofillClient;
+using ::autofill::TestAutofillDriver;
+using ::autofill::TestBrowserAutofillManager;
using ::autofill::test::CreateTestAddressFormData;
using ::testing::_;
using ::testing::NiceMock;
-class MockAutofillClient : public autofill::TestAutofillClient {
- public:
- MockAutofillClient() = default;
- MockAutofillClient(const MockAutofillClient&) = delete;
- MockAutofillClient& operator=(const MockAutofillClient&) = delete;
- ~MockAutofillClient() override = default;
-};
-
-class MockAutofillDriver : public autofill::TestAutofillDriver {
- public:
- MockAutofillDriver() = default;
- MockAutofillDriver(const MockAutofillDriver&) = delete;
- MockAutofillDriver& operator=(const MockAutofillDriver&) = delete;
- ~MockAutofillDriver() override = default;
-};
-
-class MockAutofillManager : public autofill::TestBrowserAutofillManager {
- public:
- MockAutofillManager(autofill::TestAutofillDriver* driver,
- autofill::TestAutofillClient* client)
- : autofill::TestBrowserAutofillManager(driver, client) {}
- MockAutofillManager(const MockAutofillManager&) = delete;
- MockAutofillManager& operator=(const MockAutofillManager&) = delete;
- ~MockAutofillManager() override = default;
-};
-
-void OnTextFieldDidChangeForAutofillManager(
- AutofillManager* autofill_manager,
- base::test::TaskEnvironment& task_environment) {
+void OnTextFieldDidChangeForAutofillManager(AutofillManager& autofill_manager) {
FormData form = CreateTestAddressFormData();
- autofill_manager->OnTextFieldDidChange(
+ autofill_manager.OnTextFieldDidChange(
form, form.fields.front(), gfx::RectF(),
autofill::AutofillTickClock::NowTicks());
- task_environment.RunUntilIdle();
}
-void OnFormsSeenForAutofillManager(
- AutofillManager* autofill_manager,
- content::RenderFrameHost* rfh,
- base::test::TaskEnvironment& task_environment) {
+void OnFormsSeenForAutofillManager(AutofillManager& autofill_manager,
+ content::RenderFrameHost* rfh) {
FormData form = CreateTestAddressFormData();
if (rfh) {
form.host_frame = autofill::LocalFrameToken(rfh->GetFrameToken().value());
}
- autofill_manager->OnFormsSeen({form}, std::vector<autofill::FormGlobalId>());
- task_environment.RunUntilIdle();
+ autofill_manager.OnFormsSeen({form}, {});
}
} // namespace
@@ -99,57 +63,59 @@
void SetUp() override {
client_.SetPrefs(autofill::test::PrefServiceForTesting());
- driver_ = std::make_unique<NiceMock<MockAutofillDriver>>();
- manager_ = std::make_unique<MockAutofillManager>(driver_.get(), &client_);
+ driver_ = std::make_unique<TestAutofillDriver>();
+ driver_->set_autofill_manager(
+ std::make_unique<TestBrowserAutofillManager>(driver_.get(), &client_));
}
- void TearDown() override { driver_.reset(); }
-
- MockAutofillManager* autofill_manager() { return manager_.get(); }
-
- void DestroyManager() { manager_.release(); }
-
protected:
+ void DestroyDriver() { driver_.reset(); }
+
+ TestBrowserAutofillManager& autofill_manager() {
+ return static_cast<TestBrowserAutofillManager&>(
+ driver_->GetAutofillManager());
+ }
+
+ private:
base::test::TaskEnvironment task_environment_;
autofill::test::AutofillUnitTestEnvironment autofill_test_environment_;
- NiceMock<MockAutofillClient> client_;
- std::unique_ptr<MockAutofillDriver> driver_;
- std::unique_ptr<MockAutofillManager> manager_;
+ TestAutofillClient client_;
+ std::unique_ptr<TestAutofillDriver> driver_;
};
TEST_F(AutofillObserverImplTest, TestFormInteraction) {
base::MockOnceCallback<void(content::GlobalRenderFrameHostId)> callback;
content::GlobalRenderFrameHostId id = content::GlobalRenderFrameHostId();
- AutofillObserverImpl obsever(id, autofill_manager(), callback.Get());
+ AutofillObserverImpl observer(id, &autofill_manager(), callback.Get());
- EXPECT_CALL(callback, Run(id)).Times(1);
- OnTextFieldDidChangeForAutofillManager(autofill_manager(), task_environment_);
+ EXPECT_CALL(callback, Run(id));
+ OnTextFieldDidChangeForAutofillManager(autofill_manager());
// Observer should no longer get notified after the first interaction.
EXPECT_CALL(callback, Run(id)).Times(0);
- OnTextFieldDidChangeForAutofillManager(autofill_manager(), task_environment_);
+ OnTextFieldDidChangeForAutofillManager(autofill_manager());
}
TEST_F(AutofillObserverImplTest, TestNoFormInteraction) {
content::GlobalRenderFrameHostId id = content::GlobalRenderFrameHostId();
base::MockOnceCallback<void(content::GlobalRenderFrameHostId)> callback;
- auto* observer =
- new AutofillObserverImpl(id, autofill_manager(), callback.Get());
+ auto observer = std::make_unique<AutofillObserverImpl>(
+ id, &autofill_manager(), callback.Get());
EXPECT_CALL(callback, Run(id)).Times(0);
- delete observer;
+ observer.reset();
}
TEST_F(AutofillObserverImplTest, TestAutofillManagerDestroy) {
content::GlobalRenderFrameHostId id = content::GlobalRenderFrameHostId();
base::MockOnceCallback<void(content::GlobalRenderFrameHostId)> callback;
- auto* observer =
- new AutofillObserverImpl(id, autofill_manager(), callback.Get());
+ auto observer = std::make_unique<AutofillObserverImpl>(
+ id, &autofill_manager(), callback.Get());
- DestroyManager();
+ DestroyDriver();
EXPECT_CALL(callback, Run(id)).Times(0);
- delete observer;
+ observer.reset();
}
// === TabInteractionRecorderAndroidTest ===
@@ -163,14 +129,9 @@
ChromeRenderViewHostTestHarness::SetUp();
client_.SetPrefs(autofill::test::PrefServiceForTesting());
- driver_ = std::make_unique<NiceMock<MockAutofillDriver>>();
- manager_ = std::make_unique<MockAutofillManager>(driver_.get(), &client_);
- }
-
- void TearDown() override {
- manager_.reset();
- driver_.reset();
- ChromeRenderViewHostTestHarness::TearDown();
+ driver_ = std::make_unique<TestAutofillDriver>();
+ driver_->set_autofill_manager(
+ std::make_unique<TestBrowserAutofillManager>(driver_.get(), &client_));
}
std::unique_ptr<content::WebContents> CreateTestWebContents() {
@@ -179,7 +140,7 @@
TabInteractionRecorderAndroid::CreateForWebContents(contents.get());
auto* helper =
TabInteractionRecorderAndroid::FromWebContents(contents.get());
- helper->SetAutofillManagerForTest(autofill_manager());
+ helper->SetAutofillManagerForTest(&autofill_manager());
// Simulate a navigation event to force the initialization of the main
// frame.
@@ -189,14 +150,16 @@
return contents;
}
- MockAutofillManager* autofill_manager() { return manager_.get(); }
-
protected:
- base::test::ScopedFeatureList test_feature_list_;
+ TestBrowserAutofillManager& autofill_manager() {
+ return static_cast<TestBrowserAutofillManager&>(
+ driver_->GetAutofillManager());
+ }
+
+ private:
autofill::test::AutofillUnitTestEnvironment autofill_test_environment_;
- NiceMock<MockAutofillClient> client_;
- std::unique_ptr<MockAutofillDriver> driver_;
- std::unique_ptr<MockAutofillManager> manager_;
+ TestAutofillClient client_;
+ std::unique_ptr<TestAutofillDriver> driver_;
};
TEST_F(TabInteractionRecorderAndroidTest, HadFormInteraction) {
@@ -206,8 +169,7 @@
EXPECT_FALSE(helper->has_form_interactions_in_session());
EXPECT_EQ(nullptr, FormInteractionData::GetForCurrentDocument(
contents->GetPrimaryMainFrame()));
- OnTextFieldDidChangeForAutofillManager(autofill_manager(),
- *task_environment());
+ OnTextFieldDidChangeForAutofillManager(autofill_manager());
EXPECT_TRUE(helper->has_form_interactions_in_session());
EXPECT_TRUE(FormInteractionData::GetForCurrentDocument(
contents->GetPrimaryMainFrame())
@@ -225,8 +187,7 @@
EXPECT_FALSE(helper->has_form_interactions_in_session());
EXPECT_EQ(nullptr, FormInteractionData::GetForCurrentDocument(
contents->GetPrimaryMainFrame()));
- OnTextFieldDidChangeForAutofillManager(autofill_manager(),
- *task_environment());
+ OnTextFieldDidChangeForAutofillManager(autofill_manager());
EXPECT_TRUE(helper->has_form_interactions_in_session());
EXPECT_TRUE(FormInteractionData::GetForCurrentDocument(
contents->GetPrimaryMainFrame())
@@ -294,8 +255,7 @@
helper->DidGetUserInteraction(blink::WebTouchEvent());
EXPECT_EQ(nullptr, FormInteractionData::GetForCurrentDocument(
contents->GetPrimaryMainFrame()));
- OnTextFieldDidChangeForAutofillManager(autofill_manager(),
- *task_environment());
+ OnTextFieldDidChangeForAutofillManager(autofill_manager());
EXPECT_TRUE(FormInteractionData::GetForCurrentDocument(
contents->GetPrimaryMainFrame())
->FormInteractionData::GetHasFormInteractionData());
@@ -318,8 +278,8 @@
TEST_F(TabInteractionRecorderAndroidTest, TestFormSeen) {
std::unique_ptr<content::WebContents> contents = CreateTestWebContents();
- OnFormsSeenForAutofillManager(
- autofill_manager(), contents->GetPrimaryMainFrame(), *task_environment());
+ OnFormsSeenForAutofillManager(autofill_manager(),
+ contents->GetPrimaryMainFrame());
EXPECT_NE(FormInteractionData::GetForCurrentDocument(
contents->GetPrimaryMainFrame()),
@@ -331,8 +291,7 @@
TEST_F(TabInteractionRecorderAndroidTest, TestFormSeenInDifferentFrame) {
std::unique_ptr<content::WebContents> contents = CreateTestWebContents();
- OnFormsSeenForAutofillManager(autofill_manager(), nullptr,
- *task_environment());
+ OnFormsSeenForAutofillManager(autofill_manager(), nullptr);
EXPECT_EQ(FormInteractionData::GetForCurrentDocument(
contents->GetPrimaryMainFrame()),