Home | History | Annotate | Download | only in launcher
      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/launcher/launcher_navigator.h"
      6 
      7 #include "ash/launcher/launcher.h"
      8 #include "ash/launcher/launcher_model.h"
      9 #include "ash/launcher/launcher_types.h"
     10 #include "base/compiler_specific.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace ash {
     14 
     15 namespace {
     16 
     17 class LauncherNavigatorTest : public testing::Test {
     18  public:
     19   LauncherNavigatorTest() {}
     20 
     21  protected:
     22   virtual void SetUp() OVERRIDE {
     23     model_.reset(new LauncherModel);
     24 
     25     // Initially, applist launcher item is only created.
     26     int total_num = model_->item_count();
     27     EXPECT_EQ(1, total_num);
     28     EXPECT_TRUE(model_->items()[0].type == TYPE_APP_LIST);
     29 
     30     // Add BROWSER_SHORTCUT for test.
     31     LauncherItem browser_shortcut;
     32     browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
     33     model_->Add(browser_shortcut);
     34   }
     35 
     36   void SetupMockLauncherModel(LauncherItemType* types,
     37                               int types_length,
     38                               int focused_index) {
     39     for (int i = 0; i < types_length; ++i) {
     40       LauncherItem new_item;
     41       new_item.type = types[i];
     42       new_item.status =
     43           (types[i] == TYPE_TABBED) ? STATUS_RUNNING : STATUS_CLOSED;
     44       model_->Add(new_item);
     45     }
     46 
     47     // Set the focused item.
     48     if (focused_index >= 0) {
     49       LauncherItem focused_item =model_->items()[focused_index];
     50       if (focused_item.type == TYPE_TABBED) {
     51         focused_item.status = STATUS_ACTIVE;
     52         model_->Set(focused_index, focused_item);
     53       }
     54     }
     55   }
     56 
     57   const LauncherModel& model() { return *model_.get(); }
     58 
     59  private:
     60   scoped_ptr<LauncherModel> model_;
     61 
     62   DISALLOW_COPY_AND_ASSIGN(LauncherNavigatorTest);
     63 };
     64 
     65 } // namespace
     66 
     67 TEST_F(LauncherNavigatorTest, BasicCycle) {
     68   // An app shortcut and three windows
     69   LauncherItemType types[] = {
     70     TYPE_APP_SHORTCUT, TYPE_TABBED, TYPE_TABBED, TYPE_TABBED,
     71   };
     72   // LauncherModel automatically adds BROWSER_SHORTCUT item at the
     73   // beginning, so '2' refers the first TYPE_TABBED item.
     74   SetupMockLauncherModel(types, arraysize(types), 2);
     75 
     76   EXPECT_EQ(3, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
     77 
     78   // Fourth one.  It will skip the APP_SHORTCUT at the beginning of the list and
     79   // the APP_LIST item which is automatically added at the end of items.
     80   EXPECT_EQ(4, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
     81 }
     82 
     83 TEST_F(LauncherNavigatorTest, WrapToBeginning) {
     84   LauncherItemType types[] = {
     85     TYPE_APP_SHORTCUT, TYPE_TABBED, TYPE_TABBED, TYPE_TABBED,
     86   };
     87   SetupMockLauncherModel(types, arraysize(types), 4);
     88 
     89   // Second one.  It skips the APP_LIST item at the end of the list,
     90   // wraps to the beginning, and skips BROWSER_SHORTCUT and APP_SHORTCUT
     91   // at the beginning of the list.
     92   EXPECT_EQ(2, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
     93 }
     94 
     95 TEST_F(LauncherNavigatorTest, Empty) {
     96   SetupMockLauncherModel(NULL, 0, -1);
     97   EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
     98   EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
     99 }
    100 
    101 TEST_F(LauncherNavigatorTest, SingleEntry) {
    102   LauncherItemType type = TYPE_TABBED;
    103   SetupMockLauncherModel(&type, 1, 1);
    104 
    105   // If there's only one item there and it is already active, there's no item
    106   // to be activated next.
    107   EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
    108   EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
    109 }
    110 
    111 TEST_F(LauncherNavigatorTest, NoActive) {
    112   LauncherItemType types[] = {
    113     TYPE_TABBED, TYPE_TABBED,
    114   };
    115   // Special case: no items are 'STATUS_ACTIVE'.
    116   SetupMockLauncherModel(types, arraysize(types), -1);
    117 
    118   // If there are no active status, pick the first running item as a fallback.
    119   EXPECT_EQ(1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
    120   EXPECT_EQ(1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
    121 }
    122 
    123 }  // namespace ash
    124