summaryrefslogtreecommitdiffstats
path: root/ash/test/test_launcher_delegate.cc
blob: 3f4084931d4183f53cb6ca6784a95ac670d7bdb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/test/test_launcher_delegate.h"

#include "ash/launcher/launcher_model.h"
#include "ash/launcher/launcher_util.h"
#include "ash/wm/window_util.h"
#include "base/utf_string_conversions.h"
#include "grit/ash_resources.h"
#include "ui/aura/window.h"

namespace ash {
namespace test {

TestLauncherDelegate* TestLauncherDelegate::instance_ = NULL;

TestLauncherDelegate::TestLauncherDelegate(LauncherModel* model)
    : model_(model) {
  CHECK(!instance_);
  instance_ = this;
}

TestLauncherDelegate::~TestLauncherDelegate() {
  instance_ = NULL;
}

void TestLauncherDelegate::AddLauncherItem(aura::Window* window) {
  AddLauncherItem(window, STATUS_CLOSED);
}

void TestLauncherDelegate::AddLauncherItem(
    aura::Window* window,
    LauncherItemStatus status) {
  ash::LauncherItem item;
  if (window->type() == aura::client::WINDOW_TYPE_PANEL)
    item.type = ash::TYPE_APP_PANEL;
  else
    item.type = ash::TYPE_TABBED;
  DCHECK(window_to_id_.find(window) == window_to_id_.end());
  window_to_id_[window] = model_->next_id();
  item.status = status;
  model_->Add(item);
  if (observed_windows_.find(window->parent()) == observed_windows_.end()) {
    window->parent()->AddObserver(this);
    observed_windows_.insert(window->parent());
  }
}

void TestLauncherDelegate::OnWillRemoveWindow(aura::Window* window) {
  ash::LauncherID id = GetIDByWindow(window);
  if (id == 0)
    return;
  int index = model_->ItemIndexByID(id);
  DCHECK_NE(-1, index);
  model_->RemoveItemAt(index);
  window_to_id_.erase(window);
  ObservedWindows::iterator it = observed_windows_.find(window->parent());
  if (it != observed_windows_.end()) {
    window->parent()->RemoveObserver(this);
    observed_windows_.erase(it);
  }
}

void TestLauncherDelegate::OnBrowserShortcutClicked(int event_flags) {
}

void TestLauncherDelegate::ItemClicked(const ash::LauncherItem& item,
                                       const ui::Event& event) {
  aura::Window* window = GetWindowByID(item.id);
  launcher::MoveToEventRootIfPanel(window, event);
  window->Show();
  ash::wm::ActivateWindow(window);
}

int TestLauncherDelegate::GetBrowserShortcutResourceId() {
  return IDR_AURA_LAUNCHER_BROWSER_SHORTCUT;
}

string16 TestLauncherDelegate::GetTitle(const ash::LauncherItem& item) {
  aura::Window* window = GetWindowByID(item.id);
  return window ? window->title() : string16();
}

ui::MenuModel* TestLauncherDelegate::CreateContextMenu(
    const ash::LauncherItem& item,
    aura::RootWindow* root) {
  return NULL;
}

ash::LauncherMenuModel* TestLauncherDelegate::CreateApplicationMenu(
    const ash::LauncherItem& item,
    int event_flags) {
  return NULL;
}

ash::LauncherID TestLauncherDelegate::GetIDByWindow(aura::Window* window) {
  WindowToID::const_iterator found = window_to_id_.find(window);
  if (found == window_to_id_.end())
    return 0;
  return found->second;
}

aura::Window* TestLauncherDelegate::GetWindowByID(ash::LauncherID id) {
  for (WindowToID::const_iterator it = window_to_id_.begin();
      it != window_to_id_.end();
      it++) {
    if (it->second == id)
      return it->first;
  }
  return NULL;
}

bool TestLauncherDelegate::IsDraggable(const ash::LauncherItem& item) {
  return true;
}

}  // namespace test
}  // namespace ash