1 // Copyright (c) 2012 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/files/scoped_temp_dir.h" 6 #include "base/path_service.h" 7 #include "base/strings/string_number_conversions.h" 8 #include "base/time/time.h" 9 #include "chrome/browser/webdata/web_apps_table.h" 10 #include "components/webdata/common/web_database.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "third_party/skia/include/core/SkBitmap.h" 13 #include "url/gurl.h" 14 15 using base::Time; 16 17 class WebAppsTableTest : public testing::Test { 18 public: 19 WebAppsTableTest() {} 20 virtual ~WebAppsTableTest() {} 21 22 protected: 23 virtual void SetUp() { 24 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 25 file_ = temp_dir_.path().AppendASCII("TestWebDatabase"); 26 27 table_.reset(new WebAppsTable); 28 db_.reset(new WebDatabase); 29 db_->AddTable(table_.get()); 30 ASSERT_EQ(sql::INIT_OK, db_->Init(file_)); 31 } 32 33 base::FilePath file_; 34 base::ScopedTempDir temp_dir_; 35 scoped_ptr<WebAppsTable> table_; 36 scoped_ptr<WebDatabase> db_; 37 38 private: 39 DISALLOW_COPY_AND_ASSIGN(WebAppsTableTest); 40 }; 41 42 43 TEST_F(WebAppsTableTest, WebAppHasAllImages) { 44 GURL url("http://google.com/"); 45 46 // Initial value for unknown web app should be false. 47 EXPECT_FALSE(table_->GetWebAppHasAllImages(url)); 48 49 // Set the value and make sure it took. 50 EXPECT_TRUE(table_->SetWebAppHasAllImages(url, true)); 51 EXPECT_TRUE(table_->GetWebAppHasAllImages(url)); 52 53 // Remove the app and make sure value reverts to default. 54 EXPECT_TRUE(table_->RemoveWebApp(url)); 55 EXPECT_FALSE(table_->GetWebAppHasAllImages(url)); 56 } 57 58 TEST_F(WebAppsTableTest, WebAppImages) { 59 GURL url("http://google.com/"); 60 61 // Web app should initially have no images. 62 std::vector<SkBitmap> images; 63 ASSERT_TRUE(table_->GetWebAppImages(url, &images)); 64 ASSERT_EQ(0U, images.size()); 65 66 // Add an image. 67 SkBitmap image; 68 image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); 69 image.allocPixels(); 70 image.eraseColor(SK_ColorBLACK); 71 ASSERT_TRUE(table_->SetWebAppImage(url, image)); 72 73 // Make sure we get the image back. 74 ASSERT_TRUE(table_->GetWebAppImages(url, &images)); 75 ASSERT_EQ(1U, images.size()); 76 ASSERT_EQ(16, images[0].width()); 77 ASSERT_EQ(16, images[0].height()); 78 79 // Add another 16x16 image and make sure it replaces the original. 80 image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); 81 image.allocPixels(); 82 image.eraseColor(SK_ColorBLACK); 83 84 // Set some random pixels so that we can identify the image. We don't use 85 // transparent images because of pre-multiplication rounding errors. 86 SkColor test_pixel_1 = 0xffccbbaa; 87 SkColor test_pixel_2 = 0xffaabbaa; 88 SkColor test_pixel_3 = 0xff339966; 89 image.getAddr32(0, 1)[0] = test_pixel_1; 90 image.getAddr32(0, 1)[1] = test_pixel_2; 91 image.getAddr32(0, 1)[2] = test_pixel_3; 92 93 ASSERT_TRUE(table_->SetWebAppImage(url, image)); 94 images.clear(); 95 ASSERT_TRUE(table_->GetWebAppImages(url, &images)); 96 ASSERT_EQ(1U, images.size()); 97 ASSERT_EQ(16, images[0].width()); 98 ASSERT_EQ(16, images[0].height()); 99 images[0].lockPixels(); 100 ASSERT_TRUE(images[0].getPixels() != NULL); 101 ASSERT_EQ(test_pixel_1, images[0].getAddr32(0, 1)[0]); 102 ASSERT_EQ(test_pixel_2, images[0].getAddr32(0, 1)[1]); 103 ASSERT_EQ(test_pixel_3, images[0].getAddr32(0, 1)[2]); 104 images[0].unlockPixels(); 105 106 // Add another image at a bigger size. 107 image.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); 108 image.allocPixels(); 109 image.eraseColor(SK_ColorBLACK); 110 ASSERT_TRUE(table_->SetWebAppImage(url, image)); 111 112 // Make sure we get both images back. 113 images.clear(); 114 ASSERT_TRUE(table_->GetWebAppImages(url, &images)); 115 ASSERT_EQ(2U, images.size()); 116 if (images[0].width() == 16) { 117 ASSERT_EQ(16, images[0].width()); 118 ASSERT_EQ(16, images[0].height()); 119 ASSERT_EQ(32, images[1].width()); 120 ASSERT_EQ(32, images[1].height()); 121 } else { 122 ASSERT_EQ(32, images[0].width()); 123 ASSERT_EQ(32, images[0].height()); 124 ASSERT_EQ(16, images[1].width()); 125 ASSERT_EQ(16, images[1].height()); 126 } 127 } 128