summaryrefslogtreecommitdiffstats
path: root/gfx/scoped_image_unittest.cc
blob: 8e8b3125052e7918ac28cf513889dfa98a722037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2010 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/scoped_ptr.h"
#include "gfx/scoped_image.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"

#if defined(OS_LINUX)
#include "gfx/gtk_util.h"
#elif defined(OS_MACOSX)
#include "base/mac/mac_util.h"
#include "skia/ext/skia_utils_mac.h"
#endif

namespace {

class ScopedImageTest : public testing::Test {
 public:
  SkBitmap* CreateBitmap() {
    SkBitmap* bitmap = new SkBitmap();
    bitmap->setConfig(SkBitmap::kARGB_8888_Config, 25, 25);
    bitmap->allocPixels();
    bitmap->eraseRGB(255, 0, 0);
    return bitmap;
  }

  gfx::NativeImage CreateNativeImage() {
    scoped_ptr<SkBitmap> bitmap(CreateBitmap());
#if defined(OS_MACOSX)
    NSImage* image = gfx::SkBitmapToNSImage(*(bitmap.get()));
    base::mac::NSObjectRetain(image);
    return image;
#elif defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
    return gfx::GdkPixbufFromSkBitmap(bitmap.get());
#else
    return bitmap.release();
#endif
  }
};

TEST_F(ScopedImageTest, Initialize) {
  gfx::ScopedImage<SkBitmap> image(CreateBitmap());
  EXPECT_TRUE(image.Get());
}

TEST_F(ScopedImageTest, Free) {
  gfx::ScopedImage<SkBitmap> image(CreateBitmap());
  EXPECT_TRUE(image.Get());
  image.Free();
  EXPECT_FALSE(image.Get());
}

TEST_F(ScopedImageTest, Release) {
  gfx::ScopedImage<SkBitmap> image(CreateBitmap());
  EXPECT_TRUE(image.Get());
  scoped_ptr<SkBitmap> bitmap(image.Release());
  EXPECT_FALSE(image.Get());
}

TEST_F(ScopedImageTest, Set) {
  gfx::ScopedImage<SkBitmap> image(CreateBitmap());
  EXPECT_TRUE(image.Get());
  SkBitmap* image2 = CreateBitmap();
  image.Set(image2);
  EXPECT_EQ(image2, image.Get());
}

TEST_F(ScopedImageTest, NativeInitialize) {
  gfx::ScopedNativeImage image(CreateNativeImage());
  EXPECT_TRUE(image.Get());
}

TEST_F(ScopedImageTest, NativeFree) {
  gfx::ScopedNativeImage image(CreateNativeImage());
  EXPECT_TRUE(image.Get());
  image.Free();
  EXPECT_FALSE(image.Get());
}

TEST_F(ScopedImageTest, NativeRelease) {
  gfx::ScopedNativeImage image(CreateNativeImage());
  EXPECT_TRUE(image.Get());
  gfx::ScopedNativeImage image2(image.Release());
  EXPECT_FALSE(image.Get());
  EXPECT_TRUE(image2.Get());
}

TEST_F(ScopedImageTest, NativeSet) {
  gfx::ScopedNativeImage image(CreateNativeImage());
  EXPECT_TRUE(image.Get());
  gfx::NativeImage image2 = CreateNativeImage();
  image.Set(image2);
  EXPECT_EQ(image2, image.Get());
}

}  // namespace