Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / test / app_list_test_model.cc
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
10 namespace app_list {
11 namespace test {
12
13 // static
14 const char AppListTestModel::kItemType[] = "TestItem";
15
16 // AppListTestModel::AppListTestItem
17
18 AppListTestModel::AppListTestItem::AppListTestItem(
19     const std::string& id,
20     AppListTestModel* model)
21     : AppListItem(id),
22       model_(model) {
23 }
24 AppListTestModel::AppListTestItem::~AppListTestItem() {
25 }
26
27 void AppListTestModel::AppListTestItem::Activate(int event_flags) {
28   model_->ItemActivated(this);
29 }
30
31 const char* AppListTestModel::AppListTestItem::GetItemType() const {
32   return AppListTestModel::kItemType;
33 }
34
35 void AppListTestModel::AppListTestItem::SetPosition(
36     const syncer::StringOrdinal& new_position) {
37   set_position(new_position);
38 }
39
40 // AppListTestModel
41
42 AppListTestModel::AppListTestModel()
43     : activate_count_(0),
44       last_activated_(NULL) {
45 }
46
47 AppListItem* AppListTestModel::AddItem(AppListItem* item) {
48   return AppListModel::AddItem(make_scoped_ptr(item));
49 }
50
51 AppListItem* AppListTestModel::AddItemToFolder(AppListItem* item,
52                                                const std::string& folder_id) {
53   return AppListModel::AddItemToFolder(make_scoped_ptr(item), folder_id);
54 }
55
56 void AppListTestModel::MoveItemToFolder(AppListItem* item,
57                                           const std::string& folder_id) {
58   AppListModel::MoveItemToFolder(item, folder_id);
59 }
60
61
62 std::string AppListTestModel::GetItemName(int id) {
63   return base::StringPrintf("Item %d", id);
64 }
65
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));
70 }
71
72 AppListFolderItem* AppListTestModel::CreateAndPopulateFolderWithApps(int n) {
73   DCHECK_GT(n, 1);
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());
80   }
81   AppListItem* merged_item = FindItem(merged_item_id);
82   DCHECK(merged_item->GetItemType() == AppListFolderItem::kItemType);
83   return static_cast<AppListFolderItem*>(merged_item);
84 }
85
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));
91 }
92
93 void AppListTestModel::PopulateAppWithId(int id) {
94   CreateAndAddItem(GetItemName(id));
95 }
96
97 std::string AppListTestModel::GetModelContent() {
98   std::string content;
99   for (size_t i = 0; i < top_level_item_list()->item_count(); ++i) {
100     if (i > 0)
101       content += ',';
102     content += top_level_item_list()->item_at(i)->id();
103   }
104   return content;
105 }
106
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;
112   if (nitems == 0) {
113     position = syncer::StringOrdinal::CreateInitialOrdinal();
114   } else {
115     position =
116         top_level_item_list()->item_at(nitems - 1)->position().CreateAfter();
117   }
118   item->SetPosition(position);
119   SetItemName(item, id);
120   return item;
121 }
122
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);
128 }
129 void AppListTestModel::HighlightItemAt(int index) {
130   AppListItem* item = top_level_item_list()->item_at(index);
131   item->SetHighlighted(true);
132 }
133
134 void AppListTestModel::ItemActivated(AppListTestItem* item) {
135   last_activated_ = item;
136   ++activate_count_;
137 }
138
139 }  // namespace test
140 }  // namespace app_list