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