diff options
Diffstat (limited to 'ui/gfx/geometry')
-rw-r--r-- | ui/gfx/geometry/mojo/BUILD.gn | 49 | ||||
-rw-r--r-- | ui/gfx/geometry/mojo/DEPS | 4 | ||||
-rw-r--r-- | ui/gfx/geometry/mojo/geometry.mojom | 63 | ||||
-rw-r--r-- | ui/gfx/geometry/mojo/geometry.typemap | 32 | ||||
-rw-r--r-- | ui/gfx/geometry/mojo/geometry_struct_traits.h | 146 | ||||
-rw-r--r-- | ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc | 206 | ||||
-rw-r--r-- | ui/gfx/geometry/mojo/geometry_traits_test_service.mojom | 41 |
7 files changed, 541 insertions, 0 deletions
diff --git a/ui/gfx/geometry/mojo/BUILD.gn b/ui/gfx/geometry/mojo/BUILD.gn new file mode 100644 index 0000000000..f6cda9d3be --- /dev/null +++ b/ui/gfx/geometry/mojo/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright 2016 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. + +import("//mojo/public/tools/bindings/mojom.gni") + +# This target does NOT depend on skia. One can depend on this target to avoid +# picking up a dependency on skia. +mojom("mojo") { + sources = [ + "geometry.mojom", + ] +} + +mojom("test_interfaces") { + sources = [ + "geometry_traits_test_service.mojom", + ] + + public_deps = [ + ":mojo", + ] +} + +source_set("unit_test") { + testonly = true + + sources = [ + "geometry_struct_traits_unittest.cc", + ] + + deps = [ + ":test_interfaces", + "//base", + "//mojo/public/cpp/bindings", + "//testing/gtest", + "//ui/gfx/geometry", + ] +} + +source_set("struct_traits") { + sources = [ + "geometry_struct_traits.h", + ] + public_deps = [ + ":mojo_shared_cpp_sources", + "//ui/gfx/geometry", + ] +} diff --git a/ui/gfx/geometry/mojo/DEPS b/ui/gfx/geometry/mojo/DEPS new file mode 100644 index 0000000000..3ad6543823 --- /dev/null +++ b/ui/gfx/geometry/mojo/DEPS @@ -0,0 +1,4 @@ +include_rules = [ + "+mojo/public", + "+ui/gfx/geometry", +] diff --git a/ui/gfx/geometry/mojo/geometry.mojom b/ui/gfx/geometry/mojo/geometry.mojom new file mode 100644 index 0000000000..97143866da --- /dev/null +++ b/ui/gfx/geometry/mojo/geometry.mojom @@ -0,0 +1,63 @@ +// 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. + +module gfx.mojom; + +struct Point { + int32 x; + int32 y; +}; + +struct PointF { + float x; + float y; +}; + +struct Size { + int32 width; + int32 height; +}; + +struct SizeF { + float width; + float height; +}; + +struct Rect { + int32 x; + int32 y; + int32 width; + int32 height; +}; + +struct RectF { + float x; + float y; + float width; + float height; +}; + +struct Insets { + int32 top; + int32 left; + int32 bottom; + int32 right; +}; + +struct InsetsF { + float top; + float left; + float bottom; + float right; +}; + +struct Vector2d { + int32 x; + int32 y; +}; + +struct Vector2dF { + float x; + float y; +}; diff --git a/ui/gfx/geometry/mojo/geometry.typemap b/ui/gfx/geometry/mojo/geometry.typemap new file mode 100644 index 0000000000..686ea05cfb --- /dev/null +++ b/ui/gfx/geometry/mojo/geometry.typemap @@ -0,0 +1,32 @@ +# Copyright 2016 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. + +mojom = "//ui/gfx/geometry/mojo/geometry.mojom" +public_headers = [ + "//ui/gfx/geometry/point.h", + "//ui/gfx/geometry/point_f.h", + "//ui/gfx/geometry/size.h", + "//ui/gfx/geometry/rect.h", + "//ui/gfx/geometry/rect_f.h", + "//ui/gfx/geometry/safe_integer_conversions.h", + "//ui/gfx/geometry/insets.h", + "//ui/gfx/geometry/vector2d.h", + "//ui/gfx/geometry/vector2d_f.h", +] +traits_headers = [ "//ui/gfx/geometry/mojo/geometry_struct_traits.h" ] +deps = [ + "//ui/gfx/geometry/mojo:struct_traits", +] +type_mappings = [ + "gfx.mojom.Point=gfx::Point", + "gfx.mojom.PointF=gfx::PointF", + "gfx.mojom.Size=gfx::Size", + "gfx.mojom.SizeF=gfx::SizeF", + "gfx.mojom.Rect=gfx::Rect", + "gfx.mojom.RectF=gfx::RectF", + "gfx.mojom.Insets=gfx::Insets", + "gfx.mojom.InsetsF=gfx::InsetsF", + "gfx.mojom.Vector2d=gfx::Vector2d", + "gfx.mojom.Vector2dF=gfx::Vector2dF", +] diff --git a/ui/gfx/geometry/mojo/geometry_struct_traits.h b/ui/gfx/geometry/mojo/geometry_struct_traits.h new file mode 100644 index 0000000000..a31d7385b6 --- /dev/null +++ b/ui/gfx/geometry/mojo/geometry_struct_traits.h @@ -0,0 +1,146 @@ +// Copyright 2016 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. + +#ifndef UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ +#define UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ + +#include "ui/gfx/geometry/insets.h" +#include "ui/gfx/geometry/insets_f.h" +#include "ui/gfx/geometry/mojo/geometry.mojom-shared.h" +#include "ui/gfx/geometry/point.h" +#include "ui/gfx/geometry/point_f.h" +#include "ui/gfx/geometry/rect.h" +#include "ui/gfx/geometry/rect_f.h" +#include "ui/gfx/geometry/size.h" +#include "ui/gfx/geometry/size_f.h" +#include "ui/gfx/geometry/vector2d.h" +#include "ui/gfx/geometry/vector2d_f.h" + +namespace mojo { + +template <> +struct StructTraits<gfx::mojom::InsetsDataView, gfx::Insets> { + static int top(const gfx::Insets& p) { return p.top(); } + static int left(const gfx::Insets& p) { return p.left(); } + static int bottom(const gfx::Insets& p) { return p.bottom(); } + static int right(const gfx::Insets& p) { return p.right(); } + static bool Read(gfx::mojom::InsetsDataView data, gfx::Insets* out) { + out->Set(data.top(), data.left(), data.bottom(), data.right()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::InsetsFDataView, gfx::InsetsF> { + static float top(const gfx::InsetsF& p) { return p.top(); } + static float left(const gfx::InsetsF& p) { return p.left(); } + static float bottom(const gfx::InsetsF& p) { return p.bottom(); } + static float right(const gfx::InsetsF& p) { return p.right(); } + static bool Read(gfx::mojom::InsetsFDataView data, gfx::InsetsF* out) { + out->Set(data.top(), data.left(), data.bottom(), data.right()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::PointDataView, gfx::Point> { + static int x(const gfx::Point& p) { return p.x(); } + static int y(const gfx::Point& p) { return p.y(); } + static bool Read(gfx::mojom::PointDataView data, gfx::Point* out) { + out->SetPoint(data.x(), data.y()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::PointFDataView, gfx::PointF> { + static float x(const gfx::PointF& p) { return p.x(); } + static float y(const gfx::PointF& p) { return p.y(); } + static bool Read(gfx::mojom::PointFDataView data, gfx::PointF* out) { + out->SetPoint(data.x(), data.y()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::RectDataView, gfx::Rect> { + static int x(const gfx::Rect& p) { return p.x(); } + static int y(const gfx::Rect& p) { return p.y(); } + static int width(const gfx::Rect& p) { return p.width(); } + static int height(const gfx::Rect& p) { return p.height(); } + static bool Read(gfx::mojom::RectDataView data, gfx::Rect* out) { + if (data.width() < 0 || data.height() < 0) + return false; + + out->SetRect(data.x(), data.y(), data.width(), data.height()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::RectFDataView, gfx::RectF> { + static float x(const gfx::RectF& p) { return p.x(); } + static float y(const gfx::RectF& p) { return p.y(); } + static float width(const gfx::RectF& p) { return p.width(); } + static float height(const gfx::RectF& p) { return p.height(); } + static bool Read(gfx::mojom::RectFDataView data, gfx::RectF* out) { + if (data.width() < 0 || data.height() < 0) + return false; + + out->SetRect(data.x(), data.y(), data.width(), data.height()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::SizeDataView, gfx::Size> { + static int width(const gfx::Size& p) { return p.width(); } + static int height(const gfx::Size& p) { return p.height(); } + static bool Read(gfx::mojom::SizeDataView data, gfx::Size* out) { + if (data.width() < 0 || data.height() < 0) + return false; + + out->SetSize(data.width(), data.height()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::SizeFDataView, gfx::SizeF> { + static float width(const gfx::SizeF& p) { return p.width(); } + static float height(const gfx::SizeF& p) { return p.height(); } + static bool Read(gfx::mojom::SizeFDataView data, gfx::SizeF* out) { + if (data.width() < 0 || data.height() < 0) + return false; + + out->SetSize(data.width(), data.height()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::Vector2dDataView, gfx::Vector2d> { + static int x(const gfx::Vector2d& v) { return v.x(); } + static int y(const gfx::Vector2d& v) { return v.y(); } + static bool Read(gfx::mojom::Vector2dDataView data, gfx::Vector2d* out) { + out->set_x(data.x()); + out->set_y(data.y()); + return true; + } +}; + +template <> +struct StructTraits<gfx::mojom::Vector2dFDataView, gfx::Vector2dF> { + static float x(const gfx::Vector2dF& v) { return v.x(); } + static float y(const gfx::Vector2dF& v) { return v.y(); } + static bool Read(gfx::mojom::Vector2dFDataView data, gfx::Vector2dF* out) { + out->set_x(data.x()); + out->set_y(data.y()); + return true; + } +}; + +} // namespace mojo + +#endif // UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ diff --git a/ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc b/ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc new file mode 100644 index 0000000000..d59579443a --- /dev/null +++ b/ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc @@ -0,0 +1,206 @@ +// Copyright 2016 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 "base/message_loop/message_loop.h" +#include "mojo/public/cpp/bindings/binding_set.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/gfx/geometry/mojo/geometry_traits_test_service.mojom.h" +#include "ui/gfx/geometry/point.h" + +namespace gfx { + +namespace { + +class GeometryStructTraitsTest : public testing::Test, + public mojom::GeometryTraitsTestService { + public: + GeometryStructTraitsTest() {} + + protected: + mojom::GeometryTraitsTestServicePtr GetTraitsTestProxy() { + return traits_test_bindings_.CreateInterfacePtrAndBind(this); + } + + private: + // GeometryTraitsTestService: + void EchoPoint(const Point& p, const EchoPointCallback& callback) override { + callback.Run(p); + } + + void EchoPointF(const PointF& p, + const EchoPointFCallback& callback) override { + callback.Run(p); + } + + void EchoSize(const Size& s, const EchoSizeCallback& callback) override { + callback.Run(s); + } + + void EchoSizeF(const SizeF& s, const EchoSizeFCallback& callback) override { + callback.Run(s); + } + + void EchoRect(const Rect& r, const EchoRectCallback& callback) override { + callback.Run(r); + } + + void EchoRectF(const RectF& r, const EchoRectFCallback& callback) override { + callback.Run(r); + } + + void EchoInsets(const Insets& i, + const EchoInsetsCallback& callback) override { + callback.Run(i); + } + + void EchoInsetsF(const InsetsF& i, + const EchoInsetsFCallback& callback) override { + callback.Run(i); + } + + void EchoVector2d(const Vector2d& v, + const EchoVector2dCallback& callback) override { + callback.Run(v); + } + + void EchoVector2dF(const Vector2dF& v, + const EchoVector2dFCallback& callback) override { + callback.Run(v); + } + + base::MessageLoop loop_; + mojo::BindingSet<GeometryTraitsTestService> traits_test_bindings_; + + DISALLOW_COPY_AND_ASSIGN(GeometryStructTraitsTest); +}; + +} // namespace + +TEST_F(GeometryStructTraitsTest, Point) { + const int32_t x = 1234; + const int32_t y = -5678; + gfx::Point input(x, y); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::Point output; + proxy->EchoPoint(input, &output); + EXPECT_EQ(x, output.x()); + EXPECT_EQ(y, output.y()); +} + +TEST_F(GeometryStructTraitsTest, PointF) { + const float x = 1234.5f; + const float y = 6789.6f; + gfx::PointF input(x, y); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::PointF output; + proxy->EchoPointF(input, &output); + EXPECT_EQ(x, output.x()); + EXPECT_EQ(y, output.y()); +} + +TEST_F(GeometryStructTraitsTest, Size) { + const int32_t width = 1234; + const int32_t height = 5678; + gfx::Size input(width, height); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::Size output; + proxy->EchoSize(input, &output); + EXPECT_EQ(width, output.width()); + EXPECT_EQ(height, output.height()); +} + +TEST_F(GeometryStructTraitsTest, SizeF) { + const float width = 1234.5f; + const float height = 6789.6f; + gfx::SizeF input(width, height); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::SizeF output; + proxy->EchoSizeF(input, &output); + EXPECT_EQ(width, output.width()); + EXPECT_EQ(height, output.height()); +} + +TEST_F(GeometryStructTraitsTest, Rect) { + const int32_t x = 1234; + const int32_t y = 5678; + const int32_t width = 4321; + const int32_t height = 8765; + gfx::Rect input(x, y, width, height); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::Rect output; + proxy->EchoRect(input, &output); + EXPECT_EQ(x, output.x()); + EXPECT_EQ(y, output.y()); + EXPECT_EQ(width, output.width()); + EXPECT_EQ(height, output.height()); +} + +TEST_F(GeometryStructTraitsTest, RectF) { + const float x = 1234.1f; + const float y = 5678.2f; + const float width = 4321.3f; + const float height = 8765.4f; + gfx::RectF input(x, y, width, height); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::RectF output; + proxy->EchoRectF(input, &output); + EXPECT_EQ(x, output.x()); + EXPECT_EQ(y, output.y()); + EXPECT_EQ(width, output.width()); + EXPECT_EQ(height, output.height()); +} + +TEST_F(GeometryStructTraitsTest, Insets) { + const int32_t top = 1234; + const int32_t left = 5678; + const int32_t bottom = 4321; + const int32_t right = 8765; + gfx::Insets input(top, left, bottom, right); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::Insets output; + proxy->EchoInsets(input, &output); + EXPECT_EQ(top, output.top()); + EXPECT_EQ(left, output.left()); + EXPECT_EQ(bottom, output.bottom()); + EXPECT_EQ(right, output.right()); +} + +TEST_F(GeometryStructTraitsTest, InsetsF) { + const float top = 1234.1f; + const float left = 5678.2f; + const float bottom = 4321.3f; + const float right = 8765.4f; + gfx::InsetsF input(top, left, bottom, right); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::InsetsF output; + proxy->EchoInsetsF(input, &output); + EXPECT_EQ(top, output.top()); + EXPECT_EQ(left, output.left()); + EXPECT_EQ(bottom, output.bottom()); + EXPECT_EQ(right, output.right()); +} + +TEST_F(GeometryStructTraitsTest, Vector2d) { + const int32_t x = 1234; + const int32_t y = -5678; + gfx::Vector2d input(x, y); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::Vector2d output; + proxy->EchoVector2d(input, &output); + EXPECT_EQ(x, output.x()); + EXPECT_EQ(y, output.y()); +} + +TEST_F(GeometryStructTraitsTest, Vector2dF) { + const float x = 1234.5f; + const float y = 6789.6f; + gfx::Vector2dF input(x, y); + mojom::GeometryTraitsTestServicePtr proxy = GetTraitsTestProxy(); + gfx::Vector2dF output; + proxy->EchoVector2dF(input, &output); + EXPECT_EQ(x, output.x()); + EXPECT_EQ(y, output.y()); +} + +} // namespace gfx diff --git a/ui/gfx/geometry/mojo/geometry_traits_test_service.mojom b/ui/gfx/geometry/mojo/geometry_traits_test_service.mojom new file mode 100644 index 0000000000..8d4fb94697 --- /dev/null +++ b/ui/gfx/geometry/mojo/geometry_traits_test_service.mojom @@ -0,0 +1,41 @@ +// Copyright 2016 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. + +module gfx.mojom; + +import "ui/gfx/geometry/mojo/geometry.mojom"; + +// All functions on this interface echo their arguments to test StructTraits +// serialization and deserialization. +interface GeometryTraitsTestService { + [Sync] + EchoPoint(Point p) => (Point pass); + + [Sync] + EchoPointF(PointF p) => (PointF pass); + + [Sync] + EchoSize(Size s) => (Size pass); + + [Sync] + EchoSizeF(SizeF s) => (SizeF pass); + + [Sync] + EchoRect(Rect r) => (Rect pass); + + [Sync] + EchoRectF(RectF r) => (RectF pass); + + [Sync] + EchoInsets(Insets i) => (Insets pass); + + [Sync] + EchoInsetsF(InsetsF i) => (InsetsF pass); + + [Sync] + EchoVector2d(Vector2d v) => (Vector2d pass); + + [Sync] + EchoVector2dF(Vector2dF v) => (Vector2dF pass); +}; |