| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/image/image_skia.h" | 5 #include "ui/gfx/image/image_skia.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/threading/simple_thread.h" | 12 #include "base/threading/simple_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/gfx/geometry/size.h" | 15 #include "ui/gfx/geometry/size.h" |
| 16 #include "ui/gfx/image/image_skia_rep.h" | 16 #include "ui/gfx/image/image_skia_rep.h" |
| 17 #include "ui/gfx/image/image_skia_source.h" | 17 #include "ui/gfx/image/image_skia_source.h" |
| 18 #include "ui/gfx/switches.h" | 18 #include "ui/gfx/switches.h" |
| 19 | 19 |
| 20 // Duplicated from base/threading/non_thread_safe.h so that we can be | |
| 21 // good citizens there and undef the macro. | |
| 22 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | |
| 23 #define ENABLE_NON_THREAD_SAFE 1 | |
| 24 #else | |
| 25 #define ENABLE_NON_THREAD_SAFE 0 | |
| 26 #endif | |
| 27 | |
| 28 namespace gfx { | 20 namespace gfx { |
| 29 | 21 |
| 30 namespace { | 22 namespace { |
| 31 | 23 |
| 32 class FixedSource : public ImageSkiaSource { | 24 class FixedSource : public ImageSkiaSource { |
| 33 public: | 25 public: |
| 34 explicit FixedSource(const ImageSkiaRep& image) : image_(image) {} | 26 explicit FixedSource(const ImageSkiaRep& image) : image_(image) {} |
| 35 | 27 |
| 36 ~FixedSource() override {} | 28 ~FixedSource() override {} |
| 37 | 29 |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 ImageSkia copy = image; | 340 ImageSkia copy = image; |
| 349 copy.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), | 341 copy.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), |
| 350 2.0f)); | 342 2.0f)); |
| 351 unrelated.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), | 343 unrelated.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), |
| 352 1.0f)); | 344 1.0f)); |
| 353 EXPECT_TRUE(image.BackedBySameObjectAs(copy)); | 345 EXPECT_TRUE(image.BackedBySameObjectAs(copy)); |
| 354 EXPECT_FALSE(image.BackedBySameObjectAs(unrelated)); | 346 EXPECT_FALSE(image.BackedBySameObjectAs(unrelated)); |
| 355 EXPECT_FALSE(copy.BackedBySameObjectAs(unrelated)); | 347 EXPECT_FALSE(copy.BackedBySameObjectAs(unrelated)); |
| 356 } | 348 } |
| 357 | 349 |
| 358 #if ENABLE_NON_THREAD_SAFE | 350 #if DCHECK_IS_ON() |
| 359 TEST_F(ImageSkiaTest, EmptyOnThreadTest) { | 351 TEST_F(ImageSkiaTest, EmptyOnThreadTest) { |
| 360 ImageSkia empty; | 352 ImageSkia empty; |
| 361 test::TestOnThread empty_on_thread(&empty); | 353 test::TestOnThread empty_on_thread(&empty); |
| 362 empty_on_thread.Start(); | 354 empty_on_thread.Start(); |
| 363 empty_on_thread.Join(); | 355 empty_on_thread.Join(); |
| 364 EXPECT_TRUE(empty_on_thread.can_read()); | 356 EXPECT_TRUE(empty_on_thread.can_read()); |
| 365 EXPECT_TRUE(empty_on_thread.can_modify()); | 357 EXPECT_TRUE(empty_on_thread.can_modify()); |
| 366 } | 358 } |
| 367 | 359 |
| 368 TEST_F(ImageSkiaTest, StaticOnThreadTest) { | 360 TEST_F(ImageSkiaTest, StaticOnThreadTest) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 // Check if image reps are generated for supported scale factors. | 466 // Check if image reps are generated for supported scale factors. |
| 475 EXPECT_EQ(ImageSkia::GetSupportedScales().size(), | 467 EXPECT_EQ(ImageSkia::GetSupportedScales().size(), |
| 476 image.image_reps().size()); | 468 image.image_reps().size()); |
| 477 test::TestOnThread threadsafe_on_thread(&image); | 469 test::TestOnThread threadsafe_on_thread(&image); |
| 478 threadsafe_on_thread.StartAndJoin(); | 470 threadsafe_on_thread.StartAndJoin(); |
| 479 EXPECT_TRUE(threadsafe_on_thread.can_read()); | 471 EXPECT_TRUE(threadsafe_on_thread.can_read()); |
| 480 EXPECT_FALSE(threadsafe_on_thread.can_modify()); | 472 EXPECT_FALSE(threadsafe_on_thread.can_modify()); |
| 481 EXPECT_TRUE(image.CanRead()); | 473 EXPECT_TRUE(image.CanRead()); |
| 482 EXPECT_FALSE(image.CanModify()); | 474 EXPECT_FALSE(image.CanModify()); |
| 483 } | 475 } |
| 484 #endif // ENABLE_NON_THREAD_SAFE | 476 #endif // DCHECK_IS_ON() |
| 485 | |
| 486 // Just in case we ever get lumped together with other compilation units. | |
| 487 #undef ENABLE_NON_THREAD_SAFE | |
| 488 | 477 |
| 489 TEST_F(ImageSkiaTest, Unscaled) { | 478 TEST_F(ImageSkiaTest, Unscaled) { |
| 490 SkBitmap bitmap; | 479 SkBitmap bitmap; |
| 491 | 480 |
| 492 // An ImageSkia created with 1x bitmap is unscaled. | 481 // An ImageSkia created with 1x bitmap is unscaled. |
| 493 ImageSkia image_skia = ImageSkia::CreateFrom1xBitmap(bitmap); | 482 ImageSkia image_skia = ImageSkia::CreateFrom1xBitmap(bitmap); |
| 494 EXPECT_TRUE(image_skia.GetRepresentation(1.0f).unscaled()); | 483 EXPECT_TRUE(image_skia.GetRepresentation(1.0f).unscaled()); |
| 495 ImageSkiaRep rep_2x(Size(100, 100), 2.0f); | 484 ImageSkiaRep rep_2x(Size(100, 100), 2.0f); |
| 496 | 485 |
| 497 // When reps for other scales are added, the unscaled image | 486 // When reps for other scales are added, the unscaled image |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 EXPECT_EQ(1U, image.image_reps().size()); | 612 EXPECT_EQ(1U, image.image_reps().size()); |
| 624 | 613 |
| 625 const ImageSkiaRep& rep12 = image.GetRepresentation(1.2f); | 614 const ImageSkiaRep& rep12 = image.GetRepresentation(1.2f); |
| 626 EXPECT_EQ(1.0f, rep12.scale()); | 615 EXPECT_EQ(1.0f, rep12.scale()); |
| 627 EXPECT_EQ("100x200", rep12.pixel_size().ToString()); | 616 EXPECT_EQ("100x200", rep12.pixel_size().ToString()); |
| 628 EXPECT_TRUE(rep12.unscaled()); | 617 EXPECT_TRUE(rep12.unscaled()); |
| 629 EXPECT_EQ(1U, image.image_reps().size()); | 618 EXPECT_EQ(1U, image.image_reps().size()); |
| 630 } | 619 } |
| 631 | 620 |
| 632 } // namespace gfx | 621 } // namespace gfx |
| OLD | NEW |