File: delegated_frame_host_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,635 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 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/renderer_host/delegated_frame_host.h"

#include <cstddef>
#include <memory>

#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "components/viz/client/frame_evictor.h"
#include "components/viz/common/surfaces/frame_sink_id.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_image_transport_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/test/test_context_factories.h"

namespace content {

class MockDelegatedFrameHostClient : public DelegatedFrameHostClient {
 public:
  MockDelegatedFrameHostClient() = default;
  ~MockDelegatedFrameHostClient() override = default;

  MOCK_METHOD(ui::Layer*, DelegatedFrameHostGetLayer, (), (const, override));
  MOCK_METHOD(bool, DelegatedFrameHostIsVisible, (), (const, override));
  MOCK_METHOD(SkColor, DelegatedFrameHostGetGutterColor, (), (const, override));
  MOCK_METHOD2(OnFrameTokenChanged,
               void(uint32_t frame_token, base::TimeTicks activation_time));
  MOCK_METHOD(float, GetDeviceScaleFactor, (), (const, override));
  MOCK_METHOD0(InvalidateLocalSurfaceIdOnEviction, void());
  MOCK_METHOD(viz::FrameEvictorClient::EvictIds,
              CollectSurfaceIdsForEviction,
              (),
              (override));
  MOCK_METHOD0(ShouldShowStaleContentOnEviction, bool());
};

class DelegatedFrameHostTest : public testing::Test {
 public:
  DelegatedFrameHostTest() = default;
  ~DelegatedFrameHostTest() override = default;

  DelegatedFrameHost* delegated_frame_host() {
    return delegated_frame_host_.get();
  }

  void SetUp() override;

 private:
  BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME,
      base::test::TaskEnvironment::ThreadingMode::MULTIPLE_THREADS,
      base::test::TaskEnvironment::MainThreadType::UI};
  testing::StrictMock<MockDelegatedFrameHostClient> mock_client_;
  std::unique_ptr<DelegatedFrameHost> delegated_frame_host_;
  ui::TestContextFactories context_factory_{/*enable_pixel_output=*/false};
  std::unique_ptr<ui::Compositor> compositor_;
};

void DelegatedFrameHostTest::SetUp() {
  ImageTransportFactory::SetFactory(
      std::make_unique<TestImageTransportFactory>());
  viz::FrameSinkId frame_sink_id =
      context_factory_.GetContextFactory()->AllocateFrameSinkId();
  compositor_ = std::make_unique<ui::Compositor>(
      frame_sink_id, context_factory_.GetContextFactory(),
      base::SingleThreadTaskRunner::GetCurrentDefault(),
      /*enable_pixel_canvas=*/false);

  EXPECT_CALL(mock_client_, DelegatedFrameHostIsVisible)
      .Times(1)
      .WillOnce(testing::Return(false));
  delegated_frame_host_ = std::make_unique<DelegatedFrameHost>(
      frame_sink_id, &mock_client_, /*should_register_frame_sink_id=*/false);
  delegated_frame_host_->AttachToCompositor(compositor_.get());
}

TEST_F(DelegatedFrameHostTest, NoCopyOutputRequestWithNoValidSurface) {
  auto* dfh = delegated_frame_host();
  EXPECT_FALSE(dfh->CanCopyFromCompositingSurface());

  // Navigating while hidden would not lead to a call to `EmbedSurface` so we
  // should remain unable to perform a copy.
  dfh->DidNavigateMainFramePreCommit();
  dfh->DidNavigate();
  EXPECT_FALSE(dfh->CanCopyFromCompositingSurface());

  // Since we have not called `DelegatedFrameHost::EmbedSurface` we have no
  // valid `viz::Surface` to perform readback for. The callback given to
  // `CopyFromCompositingSurface` is expected to run with an empty `SkBitmap`.
  //
  // `CopyFromCompositingSurface1 creates a
  // `ui::Compositor::ScopedKeepSurfaceAliveCallback` to ensure that we keep the
  // surface alive until the copy completes. This callback is bound on the UI
  // thread. `viz::CopyOutputRequest` tries to use background threadpools for
  // copying. The following will crash on DCHECK builds if we run on the wrong
  // sequence.
  base::RunLoop run_loop;
  dfh->CopyFromCompositingSurface(
      /*src_subrect=*/gfx::Rect(),
      /*output_size=*/gfx::Size(),
      base::BindOnce(
          [](base::RepeatingClosure quit_closure, const SkBitmap& bitmap) {
            EXPECT_TRUE(bitmap.empty());
            quit_closure.Run();
          },
          run_loop.QuitClosure()));
  run_loop.Run();
}

}  // namespace content