account_select_fill_data_unittest.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2018 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. #include "components/password_manager/ios/account_select_fill_data.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "components/autofill/core/common/password_form_fill_data.h"
  7. #include "components/password_manager/ios/test_helpers.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "testing/platform_test.h"
  11. using autofill::FieldRendererId;
  12. using autofill::FormRendererId;
  13. using autofill::PasswordFormFillData;
  14. using password_manager::AccountSelectFillData;
  15. using password_manager::FillData;
  16. using password_manager::UsernameAndRealm;
  17. using test_helpers::SetPasswordFormFillData;
  18. namespace {
  19. // Test data.
  20. const char* kUrl = "http://example.com/";
  21. const char* kFormNames[] = {"form_name1", "form_name2"};
  22. const uint32_t kFormUniqueIDs[] = {0, 3};
  23. const char* kUsernameElements[] = {"username1", "username2"};
  24. const uint32_t kUsernameUniqueIDs[] = {1, 4};
  25. const char* kUsernames[] = {"user0", "u_s_e_r"};
  26. const char* kPasswordElements[] = {"password1", "password2"};
  27. const uint32_t kPasswordUniqueIDs[] = {2, 5};
  28. const char* kPasswords[] = {"password0", "secret"};
  29. const char* kAdditionalUsernames[] = {"u$er2", nullptr};
  30. const char* kAdditionalPasswords[] = {"secret", nullptr};
  31. class AccountSelectFillDataTest : public PlatformTest {
  32. public:
  33. AccountSelectFillDataTest() {
  34. for (size_t i = 0; i < std::size(form_data_); ++i) {
  35. SetPasswordFormFillData(
  36. kUrl, kFormNames[i], kFormUniqueIDs[i], kUsernameElements[i],
  37. kUsernameUniqueIDs[i], kUsernames[i], kPasswordElements[i],
  38. kPasswordUniqueIDs[i], kPasswords[i], kAdditionalUsernames[i],
  39. kAdditionalPasswords[i], false, &form_data_[i]);
  40. }
  41. }
  42. protected:
  43. PasswordFormFillData form_data_[2];
  44. };
  45. TEST_F(AccountSelectFillDataTest, EmptyReset) {
  46. AccountSelectFillData account_select_fill_data;
  47. EXPECT_TRUE(account_select_fill_data.Empty());
  48. account_select_fill_data.Add(form_data_[0]);
  49. EXPECT_FALSE(account_select_fill_data.Empty());
  50. account_select_fill_data.Reset();
  51. EXPECT_TRUE(account_select_fill_data.Empty());
  52. }
  53. TEST_F(AccountSelectFillDataTest, IsSuggestionsAvailableOneForm) {
  54. AccountSelectFillData account_select_fill_data;
  55. account_select_fill_data.Add(form_data_[0]);
  56. // Suggestions are available for the correct form and field ids.
  57. EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
  58. form_data_[0].form_renderer_id,
  59. form_data_[0].username_field.unique_renderer_id, false));
  60. // Suggestion should be available to any password field.
  61. EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
  62. form_data_[0].form_renderer_id, FieldRendererId(404), true));
  63. // Suggestions are not available for different form renderer_id.
  64. EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
  65. FormRendererId(404), form_data_[0].username_field.unique_renderer_id,
  66. false));
  67. EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
  68. FormRendererId(404), form_data_[0].password_field.unique_renderer_id,
  69. true));
  70. // Suggestions are not available for different field id.
  71. EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
  72. form_data_[0].form_renderer_id, FieldRendererId(404), false));
  73. }
  74. TEST_F(AccountSelectFillDataTest, IsSuggestionsAvailableTwoForms) {
  75. AccountSelectFillData account_select_fill_data;
  76. account_select_fill_data.Add(form_data_[0]);
  77. account_select_fill_data.Add(form_data_[1]);
  78. // Suggestions are available for the correct form and field names.
  79. EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
  80. form_data_[0].form_renderer_id,
  81. form_data_[0].username_field.unique_renderer_id, false));
  82. // Suggestions are available for the correct form and field names.
  83. EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
  84. form_data_[1].form_renderer_id,
  85. form_data_[1].username_field.unique_renderer_id, false));
  86. // Suggestions are not available for different form id.
  87. EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
  88. FormRendererId(404), form_data_[0].username_field.unique_renderer_id,
  89. false));
  90. }
  91. TEST_F(AccountSelectFillDataTest, RetrieveSuggestionsOneForm) {
  92. AccountSelectFillData account_select_fill_data;
  93. account_select_fill_data.Add(form_data_[0]);
  94. for (bool is_password_field : {false, true}) {
  95. const FieldRendererId field_id =
  96. is_password_field ? form_data_[0].password_field.unique_renderer_id
  97. : form_data_[0].username_field.unique_renderer_id;
  98. std::vector<UsernameAndRealm> suggestions =
  99. account_select_fill_data.RetrieveSuggestions(
  100. form_data_[0].form_renderer_id, field_id, is_password_field);
  101. EXPECT_EQ(2u, suggestions.size());
  102. EXPECT_EQ(base::ASCIIToUTF16(kUsernames[0]), suggestions[0].username);
  103. EXPECT_EQ(std::string(), suggestions[0].realm);
  104. EXPECT_EQ(base::ASCIIToUTF16(kAdditionalUsernames[0]),
  105. suggestions[1].username);
  106. EXPECT_EQ(std::string(), suggestions[1].realm);
  107. }
  108. }
  109. TEST_F(AccountSelectFillDataTest, RetrieveSuggestionsTwoForm) {
  110. // Test that after adding two PasswordFormFillData, suggestions for both forms
  111. // are shown, with credentials from the second PasswordFormFillData. That
  112. // emulates the case when credentials in the Password Store were changed
  113. // between load the first and the second forms.
  114. AccountSelectFillData account_select_fill_data;
  115. account_select_fill_data.Add(form_data_[0]);
  116. account_select_fill_data.Add(form_data_[1]);
  117. std::vector<UsernameAndRealm> suggestions =
  118. account_select_fill_data.RetrieveSuggestions(
  119. form_data_[0].form_renderer_id,
  120. form_data_[0].username_field.unique_renderer_id, false);
  121. EXPECT_EQ(1u, suggestions.size());
  122. EXPECT_EQ(base::ASCIIToUTF16(kUsernames[1]), suggestions[0].username);
  123. suggestions = account_select_fill_data.RetrieveSuggestions(
  124. form_data_[1].form_renderer_id,
  125. form_data_[1].username_field.unique_renderer_id, false);
  126. EXPECT_EQ(1u, suggestions.size());
  127. EXPECT_EQ(base::ASCIIToUTF16(kUsernames[1]), suggestions[0].username);
  128. }
  129. TEST_F(AccountSelectFillDataTest, RetrievePSLMatchedSuggestions) {
  130. AccountSelectFillData account_select_fill_data;
  131. const char* kRealm = "http://a.example.com/";
  132. std::string kAdditionalRealm = "http://b.example.com/";
  133. // Make logins to be PSL matched by setting non-empy realm.
  134. form_data_[0].preferred_realm = kRealm;
  135. form_data_[0].additional_logins.begin()->realm = kAdditionalRealm;
  136. account_select_fill_data.Add(form_data_[0]);
  137. std::vector<UsernameAndRealm> suggestions =
  138. account_select_fill_data.RetrieveSuggestions(
  139. form_data_[0].form_renderer_id,
  140. form_data_[0].username_field.unique_renderer_id, false);
  141. EXPECT_EQ(2u, suggestions.size());
  142. EXPECT_EQ(base::ASCIIToUTF16(kUsernames[0]), suggestions[0].username);
  143. EXPECT_EQ(kRealm, suggestions[0].realm);
  144. EXPECT_EQ(base::ASCIIToUTF16(kAdditionalUsernames[0]),
  145. suggestions[1].username);
  146. EXPECT_EQ(kAdditionalRealm, suggestions[1].realm);
  147. }
  148. TEST_F(AccountSelectFillDataTest, GetFillData) {
  149. AccountSelectFillData account_select_fill_data;
  150. account_select_fill_data.Add(form_data_[0]);
  151. account_select_fill_data.Add(form_data_[1]);
  152. for (bool is_password_field : {false, true}) {
  153. for (size_t form_i = 0; form_i < std::size(form_data_); ++form_i) {
  154. const auto& form_data = form_data_[form_i];
  155. // Suggestions should be shown on any password field on the form. So in
  156. // case of clicking on a password field it is taken an id different from
  157. // existing field ids.
  158. const FieldRendererId password_field_id =
  159. is_password_field ? FieldRendererId(1000)
  160. : form_data.password_field.unique_renderer_id;
  161. const FieldRendererId clicked_field =
  162. is_password_field ? password_field_id
  163. : form_data.username_field.unique_renderer_id;
  164. // GetFillData() doesn't have form identifier in arguments, it should be
  165. // provided in RetrieveSuggestions().
  166. account_select_fill_data.RetrieveSuggestions(
  167. form_data.form_renderer_id, clicked_field, is_password_field);
  168. std::unique_ptr<FillData> fill_data =
  169. account_select_fill_data.GetFillData(
  170. base::ASCIIToUTF16(kUsernames[1]));
  171. ASSERT_TRUE(fill_data);
  172. EXPECT_EQ(form_data.url, fill_data->origin);
  173. EXPECT_EQ(form_data.form_renderer_id.value(), fill_data->form_id.value());
  174. EXPECT_EQ(kUsernameUniqueIDs[form_i],
  175. fill_data->username_element_id.value());
  176. EXPECT_EQ(base::ASCIIToUTF16(kUsernames[1]), fill_data->username_value);
  177. EXPECT_EQ(password_field_id, fill_data->password_element_id);
  178. EXPECT_EQ(base::ASCIIToUTF16(kPasswords[1]), fill_data->password_value);
  179. }
  180. }
  181. }
  182. TEST_F(AccountSelectFillDataTest, GetFillDataOldCredentials) {
  183. AccountSelectFillData account_select_fill_data;
  184. account_select_fill_data.Add(form_data_[0]);
  185. account_select_fill_data.Add(form_data_[1]);
  186. // GetFillData() doesn't have form identifier in arguments, it should be
  187. // provided in RetrieveSuggestions().
  188. account_select_fill_data.RetrieveSuggestions(
  189. form_data_[0].form_renderer_id,
  190. form_data_[0].username_field.unique_renderer_id, false);
  191. // AccountSelectFillData should keep only last credentials. Check that in
  192. // request of old credentials nothing is returned.
  193. std::unique_ptr<FillData> fill_data =
  194. account_select_fill_data.GetFillData(base::ASCIIToUTF16(kUsernames[0]));
  195. EXPECT_FALSE(fill_data);
  196. }
  197. } // namespace