| 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> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 88 |
| 89 } // namespace | 89 } // namespace |
| 90 | 90 |
| 91 class AppsGridViewTest : public testing::Test { | 91 class AppsGridViewTest : public testing::Test { |
| 92 public: | 92 public: |
| 93 AppsGridViewTest() {} | 93 AppsGridViewTest() {} |
| 94 virtual ~AppsGridViewTest() {} | 94 virtual ~AppsGridViewTest() {} |
| 95 | 95 |
| 96 // testing::Test overrides: | 96 // testing::Test overrides: |
| 97 virtual void SetUp() OVERRIDE { | 97 virtual void SetUp() OVERRIDE { |
| 98 apps_model_.reset(new AppListModel::Apps); | 98 model_.reset(new AppListModel); |
| 99 pagination_model_.reset(new PaginationModel); | 99 pagination_model_.reset(new PaginationModel); |
| 100 | 100 |
| 101 apps_grid_view_.reset(new AppsGridView(NULL, pagination_model_.get())); | 101 apps_grid_view_.reset(new AppsGridView(NULL, pagination_model_.get())); |
| 102 apps_grid_view_->SetLayout(kIconDimension, kCols, kRows); | 102 apps_grid_view_->SetLayout(kIconDimension, kCols, kRows); |
| 103 apps_grid_view_->SetBoundsRect(gfx::Rect(gfx::Size(kWidth, kHeight))); | 103 apps_grid_view_->SetBoundsRect(gfx::Rect(gfx::Size(kWidth, kHeight))); |
| 104 apps_grid_view_->SetModel(apps_model_.get()); | 104 apps_grid_view_->SetModel(model_.get()); |
| 105 | 105 |
| 106 test_api_.reset(new AppsGridViewTestApi(apps_grid_view_.get())); | 106 test_api_.reset(new AppsGridViewTestApi(apps_grid_view_.get())); |
| 107 } | 107 } |
| 108 virtual void TearDown() OVERRIDE { | 108 virtual void TearDown() OVERRIDE { |
| 109 apps_grid_view_.reset(); // Release apps grid view before models. | 109 apps_grid_view_.reset(); // Release apps grid view before models. |
| 110 } | 110 } |
| 111 | 111 |
| 112 protected: | 112 protected: |
| 113 void PopulateApps(int n) { | 113 void PopulateApps(int n) { |
| 114 for (int i = 0; i < n; ++i) { | 114 for (int i = 0; i < n; ++i) { |
| 115 std::string title = base::StringPrintf("Item %d", i); | 115 std::string title = base::StringPrintf("Item %d", i); |
| 116 apps_model_->Add(CreateItem(title)); | 116 model_->apps()->Add(CreateItem(title)); |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Get a string of all apps in |model| joined with ','. | 120 // Get a string of all apps in |model| joined with ','. |
| 121 std::string GetModelContent() { | 121 std::string GetModelContent() { |
| 122 std::string content; | 122 std::string content; |
| 123 for (size_t i = 0; i < apps_model_->item_count(); ++i) { | 123 for (size_t i = 0; i < model_->apps()->item_count(); ++i) { |
| 124 if (i > 0) | 124 if (i > 0) |
| 125 content += ','; | 125 content += ','; |
| 126 content += apps_model_->GetItemAt(i)->title(); | 126 content += model_->apps()->GetItemAt(i)->title(); |
| 127 } | 127 } |
| 128 return content; | 128 return content; |
| 129 } | 129 } |
| 130 | 130 |
| 131 AppListItemModel* CreateItem(const std::string& title) { | 131 AppListItemModel* CreateItem(const std::string& title) { |
| 132 AppListItemModel* item = new AppListItemModel; | 132 AppListItemModel* item = new AppListItemModel; |
| 133 item->SetTitle(title); | 133 item->SetTitle(title); |
| 134 return item; | 134 return item; |
| 135 } | 135 } |
| 136 | 136 |
| 137 void HighlightItemAt(int index) { | 137 void HighlightItemAt(int index) { |
| 138 AppListItemModel* item = apps_model_->GetItemAt(index); | 138 AppListItemModel* item = model_->apps()->GetItemAt(index); |
| 139 item->SetHighlighted(true); | 139 item->SetHighlighted(true); |
| 140 } | 140 } |
| 141 | 141 |
| 142 AppListItemView* GetItemViewAt(int index) { | 142 AppListItemView* GetItemViewAt(int index) { |
| 143 return static_cast<AppListItemView*>( | 143 return static_cast<AppListItemView*>( |
| 144 test_api_->GetViewAtModelIndex(index)); | 144 test_api_->GetViewAtModelIndex(index)); |
| 145 } | 145 } |
| 146 | 146 |
| 147 AppListItemView* GetItemViewForPoint(const gfx::Point& point) { | 147 AppListItemView* GetItemViewForPoint(const gfx::Point& point) { |
| 148 for (size_t i = 0; i < apps_model_->item_count(); ++i) { | 148 for (size_t i = 0; i < model_->apps()->item_count(); ++i) { |
| 149 AppListItemView* view = GetItemViewAt(i); | 149 AppListItemView* view = GetItemViewAt(i); |
| 150 if (view->bounds().Contains(point)) | 150 if (view->bounds().Contains(point)) |
| 151 return view; | 151 return view; |
| 152 } | 152 } |
| 153 return NULL; | 153 return NULL; |
| 154 } | 154 } |
| 155 | 155 |
| 156 gfx::Rect GetItemTileRectAt(int row, int col) { | 156 gfx::Rect GetItemTileRectAt(int row, int col) { |
| 157 DCHECK_GT(apps_model_->item_count(), 0u); | 157 DCHECK_GT(model_->apps()->item_count(), 0u); |
| 158 | 158 |
| 159 gfx::Insets insets(apps_grid_view_->GetInsets()); | 159 gfx::Insets insets(apps_grid_view_->GetInsets()); |
| 160 gfx::Rect rect(gfx::Point(insets.left(), insets.top()), | 160 gfx::Rect rect(gfx::Point(insets.left(), insets.top()), |
| 161 GetItemViewAt(0)->bounds().size()); | 161 GetItemViewAt(0)->bounds().size()); |
| 162 rect.Offset(col * rect.width(), row * rect.height()); | 162 rect.Offset(col * rect.width(), row * rect.height()); |
| 163 return rect; | 163 return rect; |
| 164 } | 164 } |
| 165 | 165 |
| 166 // Points are in |apps_grid_view_|'s coordinates. | 166 // Points are in |apps_grid_view_|'s coordinates. |
| 167 void SimulateDrag(AppsGridView::Pointer pointer, | 167 void SimulateDrag(AppsGridView::Pointer pointer, |
| 168 const gfx::Point& from, | 168 const gfx::Point& from, |
| 169 const gfx::Point& to) { | 169 const gfx::Point& to) { |
| 170 AppListItemView* view = GetItemViewForPoint(from); | 170 AppListItemView* view = GetItemViewForPoint(from); |
| 171 DCHECK(view); | 171 DCHECK(view); |
| 172 | 172 |
| 173 gfx::Point translated_from = from.Subtract(view->bounds().origin()); | 173 gfx::Point translated_from = gfx::PointAtOffsetFromOrigin( |
| 174 gfx::Point translated_to = to.Subtract(view->bounds().origin()); | 174 from - view->bounds().origin()); |
| 175 gfx::Point translated_to = gfx::PointAtOffsetFromOrigin( |
| 176 to - view->bounds().origin()); |
| 175 | 177 |
| 176 ui::MouseEvent pressed_event(ui::ET_MOUSE_PRESSED, | 178 ui::MouseEvent pressed_event(ui::ET_MOUSE_PRESSED, |
| 177 translated_from, translated_from, 0); | 179 translated_from, translated_from, 0); |
| 178 apps_grid_view_->InitiateDrag(view, pointer, pressed_event); | 180 apps_grid_view_->InitiateDrag(view, pointer, pressed_event); |
| 179 | 181 |
| 180 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED, | 182 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED, |
| 181 translated_to, translated_to, 0); | 183 translated_to, translated_to, 0); |
| 182 apps_grid_view_->UpdateDrag(view, pointer, drag_event); | 184 apps_grid_view_->UpdateDrag(view, pointer, drag_event); |
| 183 } | 185 } |
| 184 | 186 |
| 185 scoped_ptr<AppListModel::Apps> apps_model_; | 187 scoped_ptr<AppListModel> model_; |
| 186 scoped_ptr<PaginationModel> pagination_model_; | 188 scoped_ptr<PaginationModel> pagination_model_; |
| 187 scoped_ptr<AppsGridView> apps_grid_view_; | 189 scoped_ptr<AppsGridView> apps_grid_view_; |
| 188 scoped_ptr<AppsGridViewTestApi> test_api_; | 190 scoped_ptr<AppsGridViewTestApi> test_api_; |
| 189 | 191 |
| 190 MessageLoopForUI message_loop_; | 192 MessageLoopForUI message_loop_; |
| 191 | 193 |
| 192 private: | 194 private: |
| 193 DISALLOW_COPY_AND_ASSIGN(AppsGridViewTest); | 195 DISALLOW_COPY_AND_ASSIGN(AppsGridViewTest); |
| 194 }; | 196 }; |
| 195 | 197 |
| 196 TEST_F(AppsGridViewTest, CreatePage) { | 198 TEST_F(AppsGridViewTest, CreatePage) { |
| 197 // Fully populates a page. | 199 // Fully populates a page. |
| 198 const int kPages = 1; | 200 const int kPages = 1; |
| 199 PopulateApps(kPages * kTilesPerPage); | 201 PopulateApps(kPages * kTilesPerPage); |
| 200 EXPECT_EQ(kPages, pagination_model_->total_pages()); | 202 EXPECT_EQ(kPages, pagination_model_->total_pages()); |
| 201 | 203 |
| 202 // Adds one more and gets a new page created. | 204 // Adds one more and gets a new page created. |
| 203 apps_model_->Add(CreateItem(std::string("Extra"))); | 205 model_->apps()->Add(CreateItem(std::string("Extra"))); |
| 204 EXPECT_EQ(kPages + 1, pagination_model_->total_pages()); | 206 EXPECT_EQ(kPages + 1, pagination_model_->total_pages()); |
| 205 } | 207 } |
| 206 | 208 |
| 207 TEST_F(AppsGridViewTest, EnsureHighlightedVisible) { | 209 TEST_F(AppsGridViewTest, EnsureHighlightedVisible) { |
| 208 const int kPages = 3; | 210 const int kPages = 3; |
| 209 PopulateApps(kPages * kTilesPerPage); | 211 PopulateApps(kPages * kTilesPerPage); |
| 210 EXPECT_EQ(kPages, pagination_model_->total_pages()); | 212 EXPECT_EQ(kPages, pagination_model_->total_pages()); |
| 211 EXPECT_EQ(0, pagination_model_->selected_page()); | 213 EXPECT_EQ(0, pagination_model_->selected_page()); |
| 212 | 214 |
| 213 // Highlight first one and last one one first page and first page should be | 215 // Highlight first one and last one one first page and first page should be |
| 214 // selected. | 216 // selected. |
| 215 HighlightItemAt(0); | 217 HighlightItemAt(0); |
| 216 EXPECT_EQ(0, pagination_model_->selected_page()); | 218 EXPECT_EQ(0, pagination_model_->selected_page()); |
| 217 HighlightItemAt(kTilesPerPage - 1); | 219 HighlightItemAt(kTilesPerPage - 1); |
| 218 EXPECT_EQ(0, pagination_model_->selected_page()); | 220 EXPECT_EQ(0, pagination_model_->selected_page()); |
| 219 | 221 |
| 220 // Highlight first one on 2nd page and 2nd page should be selected. | 222 // Highlight first one on 2nd page and 2nd page should be selected. |
| 221 HighlightItemAt(kTilesPerPage + 1); | 223 HighlightItemAt(kTilesPerPage + 1); |
| 222 EXPECT_EQ(1, pagination_model_->selected_page()); | 224 EXPECT_EQ(1, pagination_model_->selected_page()); |
| 223 | 225 |
| 224 // Highlight last one in the model and last page should be selected. | 226 // Highlight last one in the model and last page should be selected. |
| 225 HighlightItemAt(apps_model_->item_count() - 1); | 227 HighlightItemAt(model_->apps()->item_count() - 1); |
| 226 EXPECT_EQ(kPages - 1, pagination_model_->selected_page()); | 228 EXPECT_EQ(kPages - 1, pagination_model_->selected_page()); |
| 227 } | 229 } |
| 228 | 230 |
| 229 TEST_F(AppsGridViewTest, RemoveSelectedLastApp) { | 231 TEST_F(AppsGridViewTest, RemoveSelectedLastApp) { |
| 230 const int kTotalItems = 2; | 232 const int kTotalItems = 2; |
| 231 const int kLastItemIndex = kTotalItems - 1; | 233 const int kLastItemIndex = kTotalItems - 1; |
| 232 | 234 |
| 233 PopulateApps(kTotalItems); | 235 PopulateApps(kTotalItems); |
| 234 | 236 |
| 235 AppListItemView* last_view = GetItemViewAt(kLastItemIndex); | 237 AppListItemView* last_view = GetItemViewAt(kLastItemIndex); |
| 236 apps_grid_view_->SetSelectedView(last_view); | 238 apps_grid_view_->SetSelectedView(last_view); |
| 237 apps_model_->DeleteAt(kLastItemIndex); | 239 model_->apps()->DeleteAt(kLastItemIndex); |
| 238 | 240 |
| 239 EXPECT_FALSE(apps_grid_view_->IsSelectedView(last_view)); | 241 EXPECT_FALSE(apps_grid_view_->IsSelectedView(last_view)); |
| 240 | 242 |
| 241 // No crash happens. | 243 // No crash happens. |
| 242 AppListItemView* view = GetItemViewAt(0); | 244 AppListItemView* view = GetItemViewAt(0); |
| 243 apps_grid_view_->SetSelectedView(view); | 245 apps_grid_view_->SetSelectedView(view); |
| 244 EXPECT_TRUE(apps_grid_view_->IsSelectedView(view)); | 246 EXPECT_TRUE(apps_grid_view_->IsSelectedView(view)); |
| 245 } | 247 } |
| 246 | 248 |
| 247 TEST_F(AppsGridViewTest, MouseDrag) { | 249 TEST_F(AppsGridViewTest, MouseDrag) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 262 | 264 |
| 263 // Canceling drag should keep existing order. | 265 // Canceling drag should keep existing order. |
| 264 SimulateDrag(AppsGridView::MOUSE, from, to); | 266 SimulateDrag(AppsGridView::MOUSE, from, to); |
| 265 apps_grid_view_->EndDrag(true); | 267 apps_grid_view_->EndDrag(true); |
| 266 EXPECT_EQ(std::string("Item 1,Item 0,Item 2,Item 3"), | 268 EXPECT_EQ(std::string("Item 1,Item 0,Item 2,Item 3"), |
| 267 GetModelContent()); | 269 GetModelContent()); |
| 268 test_api_->LayoutToIdealBounds(); | 270 test_api_->LayoutToIdealBounds(); |
| 269 | 271 |
| 270 // Deleting an item keeps remaining intact. | 272 // Deleting an item keeps remaining intact. |
| 271 SimulateDrag(AppsGridView::MOUSE, from, to); | 273 SimulateDrag(AppsGridView::MOUSE, from, to); |
| 272 apps_model_->DeleteAt(1); | 274 model_->apps()->DeleteAt(1); |
| 273 apps_grid_view_->EndDrag(false); | 275 apps_grid_view_->EndDrag(false); |
| 274 EXPECT_EQ(std::string("Item 1,Item 2,Item 3"), | 276 EXPECT_EQ(std::string("Item 1,Item 2,Item 3"), |
| 275 GetModelContent()); | 277 GetModelContent()); |
| 276 test_api_->LayoutToIdealBounds(); | 278 test_api_->LayoutToIdealBounds(); |
| 277 | 279 |
| 278 // Adding a launcher item cancels the drag and respects the order. | 280 // Adding a launcher item cancels the drag and respects the order. |
| 279 SimulateDrag(AppsGridView::MOUSE, from, to); | 281 SimulateDrag(AppsGridView::MOUSE, from, to); |
| 280 apps_model_->Add(CreateItem(std::string("Extra"))); | 282 model_->apps()->Add(CreateItem(std::string("Extra"))); |
| 281 apps_grid_view_->EndDrag(false); | 283 apps_grid_view_->EndDrag(false); |
| 282 EXPECT_EQ(std::string("Item 1,Item 2,Item 3,Extra"), | 284 EXPECT_EQ(std::string("Item 1,Item 2,Item 3,Extra"), |
| 283 GetModelContent()); | 285 GetModelContent()); |
| 284 test_api_->LayoutToIdealBounds(); | 286 test_api_->LayoutToIdealBounds(); |
| 285 } | 287 } |
| 286 | 288 |
| 287 TEST_F(AppsGridViewTest, MouseDragFlipPage) { | 289 TEST_F(AppsGridViewTest, MouseDragFlipPage) { |
| 288 test_api_->SetPageFlipDelay(10); | 290 test_api_->SetPageFlipDelay(10); |
| 289 pagination_model_->SetTransitionDuration(10); | 291 pagination_model_->SetTransitionDuration(10); |
| 290 | 292 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 SimulateDrag(AppsGridView::MOUSE, mouse_from, mouse_to); | 361 SimulateDrag(AppsGridView::MOUSE, mouse_from, mouse_to); |
| 360 // Finishes the drag and touch drag wins. | 362 // Finishes the drag and touch drag wins. |
| 361 apps_grid_view_->EndDrag(false); | 363 apps_grid_view_->EndDrag(false); |
| 362 EXPECT_EQ(std::string("Item 1,Item 0,Item 3,Item 2"), | 364 EXPECT_EQ(std::string("Item 1,Item 0,Item 3,Item 2"), |
| 363 GetModelContent()); | 365 GetModelContent()); |
| 364 test_api_->LayoutToIdealBounds(); | 366 test_api_->LayoutToIdealBounds(); |
| 365 } | 367 } |
| 366 | 368 |
| 367 } // namespace test | 369 } // namespace test |
| 368 } // namespace app_list | 370 } // namespace app_list |
| OLD | NEW |