| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/app_list/apps_grid_view.h" | 5 #include "ui/app_list/apps_grid_view.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stringprintf.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/app_list/app_list_item_model.h" |
| 13 #include "ui/app_list/app_list_model.h" |
| 8 #include "ui/app_list/app_list_item_view.h" | 14 #include "ui/app_list/app_list_item_view.h" |
| 15 #include "ui/app_list/pagination_model.h" |
| 16 |
| 17 namespace app_list { |
| 18 namespace test { |
| 9 | 19 |
| 10 namespace { | 20 namespace { |
| 11 | 21 |
| 12 struct ModelViewCalculateLayoutCase { | 22 const int kIconDimension = 48; |
| 13 gfx::Size screen_size; | 23 const int kCols = 2; |
| 14 int num_of_tiles; | 24 const int kRows = 2; |
| 15 gfx::Size expected_icon_size; | 25 |
| 16 int expected_rows; | 26 const int kWidth = 320; |
| 17 int expected_cols; | 27 const int kHeight = 240; |
| 18 }; | |
| 19 | 28 |
| 20 } // namespace | 29 } // namespace |
| 21 | 30 |
| 22 namespace app_list { | 31 class AppsGridViewTest : public testing::Test { |
| 32 public: |
| 33 AppsGridViewTest() {} |
| 34 virtual ~AppsGridViewTest() {} |
| 23 | 35 |
| 24 TEST(AppsGridViewTest, ModelViewCalculateLayout) { | 36 // testing::Test overrides: |
| 25 // kMinTitleWidth is the average width of 15 chars of default size 12 font in | 37 virtual void SetUp() OVERRIDE { |
| 26 // chromeos. Override here to avoid flakiness from different default font on | 38 apps_model_.reset(new AppListModel::Apps); |
| 27 // bots. | 39 pagination_model_.reset(new PaginationModel); |
| 28 const int kMinTitleWidth = 90; | |
| 29 AppListItemView::SetMinTitleWidth(kMinTitleWidth); | |
| 30 | 40 |
| 31 const int kLauncherHeight = 50; | 41 apps_grid_view_.reset(new AppsGridView(NULL, pagination_model_.get())); |
| 32 const ModelViewCalculateLayoutCase kCases[] = { | 42 apps_grid_view_->SetLayout(kIconDimension, kCols, kRows); |
| 33 { gfx::Size(1024, 768), 4, gfx::Size(128, 128), 2, 3 }, | 43 apps_grid_view_->SetBoundsRect(gfx::Rect(gfx::Size(kWidth, kHeight))); |
| 34 { gfx::Size(1024, 768), 29, gfx::Size(64, 64), 5, 6 }, | 44 apps_grid_view_->SetModel(apps_model_.get()); |
| 35 { gfx::Size(1024, 768), 40, gfx::Size(48, 48), 5, 8 }, | 45 } |
| 36 { gfx::Size(1024, 768), 48, gfx::Size(48, 48), 6, 8 }, | 46 virtual void TearDown() OVERRIDE { |
| 47 apps_grid_view_.reset(); // Release apps grid view before models. |
| 48 } |
| 37 | 49 |
| 38 { gfx::Size(1280, 1024), 4, gfx::Size(128, 128), 2, 3 }, | 50 protected: |
| 39 { gfx::Size(1280, 1024), 29, gfx::Size(64, 64), 5, 7 }, | 51 void PopulateApps(int n) { |
| 40 { gfx::Size(1280, 1024), 50, gfx::Size(64, 64), 7, 8 }, | 52 for (int i = 0; i < n; ++i) { |
| 41 { gfx::Size(1280, 1024), 70, gfx::Size(48, 48), 7, 10 }, | 53 std::string title = base::StringPrintf("Item %d", i); |
| 42 { gfx::Size(1280, 1024), 99, gfx::Size(48, 48), 9, 11 }, | 54 apps_model_->Add(CreateItem(title)); |
| 55 } |
| 56 } |
| 43 | 57 |
| 44 { gfx::Size(1600, 900), 4, gfx::Size(128, 128), 2, 3 }, | 58 AppListItemModel* CreateItem(const std::string& title) { |
| 45 { gfx::Size(1600, 900), 29, gfx::Size(64, 64), 4, 8 }, | 59 AppListItemModel* item = new AppListItemModel; |
| 60 item->SetTitle(title); |
| 61 return item; |
| 62 } |
| 46 | 63 |
| 47 // Not able to fit into screen case. | 64 void HighlightItemAt(int index) { |
| 48 { gfx::Size(1024, 768), 50, gfx::Size(32, 32), 6, 9 }, | 65 AppListItemModel* item = apps_model_->GetItemAt(index); |
| 49 { gfx::Size(1280, 1024), 100, gfx::Size(32, 32), 10, 11 }, | 66 item->SetHighlighted(true); |
| 50 }; | 67 } |
| 51 | 68 |
| 52 for (size_t i = 0; i < arraysize(kCases); ++i) { | 69 scoped_ptr<AppListModel::Apps> apps_model_; |
| 53 gfx::Size icon_size; | 70 scoped_ptr<PaginationModel> pagination_model_; |
| 54 int rows = 0; | 71 scoped_ptr<AppsGridView> apps_grid_view_; |
| 55 int cols = 0; | 72 |
| 56 gfx::Size content_size(kCases[i].screen_size.width(), | 73 private: |
| 57 kCases[i].screen_size.height() - kLauncherHeight); | 74 DISALLOW_COPY_AND_ASSIGN(AppsGridViewTest); |
| 58 AppsGridView::CalculateLayout(content_size, | 75 }; |
| 59 kCases[i].num_of_tiles, | 76 |
| 60 &icon_size, | 77 TEST_F(AppsGridViewTest, CreatePage) { |
| 61 &rows, | 78 // Fully populates a page. |
| 62 &cols); | 79 const int kPages = 1; |
| 63 EXPECT_EQ(kCases[i].expected_icon_size, icon_size) << "i=" << i; | 80 PopulateApps(kPages * apps_grid_view_->tiles_per_page()); |
| 64 EXPECT_EQ(kCases[i].expected_rows, rows) << "i=" << i; | 81 EXPECT_EQ(kPages, pagination_model_->total_pages()); |
| 65 EXPECT_EQ(kCases[i].expected_cols, cols) << "i=" << i; | 82 |
| 66 } | 83 // Adds one more and gets a new page created. |
| 84 apps_model_->Add(CreateItem(std::string("Extra"))); |
| 85 EXPECT_EQ(kPages + 1, pagination_model_->total_pages()); |
| 67 } | 86 } |
| 68 | 87 |
| 88 TEST_F(AppsGridViewTest, EnsureHighlightedVisible) { |
| 89 const int kPages = 3; |
| 90 PopulateApps(kPages * apps_grid_view_->tiles_per_page()); |
| 91 EXPECT_EQ(kPages, pagination_model_->total_pages()); |
| 92 EXPECT_EQ(0, pagination_model_->selected_page()); |
| 93 |
| 94 // Highlight first one and last one one first page and first page should be |
| 95 // selected. |
| 96 HighlightItemAt(0); |
| 97 EXPECT_EQ(0, pagination_model_->selected_page()); |
| 98 HighlightItemAt(apps_grid_view_->tiles_per_page() - 1); |
| 99 EXPECT_EQ(0, pagination_model_->selected_page()); |
| 100 |
| 101 // Highlight first one on 2nd page and 2nd page should be selected. |
| 102 HighlightItemAt(apps_grid_view_->tiles_per_page() + 1); |
| 103 EXPECT_EQ(1, pagination_model_->selected_page()); |
| 104 |
| 105 // Highlight last one in the model and last page should be selected. |
| 106 HighlightItemAt(apps_model_->item_count() - 1); |
| 107 EXPECT_EQ(kPages - 1, pagination_model_->selected_page()); |
| 108 } |
| 109 |
| 110 } // namespace test |
| 69 } // namespace app_list | 111 } // namespace app_list |
| OLD | NEW |