fast_show_pickler_unittest.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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
14using app_list::AppListItemModel;
15using app_list::AppListModel;
16
17class AppListModelPicklerUnitTest : public testing::Test {
18 protected:
19  void CheckIsSame(AppListModel* m1, AppListModel* m2) {
20    ASSERT_EQ(m1->apps()->item_count(), m2->apps()->item_count());
21    ASSERT_EQ(m1->signed_in(), m2->signed_in());
22    for (size_t i = 0; i < m1->apps()->item_count(); i++) {
23      ASSERT_EQ(m1->apps()->GetItemAt(i)->app_id(),
24                m2->apps()->GetItemAt(i)->app_id());
25      ASSERT_EQ(m1->apps()->GetItemAt(i)->title(),
26                m2->apps()->GetItemAt(i)->title());
27      ASSERT_EQ(m1->apps()->GetItemAt(i)->full_name(),
28                m2->apps()->GetItemAt(i)->full_name());
29      CompareImages(m1->apps()->GetItemAt(i)->icon(),
30                    m2->apps()->GetItemAt(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
73TEST_F(AppListModelPicklerUnitTest, EmptyModel) {
74  AppListModel model;
75  DoConsistencyChecks(&model);
76}
77
78TEST_F(AppListModelPicklerUnitTest, OneItem) {
79  AppListModel model;
80  AppListItemModel* app1 = new AppListItemModel;
81  app1->set_app_id("abc");
82  app1->SetTitleAndFullName("ht", "hello, there");
83  model.apps()->Add(app1);
84
85  DoConsistencyChecks(&model);
86}
87
88TEST_F(AppListModelPicklerUnitTest, TwoItems) {
89  AppListModel model;
90  AppListItemModel* app1 = new AppListItemModel;
91  app1->set_app_id("abc");
92  app1->SetTitleAndFullName("ht", "hello, there");
93  model.apps()->Add(app1);
94
95  AppListItemModel* app2 = new AppListItemModel;
96  app2->set_app_id("abc2");
97  app2->SetTitleAndFullName("ht2", "hello, there 2");
98  model.apps()->Add(app2);
99
100  DoConsistencyChecks(&model);
101}
102
103TEST_F(AppListModelPicklerUnitTest, Images) {
104  AppListModel model;
105  AppListItemModel* app1 = new AppListItemModel;
106  app1->set_app_id("abc");
107  app1->SetTitleAndFullName("ht", "hello, there");
108  app1->SetIcon(MakeImage(), true);
109  model.apps()->Add(app1);
110
111  AppListItemModel* app2 = new AppListItemModel;
112  app2->set_app_id("abc2");
113  app2->SetTitleAndFullName("ht2", "hello, there 2");
114  model.apps()->Add(app2);
115
116  DoConsistencyChecks(&model);
117}
118
119TEST_F(AppListModelPicklerUnitTest, EmptyImage) {
120  AppListModel model;
121  AppListItemModel* app1 = new AppListItemModel;
122  app1->set_app_id("abc");
123  app1->SetTitleAndFullName("ht", "hello, there");
124  app1->SetIcon(gfx::ImageSkia(), true);
125  model.apps()->Add(app1);
126
127  DoConsistencyChecks(&model);
128}
129
130TEST_F(AppListModelPicklerUnitTest, SignedIn) {
131  AppListModel model;
132  model.SetSignedIn(true);
133
134  DoConsistencyChecks(&model);
135}
136
137TEST_F(AppListModelPicklerUnitTest, SignedOut) {
138  AppListModel model;
139  model.SetSignedIn(false);
140
141  DoConsistencyChecks(&model);
142}
143