Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: chrome/browser/ui/ash/launcher/chrome_launcher_controller_unittest.cc

Issue 23606016: Refactor LauncherItemController and LauncherItemDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix for LauncherTest and observing LauncherModel in DelegateManager Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" 5 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "ash/ash_switches.h" 11 #include "ash/ash_switches.h"
12 #include "ash/launcher/launcher_item_delegate_manager.h"
12 #include "ash/launcher/launcher_model.h" 13 #include "ash/launcher/launcher_model.h"
13 #include "ash/launcher/launcher_model_observer.h" 14 #include "ash/launcher/launcher_model_observer.h"
15 #include "ash/shell.h"
14 #include "base/command_line.h" 16 #include "base/command_line.h"
15 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
16 #include "base/files/file_path.h" 18 #include "base/files/file_path.h"
17 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop/message_loop.h" 20 #include "base/message_loop/message_loop.h"
19 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
20 #include "base/values.h" 22 #include "base/values.h"
21 #include "chrome/browser/extensions/extension_service.h" 23 #include "chrome/browser/extensions/extension_service.h"
22 #include "chrome/browser/extensions/test_extension_system.h" 24 #include "chrome/browser/extensions/test_extension_system.h"
23 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h" 25 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
26 #include "chrome/browser/ui/ash/launcher/launcher_application_menu_item_model.h"
24 #include "chrome/browser/ui/ash/launcher/launcher_item_controller.h" 27 #include "chrome/browser/ui/ash/launcher/launcher_item_controller.h"
25 #include "chrome/browser/ui/ash/launcher/shell_window_launcher_item_controller.h " 28 #include "chrome/browser/ui/ash/launcher/shell_window_launcher_item_controller.h "
26 #include "chrome/browser/ui/browser.h" 29 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_commands.h" 30 #include "chrome/browser/ui/browser_commands.h"
28 #include "chrome/browser/ui/browser_finder.h" 31 #include "chrome/browser/ui/browser_finder.h"
29 #include "chrome/browser/ui/browser_list.h" 32 #include "chrome/browser/ui/browser_list.h"
30 #include "chrome/browser/ui/host_desktop.h" 33 #include "chrome/browser/ui/host_desktop.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h" 34 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/extensions/extension.h" 35 #include "chrome/common/extensions/extension.h"
33 #include "chrome/common/extensions/extension_constants.h" 36 #include "chrome/common/extensions/extension_constants.h"
(...skipping 1058 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 for (size_t i = 0; i < expected_items; i++) { 1095 for (size_t i = 0; i < expected_items; i++) {
1093 EXPECT_EQ(title[i], items[1 + i]->title()); 1096 EXPECT_EQ(title[i], items[1 + i]->title());
1094 // Check that the first real item has a leading separator. 1097 // Check that the first real item has a leading separator.
1095 if (i == 1) 1098 if (i == 1)
1096 EXPECT_TRUE(items[i]->HasLeadingSeparator()); 1099 EXPECT_TRUE(items[i]->HasLeadingSeparator());
1097 else 1100 else
1098 EXPECT_FALSE(items[i]->HasLeadingSeparator()); 1101 EXPECT_FALSE(items[i]->HasLeadingSeparator());
1099 } 1102 }
1100 1103
1101 scoped_ptr<ash::LauncherMenuModel> menu( 1104 scoped_ptr<ash::LauncherMenuModel> menu(
1102 controller->CreateApplicationMenu(item, 0)); 1105 new LauncherApplicationMenuItemModel(
1106 controller->GetApplicationList(item, 0)));
1103 // The first element in the menu is a spacing separator. On some systems 1107 // The first element in the menu is a spacing separator. On some systems
1104 // (e.g. Windows) such things do not exist. As such we check the existence 1108 // (e.g. Windows) such things do not exist. As such we check the existence
1105 // and adjust dynamically. 1109 // and adjust dynamically.
1106 int first_item = menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR ? 1 : 0; 1110 int first_item = menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR ? 1 : 0;
1107 int expected_menu_items = first_item + 1111 int expected_menu_items = first_item +
1108 (expected_items ? (expected_items + 3) : 2); 1112 (expected_items ? (expected_items + 3) : 2);
1109 EXPECT_EQ(expected_menu_items, menu->GetItemCount()); 1113 EXPECT_EQ(expected_menu_items, menu->GetItemCount());
1110 EXPECT_FALSE(menu->IsEnabledAt(first_item)); 1114 EXPECT_FALSE(menu->IsEnabledAt(first_item));
1111 if (expected_items) { 1115 if (expected_items) {
1112 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, 1116 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR,
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
1256 item_gmail.type = ash::TYPE_APP_SHORTCUT; 1260 item_gmail.type = ash::TYPE_APP_SHORTCUT;
1257 item_gmail.id = gmail_id; 1261 item_gmail.id = gmail_id;
1258 string16 two_menu_items[] = {title1, title2}; 1262 string16 two_menu_items[] = {title1, title2};
1259 EXPECT_TRUE(CheckMenuCreation( 1263 EXPECT_TRUE(CheckMenuCreation(
1260 launcher_controller_.get(), item_gmail, 2, two_menu_items, false)); 1264 launcher_controller_.get(), item_gmail, 2, two_menu_items, false));
1261 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); 1265 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
1262 // Execute the second item in the list (which shouldn't do anything since that 1266 // Execute the second item in the list (which shouldn't do anything since that
1263 // item is per definition already the active tab). 1267 // item is per definition already the active tab).
1264 { 1268 {
1265 scoped_ptr<ash::LauncherMenuModel> menu( 1269 scoped_ptr<ash::LauncherMenuModel> menu(
1266 launcher_controller_->CreateApplicationMenu(item_gmail, 0)); 1270 new LauncherApplicationMenuItemModel(
1271 launcher_controller_->GetApplicationList(item_gmail, 0)));
1267 // The first element in the menu is a spacing separator. On some systems 1272 // The first element in the menu is a spacing separator. On some systems
1268 // (e.g. Windows) such things do not exist. As such we check the existence 1273 // (e.g. Windows) such things do not exist. As such we check the existence
1269 // and adjust dynamically. 1274 // and adjust dynamically.
1270 int first_item = 1275 int first_item =
1271 (menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR) ? 1 : 0; 1276 (menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR) ? 1 : 0;
1272 menu->ActivatedAt(first_item + 3); 1277 menu->ActivatedAt(first_item + 3);
1273 } 1278 }
1274 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); 1279 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
1275 1280
1276 // Execute the first item. 1281 // Execute the first item.
1277 { 1282 {
1278 scoped_ptr<ash::LauncherMenuModel> menu( 1283 scoped_ptr<ash::LauncherMenuModel> menu(
1279 launcher_controller_->CreateApplicationMenu(item_gmail, 0)); 1284 new LauncherApplicationMenuItemModel(
1285 launcher_controller_->GetApplicationList(item_gmail, 0)));
1280 int first_item = 1286 int first_item =
1281 (menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR) ? 1 : 0; 1287 (menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR) ? 1 : 0;
1282 menu->ActivatedAt(first_item + 2); 1288 menu->ActivatedAt(first_item + 2);
1283 } 1289 }
1284 // Now the active tab should be the second item. 1290 // Now the active tab should be the second item.
1285 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); 1291 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
1286 } 1292 }
1287 1293
1288 // Checks that the generated menu list properly deletes items. 1294 // Checks that the generated menu list properly deletes items.
1289 TEST_F(ChromeLauncherControllerTest, V1AppMenuDeletionExecution) { 1295 TEST_F(ChromeLauncherControllerTest, V1AppMenuDeletionExecution) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1330 TEST_F(ChromeLauncherControllerTest, AppPanels) { 1336 TEST_F(ChromeLauncherControllerTest, AppPanels) {
1331 InitLauncherControllerWithBrowser(); 1337 InitLauncherControllerWithBrowser();
1332 // Browser shortcut LauncherItem is added. 1338 // Browser shortcut LauncherItem is added.
1333 EXPECT_EQ(1, model_observer_->added()); 1339 EXPECT_EQ(1, model_observer_->added());
1334 1340
1335 TestAppIconLoaderImpl* app_icon_loader = new TestAppIconLoaderImpl(); 1341 TestAppIconLoaderImpl* app_icon_loader = new TestAppIconLoaderImpl();
1336 SetAppIconLoader(app_icon_loader); 1342 SetAppIconLoader(app_icon_loader);
1337 1343
1338 // Test adding an app panel 1344 // Test adding an app panel
1339 std::string app_id = extension1_->id(); 1345 std::string app_id = extension1_->id();
1340 ShellWindowLauncherItemController app_panel_controller( 1346 ShellWindowLauncherItemController* app_panel_controller =
1341 LauncherItemController::TYPE_APP_PANEL, "id", app_id, 1347 new ShellWindowLauncherItemController(
1342 launcher_controller_.get()); 1348 LauncherItemController::TYPE_APP_PANEL,
1349 "id",
1350 app_id,
1351 launcher_controller_.get());
1343 ash::LauncherID launcher_id1 = launcher_controller_->CreateAppLauncherItem( 1352 ash::LauncherID launcher_id1 = launcher_controller_->CreateAppLauncherItem(
1344 &app_panel_controller, app_id, ash::STATUS_RUNNING); 1353 app_panel_controller, app_id, ash::STATUS_RUNNING);
1345 int panel_index = model_observer_->last_index(); 1354 int panel_index = model_observer_->last_index();
1346 EXPECT_EQ(2, model_observer_->added()); 1355 EXPECT_EQ(2, model_observer_->added());
1347 EXPECT_EQ(0, model_observer_->changed()); 1356 EXPECT_EQ(0, model_observer_->changed());
1348 EXPECT_EQ(1, app_icon_loader->fetch_count()); 1357 EXPECT_EQ(1, app_icon_loader->fetch_count());
1349 model_observer_->clear_counts(); 1358 model_observer_->clear_counts();
1350 1359
1351 // App panels should have a separate identifier than the app id 1360 // App panels should have a separate identifier than the app id
1352 EXPECT_EQ(0, launcher_controller_->GetLauncherIDForAppID(app_id)); 1361 EXPECT_EQ(0, launcher_controller_->GetLauncherIDForAppID(app_id));
1353 1362
1354 // Setting the app image image should not change the panel if it set its icon 1363 // Setting the app image image should not change the panel if it set its icon
1355 app_panel_controller.set_image_set_by_controller(true); 1364 app_panel_controller->set_image_set_by_controller(true);
1356 gfx::ImageSkia image; 1365 gfx::ImageSkia image;
1357 launcher_controller_->SetAppImage(app_id, image); 1366 launcher_controller_->SetAppImage(app_id, image);
1358 EXPECT_EQ(0, model_observer_->changed()); 1367 EXPECT_EQ(0, model_observer_->changed());
1359 model_observer_->clear_counts(); 1368 model_observer_->clear_counts();
1360 1369
1361 // Add a second app panel and verify that it get the same index as the first 1370 // Add a second app panel and verify that it get the same index as the first
1362 // one had, being added to the left of the existing panel. 1371 // one had, being added to the left of the existing panel.
1372 ShellWindowLauncherItemController* app_panel_controller2 =
1373 new ShellWindowLauncherItemController(
1374 LauncherItemController::TYPE_APP_PANEL,
1375 "id",
1376 app_id,
1377 launcher_controller_.get());
1378
1363 ash::LauncherID launcher_id2 = launcher_controller_->CreateAppLauncherItem( 1379 ash::LauncherID launcher_id2 = launcher_controller_->CreateAppLauncherItem(
1364 &app_panel_controller, app_id, ash::STATUS_RUNNING); 1380 app_panel_controller2, app_id, ash::STATUS_RUNNING);
1365 EXPECT_EQ(panel_index, model_observer_->last_index()); 1381 EXPECT_EQ(panel_index, model_observer_->last_index());
1366 EXPECT_EQ(1, model_observer_->added()); 1382 EXPECT_EQ(1, model_observer_->added());
1367 model_observer_->clear_counts(); 1383 model_observer_->clear_counts();
1368 1384
1369 launcher_controller_->CloseLauncherItem(launcher_id2); 1385 launcher_controller_->CloseLauncherItem(launcher_id2);
1370 launcher_controller_->CloseLauncherItem(launcher_id1); 1386 launcher_controller_->CloseLauncherItem(launcher_id1);
1371 EXPECT_EQ(2, model_observer_->removed()); 1387 EXPECT_EQ(2, model_observer_->removed());
1388
1389 if (!ash::Shell::HasInstance()) {
1390 delete app_panel_controller;
1391 delete app_panel_controller2;
1392 }
1372 } 1393 }
1373 1394
1374 // Tests that the Gmail extension matches more then the app itself claims with 1395 // Tests that the Gmail extension matches more then the app itself claims with
1375 // the manifest file. 1396 // the manifest file.
1376 TEST_F(ChromeLauncherControllerTest, GmailMatching) { 1397 TEST_F(ChromeLauncherControllerTest, GmailMatching) {
1377 InitLauncherControllerWithBrowser(); 1398 InitLauncherControllerWithBrowser();
1378 1399
1379 // Create a Gmail browser tab. 1400 // Create a Gmail browser tab.
1380 chrome::NewTab(browser()); 1401 chrome::NewTab(browser());
1381 string16 title = ASCIIToUTF16("Test"); 1402 string16 title = ASCIIToUTF16("Test");
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1459 1480
1460 // Move browser shortcut item from index 1 to index 3. 1481 // Move browser shortcut item from index 1 to index 3.
1461 model_->Move(1, 3); 1482 model_->Move(1, 3);
1462 EXPECT_EQ(ash::TYPE_APP_LIST, model_->items()[0].type); 1483 EXPECT_EQ(ash::TYPE_APP_LIST, model_->items()[0].type);
1463 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[1].type); 1484 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[1].type);
1464 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type); 1485 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
1465 EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[3].type); 1486 EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[3].type);
1466 1487
1467 launcher_controller_.reset(); 1488 launcher_controller_.reset();
1468 model_.reset(new ash::LauncherModel); 1489 model_.reset(new ash::LauncherModel);
1490
1491 // Clear already registered LauncherItemDelegate.
1492 if (ash::Shell::HasInstance()) {
1493 ash::Shell::GetInstance()->launcher_item_delegate_manager()->
1494 UnregisterAllLauncherItemDelegateForTest();
1495 }
1496
1469 launcher_controller_.reset( 1497 launcher_controller_.reset(
1470 ChromeLauncherController::CreateInstance(profile(), model_.get())); 1498 ChromeLauncherController::CreateInstance(profile(), model_.get()));
1471 app_tab_helper = new TestAppTabHelperImpl; 1499 app_tab_helper = new TestAppTabHelperImpl;
1472 app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1"); 1500 app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
1473 app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(1), "2"); 1501 app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(1), "2");
1474 SetAppTabHelper(app_tab_helper); 1502 SetAppTabHelper(app_tab_helper);
1475 1503
1476 launcher_controller_->Init(); 1504 launcher_controller_->Init();
1477 1505
1478 // Check LauncherItems are restored after resetting ChromeLauncherController. 1506 // Check LauncherItems are restored after resetting ChromeLauncherController.
(...skipping 23 matching lines...) Expand all
1502 ash::LauncherID id = launcher_controller_->GetLauncherIDForAppID("1"); 1530 ash::LauncherID id = launcher_controller_->GetLauncherIDForAppID("1");
1503 int app_index = model_->ItemIndexByID(id); 1531 int app_index = model_->ItemIndexByID(id);
1504 EXPECT_EQ(1, app_icon_loader->fetch_count()); 1532 EXPECT_EQ(1, app_icon_loader->fetch_count());
1505 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[app_index].type); 1533 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[app_index].type);
1506 EXPECT_TRUE(launcher_controller_->IsAppPinned("1")); 1534 EXPECT_TRUE(launcher_controller_->IsAppPinned("1"));
1507 EXPECT_FALSE(launcher_controller_->IsAppPinned("0")); 1535 EXPECT_FALSE(launcher_controller_->IsAppPinned("0"));
1508 EXPECT_EQ(initial_size + 1, model_->items().size()); 1536 EXPECT_EQ(initial_size + 1, model_->items().size());
1509 1537
1510 launcher_controller_.reset(); 1538 launcher_controller_.reset();
1511 model_.reset(new ash::LauncherModel); 1539 model_.reset(new ash::LauncherModel);
1540
1541 // Clear already registered LauncherItemDelegate.
1542 if (ash::Shell::HasInstance()) {
1543 ash::Shell::GetInstance()->launcher_item_delegate_manager()->
1544 UnregisterAllLauncherItemDelegateForTest();
1545 }
1546
1512 launcher_controller_.reset( 1547 launcher_controller_.reset(
1513 ChromeLauncherController::CreateInstance(profile(), model_.get())); 1548 ChromeLauncherController::CreateInstance(profile(), model_.get()));
1514 app_tab_helper = new TestAppTabHelperImpl; 1549 app_tab_helper = new TestAppTabHelperImpl;
1515 app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1"); 1550 app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
1516 SetAppTabHelper(app_tab_helper); 1551 SetAppTabHelper(app_tab_helper);
1517 app_icon_loader = new TestAppIconLoaderImpl; 1552 app_icon_loader = new TestAppIconLoaderImpl;
1518 SetAppIconLoader(app_icon_loader); 1553 SetAppIconLoader(app_icon_loader);
1519 launcher_controller_->Init(); 1554 launcher_controller_->Init();
1520 1555
1521 EXPECT_EQ(1, app_icon_loader->fetch_count()); 1556 EXPECT_EQ(1, app_icon_loader->fetch_count());
1522 ASSERT_EQ(initial_size + 1, model_->items().size()); 1557 ASSERT_EQ(initial_size + 1, model_->items().size());
1523 EXPECT_TRUE(launcher_controller_->IsAppPinned("1")); 1558 EXPECT_TRUE(launcher_controller_->IsAppPinned("1"));
1524 EXPECT_FALSE(launcher_controller_->IsAppPinned("0")); 1559 EXPECT_FALSE(launcher_controller_->IsAppPinned("0"));
1525 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[app_index].type); 1560 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[app_index].type);
1526 1561
1527 launcher_controller_->UnpinAppWithID("1"); 1562 launcher_controller_->UnpinAppWithID("1");
1528 ASSERT_EQ(initial_size, model_->items().size()); 1563 ASSERT_EQ(initial_size, model_->items().size());
1529 } 1564 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698