// 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.

#include "chrome/browser/ui/browser_window.h"

#import <Cocoa/Cocoa.h>

#include <memory>

#include "base/test/run_until.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/app_controller_mac.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/lifetime/application_lifetime_desktop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/bookmarks/bookmark_editor.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/toolbar/app_menu_model.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_editor_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/remote_cocoa/app_shim/native_widget_mac_nswindow.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest_mac.h"
#include "ui/accessibility/accessibility_switches.h"
#import "ui/base/cocoa/window_size_constants.h"
#include "ui/base/test/ns_ax_tree_validator.h"

// Test harness for Mac-specific behaviors of BrowserWindow.
class BrowserWindowMacTest : public InProcessBrowserTest {
 public:
  BrowserWindowMacTest() = default;

  BrowserWindowMacTest(const BrowserWindowMacTest&) = delete;
  BrowserWindowMacTest& operator=(const BrowserWindowMacTest&) = delete;
};

// Test that mainMenu commands do not attempt to validate against a Browser*
// that is destroyed.
IN_PROC_BROWSER_TEST_F(BrowserWindowMacTest, MenuCommandsAfterDestroy) {
  // Simulate AppKit (e.g. NSMenu) retaining an NSWindow.
  NSWindow* window =
      browser()->GetWindow()->GetNativeWindow().GetNativeNSWindow();
  NSMenuItem* bookmark_menu_item =
      [[[[NSApp mainMenu] itemWithTag:AppMenuModel::kBookmarksMenuPlaceholder]
          submenu] itemWithTag:IDC_BOOKMARK_THIS_TAB];

  EXPECT_TRUE(window);
  EXPECT_TRUE(bookmark_menu_item);

  ui_test_utils::BrowserDestroyedObserver observer(browser());
  chrome::CloseAllBrowsersAndQuit();
  observer.Wait();

  EXPECT_EQ([bookmark_menu_item action], @selector(commandDispatch:));

  // Try validating a command via the NSUserInterfaceValidation protocol.
  // With the delegates removed, CommandDispatcher ends up calling into the
  // NSWindow (e.g. NativeWidgetMacNSWindow)'s defaultValidateUserInterfaceItem,
  // which currently asks |super|. That is, NSWindow. Which says YES.
  EXPECT_TRUE([window validateUserInterfaceItem:bookmark_menu_item]);
}

// Test that mainMenu commands from child windows are validated by the window
// chain.
// TODO(crbug.com/40898643): Disabled because this test is flaky.
IN_PROC_BROWSER_TEST_F(BrowserWindowMacTest,
                       DISABLED_MenuCommandsFromChildWindow) {
  NativeWidgetMacNSWindow* window =
      base::apple::ObjCCastStrict<NativeWidgetMacNSWindow>(
          browser()->GetWindow()->GetNativeWindow().GetNativeNSWindow());

  // Create a child window.
  NativeWidgetMacNSWindow* child_window = [[NativeWidgetMacNSWindow alloc]
      initWithContentRect:ui::kWindowSizeDeterminedLater
                styleMask:NSWindowStyleMaskBorderless
                  backing:NSBackingStoreBuffered
                    defer:NO];
  child_window.releasedWhenClosed = NO;
  [window addChildWindow:child_window ordered:NSWindowAbove];

  NSMenuItem* show_bookmark_bar_menu_item =
      [[[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]
          itemWithTag:IDC_SHOW_BOOKMARK_BAR];

  // Make sure both windows validate the bookmark bar menu item.
  EXPECT_TRUE([window validateUserInterfaceItem:show_bookmark_bar_menu_item]);
  EXPECT_TRUE(
      [child_window validateUserInterfaceItem:show_bookmark_bar_menu_item]);

  chrome::BrowserCommandController::From(browser())->UpdateCommandEnabled(
      show_bookmark_bar_menu_item.tag, false);

  // Make sure both windows find the bookmark bar menu item invalid. The child
  // window check is the focus of this test. The child window does not have a
  // UserInterfaceItemCommandHandler. This test ensures the validation request
  // bubbles up to the parent window.
  EXPECT_FALSE([window validateUserInterfaceItem:show_bookmark_bar_menu_item]);
  EXPECT_FALSE(
      [child_window validateUserInterfaceItem:show_bookmark_bar_menu_item]);
}

class BrowserWindowMacA11yTest : public BrowserWindowMacTest {
 public:
  BrowserWindowMacA11yTest() = default;

  BrowserWindowMacA11yTest(const BrowserWindowMacA11yTest&) = delete;
  BrowserWindowMacA11yTest& operator=(const BrowserWindowMacA11yTest&) = delete;

  ~BrowserWindowMacA11yTest() override = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    BrowserWindowMacTest::SetUpCommandLine(command_line);
    command_line->AppendSwitch(switches::kForceRendererAccessibility);
  }
};

