- add sources.
[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/strings/stringprintf.h"
8 #include "ui/app_list/app_list_item_model.h"
9
10 namespace app_list {
11 namespace test {
12
13 // static
14 const char AppListTestModel::kAppType[] = "FolderItem";
15
16 class AppListTestModel::AppListTestItemModel : public AppListItemModel {
17  public:
18   AppListTestItemModel(const std::string& id, AppListTestModel* model)
19       : AppListItemModel(id),
20         model_(model) {
21   }
22   virtual ~AppListTestItemModel() {}
23
24   virtual void Activate(int event_flags) OVERRIDE {
25     model_->ItemActivated(this);
26   }
27
28   virtual const char* GetAppType() const OVERRIDE {
29     return AppListTestModel::kAppType;
30   }
31
32  private:
33   AppListTestModel* model_;
34   DISALLOW_COPY_AND_ASSIGN(AppListTestItemModel);
35 };
36
37 AppListTestModel::AppListTestModel()
38     : activate_count_(0),
39       last_activated_(NULL) {
40   SetSignedIn(true);
41 }
42
43 std::string AppListTestModel::GetItemName(int id) {
44   return base::StringPrintf("Item %d", id);
45 }
46
47 void AppListTestModel::PopulateApps(int n) {
48   int start_index = item_list()->item_count();
49   for (int i = 0; i < n; ++i)
50     CreateAndAddItem(GetItemName(start_index + i));
51 }
52
53 void AppListTestModel::PopulateAppWithId(int id) {
54   CreateAndAddItem(GetItemName(id));
55 }
56
57 std::string AppListTestModel::GetModelContent() {
58   std::string content;
59   for (size_t i = 0; i < item_list()->item_count(); ++i) {
60     if (i > 0)
61       content += ',';
62     content += item_list()->item_at(i)->title();
63   }
64   return content;
65 }
66
67 AppListItemModel* AppListTestModel::CreateItem(const std::string& title,
68                                                const std::string& full_name) {
69   AppListItemModel* item = new AppListTestItemModel(title, this);
70   size_t nitems = item_list()->item_count();
71   syncer::StringOrdinal position;
72   if (nitems == 0)
73     position = syncer::StringOrdinal::CreateInitialOrdinal();
74   else
75     position = item_list()->item_at(nitems - 1)->position().CreateAfter();
76   item->set_position(position);
77   item->SetTitleAndFullName(title, full_name);
78   return item;
79 }
80
81 void AppListTestModel::CreateAndAddItem(const std::string& title,
82                                         const std::string& full_name) {
83   item_list()->AddItem(CreateItem(title, full_name));
84 }
85
86 void AppListTestModel::CreateAndAddItem(const std::string& title) {
87   CreateAndAddItem(title, title);
88 }
89
90 void AppListTestModel::HighlightItemAt(int index) {
91   AppListItemModel* item = item_list()->item_at(index);
92   item->SetHighlighted(true);
93 }
94
95 void AppListTestModel::ItemActivated(AppListTestItemModel* item) {
96   last_activated_ = item;
97   ++activate_count_;
98 }
99
100 }  // namespace test
101 }  // namespace app_list