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

#import "components/omnibox/browser/omnibox_pref_names.h"
#import "ios/chrome/browser/shared/model/prefs/pref_names.h"
#import "ios/chrome/browser/shared/public/features/features.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_test_case.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
#import "net/test/embedded_test_server/embedded_test_server.h"

// Tests ios/chrome/browser/shared/ui/util/omnibox_util.h.
@interface OmniboxUtilTestCase : ChromeTestCase
@end

@implementation OmniboxUtilTestCase

- (void)setUp {
  [super setUp];
  [ChromeEarlGrey
      resetDataForLocalStatePref:omnibox::kIsOmniboxInBottomPosition];

  GREYAssertTrue(self.testServer->Start(), @"Server did not start.");
}

- (void)tearDownHelper {
  [super tearDownHelper];
  [ChromeEarlGrey
      resetDataForLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  [ChromeEarlGrey closeAllTabs];
}

/// Go to a web page to have a normal location bar.
- (void)loadPage {
  const GURL pageURL = self.testServer->GetURL("/pony.html");
  [ChromeEarlGrey loadURL:pageURL];
  const char pageContent[] = "pony jokes";  // See pony.html.
  [ChromeEarlGrey waitForWebStateContainingText:pageContent];
}

/// Asserts that IsBottomOmnibox is equal to `shouldBeBottomOmnibox` when the
/// feature is available.
- (void)assertIsBottomOmnibox:(BOOL)shouldBeBottomOmnibox {
  if (![ChromeEarlGrey isBottomOmniboxAvailable]) {
    GREYAssertFalse([ChromeEarlGrey isCurrentLayoutBottomOmnibox],
                    @"Bottom omnibox should not be shown when not available.");
  } else if (shouldBeBottomOmnibox) {
    GREYAssertTrue([ChromeEarlGrey isCurrentLayoutBottomOmnibox],
                   @"Omnibox should be in the bottom toolbar.");
  } else {
    GREYAssertFalse([ChromeEarlGrey isCurrentLayoutBottomOmnibox],
                    @"Omnibox should be in the top toolbar.");
  }
}

#pragma mark - Test cases

// Tests `IsCurrentLayoutBottomOmnibox` on NTP.
- (void)testIsBottomOmniboxOnNTP {
  [ChromeEarlGrey setBoolValue:NO
             forLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  [ChromeEarlGrey openNewTab];
  [self assertIsBottomOmnibox:NO];

  [ChromeEarlGrey setBoolValue:YES
             forLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  GREYWaitForAppToIdle(@"App failed to idle");
  if ([ChromeEarlGrey isChromeNextEnabled]) {
    [self assertIsBottomOmnibox:YES];
  } else {
    [self assertIsBottomOmnibox:NO];
  }
}

// Tests `IsCurrentLayoutBottomOmnibox` on incognito NTP.
- (void)testIsBottomOmniboxOnIncognitoNTP {
  [ChromeEarlGrey setBoolValue:NO
             forLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  [ChromeEarlGrey openNewIncognitoTab];
  [ChromeEarlGrey waitForIncognitoTabCount:1];
  [self assertIsBottomOmnibox:NO];

  [ChromeEarlGrey setBoolValue:YES
             forLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  GREYWaitForAppToIdle(@"App failed to idle");
  [self assertIsBottomOmnibox:YES];
}

// Tests `IsCurrentLayoutBottomOmnibox` on a web page.
- (void)testIsBottomOmniboxOnWebPage {
  [ChromeEarlGrey setBoolValue:NO
             forLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  [self loadPage];
  [self assertIsBottomOmnibox:NO];

  [ChromeEarlGrey setBoolValue:YES
             forLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  GREYWaitForAppToIdle(@"App failed to idle");
  [self assertIsBottomOmnibox:YES];
}

// Tests `IsCurrentLayoutBottomOmnibox` on landscape mode.
- (void)testIsBottomOmniboxOnLandscape {
  [EarlGrey rotateInterfaceToOrientation:UIInterfaceOrientationLandscapeLeft
                                   error:nil];

  [ChromeEarlGrey setBoolValue:NO
             forLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  [self loadPage];
  [self assertIsBottomOmnibox:NO];

  [ChromeEarlGrey setBoolValue:YES
             forLocalStatePref:omnibox::kIsOmniboxInBottomPosition];
  GREYWaitForAppToIdle(@"App failed to idle");
  if ([ChromeEarlGrey isChromeNextEnabled]) {
    [self assertIsBottomOmnibox:YES];
  } else {
    [self assertIsBottomOmnibox:NO];
  }
  [EarlGrey rotateInterfaceToOrientation:UIInterfaceOrientationPortrait
                                   error:nil];
  [self assertIsBottomOmnibox:YES];
}

@end
