test_weblayer_impl.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "weblayer/browser/java/test_jni/TestWebLayerImpl_jni.h"
  5. #include <utility>
  6. #include "base/android/callback_android.h"
  7. #include "base/android/jni_string.h"
  8. #include "base/no_destructor.h"
  9. #include "base/test/scoped_feature_list.h"
  10. #include "components/content_settings/core/browser/host_content_settings_map.h"
  11. #include "components/download/public/background_service/features.h"
  12. #include "components/translate/core/browser/translate_manager.h"
  13. #include "content/public/test/browser_test_utils.h"
  14. #include "weblayer/browser/browser_context_impl.h"
  15. #include "weblayer/browser/host_content_settings_map_factory.h"
  16. #include "weblayer/browser/profile_impl.h"
  17. #include "weblayer/browser/tab_impl.h"
  18. using base::android::AttachCurrentThread;
  19. using base::android::ScopedJavaGlobalRef;
  20. namespace weblayer {
  21. namespace {
  22. void CheckMetadata(
  23. std::unique_ptr<content::RenderFrameSubmissionObserver> observer,
  24. int top_height,
  25. int bottom_height,
  26. const ScopedJavaGlobalRef<jobject>& runnable) {
  27. const cc::RenderFrameMetadata& last_metadata =
  28. observer->LastRenderFrameMetadata();
  29. if (last_metadata.top_controls_height == top_height &&
  30. last_metadata.bottom_controls_height == bottom_height) {
  31. base::android::RunRunnableAndroid(runnable);
  32. return;
  33. }
  34. auto* const observer_ptr = observer.get();
  35. observer_ptr->NotifyOnNextMetadataChange(
  36. base::BindOnce(&CheckMetadata, std::move(observer), top_height,
  37. bottom_height, runnable));
  38. }
  39. } // namespace
  40. static void JNI_TestWebLayerImpl_WaitForBrowserControlsMetadataState(
  41. JNIEnv* env,
  42. jlong tab_impl,
  43. jint top_height,
  44. jint bottom_height,
  45. const base::android::JavaParamRef<jobject>& runnable) {
  46. TabImpl* tab = reinterpret_cast<TabImpl*>(tab_impl);
  47. auto observer = std::make_unique<content::RenderFrameSubmissionObserver>(
  48. tab->web_contents());
  49. CheckMetadata(std::move(observer), top_height, bottom_height,
  50. ScopedJavaGlobalRef<jobject>(runnable));
  51. }
  52. static void JNI_TestWebLayerImpl_SetIgnoreMissingKeyForTranslateManager(
  53. JNIEnv* env,
  54. jboolean ignore) {
  55. translate::TranslateManager::SetIgnoreMissingKeyForTesting(ignore);
  56. }
  57. static void JNI_TestWebLayerImpl_ExpediteDownloadService(JNIEnv* env) {
  58. static base::NoDestructor<base::test::ScopedFeatureList> feature_list;
  59. feature_list->InitAndEnableFeatureWithParameters(
  60. download::kDownloadServiceFeature, {{"start_up_delay_ms", "0"}});
  61. }
  62. static void JNI_TestWebLayerImpl_GrantLocationPermission(
  63. JNIEnv* env,
  64. const base::android::JavaParamRef<jstring>& jurl) {
  65. GURL url(base::android::ConvertJavaStringToUTF8(env, jurl));
  66. for (auto* profile : ProfileImpl::GetAllProfiles()) {
  67. HostContentSettingsMapFactory::GetForBrowserContext(
  68. profile->GetBrowserContext())
  69. ->SetContentSettingDefaultScope(
  70. url, url, ContentSettingsType::GEOLOCATION, CONTENT_SETTING_ALLOW);
  71. }
  72. }
  73. } // namespace weblayer