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
),
34 false /* has_shadow */);
37 AppListTestModel::AppListTestItem::~AppListTestItem() {
40 void AppListTestModel::AppListTestItem::Activate(int event_flags
) {
41 model_
->ItemActivated(this);
44 const char* AppListTestModel::AppListTestItem::GetItemType() const {
45 return AppListTestModel::kItemType
;
48 void AppListTestModel::AppListTestItem::SetPosition(
49 const syncer::StringOrdinal
& new_position
) {
50 set_position(new_position
);
55 AppListTestModel::AppListTestModel()
57 last_activated_(NULL
) {
60 AppListItem
* AppListTestModel::AddItem(AppListItem
* item
) {
61 return AppListModel::AddItem(make_scoped_ptr(item
));
64 AppListItem
* AppListTestModel::AddItemToFolder(AppListItem
* item
,
65 const std::string
& folder_id
) {
66 return AppListModel::AddItemToFolder(make_scoped_ptr(item
), folder_id
);
69 void AppListTestModel::MoveItemToFolder(AppListItem
* item
,
70 const std::string
& folder_id
) {
71 AppListModel::MoveItemToFolder(item
, folder_id
);
75 std::string
AppListTestModel::GetItemName(int id
) {
76 return base::StringPrintf("Item %d", id
);
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
));
85 AppListFolderItem
* AppListTestModel::CreateAndPopulateFolderWithApps(int n
) {
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());
94 AppListItem
* merged_item
= FindItem(merged_item_id
);
95 DCHECK(merged_item
->GetItemType() == AppListFolderItem::kItemType
);
96 return static_cast<AppListFolderItem
*>(merged_item
);
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
));
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
);
116 void AppListTestModel::PopulateAppWithId(int id
) {
117 CreateAndAddItem(GetItemName(id
));
120 std::string
AppListTestModel::GetModelContent() {
122 for (size_t i
= 0; i
< top_level_item_list()->item_count(); ++i
) {
125 content
+= top_level_item_list()->item_at(i
)->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
;
136 position
= syncer::StringOrdinal::CreateInitialOrdinal();
139 top_level_item_list()->item_at(nitems
- 1)->position().CreateAfter();
141 item
->SetPosition(position
);
142 SetItemName(item
, 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
);
152 void AppListTestModel::HighlightItemAt(int index
) {
153 AppListItem
* item
= top_level_item_list()->item_at(index
);
154 item
->SetHighlighted(true);
157 void AppListTestModel::ItemActivated(AppListTestItem
* item
) {
158 last_activated_
= item
;
163 } // namespace app_list