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

#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "content/public/test/browser_test.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension_features.h"
#include "net/dns/mock_host_resolver.h"

static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));

namespace extensions {

class FaviconApiTest : public ExtensionApiTest {
 public:
  FaviconApiTest() = default;

 protected:
  void SetUpOnMainThread() override {
    ExtensionApiTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(StartEmbeddedTestServer());
  }
};

// TODO(crbug.com/40836446): Test is flaky on Mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_Extension DISABLED_Extension
#else
#define MAYBE_Extension Extension
#endif

// Fetch favicon from an extension with the correct permission.
IN_PROC_BROWSER_TEST_F(FaviconApiTest, MAYBE_Extension) {
  // Cache the favicon by loading a test page, then fetch the favicon.
  GURL page_url = embedded_test_server()->GetURL(
      "www.example.com", "/extensions/favicon/test_file.html");
  ASSERT_TRUE(NavigateToURL(GetActiveWebContents(), page_url));

  ASSERT_TRUE(
      RunExtensionTest("favicon/extension", {.extension_url = "test.html"}))
      << message_;
}

// Fetch favicon when an extension doesn't have the necessary permission.
IN_PROC_BROWSER_TEST_F(FaviconApiTest, Permission) {
  ASSERT_TRUE(RunExtensionTest("favicon/permission_missing",
                               {.extension_url = "test.html"}))
      << message_;
}

}  // namespace extensions
