OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/ui/public/interfaces/cursor/cursor_struct_traits.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "mojo/common/common_custom_types_struct_traits.h" |
| 9 #include "mojo/public/cpp/bindings/binding_set.h" |
| 10 #include "services/ui/public/interfaces/cursor/cursor.mojom.h" |
| 11 #include "skia/public/interfaces/bitmap_array_struct_traits.h" |
| 12 #include "skia/public/interfaces/bitmap_skbitmap_struct_traits.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/base/cursor/cursor.h" |
| 15 #include "ui/base/cursor/cursor_data.h" |
| 16 #include "ui/gfx/geometry/mojo/geometry_struct_traits.h" |
| 17 |
| 18 namespace ui { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class CursorStructTraitsTest : public testing::Test { |
| 23 public: |
| 24 CursorStructTraitsTest() {} |
| 25 |
| 26 protected: |
| 27 bool EchoCursorData(const ui::CursorData& in, ui::CursorData* out) { |
| 28 return mojom::CursorData::Deserialize(mojom::CursorData::Serialize(&in), |
| 29 out); |
| 30 } |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(CursorStructTraitsTest); |
| 33 }; |
| 34 |
| 35 std::vector<SkBitmap> CreateTestCursorFrames(const gfx::Size& size, |
| 36 unsigned int count) { |
| 37 std::vector<SkBitmap> frames(count); |
| 38 for (size_t i = 0; i < count; ++i) { |
| 39 SkBitmap& bitmap = frames[i]; |
| 40 bitmap.allocN32Pixels(size.width(), size.height()); |
| 41 bitmap.eraseColor(SK_ColorRED); |
| 42 } |
| 43 return frames; |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 // Tests numeric cursor ids. |
| 49 TEST_F(CursorStructTraitsTest, TestBuiltIn) { |
| 50 for (int i = 0; i < 43; ++i) { |
| 51 ui::CursorData input(i); |
| 52 |
| 53 ui::CursorData output; |
| 54 ASSERT_TRUE(EchoCursorData(input, &output)); |
| 55 EXPECT_TRUE(output.IsType(i)); |
| 56 } |
| 57 } |
| 58 |
| 59 // Test that we copy cursor bitmaps and metadata across the wire. |
| 60 TEST_F(CursorStructTraitsTest, TestBitmapCursor) { |
| 61 const base::TimeDelta kFrameDelay = base::TimeDelta::FromMilliseconds(15); |
| 62 const gfx::Point kHotspot = gfx::Point(5, 2); |
| 63 const float kScale = 2.0f; |
| 64 |
| 65 ui::CursorData input(kHotspot, CreateTestCursorFrames(gfx::Size(10, 10), 3), |
| 66 kScale, kFrameDelay); |
| 67 |
| 68 ui::CursorData output; |
| 69 ASSERT_TRUE(EchoCursorData(input, &output)); |
| 70 |
| 71 EXPECT_EQ(kCursorCustom, output.cursor_type()); |
| 72 EXPECT_EQ(kScale, output.scale_factor()); |
| 73 EXPECT_EQ(kFrameDelay, output.frame_delay()); |
| 74 EXPECT_EQ(kHotspot, output.hotspot_in_pixels()); |
| 75 |
| 76 // Even if the pixel data is logically the same, expect that it has different |
| 77 // generation ids. |
| 78 EXPECT_FALSE(output.IsSameAs(input)); |
| 79 |
| 80 // Make a copy of output. It should be the same as output. |
| 81 ui::CursorData copy = output; |
| 82 EXPECT_TRUE(copy.IsSameAs(output)); |
| 83 |
| 84 // But make sure that the pixel data actually is equivalent. |
| 85 ASSERT_EQ(input.cursor_frames().size(), output.cursor_frames().size()); |
| 86 for (size_t f = 0; f < input.cursor_frames().size(); ++f) { |
| 87 ASSERT_EQ(input.cursor_frames()[f].width(), |
| 88 output.cursor_frames()[f].width()); |
| 89 ASSERT_EQ(input.cursor_frames()[f].height(), |
| 90 output.cursor_frames()[f].height()); |
| 91 |
| 92 input.cursor_frames()[f].lockPixels(); |
| 93 output.cursor_frames()[f].lockPixels(); |
| 94 for (int x = 0; x < input.cursor_frames()[f].width(); ++x) { |
| 95 for (int y = 0; y < input.cursor_frames()[f].height(); ++y) { |
| 96 EXPECT_EQ(input.cursor_frames()[f].getColor(x, y), |
| 97 output.cursor_frames()[f].getColor(x, y)); |
| 98 } |
| 99 } |
| 100 |
| 101 output.cursor_frames()[f].unlockPixels(); |
| 102 input.cursor_frames()[f].unlockPixels(); |
| 103 } |
| 104 } |
| 105 |
| 106 } // namespace ui |
OLD | NEW |