Upstream version 6.35.121.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 void 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 }
82
83 void AppListTestModel::PopulateAppWithId(int id) {
84   CreateAndAddItem(GetItemName(id));
85 }
86
87 std::string AppListTestModel::GetModelContent() {
88   std::string content;
89   for (size_t i = 0; i < top_level_item_list()->item_count(); ++i) {
90     if (i > 0)
91       content += ',';
92     content += top_level_item_list()->item_at(i)->id();
93   }
94   return content;
95 }
96
97 AppListTestModel::AppListTestItem* AppListTestModel::CreateItem(
98     const std::string& id) {
99   AppListTestItem* item = new AppListTestItem(id, this);
100   size_t nitems = top_level_item_list()->item_count();
101   syncer::StringOrdinal position;
102   if (nitems == 0) {
103     position = syncer::StringOrdinal::CreateInitialOrdinal();
104   } else {
105     position =
106         top_level_item_list()->item_at(nitems - 1)->position().CreateAfter();
107   }
108   item->SetPosition(position);
109   SetItemName(item, id);
110   return item;
111 }
112
113 AppListTestModel::AppListTestItem* AppListTestModel::CreateAndAddItem(
114     const std::string& id) {
115   scoped_ptr<AppListTestItem> test_item(CreateItem(id));
116   AppListItem* item = AppListModel::AddItem(test_item.PassAs<AppListItem>());
117   return static_cast<AppListTestItem*>(item);
118 }
119 void AppListTestModel::HighlightItemAt(int index) {
120   AppListItem* item = top_level_item_list()->item_at(index);
121   item->SetHighlighted(true);
122 }
123
124 void AppListTestModel::ItemActivated(AppListTestItem* item) {
125   last_activated_ = item;
126   ++activate_count_;
127 }
128
129 }  // namespace test
130 }  // namespace app_list