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

#import <XCTest/XCTest.h>

#import "components/webui/chrome_urls/pref_names.h"
#import "ios/chrome/browser/shared/model/url/chrome_url_constants.h"
#import "ios/chrome/browser/webui/ui_bundled/interstitials/interstitial_ui_constants.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
#import "ios/components/webui/web_ui_url_constants.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
#import "net/base/url_util.h"
#import "net/test/embedded_test_server/embedded_test_server.h"
#import "ui/base/l10n/l10n_util.h"
#import "url/url_util.h"

// Test case for chrome://interstitials WebUI page.
@interface InterstitialWebUITestCase : ChromeTestCase {
  std::unique_ptr<url::ScopedSchemeRegistryForTests> _schemeRegistry;
}

@end

@implementation InterstitialWebUITestCase

- (void)setUp {
  [super setUp];
  [ChromeEarlGrey setBoolValue:YES
             forLocalStatePref:chrome_urls::kInternalOnlyUisEnabled];
  _schemeRegistry = std::make_unique<url::ScopedSchemeRegistryForTests>();
  url::ClearSchemesForTests();
  url::AddStandardScheme(kChromeUIScheme, url::SCHEME_WITH_HOST);
}

- (void)tearDownHelper {
  _schemeRegistry = nullptr;
  [super tearDownHelper];
}

// Tests that chrome://interstitials loads correctly.
- (void)testLoadInterstitialUI {
  [ChromeEarlGrey loadURL:GURL(kChromeUIInterstitialsURL)];

  [ChromeEarlGrey waitForWebStateContainingText:"Choose an interstitial"];
}

// Tests that chrome://interstitials/ssl loads correctly.
- (void)testLoadSSLInterstitialUI {
  GURL SSLInterstitialURL =
      GURL(kChromeUIInterstitialsURL).Resolve(kChromeInterstitialSslPath);
  [ChromeEarlGrey loadURL:SSLInterstitialURL];

  [ChromeEarlGrey
      waitForWebStateContainingText:"Your connection is not private"];
}

// Tests that chrome://interstitials/captiveportal loads correctly.
- (void)testLoadCaptivePortalInterstitialUI {
  GURL captivePortalInterstitialURL =
      GURL(kChromeUIInterstitialsURL)
          .Resolve(kChromeInterstitialCaptivePortalPath);
  [ChromeEarlGrey loadURL:captivePortalInterstitialURL];

  [ChromeEarlGrey waitForWebStateContainingText:"Connect to Wi-Fi"];
}

// Tests that chrome://interstitials/safe_browsing?type=malware loads correctly.
- (void)testLoadSafeBrowsingMalwareInterstitialUI {
  GURL safeBrowsingURL = GURL(kChromeUIInterstitialsURL)
                             .Resolve(kChromeInterstitialSafeBrowsingPath);
  safeBrowsingURL = net::AppendQueryParameter(
      safeBrowsingURL, kChromeInterstitialSafeBrowsingTypeQueryKey,
      kChromeInterstitialSafeBrowsingTypeMalwareValue);
  [ChromeEarlGrey loadURL:safeBrowsingURL];

  [ChromeEarlGrey waitForWebStateContainingText:"Dangerous site"];
}

// Tests that chrome://interstitials/safe_browsing?type=phishing loads
// correctly.
- (void)testLoadSafeBrowsingPhishingInterstitialUI {
  GURL safeBrowsingURL = GURL(kChromeUIInterstitialsURL)
                             .Resolve(kChromeInterstitialSafeBrowsingPath);
  safeBrowsingURL = net::AppendQueryParameter(
      safeBrowsingURL, kChromeInterstitialSafeBrowsingTypeQueryKey,
      kChromeInterstitialSafeBrowsingTypePhishingValue);
  [ChromeEarlGrey loadURL:safeBrowsingURL];

  [ChromeEarlGrey waitForWebStateContainingText:"Dangerous site"];
}

// Tests that chrome://interstitials/safe_browsing?type=unwanted loads
// correctly.
- (void)testLoadSafeBrowsingUnwantedInterstitialUI {
  GURL safeBrowsingURL = GURL(kChromeUIInterstitialsURL)
                             .Resolve(kChromeInterstitialSafeBrowsingPath);
  safeBrowsingURL = net::AppendQueryParameter(
      safeBrowsingURL, kChromeInterstitialSafeBrowsingTypeQueryKey,
      kChromeInterstitialSafeBrowsingTypeUnwantedValue);
  [ChromeEarlGrey loadURL:safeBrowsingURL];

  [ChromeEarlGrey waitForWebStateContainingText:"Dangerous site"];
}

// Tests that chrome://interstitials/safe_browsing?type=clientside_phishing
// loads correctly.
- (void)testLoadSafeBrowsingClientsidePhishingInterstitialUI {
  GURL safeBrowsingURL = GURL(kChromeUIInterstitialsURL)
                             .Resolve(kChromeInterstitialSafeBrowsingPath);
  safeBrowsingURL = net::AppendQueryParameter(
      safeBrowsingURL, kChromeInterstitialSafeBrowsingTypeQueryKey,
      kChromeInterstitialSafeBrowsingTypeClientsidePhishingValue);
  [ChromeEarlGrey loadURL:safeBrowsingURL];

  [ChromeEarlGrey waitForWebStateContainingText:"Dangerous site"];
}

// Tests that chrome://interstitials/safe_browsing?type=billing loads correctly.
- (void)testLoadSafeBrowsingBillingInterstitialUI {
  GURL safeBrowsingURL = GURL(kChromeUIInterstitialsURL)
                             .Resolve(kChromeInterstitialSafeBrowsingPath);
  safeBrowsingURL = net::AppendQueryParameter(
      safeBrowsingURL, kChromeInterstitialSafeBrowsingTypeQueryKey,
      kChromeInterstitialSafeBrowsingTypeBillingValue);
  [ChromeEarlGrey loadURL:safeBrowsingURL];

  [ChromeEarlGrey waitForWebStateContainingText:
                      "The page ahead may try to charge you money"];
}

// Tests that chrome://interstitials/enterprise-warn loads correctly.
- (void)testEnterpriseWarnInterstitialUI {
  GURL enterpriseWarnURL = GURL(kChromeUIInterstitialsURL)
                               .Resolve(kChromeInterstitialEnterpriseWarn);
  [ChromeEarlGrey loadURL:enterpriseWarnURL];

  [ChromeEarlGrey waitForWebStateContainingText:
                      "The site ahead is flagged by your organization"];
}

// Tests that chrome://interstitials/enterprise-block loads correctly.
- (void)testEnterpriseBlockInterstitialUI {
  GURL enterpriseBlockURL = GURL(kChromeUIInterstitialsURL)
                                .Resolve(kChromeInterstitialEnterpriseBlock);
  [ChromeEarlGrey loadURL:enterpriseBlockURL];

  [ChromeEarlGrey waitForWebStateContainingText:
                      "The site ahead is blocked by your organization"];
}

@end
