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.
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"
17 class WebAppsTableTest
: public testing::Test
{
20 virtual ~WebAppsTableTest() {}
23 virtual void SetUp() {
24 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
25 file_
= temp_dir_
.path().AppendASCII("TestWebDatabase");
27 table_
.reset(new WebAppsTable
);
28 db_
.reset(new WebDatabase
);
29 db_
->AddTable(table_
.get());
30 ASSERT_EQ(sql::INIT_OK
, db_
->Init(file_
));
34 base::ScopedTempDir temp_dir_
;
35 scoped_ptr
<WebAppsTable
> table_
;
36 scoped_ptr
<WebDatabase
> db_
;
39 DISALLOW_COPY_AND_ASSIGN(WebAppsTableTest
);
43 TEST_F(WebAppsTableTest
, WebAppHasAllImages
) {
44 GURL
url("http://google.com/");
46 // Initial value for unknown web app should be false.
47 EXPECT_FALSE(table_
->GetWebAppHasAllImages(url
));
49 // Set the value and make sure it took.
50 EXPECT_TRUE(table_
->SetWebAppHasAllImages(url
, true));
51 EXPECT_TRUE(table_
->GetWebAppHasAllImages(url
));
53 // Remove the app and make sure value reverts to default.
54 EXPECT_TRUE(table_
->RemoveWebApp(url
));
55 EXPECT_FALSE(table_
->GetWebAppHasAllImages(url
));
58 TEST_F(WebAppsTableTest
, WebAppImages
) {
59 GURL
url("http://google.com/");
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());
68 image
.setConfig(SkBitmap::kARGB_8888_Config
, 16, 16);
70 image
.eraseColor(SK_ColorBLACK
);
71 ASSERT_TRUE(table_
->SetWebAppImage(url
, image
));
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());
79 // Add another 16x16 image and make sure it replaces the original.
80 image
.setConfig(SkBitmap::kARGB_8888_Config
, 16, 16);
82 image
.eraseColor(SK_ColorBLACK
);
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
;
93 ASSERT_TRUE(table_
->SetWebAppImage(url
, image
));
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();
106 // Add another image at a bigger size.
107 image
.setConfig(SkBitmap::kARGB_8888_Config
, 32, 32);
109 image
.eraseColor(SK_ColorBLACK
);
110 ASSERT_TRUE(table_
->SetWebAppImage(url
, image
));
112 // Make sure we get both images back.
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());
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());