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 "base/message_loop/message_loop.h" |
| 6 #include "mojo/public/cpp/bindings/binding_set.h" |
| 7 #include "services/ui/public/interfaces/cursor/cursor_struct_traits_test.mojom.h
" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/base/cursor/cursor.h" |
| 10 #include "ui/base/cursor/cursor_data.h" |
| 11 |
| 12 namespace ui { |
| 13 |
| 14 namespace { |
| 15 |
| 16 class CursorStructTraitsTest : public testing::Test, |
| 17 public mojom::CursorStructTraitsTest { |
| 18 public: |
| 19 CursorStructTraitsTest() {} |
| 20 |
| 21 protected: |
| 22 mojom::CursorStructTraitsTestPtr GetTraitsTestProxy() { |
| 23 return traits_test_bindings_.CreateInterfacePtrAndBind(this); |
| 24 } |
| 25 |
| 26 private: |
| 27 // mojom::CursorStructTraitsTest: |
| 28 void EchoCursorData(ui::CursorData in, |
| 29 const EchoCursorDataCallback& callback) override { |
| 30 callback.Run(in); |
| 31 } |
| 32 |
| 33 base::MessageLoop loop_; // A MessageLoop is needed for Mojo IPC to work. |
| 34 mojo::BindingSet<mojom::CursorStructTraitsTest> traits_test_bindings_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(CursorStructTraitsTest); |
| 37 }; |
| 38 |
| 39 std::vector<SkBitmap> CreateTestCursorFrames(const gfx::Size& size, |
| 40 unsigned int count) { |
| 41 std::vector<SkBitmap> frames(count); |
| 42 for (size_t i = 0; i < count; ++i) { |
| 43 SkBitmap& bitmap = frames[i]; |
| 44 bitmap.allocN32Pixels(size.width(), size.height()); |
| 45 bitmap.eraseColor(SK_ColorRED); |
| 46 } |
| 47 return frames; |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 // Tests numeric cursor ids. |
| 53 TEST_F(CursorStructTraitsTest, TestBuiltIn) { |
| 54 mojom::CursorStructTraitsTestPtr proxy = GetTraitsTestProxy(); |
| 55 for (int i = 0; i < 43; ++i) { |
| 56 ui::CursorData input(i); |
| 57 |
| 58 ui::CursorData output; |
| 59 ASSERT_TRUE(proxy->EchoCursorData(input, &output)); |
| 60 EXPECT_TRUE(output.IsType(i)); |
| 61 } |
| 62 } |
| 63 |
| 64 // Test that we copy cursor bitmaps and metadata across the wire. |
| 65 TEST_F(CursorStructTraitsTest, TestBitmapCursor) { |
| 66 mojom::CursorStructTraitsTestPtr proxy = GetTraitsTestProxy(); |
| 67 |
| 68 const uint32_t kFrameDelayMs = 14; |
| 69 const gfx::Point kHotspot = gfx::Point(5, 2); |
| 70 |
| 71 ui::CursorData input(kHotspot, CreateTestCursorFrames(gfx::Size(10, 10), 3), |
| 72 kFrameDelayMs); |
| 73 |
| 74 ui::CursorData output; |
| 75 ASSERT_TRUE(proxy->EchoCursorData(input, &output)); |
| 76 |
| 77 EXPECT_EQ(kCursorCustom, output.native_type()); |
| 78 EXPECT_EQ(kFrameDelayMs, output.frame_delay_ms()); |
| 79 EXPECT_EQ(kHotspot, output.hotspot()); |
| 80 |
| 81 // Even if the pixel data is logically the same, expect that it has different |
| 82 // generation ids. |
| 83 EXPECT_FALSE(output.IsSameAs(input)); |
| 84 |
| 85 // Make a copy of output. It should be the same as output. |
| 86 ui::CursorData copy = output; |
| 87 EXPECT_TRUE(copy.IsSameAs(output)); |
| 88 |
| 89 // But make sure that the pixel data actually is equivalent. |
| 90 ASSERT_EQ(input.cursor_frames().size(), output.cursor_frames().size()); |
| 91 for (size_t f = 0; f < input.cursor_frames().size(); ++f) { |
| 92 ASSERT_EQ(input.cursor_frames()[f].width(), |
| 93 output.cursor_frames()[f].width()); |
| 94 ASSERT_EQ(input.cursor_frames()[f].height(), |
| 95 output.cursor_frames()[f].height()); |
| 96 |
| 97 input.cursor_frames()[f].lockPixels(); |
| 98 output.cursor_frames()[f].lockPixels(); |
| 99 for (int x = 0; x < input.cursor_frames()[f].width(); ++x) { |
| 100 for (int y = 0; y < input.cursor_frames()[f].height(); ++y) { |
| 101 EXPECT_EQ(input.cursor_frames()[f].getColor(x, y), |
| 102 output.cursor_frames()[f].getColor(x, y)); |
| 103 } |
| 104 } |
| 105 |
| 106 output.cursor_frames()[f].unlockPixels(); |
| 107 input.cursor_frames()[f].unlockPixels(); |
| 108 } |
| 109 } |
| 110 |
| 111 } // namespace ui |
OLD | NEW |