// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "base/file_util.h" #include "base/path_service.h" #include "base/string_number_conversions.h" #include "base/time.h" #include "chrome/browser/webdata/web_database.h" #include "chrome/common/chrome_paths.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/skia/include/core/SkBitmap.h" using base::Time; class WebAppsTableTest : public testing::Test { public: WebAppsTableTest() {} virtual ~WebAppsTableTest() {} protected: virtual void SetUp() { PathService::Get(chrome::DIR_TEST_DATA, &file_); const std::string test_db = "TestWebDatabase" + base::Int64ToString(Time::Now().ToTimeT()) + ".db"; file_ = file_.AppendASCII(test_db); file_util::Delete(file_, false); } virtual void TearDown() { file_util::Delete(file_, false); } FilePath file_; private: DISALLOW_COPY_AND_ASSIGN(WebAppsTableTest); }; TEST_F(WebAppsTableTest, WebAppHasAllImages) { WebDatabase db; ASSERT_EQ(sql::INIT_OK, db.Init(file_)); GURL url("http://google.com/"); // Initial value for unknown web app should be false. EXPECT_FALSE(db.GetWebAppsTable()->GetWebAppHasAllImages(url)); // Set the value and make sure it took. EXPECT_TRUE(db.GetWebAppsTable()->SetWebAppHasAllImages(url, true)); EXPECT_TRUE(db.GetWebAppsTable()->GetWebAppHasAllImages(url)); // Remove the app and make sure value reverts to default. EXPECT_TRUE(db.GetWebAppsTable()->RemoveWebApp(url)); EXPECT_FALSE(db.GetWebAppsTable()->GetWebAppHasAllImages(url)); } TEST_F(WebAppsTableTest, WebAppImages) { WebDatabase db; ASSERT_EQ(sql::INIT_OK, db.Init(file_)); GURL url("http://google.com/"); // Web app should initially have no images. std::vector<SkBitmap> images; ASSERT_TRUE(db.GetWebAppsTable()->GetWebAppImages(url, &images)); ASSERT_EQ(0U, images.size()); // Add an image. SkBitmap image; image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); image.allocPixels(); image.eraseColor(SK_ColorBLACK); ASSERT_TRUE(db.GetWebAppsTable()->SetWebAppImage(url, image)); // Make sure we get the image back. ASSERT_TRUE(db.GetWebAppsTable()->GetWebAppImages(url, &images)); ASSERT_EQ(1U, images.size()); ASSERT_EQ(16, images[0].width()); ASSERT_EQ(16, images[0].height()); // Add another 16x16 image and make sure it replaces the original. image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); image.allocPixels(); image.eraseColor(SK_ColorBLACK); // Set some random pixels so that we can identify the image. We don't use // transparent images because of pre-multiplication rounding errors. SkColor test_pixel_1 = 0xffccbbaa; SkColor test_pixel_2 = 0xffaabbaa; SkColor test_pixel_3 = 0xff339966; image.getAddr32(0, 1)[0] = test_pixel_1; image.getAddr32(0, 1)[1] = test_pixel_2; image.getAddr32(0, 1)[2] = test_pixel_3; ASSERT_TRUE(db.GetWebAppsTable()->SetWebAppImage(url, image)); images.clear(); ASSERT_TRUE(db.GetWebAppsTable()->GetWebAppImages(url, &images)); ASSERT_EQ(1U, images.size()); ASSERT_EQ(16, images[0].width()); ASSERT_EQ(16, images[0].height()); images[0].lockPixels(); ASSERT_TRUE(images[0].getPixels() != NULL); ASSERT_EQ(test_pixel_1, images[0].getAddr32(0, 1)[0]); ASSERT_EQ(test_pixel_2, images[0].getAddr32(0, 1)[1]); ASSERT_EQ(test_pixel_3, images[0].getAddr32(0, 1)[2]); images[0].unlockPixels(); // Add another image at a bigger size. image.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); image.allocPixels(); image.eraseColor(SK_ColorBLACK); ASSERT_TRUE(db.GetWebAppsTable()->SetWebAppImage(url, image)); // Make sure we get both images back. images.clear(); ASSERT_TRUE(db.GetWebAppsTable()->GetWebAppImages(url, &images)); ASSERT_EQ(2U, images.size()); if (images[0].width() == 16) { ASSERT_EQ(16, images[0].width()); ASSERT_EQ(16, images[0].height()); ASSERT_EQ(32, images[1].width()); ASSERT_EQ(32, images[1].height()); } else { ASSERT_EQ(32, images[0].width()); ASSERT_EQ(32, images[0].height()); ASSERT_EQ(16, images[1].width()); ASSERT_EQ(16, images[1].height()); } }