// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/chrome/browser/autofill/manual_fill/model/manual_fill_credential.h"

#import "testing/gtest_mac.h"
#import "testing/platform_test.h"
#import "url/gurl.h"

using ManualFillCredentialiOSTest = PlatformTest;

// Tests that a credential is correctly created.
TEST_F(ManualFillCredentialiOSTest, Creation) {
  NSString* username = @"username@example.com";
  NSString* password = @"password";
  NSString* siteName = @"example.com";
  NSString* host = @"sub.example.com";
  GURL URL("https://www.sub.example.com");
  ManualFillCredential* credential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:nil
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_TRUE(credential);
  EXPECT_NSEQ(username, credential.username);
  EXPECT_NSEQ(password, credential.password);
  EXPECT_NSEQ(siteName, credential.siteName);
  EXPECT_NSEQ(host, credential.host);
  EXPECT_EQ(URL, credential.URL);
}

// Test equality between credentials.
TEST_F(ManualFillCredentialiOSTest, Equality) {
  NSString* username = @"username@example.com";
  NSString* password = @"password";
  NSString* siteName = @"example.com";
  NSString* host = @"sub.example.com";
  GURL URL("https://www.sub.example.com");
  ManualFillCredential* credential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:nil
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  ManualFillCredential* equalCredential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:nil
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_TRUE([credential isEqual:equalCredential]);

  ManualFillCredential* differentUsernameCredential =
      [[ManualFillCredential alloc] initWithUsername:@"username2"
                                            password:password
                                         displayName:nil
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_FALSE([credential isEqual:differentUsernameCredential]);

  ManualFillCredential* differentPasswordCredential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:@"psswd"
                                         displayName:nil
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_FALSE([credential isEqual:differentPasswordCredential]);

  ManualFillCredential* differentSiteNameCredential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:nil
                                            siteName:@"notexample.com"
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_FALSE([credential isEqual:differentSiteNameCredential]);

  ManualFillCredential* differentHostCredential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:nil
                                            siteName:siteName
                                                host:@"other.example.com"
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_FALSE([credential isEqual:differentHostCredential]);

  ManualFillCredential* differentURLCredential = [[ManualFillCredential alloc]
        initWithUsername:username
                password:password
             displayName:nil
                siteName:siteName
                    host:host
                     URL:GURL("https://www.other.example.com")
      isBackupCredential:NO];
  EXPECT_FALSE([credential isEqual:differentURLCredential]);

  ManualFillCredential* differentBackupFlag = [[ManualFillCredential alloc]
        initWithUsername:username
                password:password
             displayName:nil
                siteName:siteName
                    host:host
                     URL:GURL("https://www.other.example.com")
      isBackupCredential:YES];
  EXPECT_FALSE([credential isEqual:differentBackupFlag]);
}

// Tests that a credential is correctly created with a display name.
TEST_F(ManualFillCredentialiOSTest, CreationWithDisplayName) {
  NSString* username = @"username@example.com";
  NSString* password = @"password";
  NSString* displayName = @"Elisa Beckett";
  NSString* siteName = @"example.com";
  NSString* host = @"sub.example.com";
  GURL URL("https://www.sub.example.com");
  ManualFillCredential* credential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:displayName
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_TRUE(credential);
  EXPECT_NSEQ(username, credential.username);
  EXPECT_NSEQ(password, credential.password);
  EXPECT_NSEQ(displayName, credential.displayName);
  EXPECT_NSEQ(siteName, credential.siteName);
  EXPECT_NSEQ(host, credential.host);
  EXPECT_EQ(URL, credential.URL);
}

// Test equality between credentials with display names.
TEST_F(ManualFillCredentialiOSTest, EqualityWithDisplayName) {
  NSString* username = @"username@example.com";
  NSString* password = @"password";
  NSString* displayName = @"Elisa Beckett";
  NSString* siteName = @"example.com";
  NSString* host = @"sub.example.com";
  GURL URL("https://www.sub.example.com");
  ManualFillCredential* credential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:displayName
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  ManualFillCredential* equalCredential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:displayName
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_TRUE([credential isEqual:equalCredential]);

  ManualFillCredential* differentDisplayNameCredential =
      [[ManualFillCredential alloc] initWithUsername:username
                                            password:password
                                         displayName:@"Other Name"
                                            siteName:siteName
                                                host:host
                                                 URL:URL
                                  isBackupCredential:NO];
  EXPECT_FALSE([credential isEqual:differentDisplayNameCredential]);
}
