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

Unified Diff: components/autofill/content/browser/content_autofill_driver_factory_unittest.cc

Issue 2606473003: Use AutofillDriver* in ContentAutofillDriverFactory when possible (Closed)
Patch Set: Fix Android compilation Created 4 years 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/content/browser/content_autofill_driver_factory_unittest.cc
diff --git a/components/autofill/content/browser/content_autofill_driver_factory_unittest.cc b/components/autofill/content/browser/content_autofill_driver_factory_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3239ad8d6265079a350ebf5bbb89dccbfa3813cb
--- /dev/null
+++ b/components/autofill/content/browser/content_autofill_driver_factory_unittest.cc
@@ -0,0 +1,108 @@
+// 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/content/browser/content_autofill_driver_factory.h"
+
+#include <memory>
+//#include <utility>
+
+#include "base/bind.h"
+#include "base/memory/ptr_util.h"
+#include "components/autofill/core/browser/test_autofill_driver.h"
+#include "content/public/browser/navigation_details.h"
+#include "content/public/common/frame_navigate_params.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace autofill {
+
+namespace {
+
+constexpr char kAppLocale[] = "does_not_matter";
+constexpr AutofillManager::AutofillDownloadManagerState kDownloadState =
+ AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER;
+
+// Creates a MockContentAutofillDriver, ignoring the arguments.
+std::unique_ptr<AutofillDriver> CreateMockDriver(
+ content::RenderFrameHost*,
+ AutofillClient*,
+ const std::string&,
+ AutofillManager::AutofillDownloadManagerState);
+
+class TestContentAutofillDriverFactory : public ContentAutofillDriverFactory {
+ // This class is on purpose empty and hence identical to the
+ // ContentAutofillDriverFactory, which is being tested here. The only
+ // difference is that the constructor is made public.
+ public:
+ TestContentAutofillDriverFactory()
+ : ContentAutofillDriverFactory(nullptr,
+ nullptr,
+ kAppLocale,
+ kDownloadState,
+ base::Bind(CreateMockDriver)) {}
+};
+
+class MockAutofillDriver : public TestAutofillDriver {
+ public:
+ MOCK_METHOD0(NavigatedToDifferentPage, void());
+};
+
+std::unique_ptr<AutofillDriver> CreateMockDriver(
+ content::RenderFrameHost*,
+ AutofillClient*,
+ const std::string&,
+ AutofillManager::AutofillDownloadManagerState) {
+ return base::MakeUnique<MockAutofillDriver>();
+}
+
+// For a non-zero |id| creates a non-null pointer to a RenderFrameHost. The
+// pointers returned for different |id| values are different. The pointers DO
+// NOT point to a real RenderFrameHost object, but because the
+// ContentAutofillDriverFactory only uses them as map keys, and the
+// MockAutofillDriver instances used here ignore the RFH passed to them, this is
+// safe.
+content::RenderFrameHost* FakeRFH(int id) {
+ return reinterpret_cast<content::RenderFrameHost*>(id);
+}
+
+// Convenience accessor with a cast to MockAutofillDriver.
+MockAutofillDriver& GetDriver(TestContentAutofillDriverFactory* factory,
+ content::RenderFrameHost* frame) {
+ return *static_cast<MockAutofillDriver*>(factory->DriverForFrame(frame));
+}
+
+} // namespace
+
+class ContentAutofillDriverFactoryTest : public testing::Test {
+ protected:
+ base::MessageLoop message_loop_; // For TestAutofillDriver.
+ TestContentAutofillDriverFactory factory_;
+};
+
+TEST_F(ContentAutofillDriverFactoryTest, DidNavigateAnyFrame_DifferentPage) {
+ content::RenderFrameHost* const frame = FakeRFH(1);
+ factory_.RenderFrameCreated(frame);
+
+ content::LoadCommittedDetails details = content::LoadCommittedDetails();
+ details.is_main_frame = true;
+ details.is_in_page = false;
+ EXPECT_TRUE(details.is_navigation_to_different_page());
+ EXPECT_CALL(GetDriver(&factory_, frame), NavigatedToDifferentPage());
+ factory_.DidNavigateAnyFrame(frame, details, content::FrameNavigateParams());
+}
+
+TEST_F(ContentAutofillDriverFactoryTest, DidNavigateAnyFrame_SamePage) {
+ content::RenderFrameHost* const frame = FakeRFH(1);
+ factory_.RenderFrameCreated(frame);
+
+ content::LoadCommittedDetails details = content::LoadCommittedDetails();
+ details.is_main_frame = false;
+ EXPECT_FALSE(details.is_navigation_to_different_page());
+ content::FrameNavigateParams params = content::FrameNavigateParams();
+ EXPECT_CALL(GetDriver(&factory_, frame), NavigatedToDifferentPage())
+ .Times(0);
+ factory_.DidNavigateAnyFrame(frame, details, content::FrameNavigateParams());
+}
+
+} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698