Home | History | Annotate | Download | only in browser
      1 // Copyright 2013 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/compiler_specific.h"
      6 #include "base/message_loop/message_loop.h"
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "components/autofill/core/browser/password_autofill_manager.h"
      9 #include "components/autofill/core/browser/test_autofill_driver.h"
     10 #include "testing/gmock/include/gmock/gmock.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 // The name of the username/password element in the form.
     14 const char kUsernameName[] = "username";
     15 const char kInvalidUsername[] = "no-username";
     16 const char kPasswordName[] = "password";
     17 
     18 const char kAliceUsername[] = "alice";
     19 const char kAlicePassword[] = "password";
     20 
     21 namespace {
     22 
     23 class MockAutofillDriver : public autofill::TestAutofillDriver {
     24  public:
     25   MockAutofillDriver() {}
     26   MOCK_METHOD1(RendererShouldAcceptPasswordAutofillSuggestion,
     27                void(const base::string16&));
     28 };
     29 
     30 }  // namespace
     31 
     32 namespace autofill {
     33 
     34 class PasswordAutofillManagerTest : public testing::Test {
     35  protected:
     36   PasswordAutofillManagerTest() :
     37       password_autofill_manager_(&autofill_driver_) {}
     38 
     39   virtual void SetUp() OVERRIDE {
     40     // Add a preferred login and an additional login to the FillData.
     41     base::string16 username1 = ASCIIToUTF16(kAliceUsername);
     42     base::string16 password1 = ASCIIToUTF16(kAlicePassword);
     43 
     44     username_field_.name = ASCIIToUTF16(kUsernameName);
     45     username_field_.value = username1;
     46     fill_data_.basic_data.fields.push_back(username_field_);
     47 
     48     FormFieldData password_field;
     49     password_field.name = ASCIIToUTF16(kPasswordName);
     50     password_field.value = password1;
     51     fill_data_.basic_data.fields.push_back(password_field);
     52 
     53     password_autofill_manager_.AddPasswordFormMapping(username_field_,
     54                                                       fill_data_);
     55   }
     56 
     57   MockAutofillDriver* autofill_driver() {
     58     return &autofill_driver_;
     59   }
     60 
     61   PasswordAutofillManager* password_autofill_manager() {
     62     return &password_autofill_manager_;
     63   }
     64 
     65   const FormFieldData& username_field() { return username_field_; }
     66 
     67  private:
     68   PasswordFormFillData fill_data_;
     69   FormFieldData username_field_;
     70 
     71   // The TestAutofillDriver uses a SequencedWorkerPool which expects the
     72   // existence of a MessageLoop.
     73   base::MessageLoop message_loop_;
     74   MockAutofillDriver autofill_driver_;
     75   PasswordAutofillManager password_autofill_manager_;
     76 };
     77 
     78 TEST_F(PasswordAutofillManagerTest, DidAcceptAutofillSuggestion) {
     79   EXPECT_CALL(*autofill_driver(),
     80       RendererShouldAcceptPasswordAutofillSuggestion(
     81           ASCIIToUTF16(kAliceUsername)));
     82   EXPECT_TRUE(password_autofill_manager()->DidAcceptAutofillSuggestion(
     83       username_field(), ASCIIToUTF16(kAliceUsername)));
     84 
     85   EXPECT_CALL(*autofill_driver(),
     86       RendererShouldAcceptPasswordAutofillSuggestion(
     87           ASCIIToUTF16(kInvalidUsername))).Times(0);
     88   EXPECT_FALSE(password_autofill_manager()->DidAcceptAutofillSuggestion(
     89       username_field(), ASCIIToUTF16(kInvalidUsername)));
     90 
     91   FormFieldData invalid_username_field;
     92   invalid_username_field.name = ASCIIToUTF16(kInvalidUsername);
     93 
     94   EXPECT_FALSE(password_autofill_manager()->DidAcceptAutofillSuggestion(
     95       invalid_username_field, ASCIIToUTF16(kAliceUsername)));
     96 
     97   password_autofill_manager()->Reset();
     98   EXPECT_FALSE(password_autofill_manager()->DidAcceptAutofillSuggestion(
     99       username_field(), ASCIIToUTF16(kAliceUsername)));
    100 }
    101 
    102 }  // namespace autofill
    103