chromium/chrome/browser/geolocation/geolocation_permission_context_delegate_unittest.cc

// Copyright 2020 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/content_settings/page_specific_content_settings_delegate.h"
#include "chrome/browser/permissions/permission_manager_factory.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_manager.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/test/mock_permission_prompt_factory.h"
#include "content/public/browser/permission_request_description.h"
#include "content/public/browser/permission_result.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/android/search_permissions/search_permissions_service.h"
#include "components/location/android/location_settings_dialog_outcome.h"
#include "components/location/android/mock_location_settings.h"
#include "components/permissions/contexts/geolocation_permission_context_android.h"
#endif

#if BUILDFLAG(IS_ANDROID)
namespace {
constexpr char kDSETestUrl[] = "https://www.dsetest.com";

class TestSearchEngineDelegate
    : public SearchPermissionsService::SearchEngineDelegate {
 public:
  std::u16string GetDSEName() override { return std::u16string(); }

  url::Origin GetDSEOrigin() override {
    return url::Origin::Create(GURL(kDSETestUrl));
  }
};
}  // namespace
#endif

class GeolocationPermissionContextDelegateTests
    : public ChromeRenderViewHostTestHarness {};

TEST_F(GeolocationPermissionContextDelegateTests, TabContentSettingIsUpdated) {}

#if BUILDFLAG(IS_ANDROID)
// TODO(crbug.com/40835241): Flaky.
TEST_F(GeolocationPermissionContextDelegateTests,
       DISABLED_SearchGeolocationInIncognito) {
  url::Origin requesting_frame_url = url::Origin::Create(GURL(kDSETestUrl));

  SearchPermissionsService* service =
      SearchPermissionsService::Factory::GetForBrowserContext(profile());
  std::unique_ptr<TestSearchEngineDelegate> delegate =
      std::make_unique<TestSearchEngineDelegate>();
  service->SetSearchEngineDelegateForTest(std::move(delegate));
  service->InitializeSettingsIfNeeded();

  // The DSE geolocation should not be auto-granted even in a non-OTR profile.
  ASSERT_EQ(
      blink::mojom::PermissionStatus::ASK,
      GetPermissionResultForOriginWithoutContext(
          profile(), blink::PermissionType::GEOLOCATION, requesting_frame_url)
          .status);

  Profile* otr_profile =
      profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true);

  // The DSE geolocation should not be auto-granted in an OTR profile.
  ASSERT_EQ(
      blink::mojom::PermissionStatus::ASK,
      GetPermissionResultForOriginWithoutContext(
          otr_profile, blink::PermissionType::GEOLOCATION, requesting_frame_url)
          .status);
}
#endif