Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2012 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_launcher_delegate.h"
      6 
      7 #include "ash/launcher/launcher_model.h"
      8 #include "ash/launcher/launcher_util.h"
      9 #include "ash/wm/window_util.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 #include "grit/ash_resources.h"
     12 #include "ui/aura/window.h"
     13 
     14 namespace ash {
     15 namespace test {
     16 
     17 TestLauncherDelegate* TestLauncherDelegate::instance_ = NULL;
     18 
     19 TestLauncherDelegate::TestLauncherDelegate(LauncherModel* model)
     20     : model_(model) {
     21   CHECK(!instance_);
     22   instance_ = this;
     23 }
     24 
     25 TestLauncherDelegate::~TestLauncherDelegate() {
     26   instance_ = NULL;
     27 }
     28 
     29 void TestLauncherDelegate::AddLauncherItem(aura::Window* window) {
     30   AddLauncherItem(window, STATUS_CLOSED);
     31 }
     32 
     33 void TestLauncherDelegate::AddLauncherItem(
     34     aura::Window* window,
     35     LauncherItemStatus status) {
     36   ash::LauncherItem item;
     37   if (window->type() == aura::client::WINDOW_TYPE_PANEL)
     38     item.type = ash::TYPE_APP_PANEL;
     39   else
     40     item.type = ash::TYPE_TABBED;
     41   DCHECK(window_to_id_.find(window) == window_to_id_.end());
     42   window_to_id_[window] = model_->next_id();
     43   item.status = status;
     44   model_->Add(item);
     45   window->AddObserver(this);
     46 }
     47 
     48 void TestLauncherDelegate::RemoveLauncherItemForWindow(aura::Window* window) {
     49   ash::LauncherID id = GetIDByWindow(window);
     50   if (id == 0)
     51     return;
     52   int index = model_->ItemIndexByID(id);
     53   DCHECK_NE(-1, index);
     54   model_->RemoveItemAt(index);
     55   window_to_id_.erase(window);
     56   window->RemoveObserver(this);
     57 }
     58 
     59 void TestLauncherDelegate::OnWindowDestroying(aura::Window* window) {
     60   RemoveLauncherItemForWindow(window);
     61 }
     62 
     63 void TestLauncherDelegate::OnWindowHierarchyChanging(
     64       const HierarchyChangeParams& params) {
     65   // The window may be legitimately reparented while staying open if it moves
     66   // to another display or container. If the window does not have a new parent
     67   // then remove the launcher item.
     68   if (!params.new_parent)
     69     RemoveLauncherItemForWindow(params.target);
     70 }
     71 
     72 void TestLauncherDelegate::ItemSelected(const ash::LauncherItem& item,
     73                                        const ui::Event& event) {
     74   aura::Window* window = GetWindowByID(item.id);
     75   if (window->type() == aura::client::WINDOW_TYPE_PANEL)
     76     ash::wm::MoveWindowToEventRoot(window, event);
     77   window->Show();
     78   ash::wm::ActivateWindow(window);
     79 }
     80 
     81 base::string16 TestLauncherDelegate::GetTitle(const ash::LauncherItem& item) {
     82   aura::Window* window = GetWindowByID(item.id);
     83   return window ? window->title() : base::string16();
     84 }
     85 
     86 ui::MenuModel* TestLauncherDelegate::CreateContextMenu(
     87     const ash::LauncherItem& item,
     88     aura::RootWindow* root) {
     89   return NULL;
     90 }
     91 
     92 ash::LauncherMenuModel* TestLauncherDelegate::CreateApplicationMenu(
     93     const ash::LauncherItem& item,
     94     int event_flags) {
     95   return NULL;
     96 }
     97 
     98 ash::LauncherID TestLauncherDelegate::GetIDByWindow(aura::Window* window) {
     99   WindowToID::const_iterator found = window_to_id_.find(window);
    100   if (found == window_to_id_.end())
    101     return 0;
    102   return found->second;
    103 }
    104 
    105 aura::Window* TestLauncherDelegate::GetWindowByID(ash::LauncherID id) {
    106   for (WindowToID::const_iterator it = window_to_id_.begin();
    107       it != window_to_id_.end();
    108       it++) {
    109     if (it->second == id)
    110       return it->first;
    111   }
    112   return NULL;
    113 }
    114 
    115 bool TestLauncherDelegate::IsDraggable(const ash::LauncherItem& item) {
    116   return true;
    117 }
    118 
    119 bool TestLauncherDelegate::ShouldShowTooltip(const ash::LauncherItem& item) {
    120   return true;
    121 }
    122 
    123 void TestLauncherDelegate::OnLauncherCreated(Launcher* launcher) {
    124 }
    125 
    126 void TestLauncherDelegate::OnLauncherDestroyed(Launcher* launcher) {
    127 }
    128 
    129 bool TestLauncherDelegate::IsPerAppLauncher() {
    130   return true;
    131 }
    132 
    133 LauncherID TestLauncherDelegate::GetLauncherIDForAppID(
    134     const std::string& app_id) {
    135   return 0;
    136 }
    137 
    138 void TestLauncherDelegate::PinAppWithID(const std::string& app_id) {
    139 }
    140 
    141 bool TestLauncherDelegate::IsAppPinned(const std::string& app_id) {
    142   return false;
    143 }
    144 
    145 void TestLauncherDelegate::UnpinAppsWithID(const std::string& app_id) {
    146 }
    147 
    148 }  // namespace test
    149 }  // namespace ash
    150