- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / test / fast_show_pickler_unittest.cc
1 // Copyright 2013 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 "chrome/browser/ui/app_list/fast_show_pickler.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "third_party/skia/include/core/SkColorPriv.h"
9 #include "ui/app_list/app_list_item_model.h"
10 #include "ui/app_list/app_list_model.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/gfx/skia_util.h"
13
14 using app_list::AppListItemModel;
15 using app_list::AppListModel;
16
17 class AppListModelPicklerUnitTest : public testing::Test {
18  protected:
19   void CheckIsSame(AppListModel* m1, AppListModel* m2) {
20     ASSERT_EQ(m1->item_list()->item_count(), m2->item_list()->item_count());
21     ASSERT_EQ(m1->signed_in(), m2->signed_in());
22     for (size_t i = 0; i < m1->item_list()->item_count(); i++) {
23       ASSERT_EQ(m1->item_list()->item_at(i)->id(),
24                 m2->item_list()->item_at(i)->id());
25       ASSERT_EQ(m1->item_list()->item_at(i)->title(),
26                 m2->item_list()->item_at(i)->title());
27       ASSERT_EQ(m1->item_list()->item_at(i)->full_name(),
28                 m2->item_list()->item_at(i)->full_name());
29       CompareImages(m1->item_list()->item_at(i)->icon(),
30                     m2->item_list()->item_at(i)->icon());
31     }
32   }
33
34   void CompareImages(const gfx::ImageSkia& image1,
35                      const gfx::ImageSkia& image2) {
36     std::vector<gfx::ImageSkiaRep> reps1(image1.image_reps());
37     std::vector<gfx::ImageSkiaRep> reps2(image2.image_reps());
38     ASSERT_EQ(reps1.size(), reps2.size());
39     for (size_t i = 0; i < reps1.size(); ++i) {
40       ASSERT_TRUE(
41           gfx::BitmapsAreEqual(reps1[i].sk_bitmap(), reps2[i].sk_bitmap()));
42       ASSERT_EQ(reps1[i].scale(), reps2[i].scale());
43     }
44   }
45
46   scoped_ptr<AppListModel> CopyViaPickle(AppListModel* model) {
47     scoped_ptr<Pickle> pickle(
48         FastShowPickler::PickleAppListModelForFastShow(model));
49     return FastShowPickler::UnpickleAppListModelForFastShow(pickle.get());
50   }
51
52   void DoConsistencyChecks(AppListModel* model) {
53     scoped_ptr<AppListModel> model2(CopyViaPickle(model));
54     AppListModel dest_model;
55     FastShowPickler::CopyOver(model2.get(), &dest_model);
56
57     CheckIsSame(model, model2.get());
58     CheckIsSame(model, &dest_model);
59     CheckIsSame(model2.get(), &dest_model);
60   }
61
62   gfx::ImageSkia MakeImage() {
63     const int kWidth = 10;
64     const int kHeight = 10;
65     SkBitmap bitmap;
66     bitmap.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight);
67     bitmap.allocPixels();
68     bitmap.eraseARGB(255, 1, 2, 3);
69     return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
70   }
71 };
72
73 TEST_F(AppListModelPicklerUnitTest, EmptyModel) {
74   AppListModel model;
75   DoConsistencyChecks(&model);
76 }
77
78 TEST_F(AppListModelPicklerUnitTest, OneItem) {
79   AppListModel model;
80   AppListItemModel* app1 = new AppListItemModel("abc");
81   app1->SetTitleAndFullName("ht", "hello, there");
82   model.item_list()->AddItem(app1);
83
84   DoConsistencyChecks(&model);
85 }
86
87 TEST_F(AppListModelPicklerUnitTest, TwoItems) {
88   AppListModel model;
89   AppListItemModel* app1 = new AppListItemModel("abc");
90   app1->SetTitleAndFullName("ht", "hello, there");
91   model.item_list()->AddItem(app1);
92
93   AppListItemModel* app2 = new AppListItemModel("abc2");
94   app2->SetTitleAndFullName("ht2", "hello, there 2");
95   model.item_list()->AddItem(app2);
96
97   DoConsistencyChecks(&model);
98 }
99
100 TEST_F(AppListModelPicklerUnitTest, Images) {
101   AppListModel model;
102   AppListItemModel* app1 = new AppListItemModel("abc");
103   app1->SetTitleAndFullName("ht", "hello, there");
104   app1->SetIcon(MakeImage(), true);
105   model.item_list()->AddItem(app1);
106
107   AppListItemModel* app2 = new AppListItemModel("abc2");
108   app2->SetTitleAndFullName("ht2", "hello, there 2");
109   model.item_list()->AddItem(app2);
110
111   DoConsistencyChecks(&model);
112 }
113
114 TEST_F(AppListModelPicklerUnitTest, EmptyImage) {
115   AppListModel model;
116   AppListItemModel* app1 = new AppListItemModel("abc");
117   app1->SetTitleAndFullName("ht", "hello, there");
118   app1->SetIcon(gfx::ImageSkia(), true);
119   model.item_list()->AddItem(app1);
120
121   DoConsistencyChecks(&model);
122 }
123
124 TEST_F(AppListModelPicklerUnitTest, SignedIn) {
125   AppListModel model;
126   model.SetSignedIn(true);
127
128   DoConsistencyChecks(&model);
129 }
130
131 TEST_F(AppListModelPicklerUnitTest, SignedOut) {
132   AppListModel model;
133   model.SetSignedIn(false);
134
135   DoConsistencyChecks(&model);
136 }