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