// Copyright 2022 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/bubble/ui_bundled/bubble_view_controller.h"

#import "base/apple/foundation_util.h"
#import "base/test/scoped_feature_list.h"
#import "ios/chrome/browser/bubble/ui_bundled/bubble_constants.h"
#import "ios/chrome/browser/bubble/ui_bundled/bubble_unittest_util.h"
#import "ios/chrome/browser/bubble/ui_bundled/bubble_view.h"
#import "ios/chrome/browser/shared/public/features/features.h"
#import "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#import "testing/platform_test.h"

// Fixture to test BubbleViewController.
class BubbleViewControllerTest : public PlatformTest {
 public:
  BubbleViewControllerTest()
      : text_(@"Text"),
        title_text_(@"Title"),
        arrow_direction_(BubbleArrowDirectionUp),
        alignment_(BubbleAlignmentTopOrLeading) {}

 protected:
  // Text for the bubble view.
  NSString* text_;
  // Title for the bubble view.
  NSString* title_text_;
  // The direction that the bubble's arrow points.
  const BubbleArrowDirection arrow_direction_;
  // The alignment of the bubble's arrow relative to the rest of the bubble.
  const BubbleAlignment alignment_;

  // Tests that `bubble_view_controller`'s bubbleView contains the expected
  // subviews.
  void ExpectBubbleViewContent(BubbleViewController* bubble_view_controller,
                               BOOL expect_close_button,
                               BOOL expect_title,
                               BOOL expect_snooze_button) {
    BubbleView* bubbleView =
        base::apple::ObjCCastStrict<BubbleView>(bubble_view_controller.view);
    EXPECT_TRUE(bubbleView);
    UIView* closeButton = GetCloseButtonFromBubbleView(bubbleView);
    UIView* titleView = GetTitleLabelFromBubbleView(bubbleView);
    UIView* snoozeButton = GetSnoozeButtonFromBubbleView(bubbleView);
    EXPECT_EQ(expect_close_button, static_cast<bool>(closeButton));
    EXPECT_EQ(expect_title, static_cast<bool>(titleView));
    EXPECT_EQ(expect_snooze_button, static_cast<bool>(snoozeButton));
  }

  base::test::ScopedFeatureList feature_list_;
};

// Tests that with BubbleViewTypeDefault, bubble view contains the expected
// subviews.
TEST_F(BubbleViewControllerTest, BubbleTypeDefaultContent) {
  BubbleViewController* bubble_view_controller =
      [[BubbleViewController alloc] initWithText:text_
                                           title:title_text_
                                  arrowDirection:arrow_direction_
                                       alignment:alignment_
                                  bubbleViewType:BubbleViewTypeDefault
                                 pageControlPage:BubblePageControlPageNone
                                        delegate:nil];
  ExpectBubbleViewContent(bubble_view_controller, false, false, false);
}

// Tests that with BubbleViewTypeWithClose, bubble view contains the expected
// subviews.
TEST_F(BubbleViewControllerTest, BubbleTypeWithCloseContent) {
  BubbleViewController* bubble_view_controller =
      [[BubbleViewController alloc] initWithText:text_
                                           title:title_text_
                                  arrowDirection:arrow_direction_
                                       alignment:alignment_
                                  bubbleViewType:BubbleViewTypeWithClose
                                 pageControlPage:BubblePageControlPageNone
                                        delegate:nil];
  ExpectBubbleViewContent(bubble_view_controller, true, false, false);
}

// Tests that with BubbleViewTypeRich, bubble view contains the expected
// subviews.
TEST_F(BubbleViewControllerTest, BubbleTypeRichContent) {
  BubbleViewController* bubble_view_controller =
      [[BubbleViewController alloc] initWithText:text_
                                           title:title_text_
                                  arrowDirection:arrow_direction_
                                       alignment:alignment_
                                  bubbleViewType:BubbleViewTypeRich
                                 pageControlPage:BubblePageControlPageNone
                                        delegate:nil];
  ExpectBubbleViewContent(bubble_view_controller, false, true, false);
}
