• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ui/app_list/test/app_list_test_model.h"
6 
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/stringprintf.h"
9 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "ui/app_list/app_list_constants.h"
11 #include "ui/gfx/image/image_skia.h"
12 
13 namespace app_list {
14 namespace test {
15 
CreateImageSkia(int width,int height)16 gfx::ImageSkia CreateImageSkia(int width, int height) {
17   SkBitmap bitmap;
18   bitmap.allocN32Pixels(width, height);
19   bitmap.eraseARGB(255, 0, 255, 0);
20   return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
21 }
22 
23 // static
24 const char AppListTestModel::kItemType[] = "TestItem";
25 
26 // AppListTestModel::AppListTestItem
27 
AppListTestItem(const std::string & id,AppListTestModel * model)28 AppListTestModel::AppListTestItem::AppListTestItem(
29     const std::string& id,
30     AppListTestModel* model)
31     : AppListItem(id),
32       model_(model) {
33   SetIcon(CreateImageSkia(kGridIconDimension, kGridIconDimension),
34           false /* has_shadow */);
35 }
36 
~AppListTestItem()37 AppListTestModel::AppListTestItem::~AppListTestItem() {
38 }
39 
Activate(int event_flags)40 void AppListTestModel::AppListTestItem::Activate(int event_flags) {
41   model_->ItemActivated(this);
42 }
43 
GetItemType() const44 const char* AppListTestModel::AppListTestItem::GetItemType() const {
45   return AppListTestModel::kItemType;
46 }
47 
SetPosition(const syncer::StringOrdinal & new_position)48 void AppListTestModel::AppListTestItem::SetPosition(
49     const syncer::StringOrdinal& new_position) {
50   set_position(new_position);
51 }
52 
53 // AppListTestModel
54 
AppListTestModel()55 AppListTestModel::AppListTestModel()
56     : activate_count_(0),
57       last_activated_(NULL) {
58 }
59 
AddItem(AppListItem * item)60 AppListItem* AppListTestModel::AddItem(AppListItem* item) {
61   return AppListModel::AddItem(make_scoped_ptr(item));
62 }
63 
AddItemToFolder(AppListItem * item,const std::string & folder_id)64 AppListItem* AppListTestModel::AddItemToFolder(AppListItem* item,
65                                                const std::string& folder_id) {
66   return AppListModel::AddItemToFolder(make_scoped_ptr(item), folder_id);
67 }
68 
MoveItemToFolder(AppListItem * item,const std::string & folder_id)69 void AppListTestModel::MoveItemToFolder(AppListItem* item,
70                                         const std::string& folder_id) {
71   AppListModel::MoveItemToFolder(item, folder_id);
72 }
73 
74 
GetItemName(int id)75 std::string AppListTestModel::GetItemName(int id) {
76   return base::StringPrintf("Item %d", id);
77 }
78 
PopulateApps(int n)79 void AppListTestModel::PopulateApps(int n) {
80   int start_index = static_cast<int>(top_level_item_list()->item_count());
81   for (int i = 0; i < n; ++i)
82     CreateAndAddItem(GetItemName(start_index + i));
83 }
84 
CreateAndPopulateFolderWithApps(int n)85 AppListFolderItem* AppListTestModel::CreateAndPopulateFolderWithApps(int n) {
86   DCHECK_GT(n, 1);
87   int start_index = static_cast<int>(top_level_item_list()->item_count());
88   AppListTestItem* item = CreateAndAddItem(GetItemName(start_index));
89   std::string merged_item_id = item->id();
90   for (int i = 1; i < n; ++i) {
91     AppListTestItem* new_item = CreateAndAddItem(GetItemName(start_index + i));
92     merged_item_id = AppListModel::MergeItems(merged_item_id, new_item->id());
93   }
94   AppListItem* merged_item = FindItem(merged_item_id);
95   DCHECK(merged_item->GetItemType() == AppListFolderItem::kItemType);
96   return static_cast<AppListFolderItem*>(merged_item);
97 }
98 
CreateAndAddOemFolder(const std::string & id)99 AppListFolderItem* AppListTestModel::CreateAndAddOemFolder(
100     const std::string& id) {
101   AppListFolderItem* folder =
102       new AppListFolderItem(id, AppListFolderItem::FOLDER_TYPE_OEM);
103   return static_cast<AppListFolderItem*>(AddItem(folder));
104 }
105 
CreateSingleItemFolder(const std::string & folder_id,const std::string & item_id)106 AppListFolderItem* AppListTestModel::CreateSingleItemFolder(
107     const std::string& folder_id,
108     const std::string& item_id) {
109   AppListTestItem* item = CreateItem(item_id);
110   AddItemToFolder(item, folder_id);
111   AppListItem* folder_item = FindItem(folder_id);
112   DCHECK(folder_item->GetItemType() == AppListFolderItem::kItemType);
113   return static_cast<AppListFolderItem*>(folder_item);
114 }
115 
PopulateAppWithId(int id)116 void AppListTestModel::PopulateAppWithId(int id) {
117   CreateAndAddItem(GetItemName(id));
118 }
119 
GetModelContent()120 std::string AppListTestModel::GetModelContent() {
121   std::string content;
122   for (size_t i = 0; i < top_level_item_list()->item_count(); ++i) {
123     if (i > 0)
124       content += ',';
125     content += top_level_item_list()->item_at(i)->id();
126   }
127   return content;
128 }
129 
CreateItem(const std::string & id)130 AppListTestModel::AppListTestItem* AppListTestModel::CreateItem(
131     const std::string& id) {
132   AppListTestItem* item = new AppListTestItem(id, this);
133   size_t nitems = top_level_item_list()->item_count();
134   syncer::StringOrdinal position;
135   if (nitems == 0) {
136     position = syncer::StringOrdinal::CreateInitialOrdinal();
137   } else {
138     position =
139         top_level_item_list()->item_at(nitems - 1)->position().CreateAfter();
140   }
141   item->SetPosition(position);
142   SetItemName(item, id);
143   return item;
144 }
145 
CreateAndAddItem(const std::string & id)146 AppListTestModel::AppListTestItem* AppListTestModel::CreateAndAddItem(
147     const std::string& id) {
148   scoped_ptr<AppListTestItem> test_item(CreateItem(id));
149   AppListItem* item = AppListModel::AddItem(test_item.PassAs<AppListItem>());
150   return static_cast<AppListTestItem*>(item);
151 }
HighlightItemAt(int index)152 void AppListTestModel::HighlightItemAt(int index) {
153   AppListItem* item = top_level_item_list()->item_at(index);
154   item->SetHighlighted(true);
155 }
156 
ItemActivated(AppListTestItem * item)157 void AppListTestModel::ItemActivated(AppListTestItem* item) {
158   last_activated_ = item;
159   ++activate_count_;
160 }
161 
162 }  // namespace test
163 }  // namespace app_list
164