- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / test / integration / performance / autofill_sync_perf_test.cc
1 // Copyright (c) 2012 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 "base/strings/stringprintf.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/sync/profile_sync_service_harness.h"
8 #include "chrome/browser/sync/test/integration/autofill_helper.h"
9 #include "chrome/browser/sync/test/integration/bookmarks_helper.h"
10 #include "chrome/browser/sync/test/integration/performance/sync_timing_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_test.h"
12 #include "components/autofill/core/browser/autofill_common_test.h"
13 #include "components/autofill/core/browser/autofill_profile.h"
14 #include "components/autofill/core/browser/webdata/autofill_entry.h"
15
16 using autofill::ServerFieldType;
17 using autofill::AutofillKey;
18 using autofill::AutofillProfile;
19
20 using autofill_helper::AllProfilesMatch;
21 using autofill_helper::GetAllKeys;
22 using autofill_helper::GetAllProfiles;
23 using autofill_helper::GetKeyCount;
24 using autofill_helper::GetProfileCount;
25 using autofill_helper::RemoveKeys;
26 using autofill_helper::SetProfiles;
27
28 // See comments in typed_urls_sync_perf_test.cc for reasons for these
29 // magic numbers.
30 //
31 // TODO(akalin): If this works, decomp the magic number calculation
32 // into a macro and have all the perf tests use it.
33 static const int kNumKeys = 163;
34 static const int kNumProfiles = 163;
35
36 class AutofillSyncPerfTest : public SyncTest {
37  public:
38   AutofillSyncPerfTest()
39       : SyncTest(TWO_CLIENT),
40         guid_number_(0),
41         name_number_(0),
42         value_number_(0) {}
43
44   // Adds |num_profiles| new autofill profiles to the sync profile |profile|.
45   void AddProfiles(int profile, int num_profiles);
46
47   // Updates all autofill profiles for the sync profile |profile|.
48   void UpdateProfiles(int profile);
49
50   // Removes all autofill profiles from |profile|.
51   void RemoveProfiles(int profile);
52
53   // Adds |num_keys| new autofill keys to the sync profile |profile|.
54   void AddKeys(int profile, int num_keys);
55
56  private:
57   // Returns a new unique autofill profile.
58   const AutofillProfile NextAutofillProfile();
59
60   // Returns a new unique autofill key.
61   const AutofillKey NextAutofillKey();
62
63   // Returns an unused unique guid.
64   const std::string NextGUID();
65
66   // Returns a unique guid based on the input integer |n|.
67   const std::string IntToGUID(int n);
68
69   // Returns a new unused unique name.
70   const std::string NextName();
71
72   // Returns a unique name based on the input integer |n|.
73   const std::string IntToName(int n);
74
75   // Returns a new unused unique value for autofill entries.
76   const std::string NextValue();
77
78   // Returnes a unique value based on the input integer |n|.
79   const std::string IntToValue(int n);
80
81   int guid_number_;
82   int name_number_;
83   int value_number_;
84   DISALLOW_COPY_AND_ASSIGN(AutofillSyncPerfTest);
85 };
86
87 void AutofillSyncPerfTest::AddProfiles(int profile, int num_profiles) {
88   const std::vector<AutofillProfile*>& all_profiles =
89       GetAllProfiles(profile);
90   std::vector<AutofillProfile> autofill_profiles;
91   for (size_t i = 0; i < all_profiles.size(); ++i) {
92     autofill_profiles.push_back(*all_profiles[i]);
93   }
94   for (int i = 0; i < num_profiles; ++i) {
95     autofill_profiles.push_back(NextAutofillProfile());
96   }
97   SetProfiles(profile, &autofill_profiles);
98 }
99
100 void AutofillSyncPerfTest::UpdateProfiles(int profile) {
101   const std::vector<AutofillProfile*>& all_profiles =
102       GetAllProfiles(profile);
103   std::vector<AutofillProfile> autofill_profiles;
104   for (size_t i = 0; i < all_profiles.size(); ++i) {
105     autofill_profiles.push_back(*all_profiles[i]);
106     autofill_profiles.back().SetRawInfo(autofill::NAME_FIRST,
107                                         UTF8ToUTF16(NextName()));
108   }
109   SetProfiles(profile, &autofill_profiles);
110 }
111
112 void AutofillSyncPerfTest::RemoveProfiles(int profile) {
113   std::vector<AutofillProfile> empty;
114   SetProfiles(profile, &empty);
115 }
116
117 void AutofillSyncPerfTest::AddKeys(int profile, int num_keys) {
118   std::set<AutofillKey> keys;
119   for (int i = 0; i < num_keys; ++i) {
120     keys.insert(NextAutofillKey());
121   }
122   autofill_helper::AddKeys(profile, keys);
123 }
124
125 const AutofillProfile AutofillSyncPerfTest::NextAutofillProfile() {
126   AutofillProfile profile;
127   autofill::test::SetProfileInfoWithGuid(&profile, NextGUID().c_str(),
128                                          NextName().c_str(), "", "", "", "", "",
129                                          "", "", "", "", "", "");
130   return profile;
131 }
132
133 const AutofillKey AutofillSyncPerfTest::NextAutofillKey() {
134   return AutofillKey(NextName().c_str(), NextName().c_str());
135 }
136
137 const std::string AutofillSyncPerfTest::NextGUID() {
138   return IntToGUID(guid_number_++);
139 }
140
141 const std::string AutofillSyncPerfTest::IntToGUID(int n) {
142   return base::StringPrintf("00000000-0000-0000-0000-%012X", n);
143 }
144
145 const std::string AutofillSyncPerfTest::NextName() {
146   return IntToName(name_number_++);
147 }
148
149 const std::string AutofillSyncPerfTest::IntToName(int n) {
150   return base::StringPrintf("Name%d", n);
151 }
152
153 const std::string AutofillSyncPerfTest::NextValue() {
154   return IntToValue(value_number_++);
155 }
156
157 const std::string AutofillSyncPerfTest::IntToValue(int n) {
158   return base::StringPrintf("Value%d", n);
159 }
160
161 void ForceSync(int profile) {
162   static int id = 0;
163   ++id;
164   EXPECT_TRUE(
165       bookmarks_helper::AddURL(profile, 0,
166                                bookmarks_helper::IndexedURLTitle(id),
167                                GURL(bookmarks_helper::IndexedURL(id))) != NULL);
168 }
169
170 IN_PROC_BROWSER_TEST_F(AutofillSyncPerfTest, AutofillProfiles_P0) {
171   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
172
173   // TCM ID - 7557873.
174   AddProfiles(0, kNumProfiles);
175   base::TimeDelta dt =
176       SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
177   ASSERT_EQ(kNumProfiles, GetProfileCount(1));
178   SyncTimingHelper::PrintResult("autofill", "add_autofill_profiles", dt);
179
180   // TCM ID - 7549835.
181   UpdateProfiles(0);
182   dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
183   ASSERT_EQ(kNumProfiles, GetProfileCount(1));
184   SyncTimingHelper::PrintResult("autofill", "update_autofill_profiles", dt);
185
186   // TCM ID - 7553678.
187   RemoveProfiles(0);
188   dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
189   ASSERT_EQ(0, GetProfileCount(1));
190   SyncTimingHelper::PrintResult("autofill", "delete_autofill_profiles", dt);
191 }
192
193 IN_PROC_BROWSER_TEST_F(AutofillSyncPerfTest, Autofill_P0) {
194   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
195
196   AddKeys(0, kNumKeys);
197   // TODO(lipalani): fix this. The following line is added to force sync.
198   ForceSync(0);
199   base::TimeDelta dt =
200       SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
201   ASSERT_EQ(kNumKeys, GetKeyCount(1));
202   SyncTimingHelper::PrintResult("autofill", "add_autofill_keys", dt);
203
204   RemoveKeys(0);
205   // TODO(lipalani): fix this. The following line is added to force sync.
206   ForceSync(0);
207   dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
208   ASSERT_EQ(0, GetKeyCount(1));
209   SyncTimingHelper::PrintResult("autofill", "delete_autofill_keys", dt);
210 }