Home | History | Annotate | Download | only in test
      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.h"
     10 #include "ui/app_list/app_list_model.h"
     11 #include "ui/app_list/test/app_list_test_model.h"
     12 #include "ui/gfx/image/image_skia.h"
     13 #include "ui/gfx/skia_util.h"
     14 
     15 using app_list::AppListItem;
     16 using app_list::AppListModel;
     17 
     18 class AppListModelPicklerUnitTest : public testing::Test {
     19  protected:
     20   void CheckIsSame(AppListModel* m1, AppListModel* m2) {
     21     ASSERT_EQ(m1->top_level_item_list()->item_count(),
     22               m2->top_level_item_list()->item_count());
     23     for (size_t i = 0; i < m1->top_level_item_list()->item_count(); i++) {
     24       ASSERT_EQ(m1->top_level_item_list()->item_at(i)->id(),
     25                 m2->top_level_item_list()->item_at(i)->id());
     26       ASSERT_EQ(m1->top_level_item_list()->item_at(i)->name(),
     27                 m2->top_level_item_list()->item_at(i)->name());
     28       ASSERT_EQ(m1->top_level_item_list()->item_at(i)->short_name(),
     29                 m2->top_level_item_list()->item_at(i)->short_name());
     30       CompareImages(m1->top_level_item_list()->item_at(i)->icon(),
     31                     m2->top_level_item_list()->item_at(i)->icon());
     32     }
     33   }
     34 
     35   void CompareImages(const gfx::ImageSkia& image1,
     36                      const gfx::ImageSkia& image2) {
     37     std::vector<gfx::ImageSkiaRep> reps1(image1.image_reps());
     38     std::vector<gfx::ImageSkiaRep> reps2(image2.image_reps());
     39     ASSERT_EQ(reps1.size(), reps2.size());
     40     for (size_t i = 0; i < reps1.size(); ++i) {
     41       ASSERT_TRUE(
     42           gfx::BitmapsAreEqual(reps1[i].sk_bitmap(), reps2[i].sk_bitmap()));
     43       ASSERT_EQ(reps1[i].scale(), reps2[i].scale());
     44     }
     45   }
     46 
     47   scoped_ptr<AppListModel> CopyViaPickle(AppListModel* model) {
     48     scoped_ptr<Pickle> pickle(
     49         FastShowPickler::PickleAppListModelForFastShow(model));
     50     return FastShowPickler::UnpickleAppListModelForFastShow(pickle.get());
     51   }
     52 
     53   void DoConsistencyChecks(AppListModel* model) {
     54     scoped_ptr<AppListModel> model2(CopyViaPickle(model));
     55     AppListModel dest_model;
     56     FastShowPickler::CopyOver(model2.get(), &dest_model);
     57 
     58     CheckIsSame(model, model2.get());
     59     CheckIsSame(model, &dest_model);
     60     CheckIsSame(model2.get(), &dest_model);
     61   }
     62 
     63   gfx::ImageSkia MakeImage() {
     64     const int kWidth = 10;
     65     const int kHeight = 10;
     66     SkBitmap bitmap;
     67     bitmap.allocN32Pixels(kWidth, kHeight);
     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   model.AddItem(make_scoped_ptr(new AppListItem("abc")).Pass());
     81   DoConsistencyChecks(&model);
     82 }
     83 
     84 TEST_F(AppListModelPicklerUnitTest, TwoItems) {
     85   AppListModel model;
     86   AppListItem* app1 =
     87       model.AddItem(make_scoped_ptr(new AppListItem("abc")).Pass());
     88   model.SetItemNameAndShortName(app1, "hello, there", "ht");
     89   AppListItem* app2 =
     90       model.AddItem(make_scoped_ptr(new AppListItem("abc2")).Pass());
     91   model.SetItemNameAndShortName(app2, "hello, there 2", "ht2");
     92 
     93   DoConsistencyChecks(&model);
     94 }
     95 
     96 TEST_F(AppListModelPicklerUnitTest, Images) {
     97   AppListModel model;
     98   AppListItem* app1 =
     99       model.AddItem(make_scoped_ptr(new AppListItem("abc")).Pass());
    100   model.SetItemName(app1, "hello, there");
    101   app1->SetIcon(MakeImage(), true);
    102   AppListItem* app2 =
    103       model.AddItem(make_scoped_ptr(new AppListItem("abc2")).Pass());
    104   model.SetItemName(app2, "hello, there 2");
    105 
    106   DoConsistencyChecks(&model);
    107 }
    108 
    109 TEST_F(AppListModelPicklerUnitTest, EmptyImage) {
    110   AppListModel model;
    111   AppListItem* app1 =
    112       model.AddItem(make_scoped_ptr(new AppListItem("abc")).Pass());
    113   model.SetItemName(app1, "hello, there");
    114   app1->SetIcon(gfx::ImageSkia(), true);
    115 
    116   DoConsistencyChecks(&model);
    117 }
    118