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