1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134

content / browser / display_cutout / display_cutout_host_impl_unittest.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/display_cutout/display_cutout_host_impl.h"

#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/features.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_renderer_host.h"
#include "content/test/test_web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace content {

namespace {

class TestDisplayCutoutHostImpl : public DisplayCutoutHostImpl {
 public:
  explicit TestDisplayCutoutHostImpl(WebContentsImpl* web_contents_impl)
      : DisplayCutoutHostImpl(web_contents_impl) {}

  void ResetSafeArea() {
    did_send_safe_area_ = false;
    safe_area_insets_ = gfx::Insets(0);
  }

  bool did_send_safe_area() { return did_send_safe_area_; }
  gfx::Insets safe_area_insets() { return safe_area_insets_; }

 protected:
  // Send the safe area insets to a |RenderFrameHost|.
  void SendSafeAreaToFrame(RenderFrameHost* rfh, gfx::Insets insets) override {
    did_send_safe_area_ = true;
    safe_area_insets_ = insets;
    DisplayCutoutHostImpl::SendSafeAreaToFrame(rfh, insets);
  }

 private:
  bool did_send_safe_area_ = false;
  gfx::Insets safe_area_insets_;
};

}  // namespace

class DisplayCutoutHostImplTest : public RenderViewHostTestHarness {
 protected:
  void SetUp() override {
    RenderViewHostTestHarness::SetUp();
    SetContents(CreateTestWebContents());

    std::unique_ptr<TestDisplayCutoutHostImpl> test_display_cutout_host_impl =
        absl::make_unique<TestDisplayCutoutHostImpl>(test_web_contents());
    test_display_cutout_host_ = test_display_cutout_host_impl.get();
    test_web_contents()->SetSafeAreaInsetsHost(
        std::move(test_display_cutout_host_impl));
  }

  TestWebContents* test_web_contents() const {
    return static_cast<TestWebContents*>(web_contents());
  }

  TestDisplayCutoutHostImpl* test_display_cutout_host() const {
    return test_display_cutout_host_;
  }

  void ResetSafeArea() { test_display_cutout_host()->ResetSafeArea(); }

  void NavigateToCover() {
    FocusWebContentsOnMainFrame();
    // Simulate window insets changing, e.g. java's
    // DisplayCutoutController#onSafeAreaChanged notified from InsetObserver.
    test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets(42));
    NavigateAndCommit(GURL("www.viewportFitCover.com"));
    test_web_contents()->NotifyViewportFitChanged(
        blink::mojom::ViewportFit::kCover);
  }

  void NavigateToAuto() {
    FocusWebContentsOnMainFrame();
    test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets(0));
    NavigateAndCommit(GURL("www.viewportFitAuto.com"));
    test_web_contents()->NotifyViewportFitChanged(
        blink::mojom::ViewportFit::kAuto);
  }

 private:
  raw_ptr<TestDisplayCutoutHostImpl> test_display_cutout_host_;
};

TEST_F(DisplayCutoutHostImplTest, AutoToCover) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures(
      /*enabled_features=*/{features::kDrawCutoutEdgeToEdge},
      /*disabled_features=*/{});

  ResetSafeArea();
  NavigateToAuto();
  EXPECT_TRUE(test_display_cutout_host()->did_send_safe_area());
  EXPECT_EQ(0, test_display_cutout_host()->safe_area_insets().top())
      << "No Display Cutout, so the top inset should have been zero";

  ResetSafeArea();
  NavigateToCover();
  EXPECT_TRUE(test_display_cutout_host()->did_send_safe_area());
  EXPECT_NE(0, test_display_cutout_host()->safe_area_insets().top())
      << "The Display Cutout should have caused a non-zero top inset";
}

TEST_F(DisplayCutoutHostImplTest, CoverToAuto) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures(
      /*enabled_features=*/{features::kDrawCutoutEdgeToEdge},
      /*disabled_features=*/{});

  ResetSafeArea();
  NavigateToCover();
  EXPECT_TRUE(test_display_cutout_host()->did_send_safe_area());
  EXPECT_NE(0, test_display_cutout_host()->safe_area_insets().top())
      << "The Display Cutout should have caused a non-zero top inset";

  ResetSafeArea();
  NavigateToAuto();
  EXPECT_TRUE(test_display_cutout_host()->did_send_safe_area());
  EXPECT_EQ(0, test_display_cutout_host()->safe_area_insets().top())
      << "No Display Cutout, so the top inset should have been zero";
}

}  // namespace content