Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: ui/gfx/image/image_skia_unittest.cc

Issue 10694045: Loading/Creating images for mutiple scale factors on the fly (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/image/image_skia_source.h ('k') | ui/ui.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/gfx/image/image_skia.h"
6
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/image/image_skia_rep.h"
10 #include "ui/gfx/image/image_skia_source.h"
11 #include "ui/gfx/size.h"
12 #include "ui/base/layout.h"
13
14 namespace gfx {
15
16 namespace {
17
18 ImageSkiaRep CreateImage(const gfx::Size& size, ui::ScaleFactor scale_factor) {
19 SkBitmap bitmap;
20 gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor));
21 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
22 pixel_size.width(), pixel_size.height());
23 bitmap.allocPixels();
24 return ImageSkiaRep(bitmap, scale_factor);
25 }
26
27 class FixedSource : public ImageSkiaSource {
28 public:
29 FixedSource(const ImageSkiaRep& image) : image_(image) {}
30
31 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
32 return image_;
33 }
34
35 private:
36 ImageSkiaRep image_;
37
38 DISALLOW_COPY_AND_ASSIGN(FixedSource);
39 };
40
41 class DynamicSource : public ImageSkiaSource {
42 public:
43 DynamicSource(const gfx::Size& size) : size_(size) {}
44
45 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE {
46 return CreateImage(size_, scale_factor);
47 }
48
49 private:
50 gfx::Size size_;
51
52 DISALLOW_COPY_AND_ASSIGN(DynamicSource);
53 };
54
55 } // namespace;
56
57 typedef testing::Test ImageSkiaTest;
58
59 TEST(ImageSkiaTest, NoSource) {
60 ImageSkia image_skia(NULL, Size(100, 200));
61 EXPECT_FALSE(image_skia.empty());
62 EXPECT_FALSE(image_skia.isNull()); // Is this expected?
oshima 2012/07/01 19:01:57 peter, is this correct?
pkotwicz 2012/07/01 22:22:23 This is correct. When an ImageSkia has an ImageSki
oshima 2012/07/02 16:43:34 Actually, the soruce should never be NULL (there i
pkotwicz 2012/07/02 17:13:53 You are correct that the source should never be NU
oshima 2012/07/02 17:26:47 Do you have real example you want to do this? That
oshima 2012/07/02 17:48:43 By the way, I'm thinking of introducing "primary s
pkotwicz 2012/07/02 18:05:12 I don't know of any case where you would want a so
63 EXPECT_EQ(100, image_skia.width());
64 EXPECT_EQ(200, image_skia.height());
65 }
66
67 TEST(ImageSkiaTest, FixedSource) {
68 ImageSkiaRep image(CreateImage(Size(100, 200), ui::SCALE_FACTOR_100P));
69 ImageSkia image_skia(new FixedSource(image), Size(100, 200));
70 EXPECT_EQ(0U, image_skia.image_reps().size());
71
72 const ImageSkiaRep& result_100p =
73 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P);
74 EXPECT_EQ(100, result_100p.GetWidth());
75 EXPECT_EQ(200, result_100p.GetHeight());
76 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor());
77 EXPECT_EQ(1U, image_skia.image_reps().size());
78
79 const ImageSkiaRep& result_200p =
80 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P);
81
82 EXPECT_EQ(100, result_200p.GetWidth());
83 EXPECT_EQ(200, result_200p.GetHeight());
84 EXPECT_EQ(100, result_200p.pixel_width());
85 EXPECT_EQ(200, result_200p.pixel_height());
86 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_200p.scale_factor());
87 EXPECT_EQ(2U, image_skia.image_reps().size());
88
89 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P);
90 EXPECT_EQ(2U, image_skia.image_reps().size());
91
92 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P);
93 EXPECT_EQ(2U, image_skia.image_reps().size());
94 }
95
96 TEST(ImageSkiaTest, DynamicSource) {
97 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200));
98 EXPECT_EQ(0U, image_skia.image_reps().size());
99 const ImageSkiaRep& result_100p =
100 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P);
101 EXPECT_EQ(100, result_100p.GetWidth());
102 EXPECT_EQ(200, result_100p.GetHeight());
103 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor());
104 EXPECT_EQ(1U, image_skia.image_reps().size());
105
106 const ImageSkiaRep& result_200p =
107 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P);
108 EXPECT_EQ(100, result_200p.GetWidth());
109 EXPECT_EQ(200, result_200p.GetHeight());
110 EXPECT_EQ(200, result_200p.pixel_width());
111 EXPECT_EQ(400, result_200p.pixel_height());
112 EXPECT_EQ(ui::SCALE_FACTOR_200P, result_200p.scale_factor());
113 EXPECT_EQ(2U, image_skia.image_reps().size());
114
115 // Get the representation again and make sure they
116 // retuns the sam
117 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P);
118 EXPECT_EQ(2U, image_skia.image_reps().size());
119 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P);
120 EXPECT_EQ(2U, image_skia.image_reps().size());
121 }
122
123 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/image/image_skia_source.h ('k') | ui/ui.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698