#include "ui/compositor/compositor.h"
#include <stdint.h>
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/power_monitor_test.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "cc/metrics/frame_sequence_tracker.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_delegate.h"
#include "ui/compositor/test/draw_waiter_for_test.h"
#include "ui/compositor/test/in_process_context_factory.h"
#include "ui/compositor/test/test_context_factories.h"
#include "ui/display/types/display_constants.h"
Mock;
_;
namespace ui {
namespace {
class MockCompositorObserver : public CompositorObserver { … };
class CompositorTest : public testing::Test { … };
class CompositorTestWithMockedTime : public CompositorTest { … };
class CompositorTestWithMessageLoop : public CompositorTest { … };
class TestCompositorAnimationObserver : public CompositorAnimationObserver { … };
}
TEST_F(CompositorTestWithMockedTime, AnimationObserverBasic) { … }
TEST_F(CompositorTestWithMockedTime, AnimationObserverResetAfterResume) { … }
TEST_F(CompositorTestWithMessageLoop, ShouldUpdateDisplayProperties) { … }
TEST_F(CompositorTestWithMockedTime,
ReleaseWidgetWithOutputSurfaceNeverCreated) { … }
TEST_F(CompositorTestWithMessageLoop, MoveThroughputTracker) { … }
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(CompositorTestWithMessageLoop, ThroughputTracker) {
auto root_layer = std::make_unique<Layer>(ui::LAYER_SOLID_COLOR);
viz::ParentLocalSurfaceIdAllocator allocator;
allocator.GenerateId();
root_layer->SetBounds(gfx::Rect(10, 10));
compositor()->SetRootLayer(root_layer.get());
compositor()->SetScaleAndSize(1.0f, gfx::Size(10, 10),
allocator.GetCurrentLocalSurfaceId());
ASSERT_TRUE(compositor()->IsVisible());
ThroughputTracker tracker = compositor()->RequestNewThroughputTracker();
base::RunLoop run_loop;
tracker.Start(base::BindLambdaForTesting(
[&](const cc::FrameSequenceMetrics::CustomReportData& data) {
EXPECT_GT(data.frames_expected_v3, 0u);
EXPECT_GT(data.frames_expected_v3 - data.frames_dropped_v3, 0u);
run_loop.Quit();
}));
for (int i = 0; i < 5; ++i) {
compositor()->ScheduleFullRedraw();
DrawWaiterForTest::WaitForCompositingEnded(compositor());
}
EXPECT_TRUE(tracker.Stop());
for (int i = 0; i < 5; ++i) {
compositor()->ScheduleFullRedraw();
DrawWaiterForTest::WaitForCompositingEnded(compositor());
}
run_loop.Run();
}
TEST_F(CompositorTestWithMessageLoop, ThroughputTrackerOutliveCompositor) {
auto tracker = compositor()->RequestNewThroughputTracker();
tracker.Start(base::BindLambdaForTesting(
[&](const cc::FrameSequenceMetrics::CustomReportData& data) {
ADD_FAILURE() << "No report should happen";
}));
DestroyCompositor();
EXPECT_FALSE(tracker.Stop());
}
TEST_F(CompositorTestWithMessageLoop, ThroughputTrackerCallbackStateChange) {
auto root_layer = std::make_unique<Layer>(ui::LAYER_SOLID_COLOR);
viz::ParentLocalSurfaceIdAllocator allocator;
allocator.GenerateId();
root_layer->SetBounds(gfx::Rect(10, 10));
compositor()->SetRootLayer(root_layer.get());
compositor()->SetScaleAndSize(1.0f, gfx::Size(10, 10),
allocator.GetCurrentLocalSurfaceId());
ASSERT_TRUE(compositor()->IsVisible());
ThroughputTracker tracker = compositor()->RequestNewThroughputTracker();
base::RunLoop run_loop;
tracker.Start(base::BindLambdaForTesting(
[&](const cc::FrameSequenceMetrics::CustomReportData& data) {
tracker.Cancel();
ThroughputTracker another_tracker =
compositor()->RequestNewThroughputTracker();
another_tracker.Start(base::DoNothing());
run_loop.Quit();
}));
for (int i = 0; i < 5; ++i) {
compositor()->ScheduleFullRedraw();
DrawWaiterForTest::WaitForCompositingEnded(compositor());
}
EXPECT_TRUE(tracker.Stop());
for (int i = 0; i < 5; ++i) {
compositor()->ScheduleFullRedraw();
DrawWaiterForTest::WaitForCompositingEnded(compositor());
}
run_loop.Run();
}
TEST_F(CompositorTestWithMessageLoop, ThroughputTrackerInvoluntaryReport) {
auto root_layer = std::make_unique<Layer>(ui::LAYER_SOLID_COLOR);
viz::ParentLocalSurfaceIdAllocator allocator;
allocator.GenerateId();
root_layer->SetBounds(gfx::Rect(10, 10));
compositor()->SetRootLayer(root_layer.get());
compositor()->SetScaleAndSize(1.0f, gfx::Size(10, 10),
allocator.GetCurrentLocalSurfaceId());
ASSERT_TRUE(compositor()->IsVisible());
ThroughputTracker tracker = compositor()->RequestNewThroughputTracker();
tracker.Start(base::BindLambdaForTesting(
[&](const cc::FrameSequenceMetrics::CustomReportData& data) {
ADD_FAILURE() << "No report should happen";
}));
for (int i = 0; i < 5; ++i) {
compositor()->ScheduleFullRedraw();
DrawWaiterForTest::WaitForCompositingEnded(compositor());
}
compositor()->SetVisible(false);
compositor()->ReleaseAcceleratedWidget();
EXPECT_FALSE(tracker.Stop());
}
#endif
#if BUILDFLAG(IS_WIN)
#define MAYBE_CreateAndReleaseOutputSurface …
#else
#define MAYBE_CreateAndReleaseOutputSurface …
#endif
TEST_F(CompositorTestWithMessageLoop, MAYBE_CreateAndReleaseOutputSurface) { … }
class LayerDelegateThatAddsDuringUpdateVisualState : public LayerDelegate { … };
TEST_F(CompositorTestWithMessageLoop, AddLayerDuringUpdateVisualState) { … }
TEST_F(CompositorTestWithMessageLoop, CompositorVisibilityChanges) { … }
}