This source file includes following definitions.
- moved_count_
- StateStringAndClear
- ShelfItemAdded
- ShelfItemRemoved
- ShelfItemChanged
- ShelfItemMoved
- ShelfStatusChanged
- AddToResult
- SetUp
- TearDown
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "ash/shelf/shelf_model.h"
#include <set>
#include <string>
#include "ash/ash_switches.h"
#include "ash/shelf/shelf_model_observer.h"
#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
class TestShelfModelObserver : public ShelfModelObserver {
public:
TestShelfModelObserver()
: added_count_(0),
removed_count_(0),
changed_count_(0),
moved_count_(0) {
}
std::string StateStringAndClear() {
std::string result;
AddToResult("added=%d", added_count_, &result);
AddToResult("removed=%d", removed_count_, &result);
AddToResult("changed=%d", changed_count_, &result);
AddToResult("moved=%d", moved_count_, &result);
added_count_ = removed_count_ = changed_count_ = moved_count_ = 0;
return result;
}
virtual void ShelfItemAdded(int index) OVERRIDE {
added_count_++;
}
virtual void ShelfItemRemoved(int index, ShelfID id) OVERRIDE {
removed_count_++;
}
virtual void ShelfItemChanged(int index,
const ShelfItem& old_item) OVERRIDE {
changed_count_++;
}
virtual void ShelfItemMoved(int start_index, int target_index) OVERRIDE {
moved_count_++;
}
virtual void ShelfStatusChanged() OVERRIDE {
}
private:
void AddToResult(const std::string& format, int count, std::string* result) {
if (!count)
return;
if (!result->empty())
*result += " ";
*result += base::StringPrintf(format.c_str(), count);
}
int added_count_;
int removed_count_;
int changed_count_;
int moved_count_;
DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver);
};
}
class ShelfModelTest : public testing::Test {
public:
ShelfModelTest() {}
virtual ~ShelfModelTest() {}
virtual void SetUp() {
model_.reset(new ShelfModel);
observer_.reset(new TestShelfModelObserver);
EXPECT_EQ(0, model_->item_count());
ShelfItem item;
item.type = TYPE_APP_LIST;
model_->Add(item);
EXPECT_EQ(1, model_->item_count());
model_->AddObserver(observer_.get());
}
virtual void TearDown() {
observer_.reset();
model_.reset();
}
scoped_ptr<ShelfModel> model_;
scoped_ptr<TestShelfModelObserver> observer_;
private:
DISALLOW_COPY_AND_ASSIGN(ShelfModelTest);
};
TEST_F(ShelfModelTest, BasicAssertions) {
ShelfItem item;
item.type = TYPE_APP_SHORTCUT;
int index = model_->Add(item);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ("added=1", observer_->StateStringAndClear());
ShelfID original_id = model_->items()[index].id;
item.type = TYPE_PLATFORM_APP;
model_->Set(index, item);
EXPECT_EQ(original_id, model_->items()[index].id);
EXPECT_EQ("changed=1", observer_->StateStringAndClear());
EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[index].type);
model_->RemoveItemAt(index);
EXPECT_EQ(1, model_->item_count());
EXPECT_EQ("removed=1", observer_->StateStringAndClear());
item.type = TYPE_APP_SHORTCUT;
index = model_->Add(item);
observer_->StateStringAndClear();
model_->Set(index, item);
EXPECT_EQ("changed=1", observer_->StateStringAndClear());
EXPECT_EQ(TYPE_APP_SHORTCUT, model_->items()[index].type);
item.type = TYPE_APP_SHORTCUT;
model_->Add(item);
observer_->StateStringAndClear();
model_->Move(1, 0);
EXPECT_EQ("moved=1", observer_->StateStringAndClear());
model_->Move(0, 1);
EXPECT_EQ("moved=1", observer_->StateStringAndClear());
std::set<ShelfID> ids;
for (int i = 0; i < model_->item_count(); ++i)
ids.insert(model_->items()[i].id);
EXPECT_EQ(model_->item_count(), static_cast<int>(ids.size()));
}
TEST_F(ShelfModelTest, AddIndices) {
ShelfItem browser_shortcut;
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
int browser_shortcut_index = model_->Add(browser_shortcut);
EXPECT_EQ(1, browser_shortcut_index);
ShelfItem item;
item.type = TYPE_PLATFORM_APP;
int platform_app_index1 = model_->Add(item);
EXPECT_EQ(2, platform_app_index1);
int platform_app_index2 = model_->Add(item);
EXPECT_EQ(3, platform_app_index2);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index1 = model_->Add(item);
EXPECT_EQ(2, app_shortcut_index1);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index2 = model_->Add(item);
EXPECT_EQ(3, app_shortcut_index2);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index3 = model_->AddAt(1, item);
EXPECT_EQ(1, app_shortcut_index3);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index4 = model_->AddAt(6, item);
EXPECT_EQ(5, app_shortcut_index4);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index5 = model_->AddAt(3, item);
EXPECT_EQ(3, app_shortcut_index5);
EXPECT_EQ(model_->item_count(), model_->FirstPanelIndex());
item.type = TYPE_PLATFORM_APP;
int platform_app_index3 = model_->AddAt(3, item);
EXPECT_EQ(7, platform_app_index3);
item.type = TYPE_APP_PANEL;
int app_panel_index1 = model_->AddAt(2, item);
EXPECT_EQ(10, app_panel_index1);
item.type = TYPE_PLATFORM_APP;
int platform_app_index4 = model_->AddAt(11, item);
EXPECT_EQ(10, platform_app_index4);
item.type = TYPE_APP_PANEL;
int app_panel_index2 = model_->AddAt(12, item);
EXPECT_EQ(12, app_panel_index2);
item.type = TYPE_PLATFORM_APP;
int platform_app_index5 = model_->AddAt(7, item);
EXPECT_EQ(7, platform_app_index5);
item.type = TYPE_APP_PANEL;
int app_panel_index3 = model_->AddAt(13, item);
EXPECT_EQ(13, app_panel_index3);
EXPECT_EQ(12, model_->FirstPanelIndex());
EXPECT_EQ(TYPE_BROWSER_SHORTCUT, model_->items()[2].type);
EXPECT_EQ(TYPE_APP_LIST, model_->items()[0].type);
}
TEST_F(ShelfModelTest, FirstRunningAppIndexUsingWindowedAppFirst) {
ShelfItem item;
item.type = TYPE_BROWSER_SHORTCUT;
EXPECT_EQ(1, model_->Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
item.type = TYPE_APP_PANEL;
EXPECT_EQ(2, model_->Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
item.type = TYPE_APP_SHORTCUT;
EXPECT_EQ(2, model_->Add(item));
EXPECT_EQ(3, model_->FirstRunningAppIndex());
item.type = TYPE_WINDOWED_APP;
int running_app_index = model_->Add(item);
EXPECT_EQ(3, running_app_index);
EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex());
item.type = TYPE_PLATFORM_APP;
EXPECT_EQ(running_app_index + 1, model_->Add(item));
EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex());
}
TEST_F(ShelfModelTest, FirstRunningAppIndexUsingPlatformAppFirst) {
ShelfItem item;
item.type = TYPE_BROWSER_SHORTCUT;
EXPECT_EQ(1, model_->Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
item.type = TYPE_APP_PANEL;
EXPECT_EQ(2, model_->Add(item));
EXPECT_EQ(2, model_->FirstRunningAppIndex());
item.type = TYPE_APP_SHORTCUT;
EXPECT_EQ(2, model_->Add(item));
EXPECT_EQ(3, model_->FirstRunningAppIndex());
item.type = TYPE_PLATFORM_APP;
int running_app_index = model_->Add(item);
EXPECT_EQ(3, running_app_index);
EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex());
item.type = TYPE_WINDOWED_APP;
EXPECT_EQ(running_app_index + 1, model_->Add(item));
EXPECT_EQ(running_app_index, model_->FirstRunningAppIndex());
}
TEST_F(ShelfModelTest, AddIndicesForLegacyShelfLayout) {
CommandLine::ForCurrentProcess()->AppendSwitch(
ash::switches::kAshDisableAlternateShelfLayout);
ShelfItem browser_shortcut;
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
int browser_shortcut_index = model_->Add(browser_shortcut);
EXPECT_EQ(0, browser_shortcut_index);
ShelfItem item;
item.type = TYPE_PLATFORM_APP;
int platform_app_index1 = model_->Add(item);
EXPECT_EQ(1, platform_app_index1);
int platform_app_index2 = model_->Add(item);
EXPECT_EQ(2, platform_app_index2);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index1 = model_->Add(item);
EXPECT_EQ(1, app_shortcut_index1);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index2 = model_->Add(item);
EXPECT_EQ(2, app_shortcut_index2);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index3 = model_->AddAt(0, item);
EXPECT_EQ(0, app_shortcut_index3);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index4 = model_->AddAt(5, item);
EXPECT_EQ(4, app_shortcut_index4);
item.type = TYPE_APP_SHORTCUT;
int app_shortcut_index5 = model_->AddAt(2, item);
EXPECT_EQ(2, app_shortcut_index5);
EXPECT_EQ(model_->item_count(), model_->FirstPanelIndex());
item.type = TYPE_PLATFORM_APP;
int platform_app_index3 = model_->AddAt(2, item);
EXPECT_EQ(6, platform_app_index3);
item.type = TYPE_APP_PANEL;
int app_panel_index1 = model_->AddAt(2, item);
EXPECT_EQ(10, app_panel_index1);
item.type = TYPE_PLATFORM_APP;
int platform_app_index4 = model_->AddAt(11, item);
EXPECT_EQ(9, platform_app_index4);
item.type = TYPE_APP_PANEL;
int app_panel_index2 = model_->AddAt(12, item);
EXPECT_EQ(12, app_panel_index2);
item.type = TYPE_PLATFORM_APP;
int platform_app_index5 = model_->AddAt(7, item);
EXPECT_EQ(7, platform_app_index5);
item.type = TYPE_APP_PANEL;
int app_panel_index3 = model_->AddAt(13, item);
EXPECT_EQ(13, app_panel_index3);
EXPECT_EQ(12, model_->FirstPanelIndex());
EXPECT_EQ(TYPE_BROWSER_SHORTCUT, model_->items()[1].type);
EXPECT_EQ(TYPE_APP_LIST, model_->items()[model_->FirstPanelIndex() - 1].type);
}
TEST_F(ShelfModelTest, ShelfIDTests) {
ShelfID id = model_->next_id();
EXPECT_EQ(model_->next_id(), id);
EXPECT_EQ(model_->reserve_external_id(), id);
EXPECT_EQ(1, model_->item_count());
ShelfID id2 = model_->next_id();
EXPECT_NE(id2, id);
ShelfItem item;
item.type = TYPE_PLATFORM_APP;
model_->Add(item);
EXPECT_NE(model_->next_id(), id2);
}
TEST_F(ShelfModelTest, CorrectMoveItemsWhenStateChange) {
ShelfItem browser_shortcut;
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
int browser_shortcut_index = model_->Add(browser_shortcut);
EXPECT_EQ(TYPE_APP_LIST, model_->items()[0].type);
EXPECT_EQ(1, browser_shortcut_index);
ShelfItem item;
item.type = TYPE_APP_SHORTCUT;
int app1_index = model_->Add(item);
EXPECT_EQ(2, app1_index);
int app2_index = model_->Add(item);
EXPECT_EQ(3, app2_index);
int app3_index = model_->Add(item);
EXPECT_EQ(4, app3_index);
item.type = TYPE_PLATFORM_APP;
model_->Set(app2_index, item);
EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[4].type);
}
TEST_F(ShelfModelTest, CorrectMoveItemsWhenStateChangeForLegacyShelfLayout) {
CommandLine::ForCurrentProcess()->AppendSwitch(
ash::switches::kAshDisableAlternateShelfLayout);
ShelfItem browser_shortcut;
browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
int browser_shortcut_index = model_->Add(browser_shortcut);
EXPECT_EQ(0, browser_shortcut_index);
EXPECT_EQ(TYPE_APP_LIST, model_->items()[1].type);
ShelfItem item;
item.type = TYPE_APP_SHORTCUT;
int app1_index = model_->Add(item);
EXPECT_EQ(1, app1_index);
int app2_index = model_->Add(item);
EXPECT_EQ(2, app2_index);
int app3_index = model_->Add(item);
EXPECT_EQ(3, app3_index);
item.type = TYPE_PLATFORM_APP;
model_->Set(app2_index, item);
EXPECT_EQ(TYPE_PLATFORM_APP, model_->items()[3].type);
}
}