OLD | NEW |
| (Empty) |
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 "ash/test/test_shelf_delegate.h" | |
6 | |
7 #include "ash/shelf/shelf_model.h" | |
8 #include "ash/shell.h" | |
9 #include "base/strings/string_util.h" | |
10 | |
11 namespace ash { | |
12 namespace test { | |
13 | |
14 TestShelfDelegate* TestShelfDelegate::instance_ = nullptr; | |
15 | |
16 TestShelfDelegate::TestShelfDelegate() { | |
17 CHECK(!instance_); | |
18 instance_ = this; | |
19 } | |
20 | |
21 TestShelfDelegate::~TestShelfDelegate() { | |
22 instance_ = nullptr; | |
23 } | |
24 | |
25 ShelfID TestShelfDelegate::GetShelfIDForAppID(const std::string& app_id) { | |
26 // Get shelf id for |app_id| and an empty |launch_id|. | |
27 return GetShelfIDForAppIDAndLaunchID(app_id, std::string()); | |
28 } | |
29 | |
30 ShelfID TestShelfDelegate::GetShelfIDForAppIDAndLaunchID( | |
31 const std::string& app_id, | |
32 const std::string& launch_id) { | |
33 for (const ShelfItem& item : Shell::Get()->shelf_model()->items()) { | |
34 // Ash's ShelfWindowWatcher handles app panel windows separately. | |
35 if (item.type != TYPE_APP_PANEL && item.app_launch_id.app_id() == app_id && | |
36 item.app_launch_id.launch_id() == launch_id) { | |
37 return item.id; | |
38 } | |
39 } | |
40 return kInvalidShelfID; | |
41 } | |
42 | |
43 const std::string& TestShelfDelegate::GetAppIDForShelfID(ShelfID id) { | |
44 ShelfModel* model = Shell::Get()->shelf_model(); | |
45 ShelfItems::const_iterator item = model->ItemByID(id); | |
46 return item != model->items().end() ? item->app_launch_id.app_id() | |
47 : base::EmptyString(); | |
48 } | |
49 | |
50 void TestShelfDelegate::PinAppWithID(const std::string& app_id) { | |
51 // If the app is already pinned, do nothing and return. | |
52 if (IsAppPinned(app_id)) | |
53 return; | |
54 | |
55 // Convert an existing item to be pinned, or create a new pinned item. | |
56 ShelfModel* model = Shell::Get()->shelf_model(); | |
57 const int index = model->ItemIndexByID(GetShelfIDForAppID(app_id)); | |
58 if (index >= 0) { | |
59 ShelfItem item = model->items()[index]; | |
60 DCHECK_EQ(item.type, TYPE_APP); | |
61 DCHECK(!item.pinned_by_policy); | |
62 item.type = TYPE_PINNED_APP; | |
63 model->Set(index, item); | |
64 } else if (!app_id.empty()) { | |
65 ShelfItem item; | |
66 item.type = TYPE_PINNED_APP; | |
67 item.app_launch_id = AppLaunchId(app_id); | |
68 model->Add(item); | |
69 } | |
70 } | |
71 | |
72 bool TestShelfDelegate::IsAppPinned(const std::string& app_id) { | |
73 ShelfID shelf_id = GetShelfIDForAppID(app_id); | |
74 ShelfModel* model = Shell::Get()->shelf_model(); | |
75 ShelfItems::const_iterator item = model->ItemByID(shelf_id); | |
76 return item != model->items().end() && item->type == TYPE_PINNED_APP; | |
77 } | |
78 | |
79 void TestShelfDelegate::UnpinAppWithID(const std::string& app_id) { | |
80 // If the app is already not pinned, do nothing and return. | |
81 if (!IsAppPinned(app_id)) | |
82 return; | |
83 | |
84 // Remove the item if it is closed, or mark it as unpinned. | |
85 ShelfModel* model = Shell::Get()->shelf_model(); | |
86 const int index = model->ItemIndexByID(GetShelfIDForAppID(app_id)); | |
87 ShelfItem item = model->items()[index]; | |
88 DCHECK_EQ(item.type, TYPE_PINNED_APP); | |
89 DCHECK(!item.pinned_by_policy); | |
90 if (item.status == STATUS_CLOSED) { | |
91 model->RemoveItemAt(index); | |
92 } else { | |
93 item.type = TYPE_APP; | |
94 model->Set(index, item); | |
95 } | |
96 } | |
97 | |
98 } // namespace test | |
99 } // namespace ash | |
OLD | NEW |