nsimage_cache_unittest.mm revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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#import <Cocoa/Cocoa.h>
6
7#include "app/mac/nsimage_cache.h"
8#include "base/file_path.h"
9#include "base/mac/mac_util.h"
10#include "base/path_service.h"
11#include "chrome/common/chrome_constants.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "testing/platform_test.h"
14
15// This tests nsimage_cache, which lives in base/.  The unit test is in
16// chrome/ because it depends on having a built-up Chrome present.
17
18namespace {
19
20class NSImageCacheTest : public PlatformTest {
21 public:
22  NSImageCacheTest() {
23    // Look in the framework bundle for resources.
24    FilePath path;
25    PathService::Get(base::DIR_EXE, &path);
26    path = path.Append(chrome::kFrameworkName);
27    base::mac::SetOverrideAppBundlePath(path);
28  }
29  virtual ~NSImageCacheTest() {
30    base::mac::SetOverrideAppBundle(nil);
31  }
32};
33
34TEST_F(NSImageCacheTest, LookupFound) {
35  EXPECT_TRUE(app::mac::GetCachedImageWithName(@"back_Template.pdf") != nil)
36      << "Failed to find the toolbar image?";
37}
38
39TEST_F(NSImageCacheTest, LookupCached) {
40  EXPECT_EQ(app::mac::GetCachedImageWithName(@"back_Template.pdf"),
41            app::mac::GetCachedImageWithName(@"back_Template.pdf"))
42    << "Didn't get the same NSImage back?";
43}
44
45TEST_F(NSImageCacheTest, LookupMiss) {
46  EXPECT_TRUE(app::mac::GetCachedImageWithName(@"should_not.exist") == nil)
47      << "There shouldn't be an image with this name?";
48}
49
50TEST_F(NSImageCacheTest, LookupFoundAndClear) {
51  NSImage *first = app::mac::GetCachedImageWithName(@"back_Template.pdf");
52  // Hang on to the first image so that the second one doesn't get allocated
53  // in the same location by (bad) luck.
54  [[first retain] autorelease];
55  EXPECT_TRUE(first != nil)
56      << "Failed to find the toolbar image?";
57  app::mac::ClearCachedImages();
58  NSImage *second = app::mac::GetCachedImageWithName(@"back_Template.pdf");
59  EXPECT_TRUE(second != nil)
60      << "Failed to find the toolbar image...again?";
61  EXPECT_NE(second, first)
62      << "how'd we get the same image after a cache clear?";
63}
64
65TEST_F(NSImageCacheTest, AutoTemplating) {
66  NSImage *templateImage =
67      app::mac::GetCachedImageWithName(@"back_Template.pdf");
68  EXPECT_TRUE([templateImage isTemplate] == YES)
69      << "Image ending in 'Template' should be marked as being a template";
70  NSImage *nonTemplateImage =
71      app::mac::GetCachedImageWithName(@"aliasCursor.png");
72  EXPECT_FALSE([nonTemplateImage isTemplate] == YES)
73      << "Image not ending in 'Template' should not be marked as being a "
74         "template";
75}
76
77}  // namespace
78