IN_PROC_BROWSER_TEST_F(BrowserWindowMacA11yTest, A11yTreeIsWellFormed) {
  NSWindow* window =
      browser()->GetWindow()->GetNativeWindow().GetNativeNSWindow();
  size_t nodes_visited = 0;
  std::optional<ui::NSAXTreeProblemDetails> details =
      ui::ValidateNSAXTree(window, &nodes_visited);
  EXPECT_FALSE(details.has_value()) << details->ToString();

  // There should be at least a handful of AX nodes in the tree - fail this test
  // if for some reason (eg) the window has no children, which would otherwise
  // be a well-formed AX tree.
  EXPECT_GE(nodes_visited, 10U);

  if (HasFailure()) {
    ui::PrintNSAXTree(window);
  }
}

IN_PROC_BROWSER_TEST_F(BrowserWindowMacA11yTest,
                       AccessibilityDocumentExposedOnWindow) {
  GURL url_before(R"HTML(data:text/html,before)HTML");
  EXPECT_TRUE(AddTabAtIndex(0, url_before, ui::PAGE_TRANSITION_TYPED));

  NSWindow* window =
      browser()->GetWindow()->GetNativeWindow().GetNativeNSWindow();
  ASSERT_NE(nullptr, window);
  EXPECT_NSEQ([NSString stringWithUTF8String:url_before.spec().c_str()],
              [window accessibilityDocument]);

  GURL url_after(R"HTML(data:text/html,after)HTML");
  EXPECT_TRUE(AddTabAtIndex(1, url_after, ui::PAGE_TRANSITION_TYPED));
  EXPECT_NSEQ([NSString stringWithUTF8String:url_after.spec().c_str()],
              [window accessibilityDocument]);
}

IN_PROC_BROWSER_TEST_F(BrowserWindowMacTest, DisableCommandsWhenSheetAttached) {
  NSWindow* window =
      browser()->GetWindow()->GetNativeWindow().GetNativeNSWindow();
  ASSERT_FALSE([AppController.sharedController keyWindowIsModal]);

  // Retrieve and initialize the menu items for
  // IDC_BOOKMARK_ALL_TABS / IDC_PRINT / IDC_SAVE_PAGE.
  ASSERT_TRUE(
      AddTabAtIndex(0, GURL("chrome://newtab/"), ui::PAGE_TRANSITION_TYPED));
  NSMenuItem* bookmark_all_tabs_item =
      [[[[NSApp mainMenu] itemWithTag:AppMenuModel::kBookmarksMenuPlaceholder]
          submenu] itemWithTag:IDC_BOOKMARK_ALL_TABS];
  ASSERT_TRUE(bookmark_all_tabs_item);
  NSMenuItem* print_item = [[[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU]
      submenu] itemWithTag:IDC_PRINT];
  ASSERT_TRUE(print_item);
  NSMenuItem* save_item = [[[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU]
      submenu] itemWithTag:IDC_SAVE_PAGE];
  ASSERT_TRUE(save_item);

  // These commands should be enabled when the sheet is not attached.
  EXPECT_TRUE([window validateUserInterfaceItem:bookmark_all_tabs_item]);
  EXPECT_TRUE([window validateUserInterfaceItem:print_item]);
  EXPECT_TRUE([window validateUserInterfaceItem:save_item]);

  // Open bookmark sheet dialog.
  auto* bookmark_model =
      BookmarkModelFactory::GetForBrowserContext(browser()->GetProfile());
  auto editor = std::make_unique<BookmarkEditorView>(
      browser()->GetProfile(),
      BookmarkEditor::EditDetails::MoveNodes(
          bookmark_model,
          {bookmark_model->AddURL(bookmark_model->other_node(), 0, u"bookmark",
                                  GURL("http://www.google.com"))}),
      BookmarkEditor::SHOW_TREE, base::DoNothing());
  editor->Show(browser()->GetWindow()->GetNativeWindow());
  auto* editor_raw = editor.release();
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return [AppController.sharedController keyWindowIsModal]; }));

  // These commands should be disabled when the sheet is attached.
  EXPECT_FALSE([window validateUserInterfaceItem:bookmark_all_tabs_item]);
  EXPECT_FALSE([window validateUserInterfaceItem:print_item]);
  EXPECT_FALSE([window validateUserInterfaceItem:save_item]);

  // Close the sheet dialog.
  editor_raw->GetWidget()->CloseNow();
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return ![AppController.sharedController keyWindowIsModal]; }));

  // These commands should be enabled again when the sheet is removed.
  EXPECT_TRUE([window validateUserInterfaceItem:bookmark_all_tabs_item]);
  EXPECT_TRUE([window validateUserInterfaceItem:print_item]);
  EXPECT_TRUE([window validateUserInterfaceItem:save_item]);
}
