| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/autofill/content/browser/content_autofill_driver_factory.h" |
| 6 |
| 7 #include <memory> |
| 8 //#include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "components/autofill/core/browser/test_autofill_driver.h" |
| 13 #include "content/public/browser/navigation_details.h" |
| 14 #include "content/public/common/frame_navigate_params.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace autofill { |
| 19 |
| 20 namespace { |
| 21 |
| 22 constexpr char kAppLocale[] = "does_not_matter"; |
| 23 constexpr AutofillManager::AutofillDownloadManagerState kDownloadState = |
| 24 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER; |
| 25 |
| 26 // Creates a MockContentAutofillDriver, ignoring the arguments. |
| 27 std::unique_ptr<AutofillDriver> CreateMockDriver( |
| 28 content::RenderFrameHost*, |
| 29 AutofillClient*, |
| 30 const std::string&, |
| 31 AutofillManager::AutofillDownloadManagerState); |
| 32 |
| 33 class TestContentAutofillDriverFactory : public ContentAutofillDriverFactory { |
| 34 // This class is on purpose empty and hence identical to the |
| 35 // ContentAutofillDriverFactory, which is being tested here. The only |
| 36 // difference is that the constructor is made public. |
| 37 public: |
| 38 TestContentAutofillDriverFactory() |
| 39 : ContentAutofillDriverFactory(nullptr, |
| 40 nullptr, |
| 41 kAppLocale, |
| 42 kDownloadState, |
| 43 base::Bind(CreateMockDriver)) {} |
| 44 }; |
| 45 |
| 46 class MockAutofillDriver : public TestAutofillDriver { |
| 47 public: |
| 48 MOCK_METHOD0(NavigatedToDifferentPage, void()); |
| 49 }; |
| 50 |
| 51 std::unique_ptr<AutofillDriver> CreateMockDriver( |
| 52 content::RenderFrameHost*, |
| 53 AutofillClient*, |
| 54 const std::string&, |
| 55 AutofillManager::AutofillDownloadManagerState) { |
| 56 return base::MakeUnique<MockAutofillDriver>(); |
| 57 } |
| 58 |
| 59 // For a non-zero |id| creates a non-null pointer to a RenderFrameHost. The |
| 60 // pointers returned for different |id| values are different. The pointers DO |
| 61 // NOT point to a real RenderFrameHost object, but because the |
| 62 // ContentAutofillDriverFactory only uses them as map keys, and the |
| 63 // MockAutofillDriver instances used here ignore the RFH passed to them, this is |
| 64 // safe. |
| 65 content::RenderFrameHost* FakeRFH(int id) { |
| 66 return reinterpret_cast<content::RenderFrameHost*>(id); |
| 67 } |
| 68 |
| 69 // Convenience accessor with a cast to MockAutofillDriver. |
| 70 MockAutofillDriver& GetDriver(TestContentAutofillDriverFactory* factory, |
| 71 content::RenderFrameHost* frame) { |
| 72 return *static_cast<MockAutofillDriver*>(factory->DriverForFrame(frame)); |
| 73 } |
| 74 |
| 75 } // namespace |
| 76 |
| 77 class ContentAutofillDriverFactoryTest : public testing::Test { |
| 78 protected: |
| 79 base::MessageLoop message_loop_; // For TestAutofillDriver. |
| 80 TestContentAutofillDriverFactory factory_; |
| 81 }; |
| 82 |
| 83 TEST_F(ContentAutofillDriverFactoryTest, DidNavigateAnyFrame_DifferentPage) { |
| 84 content::RenderFrameHost* const frame = FakeRFH(1); |
| 85 factory_.RenderFrameCreated(frame); |
| 86 |
| 87 content::LoadCommittedDetails details = content::LoadCommittedDetails(); |
| 88 details.is_main_frame = true; |
| 89 details.is_in_page = false; |
| 90 EXPECT_TRUE(details.is_navigation_to_different_page()); |
| 91 EXPECT_CALL(GetDriver(&factory_, frame), NavigatedToDifferentPage()); |
| 92 factory_.DidNavigateAnyFrame(frame, details, content::FrameNavigateParams()); |
| 93 } |
| 94 |
| 95 TEST_F(ContentAutofillDriverFactoryTest, DidNavigateAnyFrame_SamePage) { |
| 96 content::RenderFrameHost* const frame = FakeRFH(1); |
| 97 factory_.RenderFrameCreated(frame); |
| 98 |
| 99 content::LoadCommittedDetails details = content::LoadCommittedDetails(); |
| 100 details.is_main_frame = false; |
| 101 EXPECT_FALSE(details.is_navigation_to_different_page()); |
| 102 content::FrameNavigateParams params = content::FrameNavigateParams(); |
| 103 EXPECT_CALL(GetDriver(&factory_, frame), NavigatedToDifferentPage()) |
| 104 .Times(0); |
| 105 factory_.DidNavigateAnyFrame(frame, details, content::FrameNavigateParams()); |
| 106 } |
| 107 |
| 108 } // namespace autofill |
| OLD | NEW |