image_loading_tracker_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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#include "base/message_loop.h"
6#include "base/path_service.h"
7#include "chrome/browser/chrome_thread.h"
8#include "chrome/browser/extensions/image_loading_tracker.h"
9#include "chrome/common/chrome_paths.h"
10#include "chrome/common/extensions/extension.h"
11#include "chrome/common/extensions/extension_icon_set.h"
12#include "chrome/common/extensions/extension_resource.h"
13#include "chrome/common/json_value_serializer.h"
14#include "chrome/common/notification_service.h"
15#include "chrome/common/notification_type.h"
16#include "gfx/size.h"
17#include "testing/gtest/include/gtest/gtest.h"
18#include "third_party/skia/include/core/SkBitmap.h"
19
20class ImageLoadingTrackerTest : public testing::Test,
21                                public ImageLoadingTracker::Observer {
22 public:
23  ImageLoadingTrackerTest()
24      : image_loaded_count_(0),
25        quit_in_image_loaded_(false),
26        ui_thread_(ChromeThread::UI, &ui_loop_),
27        file_thread_(ChromeThread::FILE),
28        io_thread_(ChromeThread::IO) {
29  }
30
31  virtual void OnImageLoaded(SkBitmap* image, ExtensionResource resource,
32                             int index) {
33    image_loaded_count_++;
34    if (quit_in_image_loaded_)
35      MessageLoop::current()->Quit();
36    if (image)
37      image_ = *image;
38    else
39      image_.reset();
40  }
41
42  void WaitForImageLoad() {
43    quit_in_image_loaded_ = true;
44    MessageLoop::current()->Run();
45    quit_in_image_loaded_ = false;
46  }
47
48  int image_loaded_count() {
49    int result = image_loaded_count_;
50    image_loaded_count_ = 0;
51    return result;
52  }
53
54  Extension* CreateExtension() {
55    // Create and load an extension.
56    FilePath test_file;
57    if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) {
58      EXPECT_FALSE(true);
59      return NULL;
60    }
61    test_file = test_file.AppendASCII("extensions")
62                         .AppendASCII("image_loading_tracker");
63    int error_code = 0;
64    std::string error;
65    JSONFileValueSerializer serializer(test_file.AppendASCII("app.json"));
66    scoped_ptr<DictionaryValue> valid_value(
67        static_cast<DictionaryValue*>(serializer.Deserialize(&error_code,
68                                                             &error)));
69    EXPECT_EQ(0, error_code) << error;
70    if (error_code != 0)
71      return NULL;
72
73    EXPECT_TRUE(valid_value.get());
74    if (!valid_value.get())
75      return NULL;
76
77    scoped_ptr<Extension> extension(new Extension(test_file));
78    if (!extension->InitFromValue(*valid_value, false, &error))
79      return NULL;
80
81    return extension.release();
82  }
83
84  SkBitmap image_;
85
86 private:
87  virtual void SetUp() {
88    file_thread_.Start();
89    io_thread_.Start();
90  }
91
92  int image_loaded_count_;
93  bool quit_in_image_loaded_;
94  MessageLoop ui_loop_;
95  ChromeThread ui_thread_;
96  ChromeThread file_thread_;
97  ChromeThread io_thread_;
98};
99
100// Tests asking ImageLoadingTracker to cache pushes the result to the Extension.
101TEST_F(ImageLoadingTrackerTest, Cache) {
102  scoped_ptr<Extension> extension(CreateExtension());
103  ASSERT_TRUE(extension.get() != NULL);
104
105  ExtensionResource image_resource =
106      extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
107                                 ExtensionIconSet::MATCH_EXACTLY);
108  gfx::Size max_size(Extension::EXTENSION_ICON_SMALLISH,
109                     Extension::EXTENSION_ICON_SMALLISH);
110  ImageLoadingTracker loader(static_cast<ImageLoadingTracker::Observer*>(this));
111  loader.LoadImage(extension.get(),
112                   image_resource,
113                   max_size,
114                   ImageLoadingTracker::CACHE);
115
116  // The image isn't cached, so we should not have received notification.
117  EXPECT_EQ(0, image_loaded_count());
118
119  WaitForImageLoad();
120
121  // We should have gotten the image.
122  EXPECT_EQ(1, image_loaded_count());
123
124  // Check that the image was loaded.
125  EXPECT_EQ(Extension::EXTENSION_ICON_SMALLISH, image_.width());
126
127  // The image should be cached in the Extension.
128  EXPECT_TRUE(extension->HasCachedImage(image_resource, max_size));
129
130  // Make sure the image is in the extension.
131  EXPECT_EQ(Extension::EXTENSION_ICON_SMALLISH,
132            extension->GetCachedImage(image_resource, max_size).width());
133
134  // Ask the tracker for the image again, this should call us back immediately.
135  loader.LoadImage(extension.get(),
136                   image_resource,
137                   max_size,
138                   ImageLoadingTracker::CACHE);
139  // We should have gotten the image.
140  EXPECT_EQ(1, image_loaded_count());
141
142  // Check that the image was loaded.
143  EXPECT_EQ(Extension::EXTENSION_ICON_SMALLISH, image_.width());
144}
145
146// Tests deleting an extension while waiting for the image to load doesn't cause
147// problems.
148TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) {
149  scoped_ptr<Extension> extension(CreateExtension());
150  ASSERT_TRUE(extension.get() != NULL);
151
152  ExtensionResource image_resource =
153      extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
154                                 ExtensionIconSet::MATCH_EXACTLY);
155  ImageLoadingTracker loader(static_cast<ImageLoadingTracker::Observer*>(this));
156  loader.LoadImage(extension.get(),
157                   image_resource,
158                   gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
159                             Extension::EXTENSION_ICON_SMALLISH),
160                   ImageLoadingTracker::CACHE);
161
162  // The image isn't cached, so we should not have received notification.
163  EXPECT_EQ(0, image_loaded_count());
164
165  // Send out notification the extension was uninstalled.
166  NotificationService::current()->Notify(
167      NotificationType::EXTENSION_UNLOADED,
168      NotificationService::AllSources(),
169      Details<Extension>(extension.get()));
170
171  // Chuck the extension, that way if anyone tries to access it we should crash
172  // or get valgrind errors.
173  extension.reset();
174
175  WaitForImageLoad();
176
177  // Even though we deleted the extension, we should still get the image.
178  // We should still have gotten the image.
179  EXPECT_EQ(1, image_loaded_count());
180
181  // Check that the image was loaded.
182  EXPECT_EQ(Extension::EXTENSION_ICON_SMALLISH, image_.width());
183}
184