Home | History | Annotate | Download | only in tabs
      1 // Copyright (c) 2011 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 <algorithm>
      6 #include <string>
      7 
      8 #include "base/string_number_conversions.h"
      9 #include "chrome/browser/tabs/tab_strip_selection_model.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 typedef testing::Test TabStripSelectionModelTest;
     13 
     14 // Returns the state of the selection model as a string. The format is:
     15 // 'active=X anchor=X selection=X X X...'.
     16 static std::string StateAsString(const TabStripSelectionModel& model) {
     17   std::string result = "active=" + base::IntToString(model.active()) +
     18       " anchor=" + base::IntToString(model.anchor()) +
     19       " selection=";
     20   const TabStripSelectionModel::SelectedIndices& selection(
     21       model.selected_indices());
     22   for (size_t i = 0; i < selection.size(); ++i) {
     23     if (i != 0)
     24       result += " ";
     25     result += base::IntToString(selection[i]);
     26   }
     27   return result;
     28 }
     29 
     30 TEST_F(TabStripSelectionModelTest, InitialState) {
     31   TabStripSelectionModel model;
     32   EXPECT_EQ("active=-1 anchor=-1 selection=", StateAsString(model));
     33   EXPECT_TRUE(model.empty());
     34 }
     35 
     36 TEST_F(TabStripSelectionModelTest, SetSelectedIndex) {
     37   TabStripSelectionModel model;
     38   model.SetSelectedIndex(2);
     39   EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
     40   EXPECT_FALSE(model.empty());
     41 }
     42 
     43 TEST_F(TabStripSelectionModelTest, IncrementFrom) {
     44   TabStripSelectionModel model;
     45   model.SetSelectedIndex(1);
     46   model.IncrementFrom(1);
     47   EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
     48 
     49   // Increment from 4. This shouldn't effect the selection as its past the
     50   // end of the selection.
     51   model.IncrementFrom(4);
     52   EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
     53 }
     54 
     55 TEST_F(TabStripSelectionModelTest, DecrementFrom) {
     56   TabStripSelectionModel model;
     57   model.SetSelectedIndex(2);
     58   model.DecrementFrom(0);
     59   EXPECT_EQ("active=1 anchor=1 selection=1", StateAsString(model));
     60 
     61   // Shift down from 1. As the selection as the index being removed, this should
     62   // clear the selection.
     63   model.DecrementFrom(1);
     64   EXPECT_EQ("active=-1 anchor=-1 selection=", StateAsString(model));
     65 
     66   // Reset the selection to 2, and shift down from 4. This shouldn't do
     67   // anything.
     68   model.SetSelectedIndex(2);
     69   model.DecrementFrom(4);
     70   EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
     71 }
     72 
     73 TEST_F(TabStripSelectionModelTest, IsSelected) {
     74   TabStripSelectionModel model;
     75   model.SetSelectedIndex(2);
     76   EXPECT_FALSE(model.IsSelected(0));
     77   EXPECT_TRUE(model.IsSelected(2));
     78 }
     79 
     80 TEST_F(TabStripSelectionModelTest, AddIndexToSelected) {
     81   TabStripSelectionModel model;
     82   model.AddIndexToSelection(2);
     83   EXPECT_EQ("active=-1 anchor=-1 selection=2", StateAsString(model));
     84 
     85   model.AddIndexToSelection(4);
     86   EXPECT_EQ("active=-1 anchor=-1 selection=2 4", StateAsString(model));
     87 }
     88 
     89 TEST_F(TabStripSelectionModelTest, RemoveIndexFromSelection) {
     90   TabStripSelectionModel model;
     91   model.SetSelectedIndex(2);
     92   model.AddIndexToSelection(4);
     93   EXPECT_EQ("active=2 anchor=2 selection=2 4", StateAsString(model));
     94 
     95   model.RemoveIndexFromSelection(4);
     96   EXPECT_EQ("active=2 anchor=2 selection=2", StateAsString(model));
     97 
     98   model.RemoveIndexFromSelection(2);
     99   EXPECT_EQ("active=2 anchor=2 selection=", StateAsString(model));
    100 }
    101 
    102 TEST_F(TabStripSelectionModelTest, Clear) {
    103   TabStripSelectionModel model;
    104   model.SetSelectedIndex(2);
    105 
    106   model.Clear();
    107   EXPECT_EQ("active=-1 anchor=-1 selection=", StateAsString(model));
    108 }
    109 
    110 TEST_F(TabStripSelectionModelTest, MoveToLeft) {
    111   TabStripSelectionModel model;
    112   model.SetSelectedIndex(0);
    113   model.AddIndexToSelection(4);
    114   model.AddIndexToSelection(10);
    115   model.set_anchor(4);
    116   model.set_active(4);
    117   model.Move(4, 0);
    118   EXPECT_EQ("active=0 anchor=0 selection=0 1 10", StateAsString(model));
    119 }
    120 
    121 TEST_F(TabStripSelectionModelTest, MoveToRight) {
    122   TabStripSelectionModel model;
    123   model.SetSelectedIndex(0);
    124   model.AddIndexToSelection(4);
    125   model.AddIndexToSelection(10);
    126   model.set_anchor(0);
    127   model.set_active(0);
    128   model.Move(0, 3);
    129   EXPECT_EQ("active=3 anchor=3 selection=3 4 10", StateAsString(model));
    130 }
    131 
    132 TEST_F(TabStripSelectionModelTest, Copy) {
    133   TabStripSelectionModel model;
    134   model.SetSelectedIndex(0);
    135   model.AddIndexToSelection(4);
    136   model.AddIndexToSelection(10);
    137   EXPECT_EQ("active=0 anchor=0 selection=0 4 10", StateAsString(model));
    138   TabStripSelectionModel model2;
    139   model2.Copy(model);
    140   EXPECT_EQ("active=0 anchor=0 selection=0 4 10", StateAsString(model2));
    141 }
    142 
    143 TEST_F(TabStripSelectionModelTest, AddSelectionFromAnchorTo) {
    144   TabStripSelectionModel model;
    145   model.SetSelectedIndex(2);
    146 
    147   model.AddSelectionFromAnchorTo(4);
    148   EXPECT_EQ("active=4 anchor=2 selection=2 3 4", StateAsString(model));
    149 
    150   model.AddSelectionFromAnchorTo(0);
    151   EXPECT_EQ("active=0 anchor=2 selection=0 1 2 3 4", StateAsString(model));
    152 }
    153