1 // Copyright 2017 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/core/browser/autofill_driver_factory.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/test/task_environment.h"
13 #include "components/autofill/core/browser/test_autofill_client.h"
14 #include "components/autofill/core/browser/test_autofill_driver.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 class MockAutofillClient : public TestAutofillClient {
23  public:
24   MOCK_METHOD1(HideAutofillPopup, void(PopupHidingReason));
25 };
26 
27 // Just a stub AutofillDriver implementation which announces its construction
28 // and desctruction by updating the passed |instance_counter|. It also records
29 // when "user gesture observed" was signalled to it.
30 class CountingAutofillDriver : public TestAutofillDriver {
31  public:
CountingAutofillDriver(int * instance_counter)32   CountingAutofillDriver(int* instance_counter)
33       : instance_counter_(instance_counter) {
34     ++*instance_counter;
35   }
36 
~CountingAutofillDriver()37   ~CountingAutofillDriver() override { --*instance_counter_; }
38 
39  private:
40   int* const instance_counter_;
41 
42   DISALLOW_COPY_AND_ASSIGN(CountingAutofillDriver);
43 };
44 
45 // Code-wise this class is identitcal to AutofillDriverFactory, but exposes the
46 // protected API to the test. Do not modify any of the methods, only include
47 // "using" declarations to make the AutofillDriverFactory methods public.
48 class PublicAutofillDriverFactory : public AutofillDriverFactory {
49  public:
PublicAutofillDriverFactory(AutofillClient * client)50   explicit PublicAutofillDriverFactory(AutofillClient* client)
51       : AutofillDriverFactory(client) {}
52 
~PublicAutofillDriverFactory()53   ~PublicAutofillDriverFactory() {}
54 
55   using AutofillDriverFactory::AddForKey;
56   using AutofillDriverFactory::DeleteForKey;
57 };
58 
59 // Wrapper around an integer, checking that the integer is 0 on desctruction.
60 class CheckedInt {
61  public:
CheckedInt()62   CheckedInt() {}
63 
~CheckedInt()64   ~CheckedInt() { EXPECT_EQ(0, val_); }
65 
val()66   int* val() { return &val_; }
67 
68  private:
69   int val_ = 0;
70 };
71 
72 }  // namespace
73 
74 class AutofillDriverFactoryTest : public testing::Test {
75  public:
AutofillDriverFactoryTest()76   AutofillDriverFactoryTest() : factory_(&client_) {}
77 
~AutofillDriverFactoryTest()78   ~AutofillDriverFactoryTest() override {}
79 
80   // AutofillDriverFactory stores drivers in a map with keys, which are void*
81   // pointers. The factory never dereferences them, so their value does not
82   // matter. This is a handy function to create such pointers from integer
83   // constants.
KeyFrom(int x)84   void* KeyFrom(int x) { return reinterpret_cast<void*>(x); }
85 
86   // Convenience accessor with a cast to CountingAutofillDriver.
GetDriver(void * key)87   CountingAutofillDriver* GetDriver(void* key) {
88     return static_cast<CountingAutofillDriver*>(factory_.DriverForKey(key));
89   }
90 
CreateDriver()91   std::unique_ptr<AutofillDriver> CreateDriver() {
92     ++drivers_created_;
93     return std::make_unique<CountingAutofillDriver>(instance_counter_.val());
94   }
95 
96   base::RepeatingCallback<std::unique_ptr<AutofillDriver>()>
CreateDriverCallback()97   CreateDriverCallback() {
98     return base::BindRepeating(&AutofillDriverFactoryTest::CreateDriver,
99                                base::Unretained(this));
100   }
101 
102  protected:
103   // For TestAutofillDriver.
104   base::test::SingleThreadTaskEnvironment task_environment_;
105 
106   MockAutofillClient client_;
107 
108   CheckedInt instance_counter_;
109 
110   PublicAutofillDriverFactory factory_;
111 
112   // How many AutofillDriver instances were created with CreateDriver().
113   int drivers_created_ = 0;
114 };
115 
TEST_F(AutofillDriverFactoryTest,DriverForKey_NoKey)116 TEST_F(AutofillDriverFactoryTest, DriverForKey_NoKey) {
117   EXPECT_FALSE(factory_.DriverForKey(nullptr));
118   EXPECT_FALSE(factory_.DriverForKey(KeyFrom(1)));
119 }
120 
TEST_F(AutofillDriverFactoryTest,DriverForKey_OneKey)121 TEST_F(AutofillDriverFactoryTest, DriverForKey_OneKey) {
122   factory_.AddForKey(KeyFrom(1), CreateDriverCallback());
123   EXPECT_FALSE(factory_.DriverForKey(nullptr));
124   EXPECT_TRUE(factory_.DriverForKey(KeyFrom(1)));
125 }
126 
TEST_F(AutofillDriverFactoryTest,DriverForKey_TwoKeys)127 TEST_F(AutofillDriverFactoryTest, DriverForKey_TwoKeys) {
128   factory_.AddForKey(KeyFrom(1), CreateDriverCallback());
129   EXPECT_FALSE(factory_.DriverForKey(nullptr));
130   EXPECT_TRUE(factory_.DriverForKey(KeyFrom(1)));
131   EXPECT_EQ(1, *instance_counter_.val());
132 
133   factory_.AddForKey(nullptr, CreateDriverCallback());
134   EXPECT_TRUE(factory_.DriverForKey(nullptr));
135   EXPECT_TRUE(factory_.DriverForKey(KeyFrom(1)));
136   EXPECT_EQ(2, *instance_counter_.val());
137 }
138 
TEST_F(AutofillDriverFactoryTest,AddForKey_Duplicated)139 TEST_F(AutofillDriverFactoryTest, AddForKey_Duplicated) {
140   EXPECT_FALSE(factory_.DriverForKey(KeyFrom(1)));
141 
142   factory_.AddForKey(KeyFrom(1), CreateDriverCallback());
143   EXPECT_TRUE(factory_.DriverForKey(KeyFrom(1)));
144   EXPECT_EQ(1, drivers_created_);
145   EXPECT_EQ(1, *instance_counter_.val());
146 
147   factory_.AddForKey(KeyFrom(1), CreateDriverCallback());
148   EXPECT_TRUE(factory_.DriverForKey(KeyFrom(1)));
149   EXPECT_EQ(1, drivers_created_);
150   EXPECT_EQ(1, *instance_counter_.val());
151 }
152 
TEST_F(AutofillDriverFactoryTest,DeleteForKey)153 TEST_F(AutofillDriverFactoryTest, DeleteForKey) {
154   EXPECT_FALSE(factory_.DriverForKey(KeyFrom(1)));
155   EXPECT_EQ(0, *instance_counter_.val());
156 
157   factory_.AddForKey(KeyFrom(1), CreateDriverCallback());
158   EXPECT_TRUE(factory_.DriverForKey(KeyFrom(1)));
159   EXPECT_EQ(1, *instance_counter_.val());
160 
161   factory_.DeleteForKey(KeyFrom(1));
162   EXPECT_FALSE(factory_.DriverForKey(KeyFrom(1)));
163   EXPECT_EQ(0, *instance_counter_.val());
164 
165   // Duplicated calls should raise no errors.
166   factory_.DeleteForKey(KeyFrom(1));
167   EXPECT_FALSE(factory_.DriverForKey(KeyFrom(1)));
168   EXPECT_EQ(0, *instance_counter_.val());
169 }
170 
TEST_F(AutofillDriverFactoryTest,NavigationFinished)171 TEST_F(AutofillDriverFactoryTest, NavigationFinished) {
172   EXPECT_CALL(client_, HideAutofillPopup(PopupHidingReason::kNavigation));
173   factory_.NavigationFinished();
174 }
175 
TEST_F(AutofillDriverFactoryTest,TabHidden)176 TEST_F(AutofillDriverFactoryTest, TabHidden) {
177   EXPECT_CALL(client_, HideAutofillPopup(PopupHidingReason::kTabGone));
178   factory_.TabHidden();
179 }
180 
181 }  // namespace autofill
182