Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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/extensions/image_loading_tracker.h"
      8 #include "chrome/common/chrome_paths.h"
      9 #include "chrome/common/extensions/extension.h"
     10 #include "chrome/common/extensions/extension_icon_set.h"
     11 #include "chrome/common/extensions/extension_resource.h"
     12 #include "content/browser/browser_thread.h"
     13 #include "content/common/json_value_serializer.h"
     14 #include "content/common/notification_service.h"
     15 #include "content/common/notification_type.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 #include "third_party/skia/include/core/SkBitmap.h"
     18 #include "ui/gfx/size.h"
     19 
     20 class 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_(BrowserThread::UI, &ui_loop_),
     27         file_thread_(BrowserThread::FILE),
     28         io_thread_(BrowserThread::IO) {
     29   }
     30 
     31   virtual void OnImageLoaded(SkBitmap* image, const 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   scoped_refptr<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     return Extension::Create(test_file, Extension::INVALID, *valid_value,
     78         Extension::STRICT_ERROR_CHECKS, &error);
     79   }
     80 
     81   SkBitmap image_;
     82 
     83  private:
     84   virtual void SetUp() {
     85     file_thread_.Start();
     86     io_thread_.Start();
     87   }
     88 
     89   int image_loaded_count_;
     90   bool quit_in_image_loaded_;
     91   MessageLoop ui_loop_;
     92   BrowserThread ui_thread_;
     93   BrowserThread file_thread_;
     94   BrowserThread io_thread_;
     95 };
     96 
     97 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension.
     98 TEST_F(ImageLoadingTrackerTest, Cache) {
     99   scoped_refptr<Extension> extension(CreateExtension());
    100   ASSERT_TRUE(extension.get() != NULL);
    101 
    102   ExtensionResource image_resource =
    103       extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
    104                                  ExtensionIconSet::MATCH_EXACTLY);
    105   gfx::Size max_size(Extension::EXTENSION_ICON_SMALLISH,
    106                      Extension::EXTENSION_ICON_SMALLISH);
    107   ImageLoadingTracker loader(static_cast<ImageLoadingTracker::Observer*>(this));
    108   loader.LoadImage(extension.get(),
    109                    image_resource,
    110                    max_size,
    111                    ImageLoadingTracker::CACHE);
    112 
    113   // The image isn't cached, so we should not have received notification.
    114   EXPECT_EQ(0, image_loaded_count());
    115 
    116   WaitForImageLoad();
    117 
    118   // We should have gotten the image.
    119   EXPECT_EQ(1, image_loaded_count());
    120 
    121   // Check that the image was loaded.
    122   EXPECT_EQ(Extension::EXTENSION_ICON_SMALLISH, image_.width());
    123 
    124   // The image should be cached in the Extension.
    125   EXPECT_TRUE(extension->HasCachedImage(image_resource, max_size));
    126 
    127   // Make sure the image is in the extension.
    128   EXPECT_EQ(Extension::EXTENSION_ICON_SMALLISH,
    129             extension->GetCachedImage(image_resource, max_size).width());
    130 
    131   // Ask the tracker for the image again, this should call us back immediately.
    132   loader.LoadImage(extension.get(),
    133                    image_resource,
    134                    max_size,
    135                    ImageLoadingTracker::CACHE);
    136   // We should have gotten the image.
    137   EXPECT_EQ(1, image_loaded_count());
    138 
    139   // Check that the image was loaded.
    140   EXPECT_EQ(Extension::EXTENSION_ICON_SMALLISH, image_.width());
    141 }
    142 
    143 // Tests deleting an extension while waiting for the image to load doesn't cause
    144 // problems.
    145 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) {
    146   scoped_refptr<Extension> extension(CreateExtension());
    147   ASSERT_TRUE(extension.get() != NULL);
    148 
    149   ExtensionResource image_resource =
    150       extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
    151                                  ExtensionIconSet::MATCH_EXACTLY);
    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   UnloadedExtensionInfo details(extension.get(),
    164                                 UnloadedExtensionInfo::UNINSTALL);
    165   NotificationService::current()->Notify(
    166       NotificationType::EXTENSION_UNLOADED,
    167       NotificationService::AllSources(),
    168       Details<UnloadedExtensionInfo>(&details));
    169 
    170   // Chuck the extension, that way if anyone tries to access it we should crash
    171   // or get valgrind errors.
    172   extension = NULL;
    173 
    174   WaitForImageLoad();
    175 
    176   // Even though we deleted the extension, we should still get the image.
    177   // We should still have gotten the image.
    178   EXPECT_EQ(1, image_loaded_count());
    179 
    180   // Check that the image was loaded.
    181   EXPECT_EQ(Extension::EXTENSION_ICON_SMALLISH, image_.width());
    182 }
    183