// 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/views/content_setting_bubble_contents.h"

#include "base/run_loop.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/content_settings/content_setting_image_model.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/test/base/chrome_test_path_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/test/permission_request_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"

class ContentSettingBubbleContentsBrowserTest : public InProcessBrowserTest {
 public:
  ContentSettingBubbleContentsBrowserTest() = default;
  ~ContentSettingBubbleContentsBrowserTest() override = default;

 protected:
  GURL GetTestPageUrl(const std::string& name) {
    return chrome_test_utils::GetTestUrl(
        base::FilePath().AppendASCII("content_setting_bubble"),
        base::FilePath().AppendASCII(name));
  }

  content::WebContents* GetWebContents() {
    return browser()->GetTabStripModel()->GetActiveWebContents();
  }
};

// Flaky: https://crbug.com/40127597
IN_PROC_BROWSER_TEST_F(ContentSettingBubbleContentsBrowserTest,
                       DISABLED_HidesAtWebContentsClose) {
  // Create a second tab, so closing the test tab doesn't close the browser.
  chrome::NewTab(browser(), NewTabTypes::kNoUserAction);

  // Navigate to the test page, and have it request and be denied geolocation
  // permissions.
  ASSERT_TRUE(content::NavigateToURL(GetWebContents(),
                                     GetTestPageUrl("geolocation.html")));
  permissions::PermissionRequestManager::FromWebContents(GetWebContents())
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::DISMISS);
  ASSERT_TRUE(content::ExecJs(GetWebContents(), "geolocate();"));
  permissions::PermissionRequestObserver(GetWebContents()).Wait();

  // Press the geolocation icon and make sure its content setting bubble shows.
  LocationBarTesting* bar = BrowserWindow::FromBrowser(browser())
                                ->GetLocationBar()
                                ->GetLocationBarForTesting();
  EXPECT_TRUE(bar->TestContentSettingImagePressed(
      static_cast<size_t>(ContentSettingImageModel::ImageType::kGeolocation)));
  EXPECT_TRUE(bar->IsContentSettingBubbleShowing(
      static_cast<size_t>(ContentSettingImageModel::ImageType::kGeolocation)));

  // Close the tab, and make sure the bubble is gone. Note that window closure
  // in Aura is asynchronous, so it's necessary to spin the run loop here.
  chrome::CloseTab(browser());
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(bar->IsContentSettingBubbleShowing(
      static_cast<size_t>(ContentSettingImageModel::ImageType::kGeolocation)));
}
