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.
5 #include "ui/app_list/test/app_list_test_model.h"
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"
16 gfx::ImageSkia
CreateImageSkia(int width
, int height
) {
18 bitmap
.allocN32Pixels(width
, height
);
19 bitmap
.eraseARGB(255, 0, 255, 0);
20 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap
);
24 const char AppListTestModel::kItemType
[] = "TestItem";
26 // AppListTestModel::AppListTestItem
28 AppListTestModel::AppListTestItem::AppListTestItem(
29 const std::string
& id
,
30 AppListTestModel
* model
)
33 SetIcon(CreateImageSkia(kGridIconDimension
, kGridIconDimension
));
36 AppListTestModel::AppListTestItem::~AppListTestItem() {
39 void AppListTestModel::AppListTestItem::Activate(int event_flags
) {
40 model_
->ItemActivated(this);
43 const char* AppListTestModel::AppListTestItem::GetItemType() const {
44 return AppListTestModel::kItemType
;
47 void AppListTestModel::AppListTestItem::SetPosition(
48 const syncer::StringOrdinal
& new_position
) {
49 set_position(new_position
);
54 AppListTestModel::AppListTestModel()
56 last_activated_(NULL
) {
59 AppListItem
* AppListTestModel::AddItem(AppListItem
* item
) {
60 return AppListModel::AddItem(make_scoped_ptr(item
));
63 AppListItem
* AppListTestModel::AddItemToFolder(AppListItem
* item
,
64 const std::string
& folder_id
) {
65 return AppListModel::AddItemToFolder(make_scoped_ptr(item
), folder_id
);
68 void AppListTestModel::MoveItemToFolder(AppListItem
* item
,
69 const std::string
& folder_id
) {
70 AppListModel::MoveItemToFolder(item
, folder_id
);
74 std::string
AppListTestModel::GetItemName(int id
) {
75 return base::StringPrintf("Item %d", id
);
78 void AppListTestModel::PopulateApps(int n
) {
79 int start_index
= static_cast<int>(top_level_item_list()->item_count());
80 for (int i
= 0; i
< n
; ++i
)
81 CreateAndAddItem(GetItemName(start_index
+ i
));
84 AppListFolderItem
* AppListTestModel::CreateAndPopulateFolderWithApps(int n
) {
86 int start_index
= static_cast<int>(top_level_item_list()->item_count());
87 AppListTestItem
* item
= CreateAndAddItem(GetItemName(start_index
));
88 std::string merged_item_id
= item
->id();
89 for (int i
= 1; i
< n
; ++i
) {
90 AppListTestItem
* new_item
= CreateAndAddItem(GetItemName(start_index
+ i
));
91 merged_item_id
= AppListModel::MergeItems(merged_item_id
, new_item
->id());
93 AppListItem
* merged_item
= FindItem(merged_item_id
);
94 DCHECK(merged_item
->GetItemType() == AppListFolderItem::kItemType
);
95 return static_cast<AppListFolderItem
*>(merged_item
);
98 AppListFolderItem
* AppListTestModel::CreateAndAddOemFolder(
99 const std::string
& id
) {
100 AppListFolderItem
* folder
=
101 new AppListFolderItem(id
, AppListFolderItem::FOLDER_TYPE_OEM
);
102 return static_cast<AppListFolderItem
*>(AddItem(folder
));
105 AppListFolderItem
* AppListTestModel::CreateSingleItemFolder(
106 const std::string
& folder_id
,
107 const std::string
& item_id
) {
108 AppListTestItem
* item
= CreateItem(item_id
);
109 AddItemToFolder(item
, folder_id
);
110 AppListItem
* folder_item
= FindItem(folder_id
);
111 DCHECK(folder_item
->GetItemType() == AppListFolderItem::kItemType
);
112 return static_cast<AppListFolderItem
*>(folder_item
);
115 void AppListTestModel::PopulateAppWithId(int id
) {
116 CreateAndAddItem(GetItemName(id
));
119 std::string
AppListTestModel::GetModelContent() {
121 for (size_t i
= 0; i
< top_level_item_list()->item_count(); ++i
) {
124 content
+= top_level_item_list()->item_at(i
)->id();
129 AppListTestModel::AppListTestItem
* AppListTestModel::CreateItem(
130 const std::string
& id
) {
131 AppListTestItem
* item
= new AppListTestItem(id
, this);
132 size_t nitems
= top_level_item_list()->item_count();
133 syncer::StringOrdinal position
;
135 position
= syncer::StringOrdinal::CreateInitialOrdinal();
138 top_level_item_list()->item_at(nitems
- 1)->position().CreateAfter();
140 item
->SetPosition(position
);
141 SetItemName(item
, id
);
145 AppListTestModel::AppListTestItem
* AppListTestModel::CreateAndAddItem(
146 const std::string
& id
) {
147 scoped_ptr
<AppListTestItem
> test_item(CreateItem(id
));
148 AppListItem
* item
= AppListModel::AddItem(test_item
.Pass());
149 return static_cast<AppListTestItem
*>(item
);
151 void AppListTestModel::HighlightItemAt(int index
) {
152 AppListItem
* item
= top_level_item_list()->item_at(index
);
153 top_level_item_list()->HighlightItemInstalledFromUI(item
->id());
156 void AppListTestModel::ItemActivated(AppListTestItem
* item
) {
157 last_activated_
= item
;
162 } // namespace app_list