summaryrefslogtreecommitdiffstats
path: root/components/view_manager/view_coordinate_conversions_unittest.cc
blob: addd466bee06c83fdb7c073e12c460be6f392753 (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/view_manager/view_coordinate_conversions.h"

#include "components/view_manager/server_view.h"
#include "components/view_manager/server_view_delegate.h"
#include "components/view_manager/test_server_view_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"

namespace view_manager {

using ViewCoordinateConversionsTest = testing::Test;

TEST_F(ViewCoordinateConversionsTest, ConvertRectBetweenViews) {
  TestServerViewDelegate d1, d2, d3;
  ServerView v1(&d1, ViewId()), v2(&d2, ViewId()), v3(&d3, ViewId());
  v1.SetBounds(gfx::Rect(1, 2, 100, 100));
  v2.SetBounds(gfx::Rect(3, 4, 100, 100));
  v3.SetBounds(gfx::Rect(5, 6, 100, 100));
  v1.Add(&v2);
  v2.Add(&v3);

  EXPECT_EQ(gfx::Rect(2, 1, 8, 9),
            ConvertRectBetweenViews(&v1, &v3, gfx::Rect(10, 11, 8, 9)));

  EXPECT_EQ(gfx::Rect(18, 21, 8, 9),
            ConvertRectBetweenViews(&v3, &v1, gfx::Rect(10, 11, 8, 9)));
}

TEST_F(ViewCoordinateConversionsTest, ConvertPointFBetweenViews) {
  TestServerViewDelegate d1, d2, d3;
  ServerView v1(&d1, ViewId()), v2(&d2, ViewId()), v3(&d3, ViewId());
  v1.SetBounds(gfx::Rect(1, 2, 100, 100));
  v2.SetBounds(gfx::Rect(3, 4, 100, 100));
  v3.SetBounds(gfx::Rect(5, 6, 100, 100));
  v1.Add(&v2);
  v2.Add(&v3);

  {
    const gfx::PointF result(
        ConvertPointFBetweenViews(&v1, &v3, gfx::PointF(10.5f, 11.9f)));
    EXPECT_FLOAT_EQ(2.5f, result.x());
    EXPECT_FLOAT_EQ(1.9f, result.y());
  }

  {
    const gfx::PointF result(
        ConvertPointFBetweenViews(&v3, &v1, gfx::PointF(10.2f, 11.4f)));
    EXPECT_FLOAT_EQ(18.2f, result.x());
    EXPECT_FLOAT_EQ(21.4f, result.y());
  }
}

}  // namespace view_manager