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"
14 const char AppListTestModel::kItemType[] = "TestItem";
16 // AppListTestModel::AppListTestItem
18 AppListTestModel::AppListTestItem::AppListTestItem(
19 const std::string& id,
20 AppListTestModel* model)
24 AppListTestModel::AppListTestItem::~AppListTestItem() {
27 void AppListTestModel::AppListTestItem::Activate(int event_flags) {
28 model_->ItemActivated(this);
31 const char* AppListTestModel::AppListTestItem::GetItemType() const {
32 return AppListTestModel::kItemType;
35 void AppListTestModel::AppListTestItem::SetPosition(
36 const syncer::StringOrdinal& new_position) {
37 set_position(new_position);
42 AppListTestModel::AppListTestModel()
44 last_activated_(NULL) {
47 AppListItem* AppListTestModel::AddItem(AppListItem* item) {
48 return AppListModel::AddItem(make_scoped_ptr(item));
51 AppListItem* AppListTestModel::AddItemToFolder(AppListItem* item,
52 const std::string& folder_id) {
53 return AppListModel::AddItemToFolder(make_scoped_ptr(item), folder_id);
56 void AppListTestModel::MoveItemToFolder(AppListItem* item,
57 const std::string& folder_id) {
58 AppListModel::MoveItemToFolder(item, folder_id);
62 std::string AppListTestModel::GetItemName(int id) {
63 return base::StringPrintf("Item %d", id);
66 void AppListTestModel::PopulateApps(int n) {
67 int start_index = static_cast<int>(top_level_item_list()->item_count());
68 for (int i = 0; i < n; ++i)
69 CreateAndAddItem(GetItemName(start_index + i));
72 AppListFolderItem* AppListTestModel::CreateAndPopulateFolderWithApps(int n) {
74 int start_index = static_cast<int>(top_level_item_list()->item_count());
75 AppListTestItem* item = CreateAndAddItem(GetItemName(start_index));
76 std::string merged_item_id = item->id();
77 for (int i = 1; i < n; ++i) {
78 AppListTestItem* new_item = CreateAndAddItem(GetItemName(start_index + i));
79 merged_item_id = AppListModel::MergeItems(merged_item_id, new_item->id());
81 AppListItem* merged_item = FindItem(merged_item_id);
82 DCHECK(merged_item->GetItemType() == AppListFolderItem::kItemType);
83 return static_cast<AppListFolderItem*>(merged_item);
86 AppListFolderItem* AppListTestModel::CreateAndAddOemFolder(
87 const std::string& id) {
88 AppListFolderItem* folder =
89 new AppListFolderItem(id, AppListFolderItem::FOLDER_TYPE_OEM);
90 return static_cast<AppListFolderItem*>(AddItem(folder));
93 void AppListTestModel::PopulateAppWithId(int id) {
94 CreateAndAddItem(GetItemName(id));
97 std::string AppListTestModel::GetModelContent() {
99 for (size_t i = 0; i < top_level_item_list()->item_count(); ++i) {
102 content += top_level_item_list()->item_at(i)->id();
107 AppListTestModel::AppListTestItem* AppListTestModel::CreateItem(
108 const std::string& id) {
109 AppListTestItem* item = new AppListTestItem(id, this);
110 size_t nitems = top_level_item_list()->item_count();
111 syncer::StringOrdinal position;
113 position = syncer::StringOrdinal::CreateInitialOrdinal();
116 top_level_item_list()->item_at(nitems - 1)->position().CreateAfter();
118 item->SetPosition(position);
119 SetItemName(item, id);
123 AppListTestModel::AppListTestItem* AppListTestModel::CreateAndAddItem(
124 const std::string& id) {
125 scoped_ptr<AppListTestItem> test_item(CreateItem(id));
126 AppListItem* item = AppListModel::AddItem(test_item.PassAs<AppListItem>());
127 return static_cast<AppListTestItem*>(item);
129 void AppListTestModel::HighlightItemAt(int index) {
130 AppListItem* item = top_level_item_list()->item_at(index);
131 item->SetHighlighted(true);
134 void AppListTestModel::ItemActivated(AppListTestItem* item) {
135 last_activated_ = item;
140 } // namespace app_list