// Copyright 2015 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/browser_content/ui_bundled/browser_content_view_controller.h"

#import "testing/platform_test.h"

// Fixture for BrowserContentViewController testing.
class BrowserContentViewControllerTest : public PlatformTest {
 protected:
  void SetUp() override {
    PlatformTest::SetUp();

    view_controller_ = [[BrowserContentViewController alloc] init];
    ASSERT_TRUE(view_controller_);
    content_view_ = [[UIView alloc] init];
    ASSERT_TRUE(content_view_);
    content_view_controller_ = [[UIViewController alloc] init];
    ASSERT_TRUE(content_view_controller_);
  }
  BrowserContentViewController* view_controller_;
  UIView* content_view_;
  UIViewController* content_view_controller_;
};

// Tests adding a new content view when BrowserContentViewController does not
// currently have a content view.
TEST_F(BrowserContentViewControllerTest, AddingContentView) {
  ASSERT_FALSE([content_view_ superview]);

  view_controller_.contentView = content_view_;
  EXPECT_EQ(view_controller_.view, content_view_.superview);
}

// Tests adding a new content view when BrowserContentViewController does not
// currently have a content view controller.
TEST_F(BrowserContentViewControllerTest, AddingContentViewController) {
  ASSERT_FALSE([content_view_controller_.view superview]);

  view_controller_.contentViewController = content_view_controller_;
  EXPECT_EQ(view_controller_.view, content_view_controller_.view.superview);
}

// Tests removing previously added content view.
TEST_F(BrowserContentViewControllerTest, RemovingContentView) {
  view_controller_.contentView = content_view_;
  ASSERT_EQ(view_controller_.view, content_view_.superview);

  view_controller_.contentView = nil;
  EXPECT_FALSE([content_view_ superview]);
}

// Tests removing previously added content view controller.
TEST_F(BrowserContentViewControllerTest, RemovingContentViewController) {
  view_controller_.contentViewController = content_view_controller_;
  ASSERT_EQ(view_controller_.view, content_view_controller_.view.superview);

  view_controller_.contentViewController = nil;
  EXPECT_FALSE([content_view_controller_.view superview]);
}

// Tests adding a new content view when BrowserContentViewController already
// has a content view.
TEST_F(BrowserContentViewControllerTest, ReplacingContentView) {
  view_controller_.contentView = content_view_;
  ASSERT_EQ(view_controller_.view, content_view_.superview);

  UIView* content_view2 = [[UIView alloc] init];
  view_controller_.contentView = content_view2;
  EXPECT_FALSE([content_view_ superview]);
  EXPECT_EQ(view_controller_.view, content_view2.superview);
}

// Tests that BrowserContentViewController contentViews and
// contentViewControllers are always added at index zero, with the
// contentViewControllers above the contentView.
TEST_F(BrowserContentViewControllerTest, ContentViewIndex) {
  view_controller_.contentView = content_view_;
  ASSERT_EQ(view_controller_.view, content_view_.superview);

  [view_controller_.view addSubview:[[UIView alloc] init]];

  UIView* content_view2 = [[UIView alloc] init];
  view_controller_.contentView = content_view2;
  EXPECT_EQ([view_controller_.view.subviews indexOfObject:content_view2],
            static_cast<NSUInteger>(0));

  UIViewController* content_view_controller2 = [[UIViewController alloc] init];
  view_controller_.contentViewController = content_view_controller2;
  EXPECT_EQ([view_controller_.view.subviews
                indexOfObject:content_view_controller2.view],
            static_cast<NSUInteger>(1));
}

// Tests adding a new content view controller when
// BrowserContentViewController already has a content view or a content view
// controller.
TEST_F(BrowserContentViewControllerTest, ReplacingContentViewController) {
  view_controller_.contentView = content_view_;
  ASSERT_EQ(view_controller_.view, content_view_.superview);

  UIViewController* content_view_controller2 = [[UIViewController alloc] init];
  view_controller_.contentViewController = content_view_controller2;
  EXPECT_TRUE([content_view_ superview]);
  EXPECT_EQ(view_controller_.view, content_view_controller2.view.superview);

  UIView* content_view2 = [[UIView alloc] init];
  view_controller_.contentView = content_view2;
  EXPECT_FALSE([content_view_ superview]);
  EXPECT_FALSE([content_view_controller2.view superview]);
  EXPECT_EQ(view_controller_.view, content_view2.superview);
}
