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

ash / utility / layer_util_unittest.cc [blame]

// 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 "ash/utility/layer_util.h"

#include "base/cancelable_callback.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/timer/timer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/test/test_compositor_host.h"
#include "ui/compositor/test/test_context_factories.h"
#include "ui/gfx/geometry/rect.h"

namespace ash {
namespace {

class LayerUtilTest : public testing::Test {
 public:
  LayerUtilTest() = default;
  LayerUtilTest(const LayerUtilTest&) = delete;
  LayerUtilTest& operator=(const LayerUtilTest&) = delete;
  ~LayerUtilTest() override = default;

  // testing::Test:
  void SetUp() override {
    context_factories_ = std::make_unique<ui::TestContextFactories>(false);

    const gfx::Rect bounds(300, 300);
    host_.reset(ui::TestCompositorHost::Create(
        bounds, context_factories_->GetContextFactory()));
    host_->Show();

    compositor()->SetRootLayer(&root_);
  }
  void TearDown() override {
    host_.reset();
    context_factories_.reset();
  }

  void Advance(const base::TimeDelta& delta) {
    task_environment_.FastForwardBy(delta);
  }

  void GenerateOneFrame() { compositor()->ScheduleFullRedraw(); }

  ui::Compositor* compositor() { return host_->GetCompositor(); }
  ui::Layer* root_layer() { return &root_; }

 private:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME,
      base::test::TaskEnvironment::MainThreadType::UI};

  std::unique_ptr<ui::TestContextFactories> context_factories_;
  std::unique_ptr<ui::TestCompositorHost> host_;
  ui::Layer root_;
};

}  // namespace

TEST_F(LayerUtilTest, CopyContentToExistingLayer) {
  ui::ScopedAnimationDurationScaleMode non_zero(
      ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);

  ui::Layer layer1;
  layer1.SetBounds(gfx::Rect(100, 100));
  root_layer()->Add(&layer1);

  ui::Layer layer2;
  layer2.SetBounds(gfx::Rect(100, 100));
  root_layer()->Add(&layer2);

  {
    bool called = false;
    base::CancelableOnceCallback<void(ui::Layer**)> cancelable;
    cancelable.Reset(base::BindLambdaForTesting([&](ui::Layer** dummy) {
      called = true;
      *dummy = &layer2;
    }));
    CopyLayerContentToLayer(&layer1, cancelable.callback());

    GenerateOneFrame();
    Advance(base::Milliseconds(1000));
    EXPECT_TRUE(called);
  }

  // Test cancel scenario.
  {
    bool called = false;
    base::CancelableOnceCallback<void(ui::Layer**)> cancelable;
    cancelable.Reset(base::BindLambdaForTesting([&](ui::Layer** dummy) {
      called = true;
      *dummy = &layer2;
    }));

    CopyLayerContentToLayer(&layer1, cancelable.callback());
    cancelable.Cancel();

    GenerateOneFrame();
    Advance(base::Milliseconds(1000));
    EXPECT_FALSE(called);
  }
}

}  //  namespace ash