Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / core / browser / password_form_manager_unittest.cc
index 58db999..0e76081 100644 (file)
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "components/autofill/core/common/password_form.h"
+#include "components/password_manager/core/browser/mock_password_manager_driver.h"
 #include "components/password_manager/core/browser/mock_password_store.h"
 #include "components/password_manager/core/browser/password_form_manager.h"
 #include "components/password_manager/core/browser/password_manager.h"
-#include "components/password_manager/core/browser/password_manager_client.h"
 #include "components/password_manager/core/browser/password_manager_driver.h"
 #include "components/password_manager/core/browser/password_store.h"
+#include "components/password_manager/core/browser/stub_password_manager_client.h"
 #include "components/password_manager/core/browser/test_password_store.h"
 #include "components/password_manager/core/common/password_manager_pref_names.h"
 #include "testing/gmock/include/gmock/gmock.h"
@@ -33,6 +34,8 @@ namespace autofill {
 class AutofillManager;
 }
 
+namespace password_manager {
+
 namespace {
 
 void RunAllPendingTasks() {
@@ -42,23 +45,7 @@ void RunAllPendingTasks() {
   run_loop.Run();
 }
 
-class MockPasswordManagerDriver : public PasswordManagerDriver {
- public:
-  MockPasswordManagerDriver() {}
-  virtual ~MockPasswordManagerDriver() {}
-
-  MOCK_METHOD1(FillPasswordForm, void(const autofill::PasswordFormFillData&));
-  MOCK_METHOD0(DidLastPageLoadEncounterSSLErrors, bool());
-  MOCK_METHOD0(IsOffTheRecord, bool());
-  MOCK_METHOD0(GetPasswordGenerationManager, PasswordGenerationManager*());
-  MOCK_METHOD0(GetPasswordManager, PasswordManager*());
-  MOCK_METHOD0(GetAutofillManager, autofill::AutofillManager*());
-  MOCK_METHOD1(AllowPasswordGenerationForForm, void(autofill::PasswordForm*));
-  MOCK_METHOD1(AccountCreationFormsFound,
-               void(const std::vector<autofill::FormData>&));
-};
-
-class TestPasswordManagerClient : public PasswordManagerClient {
+class TestPasswordManagerClient : public StubPasswordManagerClient {
  public:
   explicit TestPasswordManagerClient(PasswordStore* password_store)
       : password_store_(password_store) {
@@ -699,3 +686,35 @@ TEST_F(PasswordFormManagerTest, TestUpdateIncompleteCredentials) {
   EXPECT_CALL(*mock_store(), UpdateLogin(complete_form));
   form_manager.Save();
 }
+
+TEST_F(PasswordFormManagerTest, TestScoringPublicSuffixMatch) {
+  base::MessageLoop message_loop;
+
+  TestPasswordManagerClient client(NULL);
+  MockPasswordManagerDriver driver;
+  EXPECT_CALL(driver, IsOffTheRecord()).WillRepeatedly(Return(false));
+  EXPECT_CALL(driver, AllowPasswordGenerationForForm(_));
+
+  TestPasswordManager password_manager(&client);
+  scoped_ptr<PasswordFormManager> manager(new PasswordFormManager(
+    &password_manager, &client, &driver, *observed_form(), false));
+
+  // Simulate having two matches for this form, first comes from different
+  // signon realm, but reports the same origin and action as matched form.
+  // Second candidate has the same signon realm as the form, but has a different
+  // origin and action. Public suffix match is the most important criterion so
+  // the second candidate should be selected.
+  std::vector<PasswordForm*> results;
+  results.push_back(CreateSavedMatch(false));
+  results.push_back(CreateSavedMatch(false));
+  results[0]->original_signon_realm = "http://accounts2.google.com";
+  results[1]->origin = GURL("http://accounts.google.com/a/ServiceLoginAuth2");
+  results[1]->action = GURL("http://accounts.google.com/a/ServiceLogin2");
+  SimulateFetchMatchingLoginsFromPasswordStore(manager.get());
+  SimulateResponseFromPasswordStore(manager.get(), results);
+  EXPECT_EQ(1u, password_manager.GetLatestBestMatches().size());
+  EXPECT_EQ("", password_manager.GetLatestBestMatches().begin()
+      ->second->original_signon_realm);
+}
+
+}  // namespace password_manager