Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / ash / launcher / chrome_launcher_controller_browsertest.cc
blobf7018d3144bf90c682d24b9fb5694981c661e087
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_controller.h"
9 #include "ash/shelf/shelf.h"
10 #include "ash/shelf/shelf_button.h"
11 #include "ash/shelf/shelf_constants.h"
12 #include "ash/shelf/shelf_model.h"
13 #include "ash/shelf/shelf_util.h"
14 #include "ash/shelf/shelf_view.h"
15 #include "ash/shell.h"
16 #include "ash/test/app_list_controller_test_api.h"
17 #include "ash/test/shelf_test_api.h"
18 #include "ash/test/shelf_view_test_api.h"
19 #include "ash/test/shell_test_api.h"
20 #include "ash/wm/window_state.h"
21 #include "ash/wm/window_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "chrome/browser/apps/app_browsertest_util.h"
25 #include "chrome/browser/chrome_notification_types.h"
26 #include "chrome/browser/extensions/extension_apitest.h"
27 #include "chrome/browser/extensions/extension_browsertest.h"
28 #include "chrome/browser/extensions/extension_function_test_utils.h"
29 #include "chrome/browser/extensions/extension_service.h"
30 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/browser/ui/app_list/app_list_service.h"
32 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controller.h"
33 #include "chrome/browser/ui/ash/launcher/launcher_application_menu_item_model.h"
34 #include "chrome/browser/ui/ash/launcher/launcher_item_controller.h"
35 #include "chrome/browser/ui/browser.h"
36 #include "chrome/browser/ui/browser_commands.h"
37 #include "chrome/browser/ui/browser_finder.h"
38 #include "chrome/browser/ui/browser_list.h"
39 #include "chrome/browser/ui/browser_window.h"
40 #include "chrome/browser/ui/chrome_pages.h"
41 #include "chrome/browser/ui/extensions/app_launch_params.h"
42 #include "chrome/browser/ui/extensions/application_launch.h"
43 #include "chrome/browser/ui/host_desktop.h"
44 #include "chrome/browser/ui/settings_window_manager.h"
45 #include "chrome/browser/ui/tabs/tab_strip_model.h"
46 #include "chrome/common/chrome_switches.h"
47 #include "chrome/test/base/ui_test_utils.h"
48 #include "content/public/browser/notification_service.h"
49 #include "content/public/browser/notification_source.h"
50 #include "content/public/browser/web_contents.h"
51 #include "content/public/test/browser_test_utils.h"
52 #include "extensions/browser/app_window/app_window.h"
53 #include "extensions/browser/app_window/app_window_registry.h"
54 #include "extensions/browser/app_window/native_app_window.h"
55 #include "extensions/browser/extension_system.h"
56 #include "extensions/common/constants.h"
57 #include "extensions/common/switches.h"
58 #include "extensions/test/extension_test_message_listener.h"
59 #include "testing/gtest/include/gtest/gtest.h"
60 #include "ui/app_list/app_list_switches.h"
61 #include "ui/app_list/views/app_list_item_view.h"
62 #include "ui/app_list/views/apps_grid_view.h"
63 #include "ui/app_list/views/start_page_view.h"
64 #include "ui/app_list/views/tile_item_view.h"
65 #include "ui/aura/client/aura_constants.h"
66 #include "ui/aura/window.h"
67 #include "ui/base/window_open_disposition.h"
68 #include "ui/events/event.h"
69 #include "ui/events/test/event_generator.h"
71 using extensions::AppWindow;
72 using extensions::Extension;
73 using content::WebContents;
75 namespace {
77 class TestEvent : public ui::Event {
78 public:
79 explicit TestEvent(ui::EventType type)
80 : ui::Event(type, base::TimeDelta(), 0) {
82 ~TestEvent() override {}
84 private:
85 DISALLOW_COPY_AND_ASSIGN(TestEvent);
88 class TestAppWindowRegistryObserver
89 : public extensions::AppWindowRegistry::Observer {
90 public:
91 explicit TestAppWindowRegistryObserver(Profile* profile)
92 : profile_(profile), icon_updates_(0) {
93 extensions::AppWindowRegistry::Get(profile_)->AddObserver(this);
96 ~TestAppWindowRegistryObserver() override {
97 extensions::AppWindowRegistry::Get(profile_)->RemoveObserver(this);
100 // Overridden from AppWindowRegistry::Observer:
101 void OnAppWindowIconChanged(AppWindow* app_window) override {
102 ++icon_updates_;
105 int icon_updates() { return icon_updates_; }
107 private:
108 Profile* profile_;
109 int icon_updates_;
111 DISALLOW_COPY_AND_ASSIGN(TestAppWindowRegistryObserver);
114 // Click the "All Apps" button from the app launcher start page. Assumes that
115 // the app launcher is open to the start page. On the non-experimental launcher,
116 // does nothing.
117 // |display_origin| is the top-left corner of the active display, in screen
118 // coordinates.
119 void ClickAllAppsButtonFromStartPage(ui::test::EventGenerator* generator,
120 const gfx::Point& display_origin) {
121 if (!app_list::switches::IsExperimentalAppListEnabled())
122 return;
124 ash::test::AppListControllerTestApi controller_test(
125 ash::Shell::GetInstance());
127 app_list::StartPageView* start_page_view = controller_test.GetStartPageView();
128 DCHECK(start_page_view);
130 app_list::TileItemView* all_apps_button = start_page_view->all_apps_button();
131 gfx::Rect all_apps_rect = all_apps_button->GetBoundsInScreen();
132 all_apps_rect.Offset(-display_origin.x(), -display_origin.y());
133 generator->MoveMouseTo(all_apps_rect.CenterPoint().x(),
134 all_apps_rect.CenterPoint().y());
135 generator->ClickLeftButton();
136 base::MessageLoop::current()->RunUntilIdle();
137 // Run Layout() to effectively complete the animation to the apps page.
138 controller_test.LayoutContentsView();
141 } // namespace
143 class LauncherPlatformAppBrowserTest
144 : public extensions::PlatformAppBrowserTest {
145 protected:
146 LauncherPlatformAppBrowserTest() : shelf_(NULL), controller_(NULL) {
149 ~LauncherPlatformAppBrowserTest() override {}
151 void RunTestOnMainThreadLoop() override {
152 shelf_ = ash::Shelf::ForPrimaryDisplay();
153 controller_ = ChromeLauncherController::instance();
154 return extensions::PlatformAppBrowserTest::RunTestOnMainThreadLoop();
157 ash::ShelfModel* shelf_model() {
158 return ash::test::ShellTestApi(ash::Shell::GetInstance()).shelf_model();
161 ash::ShelfID CreateAppShortcutLauncherItem(const std::string& name) {
162 return controller_->CreateAppShortcutLauncherItem(
163 name, controller_->model()->item_count());
166 const ash::ShelfItem& GetLastLauncherItem() {
167 // Unless there are any panels, the item at index [count - 1] will be
168 // the desired item.
169 return shelf_model()->items()[shelf_model()->item_count() - 1];
172 const ash::ShelfItem& GetLastLauncherPanelItem() {
173 // Panels show up on the right side of the shelf, so the desired item
174 // will be the last one.
175 return shelf_model()->items()[shelf_model()->item_count() - 1];
178 LauncherItemController* GetItemController(ash::ShelfID id) {
179 return controller_->id_to_item_controller_map_[id];
182 // Returns the number of menu items, ignoring separators.
183 int GetNumApplicationMenuItems(const ash::ShelfItem& item) {
184 const int event_flags = 0;
185 scoped_ptr<ash::ShelfMenuModel> menu(new LauncherApplicationMenuItemModel(
186 controller_->GetApplicationList(item, event_flags)));
187 int num_items = 0;
188 for (int i = 0; i < menu->GetItemCount(); ++i) {
189 if (menu->GetTypeAt(i) != ui::MenuModel::TYPE_SEPARATOR)
190 ++num_items;
192 return num_items;
195 // Activate the shelf item with the given |id|.
196 void ActivateShelfItem(int id) {
197 shelf_->ActivateShelfItem(id);
200 ash::Shelf* shelf_;
201 ChromeLauncherController* controller_;
203 private:
205 DISALLOW_COPY_AND_ASSIGN(LauncherPlatformAppBrowserTest);
208 enum RipOffCommand {
209 // Drag the item off the shelf and let the mouse go.
210 RIP_OFF_ITEM,
211 // Drag the item off the shelf, move the mouse back and then let go.
212 RIP_OFF_ITEM_AND_RETURN,
213 // Drag the item off the shelf and then issue a cancel command.
214 RIP_OFF_ITEM_AND_CANCEL,
215 // Drag the item off the shelf and do not release the mouse.
216 RIP_OFF_ITEM_AND_DONT_RELEASE_MOUSE,
219 class ShelfAppBrowserTest : public ExtensionBrowserTest {
220 protected:
221 ShelfAppBrowserTest() : shelf_(NULL), model_(NULL), controller_(NULL) {
224 ~ShelfAppBrowserTest() override {}
226 void RunTestOnMainThreadLoop() override {
227 shelf_ = ash::Shelf::ForPrimaryDisplay();
228 model_ = ash::test::ShellTestApi(ash::Shell::GetInstance()).shelf_model();
229 controller_ = ChromeLauncherController::instance();
230 return ExtensionBrowserTest::RunTestOnMainThreadLoop();
233 size_t NumberOfDetectedLauncherBrowsers(bool show_all_tabs) {
234 LauncherItemController* item_controller =
235 controller_->GetBrowserShortcutLauncherItemController();
236 int items = item_controller->GetApplicationList(
237 show_all_tabs ? ui::EF_SHIFT_DOWN : 0).size();
238 // If we have at least one item, we have also a title which we remove here.
239 return items ? (items - 1) : 0;
242 const Extension* LoadAndLaunchExtension(
243 const char* name,
244 extensions::LaunchContainer container,
245 WindowOpenDisposition disposition) {
246 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII(name)));
248 ExtensionService* service = extensions::ExtensionSystem::Get(
249 profile())->extension_service();
250 const Extension* extension =
251 service->GetExtensionById(last_loaded_extension_id(), false);
252 EXPECT_TRUE(extension);
254 OpenApplication(AppLaunchParams(profile(), extension, container,
255 disposition, extensions::SOURCE_TEST));
256 return extension;
259 ash::ShelfID CreateShortcut(const char* name) {
260 ExtensionService* service = extensions::ExtensionSystem::Get(
261 profile())->extension_service();
262 LoadExtension(test_data_dir_.AppendASCII(name));
264 // First get app_id.
265 const Extension* extension =
266 service->GetExtensionById(last_loaded_extension_id(), false);
267 const std::string app_id = extension->id();
269 // Then create a shortcut.
270 int item_count = model_->item_count();
271 ash::ShelfID shortcut_id = controller_->CreateAppShortcutLauncherItem(
272 app_id,
273 item_count);
274 controller_->PersistPinnedState();
275 EXPECT_EQ(++item_count, model_->item_count());
276 const ash::ShelfItem& item = *model_->ItemByID(shortcut_id);
277 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
278 return item.id;
281 void RemoveShortcut(ash::ShelfID id) {
282 controller_->Unpin(id);
285 // Activate the shelf item with the given |id|.
286 void ActivateShelfItem(int id) {
287 shelf_->ActivateShelfItem(id);
290 ash::ShelfID PinFakeApp(const std::string& name) {
291 return controller_->CreateAppShortcutLauncherItem(
292 name, model_->item_count());
295 // Get the index of an item which has the given type.
296 int GetIndexOfShelfItemType(ash::ShelfItemType type) {
297 return model_->GetItemIndexForType(type);
300 // Try to rip off |item_index|.
301 void RipOffItemIndex(int index,
302 ui::test::EventGenerator* generator,
303 ash::test::ShelfViewTestAPI* test,
304 RipOffCommand command) {
305 ash::ShelfButton* button = test->GetButton(index);
306 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
307 gfx::Point rip_off_point(start_point.x(), 0);
308 generator->MoveMouseTo(start_point.x(), start_point.y());
309 base::MessageLoop::current()->RunUntilIdle();
310 generator->PressLeftButton();
311 base::MessageLoop::current()->RunUntilIdle();
312 generator->MoveMouseTo(rip_off_point.x(), rip_off_point.y());
313 base::MessageLoop::current()->RunUntilIdle();
314 test->RunMessageLoopUntilAnimationsDone();
315 if (command == RIP_OFF_ITEM_AND_RETURN) {
316 generator->MoveMouseTo(start_point.x(), start_point.y());
317 base::MessageLoop::current()->RunUntilIdle();
318 test->RunMessageLoopUntilAnimationsDone();
319 } else if (command == RIP_OFF_ITEM_AND_CANCEL) {
320 // This triggers an internal cancel. Using VKEY_ESCAPE was too unreliable.
321 button->OnMouseCaptureLost();
323 if (command != RIP_OFF_ITEM_AND_DONT_RELEASE_MOUSE) {
324 generator->ReleaseLeftButton();
325 base::MessageLoop::current()->RunUntilIdle();
326 test->RunMessageLoopUntilAnimationsDone();
330 ash::Shelf* shelf_;
331 ash::ShelfModel* model_;
332 ChromeLauncherController* controller_;
334 private:
336 DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserTest);
339 class ShelfAppBrowserTestNoDefaultBrowser : public ShelfAppBrowserTest {
340 protected:
341 ShelfAppBrowserTestNoDefaultBrowser() {}
342 ~ShelfAppBrowserTestNoDefaultBrowser() override {}
344 void SetUpCommandLine(base::CommandLine* command_line) override {
345 ShelfAppBrowserTest::SetUpCommandLine(command_line);
346 command_line->AppendSwitch(switches::kNoStartupWindow);
349 private:
351 DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserTestNoDefaultBrowser);
354 // Since the default for minimizing on click might change, I added both classes
355 // to either get the minimize on click or not.
356 class ShelfAppBrowserNoMinimizeOnClick : public LauncherPlatformAppBrowserTest {
357 protected:
358 ShelfAppBrowserNoMinimizeOnClick() {}
359 ~ShelfAppBrowserNoMinimizeOnClick() override {}
361 void SetUpCommandLine(base::CommandLine* command_line) override {
362 LauncherPlatformAppBrowserTest::SetUpCommandLine(command_line);
363 command_line->AppendSwitch(
364 switches::kDisableMinimizeOnSecondLauncherItemClick);
367 private:
369 DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserNoMinimizeOnClick);
372 typedef LauncherPlatformAppBrowserTest ShelfAppBrowserMinimizeOnClick;
374 // Test that we can launch a platform app and get a running item.
375 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, LaunchUnpinned) {
376 int item_count = shelf_model()->item_count();
377 const Extension* extension = LoadAndLaunchPlatformApp("launch", "Launched");
378 AppWindow* window = CreateAppWindow(extension);
379 ++item_count;
380 ASSERT_EQ(item_count, shelf_model()->item_count());
381 const ash::ShelfItem& item = GetLastLauncherItem();
382 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item.type);
383 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
384 CloseAppWindow(window);
385 --item_count;
386 EXPECT_EQ(item_count, shelf_model()->item_count());
389 // Test that we can launch a platform app that already has a shortcut.
390 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, LaunchPinned) {
391 int item_count = shelf_model()->item_count();
393 // First get app_id.
394 const Extension* extension = LoadAndLaunchPlatformApp("launch", "Launched");
395 const std::string app_id = extension->id();
397 // Then create a shortcut.
398 ash::ShelfID shortcut_id = CreateAppShortcutLauncherItem(app_id);
399 ++item_count;
400 ASSERT_EQ(item_count, shelf_model()->item_count());
401 ash::ShelfItem item = *shelf_model()->ItemByID(shortcut_id);
402 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
403 EXPECT_EQ(ash::STATUS_CLOSED, item.status);
405 // Open a window. Confirm the item is now running.
406 AppWindow* window = CreateAppWindow(extension);
407 ash::wm::ActivateWindow(window->GetNativeWindow());
408 ASSERT_EQ(item_count, shelf_model()->item_count());
409 item = *shelf_model()->ItemByID(shortcut_id);
410 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
411 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
413 // Then close it, make sure there's still an item.
414 CloseAppWindow(window);
415 ASSERT_EQ(item_count, shelf_model()->item_count());
416 item = *shelf_model()->ItemByID(shortcut_id);
417 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
418 EXPECT_EQ(ash::STATUS_CLOSED, item.status);
421 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, PinRunning) {
422 // Run.
423 int item_count = shelf_model()->item_count();
424 const Extension* extension = LoadAndLaunchPlatformApp("launch", "Launched");
425 AppWindow* window = CreateAppWindow(extension);
426 ++item_count;
427 ASSERT_EQ(item_count, shelf_model()->item_count());
428 const ash::ShelfItem& item1 = GetLastLauncherItem();
429 ash::ShelfID id = item1.id;
430 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
431 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
433 // Create a shortcut. The app item should be after it.
434 ash::ShelfID foo_id = CreateAppShortcutLauncherItem("foo");
435 ++item_count;
436 ASSERT_EQ(item_count, shelf_model()->item_count());
437 EXPECT_LT(shelf_model()->ItemIndexByID(foo_id),
438 shelf_model()->ItemIndexByID(id));
440 // Pin the app. The item should remain.
441 controller_->Pin(id);
442 ASSERT_EQ(item_count, shelf_model()->item_count());
443 const ash::ShelfItem& item2 = *shelf_model()->ItemByID(id);
444 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item2.type);
445 EXPECT_EQ(ash::STATUS_ACTIVE, item2.status);
447 // New shortcuts should come after the item.
448 ash::ShelfID bar_id = CreateAppShortcutLauncherItem("bar");
449 ++item_count;
450 ASSERT_EQ(item_count, shelf_model()->item_count());
451 EXPECT_LT(shelf_model()->ItemIndexByID(id),
452 shelf_model()->ItemIndexByID(bar_id));
454 // Then close it, make sure the item remains.
455 CloseAppWindow(window);
456 ASSERT_EQ(item_count, shelf_model()->item_count());
459 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, UnpinRunning) {
460 int item_count = shelf_model()->item_count();
462 // First get app_id.
463 const Extension* extension = LoadAndLaunchPlatformApp("launch", "Launched");
464 const std::string app_id = extension->id();
466 // Then create a shortcut.
467 ash::ShelfID shortcut_id = CreateAppShortcutLauncherItem(app_id);
468 ++item_count;
469 ASSERT_EQ(item_count, shelf_model()->item_count());
470 ash::ShelfItem item = *shelf_model()->ItemByID(shortcut_id);
471 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
472 EXPECT_EQ(ash::STATUS_CLOSED, item.status);
474 // Create a second shortcut. This will be needed to force the first one to
475 // move once it gets unpinned.
476 ash::ShelfID foo_id = CreateAppShortcutLauncherItem("foo");
477 ++item_count;
478 ASSERT_EQ(item_count, shelf_model()->item_count());
479 EXPECT_LT(shelf_model()->ItemIndexByID(shortcut_id),
480 shelf_model()->ItemIndexByID(foo_id));
482 // Open a window. Confirm the item is now running.
483 AppWindow* window = CreateAppWindow(extension);
484 ash::wm::ActivateWindow(window->GetNativeWindow());
485 ASSERT_EQ(item_count, shelf_model()->item_count());
486 item = *shelf_model()->ItemByID(shortcut_id);
487 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
488 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
490 // Unpin the app. The item should remain.
491 controller_->Unpin(shortcut_id);
492 ASSERT_EQ(item_count, shelf_model()->item_count());
493 item = *shelf_model()->ItemByID(shortcut_id);
494 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item.type);
495 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
496 // The item should have moved after the other shortcuts.
497 EXPECT_GT(shelf_model()->ItemIndexByID(shortcut_id),
498 shelf_model()->ItemIndexByID(foo_id));
500 // Then close it, make sure the item's gone.
501 CloseAppWindow(window);
502 --item_count;
503 ASSERT_EQ(item_count, shelf_model()->item_count());
506 // Test that we can launch a platform app with more than one window.
507 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, MultipleWindows) {
508 int item_count = shelf_model()->item_count();
510 // First run app.
511 const Extension* extension = LoadAndLaunchPlatformApp("launch", "Launched");
512 AppWindow* window1 = CreateAppWindow(extension);
513 ++item_count;
514 ASSERT_EQ(item_count, shelf_model()->item_count());
515 const ash::ShelfItem& item1 = GetLastLauncherItem();
516 ash::ShelfID item_id = item1.id;
517 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
518 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
519 EXPECT_EQ(2, GetNumApplicationMenuItems(item1)); // Title + 1 window
521 // Add second window.
522 AppWindow* window2 = CreateAppWindow(extension);
523 // Confirm item stays.
524 ASSERT_EQ(item_count, shelf_model()->item_count());
525 const ash::ShelfItem& item2 = *shelf_model()->ItemByID(item_id);
526 EXPECT_EQ(ash::STATUS_ACTIVE, item2.status);
527 EXPECT_EQ(3, GetNumApplicationMenuItems(item2)); // Title + 2 windows
529 // Close second window.
530 CloseAppWindow(window2);
531 // Confirm item stays.
532 ASSERT_EQ(item_count, shelf_model()->item_count());
533 const ash::ShelfItem& item3 = *shelf_model()->ItemByID(item_id);
534 EXPECT_EQ(ash::STATUS_ACTIVE, item3.status);
535 EXPECT_EQ(2, GetNumApplicationMenuItems(item3)); // Title + 1 window
537 // Close first window.
538 CloseAppWindow(window1);
539 // Confirm item is removed.
540 --item_count;
541 ASSERT_EQ(item_count, shelf_model()->item_count());
544 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, MultipleApps) {
545 int item_count = shelf_model()->item_count();
547 // First run app.
548 const Extension* extension1 = LoadAndLaunchPlatformApp("launch", "Launched");
549 AppWindow* window1 = CreateAppWindow(extension1);
550 ++item_count;
551 ASSERT_EQ(item_count, shelf_model()->item_count());
552 const ash::ShelfItem& item1 = GetLastLauncherItem();
553 ash::ShelfID item_id1 = item1.id;
554 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
555 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
557 // Then run second app.
558 const Extension* extension2 = LoadAndLaunchPlatformApp("launch_2",
559 "Launched");
560 AppWindow* window2 = CreateAppWindow(extension2);
561 ++item_count;
562 ASSERT_EQ(item_count, shelf_model()->item_count());
563 const ash::ShelfItem& item2 = GetLastLauncherItem();
564 ash::ShelfID item_id2 = item2.id;
565 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item2.type);
566 EXPECT_EQ(ash::STATUS_ACTIVE, item2.status);
568 EXPECT_NE(item_id1, item_id2);
569 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id1)->status);
571 // Close second app.
572 CloseAppWindow(window2);
573 --item_count;
574 ASSERT_EQ(item_count, shelf_model()->item_count());
575 // First app should be active again.
576 EXPECT_EQ(ash::STATUS_ACTIVE, shelf_model()->ItemByID(item_id1)->status);
578 // Close first app.
579 CloseAppWindow(window1);
580 --item_count;
581 ASSERT_EQ(item_count, shelf_model()->item_count());
584 // Confirm that app windows can be reactivated by clicking their icons and that
585 // the correct activation order is maintained.
586 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, WindowActivation) {
587 int item_count = shelf_model()->item_count();
589 // First run app.
590 const Extension* extension1 = LoadAndLaunchPlatformApp("launch", "Launched");
591 AppWindow* window1 = CreateAppWindow(extension1);
592 ++item_count;
593 ASSERT_EQ(item_count, shelf_model()->item_count());
594 const ash::ShelfItem& item1 = GetLastLauncherItem();
595 ash::ShelfID item_id1 = item1.id;
596 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
597 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
599 // Then run second app.
600 const Extension* extension2 = LoadAndLaunchPlatformApp("launch_2",
601 "Launched");
602 AppWindow* window2 = CreateAppWindow(extension2);
603 ++item_count;
604 ASSERT_EQ(item_count, shelf_model()->item_count());
605 const ash::ShelfItem& item2 = GetLastLauncherItem();
606 ash::ShelfID item_id2 = item2.id;
607 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item2.type);
608 EXPECT_EQ(ash::STATUS_ACTIVE, item2.status);
610 EXPECT_NE(item_id1, item_id2);
611 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id1)->status);
613 // Activate first one.
614 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id1));
615 EXPECT_EQ(ash::STATUS_ACTIVE, shelf_model()->ItemByID(item_id1)->status);
616 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id2)->status);
617 EXPECT_TRUE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
618 EXPECT_FALSE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
620 // Activate second one.
621 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id2));
622 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id1)->status);
623 EXPECT_EQ(ash::STATUS_ACTIVE, shelf_model()->ItemByID(item_id2)->status);
624 EXPECT_FALSE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
625 EXPECT_TRUE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
627 // Add window for app1. This will activate it.
628 AppWindow* window1b = CreateAppWindow(extension1);
629 ash::wm::ActivateWindow(window1b->GetNativeWindow());
630 EXPECT_FALSE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
631 EXPECT_FALSE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
632 EXPECT_TRUE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
634 // Activate launcher item for app1, this will activate the first app window.
635 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id1));
636 EXPECT_TRUE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
637 EXPECT_FALSE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
638 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id1));
639 EXPECT_TRUE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
641 // Activate the second app again
642 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id2));
643 EXPECT_FALSE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
644 EXPECT_TRUE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
645 EXPECT_FALSE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
647 // Activate the first app again
648 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id1));
649 EXPECT_TRUE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
650 EXPECT_FALSE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
651 EXPECT_FALSE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
653 // Close second app.
654 CloseAppWindow(window2);
655 --item_count;
656 EXPECT_EQ(item_count, shelf_model()->item_count());
657 // First app should be active again.
658 EXPECT_EQ(ash::STATUS_ACTIVE, shelf_model()->ItemByID(item_id1)->status);
660 // Close first app.
661 CloseAppWindow(window1b);
662 CloseAppWindow(window1);
663 --item_count;
664 EXPECT_EQ(item_count, shelf_model()->item_count());
667 // Verify that ChromeLauncherController::CanInstall() returns true for ephemeral
668 // apps and false when the app is promoted to a regular installed app.
669 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, InstallEphemeralApp) {
670 int item_count = shelf_model()->item_count();
672 // Sanity check to verify that ChromeLauncherController::CanInstall() returns
673 // false for apps that are fully installed.
674 const Extension* app = LoadAndLaunchPlatformApp("launch", "Launched");
675 ASSERT_TRUE(app);
676 CreateAppWindow(app);
677 ++item_count;
678 ASSERT_EQ(item_count, shelf_model()->item_count());
679 const ash::ShelfItem& app_item = GetLastLauncherItem();
680 ash::ShelfID app_id = app_item.id;
681 EXPECT_FALSE(controller_->CanInstall(app_id));
683 // Add an ephemeral app.
684 const Extension* ephemeral_app = InstallEphemeralAppWithSourceAndFlags(
685 test_data_dir_.AppendASCII("platform_apps").AppendASCII("launch_2"),
687 extensions::Manifest::INTERNAL,
688 Extension::NO_FLAGS);
689 ASSERT_TRUE(ephemeral_app);
690 CreateAppWindow(ephemeral_app);
691 ++item_count;
692 ASSERT_EQ(item_count, shelf_model()->item_count());
693 const ash::ShelfItem& ephemeral_item = GetLastLauncherItem();
694 ash::ShelfID ephemeral_id = ephemeral_item.id;
696 // Verify that the shelf item for the ephemeral app can be installed.
697 EXPECT_TRUE(controller_->CanInstall(ephemeral_id));
699 // Promote the ephemeral app to a regular installed app.
700 ExtensionService* service =
701 extensions::ExtensionSystem::Get(profile())->extension_service();
702 service->PromoteEphemeralApp(ephemeral_app, false);
704 // Verify that the shelf item for the app can no longer be installed.
705 EXPECT_FALSE(controller_->CanInstall(ephemeral_id));
708 // Confirm that Click behavior for app windows is correnct.
709 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserNoMinimizeOnClick, AppClickBehavior) {
710 // Launch a platform app and create a window for it.
711 const Extension* extension1 = LoadAndLaunchPlatformApp("launch", "Launched");
712 AppWindow* window1 = CreateAppWindow(extension1);
713 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
714 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
715 // Confirm that a controller item was created and is the correct state.
716 const ash::ShelfItem& item1 = GetLastLauncherItem();
717 LauncherItemController* item1_controller = GetItemController(item1.id);
718 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
719 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
720 EXPECT_EQ(LauncherItemController::TYPE_APP, item1_controller->type());
721 // Clicking the item should have no effect.
722 TestEvent click_event(ui::ET_MOUSE_PRESSED);
723 item1_controller->ItemSelected(click_event);
724 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
725 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
726 // Minimize the window and confirm that the controller item is updated.
727 window1->GetBaseWindow()->Minimize();
728 EXPECT_FALSE(window1->GetNativeWindow()->IsVisible());
729 EXPECT_FALSE(window1->GetBaseWindow()->IsActive());
730 EXPECT_EQ(ash::STATUS_RUNNING, item1.status);
731 // Clicking the item should activate the window.
732 item1_controller->ItemSelected(click_event);
733 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
734 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
735 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
736 // Maximizing a window should preserve state after minimize + click.
737 window1->GetBaseWindow()->Maximize();
738 window1->GetBaseWindow()->Minimize();
739 item1_controller->ItemSelected(click_event);
740 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
741 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
742 EXPECT_TRUE(window1->GetBaseWindow()->IsMaximized());
745 // Confirm the minimizing click behavior for apps.
746 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserMinimizeOnClick,
747 PackagedAppClickBehaviorInMinimizeMode) {
748 // Launch one platform app and create a window for it.
749 const Extension* extension1 = LoadAndLaunchPlatformApp("launch", "Launched");
750 AppWindow* window1 = CreateAppWindow(extension1);
751 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
752 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
754 // Confirm that a controller item was created and is the correct state.
755 const ash::ShelfItem& item1 = GetLastLauncherItem();
756 LauncherItemController* item1_controller = GetItemController(item1.id);
757 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
758 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
759 EXPECT_EQ(LauncherItemController::TYPE_APP, item1_controller->type());
760 // Since it is already active, clicking it should minimize.
761 TestEvent click_event(ui::ET_MOUSE_PRESSED);
762 item1_controller->ItemSelected(click_event);
763 EXPECT_FALSE(window1->GetNativeWindow()->IsVisible());
764 EXPECT_FALSE(window1->GetBaseWindow()->IsActive());
765 EXPECT_TRUE(window1->GetBaseWindow()->IsMinimized());
766 EXPECT_EQ(ash::STATUS_RUNNING, item1.status);
767 // Clicking the item again should activate the window again.
768 item1_controller->ItemSelected(click_event);
769 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
770 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
771 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
772 // Maximizing a window should preserve state after minimize + click.
773 window1->GetBaseWindow()->Maximize();
774 window1->GetBaseWindow()->Minimize();
775 item1_controller->ItemSelected(click_event);
776 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
777 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
778 EXPECT_TRUE(window1->GetBaseWindow()->IsMaximized());
779 window1->GetBaseWindow()->Restore();
780 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
781 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
782 EXPECT_FALSE(window1->GetBaseWindow()->IsMaximized());
784 // Creating a second window of the same type should change the behavior so
785 // that a click does not change the activation state.
786 AppWindow* window1a = CreateAppWindow(extension1);
787 EXPECT_TRUE(window1a->GetNativeWindow()->IsVisible());
788 EXPECT_TRUE(window1a->GetBaseWindow()->IsActive());
789 // The first click does nothing.
790 item1_controller->ItemSelected(click_event);
791 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
792 EXPECT_TRUE(window1a->GetNativeWindow()->IsVisible());
793 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
794 EXPECT_FALSE(window1a->GetBaseWindow()->IsActive());
795 // The second neither.
796 item1_controller->ItemSelected(click_event);
797 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
798 EXPECT_TRUE(window1a->GetNativeWindow()->IsVisible());
799 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
800 EXPECT_FALSE(window1a->GetBaseWindow()->IsActive());
803 // Confirm that click behavior for app panels is correct.
804 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, AppPanelClickBehavior) {
805 // Enable experimental APIs to allow panel creation.
806 base::CommandLine::ForCurrentProcess()->AppendSwitch(
807 extensions::switches::kEnableExperimentalExtensionApis);
808 // Launch a platform app and create a panel window for it.
809 const Extension* extension1 = LoadAndLaunchPlatformApp("launch", "Launched");
810 AppWindow::CreateParams params;
811 params.window_type = AppWindow::WINDOW_TYPE_PANEL;
812 params.focused = false;
813 AppWindow* panel = CreateAppWindowFromParams(extension1, params);
814 EXPECT_TRUE(panel->GetNativeWindow()->IsVisible());
815 // Panels should not be active by default.
816 EXPECT_FALSE(panel->GetBaseWindow()->IsActive());
817 // Confirm that a controller item was created and is the correct state.
818 const ash::ShelfItem& item1 = GetLastLauncherPanelItem();
819 LauncherItemController* item1_controller = GetItemController(item1.id);
820 EXPECT_EQ(ash::TYPE_APP_PANEL, item1.type);
821 EXPECT_EQ(ash::STATUS_RUNNING, item1.status);
822 EXPECT_EQ(LauncherItemController::TYPE_APP_PANEL, item1_controller->type());
823 // Click the item and confirm that the panel is activated.
824 TestEvent click_event(ui::ET_MOUSE_PRESSED);
825 item1_controller->ItemSelected(click_event);
826 EXPECT_TRUE(panel->GetBaseWindow()->IsActive());
827 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
828 // Click the item again and confirm that the panel is minimized.
829 item1_controller->ItemSelected(click_event);
830 EXPECT_TRUE(panel->GetBaseWindow()->IsMinimized());
831 EXPECT_EQ(ash::STATUS_RUNNING, item1.status);
832 // Click the item again and confirm that the panel is activated.
833 item1_controller->ItemSelected(click_event);
834 EXPECT_TRUE(panel->GetNativeWindow()->IsVisible());
835 EXPECT_TRUE(panel->GetBaseWindow()->IsActive());
836 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
839 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, BrowserActivation) {
840 int item_count = shelf_model()->item_count();
842 // First run app.
843 const Extension* extension1 = LoadAndLaunchPlatformApp("launch", "Launched");
844 CreateAppWindow(extension1);
845 ++item_count;
846 ASSERT_EQ(item_count, shelf_model()->item_count());
847 const ash::ShelfItem& item1 = GetLastLauncherItem();
848 ash::ShelfID item_id1 = item1.id;
849 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
850 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
852 ash::wm::ActivateWindow(browser()->window()->GetNativeWindow());
853 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id1)->status);
856 // Test that opening an app sets the correct icon
857 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, SetIcon) {
858 TestAppWindowRegistryObserver test_observer(browser()->profile());
860 // Enable experimental APIs to allow panel creation.
861 base::CommandLine::ForCurrentProcess()->AppendSwitch(
862 extensions::switches::kEnableExperimentalExtensionApis);
864 int base_shelf_item_count = shelf_model()->item_count();
865 ExtensionTestMessageListener completed_listener("Completed", false);
866 LoadAndLaunchPlatformApp("app_icon", "Launched");
867 ASSERT_TRUE(completed_listener.WaitUntilSatisfied());
869 // Now wait until the WebContent has decoded the icons and chrome has
870 // processed it. This needs to be in a loop since the renderer runs in a
871 // different process.
872 while (test_observer.icon_updates() < 3) {
873 base::RunLoop run_loop;
874 run_loop.RunUntilIdle();
877 // This test creates one app window and one panel window.
878 int shelf_item_count = shelf_model()->item_count();
879 ASSERT_EQ(base_shelf_item_count + 2, shelf_item_count);
880 // The Panel will be the last item, the app second-to-last.
881 const ash::ShelfItem& app_item =
882 shelf_model()->items()[shelf_item_count - 2];
883 const ash::ShelfItem& panel_item =
884 shelf_model()->items()[shelf_item_count - 1];
885 const LauncherItemController* app_item_controller =
886 GetItemController(app_item.id);
887 const LauncherItemController* panel_item_controller =
888 GetItemController(panel_item.id);
889 // Icons for Apps are set by the AppWindowLauncherController, so
890 // image_set_by_controller() should be set.
891 EXPECT_TRUE(app_item_controller->image_set_by_controller());
892 EXPECT_TRUE(panel_item_controller->image_set_by_controller());
893 // Ensure icon heights are correct (see test.js in app_icon/ test directory)
894 EXPECT_EQ(ash::kShelfSize, app_item.image.height());
895 EXPECT_EQ(64, panel_item.image.height());
898 // Test that we can launch an app with a shortcut.
899 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchPinned) {
900 TabStripModel* tab_strip = browser()->tab_strip_model();
901 int tab_count = tab_strip->count();
902 ash::ShelfID shortcut_id = CreateShortcut("app1");
903 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
904 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
905 EXPECT_EQ(++tab_count, tab_strip->count());
906 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
907 WebContents* tab = tab_strip->GetActiveWebContents();
908 content::WebContentsDestroyedWatcher destroyed_watcher(tab);
909 browser()->tab_strip_model()->CloseSelectedTabs();
910 destroyed_watcher.Wait();
911 EXPECT_EQ(--tab_count, tab_strip->count());
912 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
915 // Launch the app first and then create the shortcut.
916 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchUnpinned) {
917 TabStripModel* tab_strip = browser()->tab_strip_model();
918 int tab_count = tab_strip->count();
919 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB,
920 NEW_FOREGROUND_TAB);
921 EXPECT_EQ(++tab_count, tab_strip->count());
922 ash::ShelfID shortcut_id = CreateShortcut("app1");
923 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
924 WebContents* tab = tab_strip->GetActiveWebContents();
925 content::WebContentsDestroyedWatcher destroyed_watcher(tab);
926 browser()->tab_strip_model()->CloseSelectedTabs();
927 destroyed_watcher.Wait();
928 EXPECT_EQ(--tab_count, tab_strip->count());
929 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
932 // Launches an app in the background and then tries to open it. This is test for
933 // a crash we had.
934 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchInBackground) {
935 TabStripModel* tab_strip = browser()->tab_strip_model();
936 int tab_count = tab_strip->count();
937 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB,
938 NEW_BACKGROUND_TAB);
939 EXPECT_EQ(++tab_count, tab_strip->count());
940 ChromeLauncherController::instance()->LaunchApp(last_loaded_extension_id(),
941 ash::LAUNCH_FROM_UNKNOWN,
945 // Confirm that clicking a icon for an app running in one of 2 maxmized windows
946 // activates the right window.
947 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchMaximized) {
948 aura::Window* window1 = browser()->window()->GetNativeWindow();
949 ash::wm::WindowState* window1_state = ash::wm::GetWindowState(window1);
950 window1_state->Maximize();
951 content::WindowedNotificationObserver open_observer(
952 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
953 content::NotificationService::AllSources());
954 chrome::NewEmptyWindow(browser()->profile(), chrome::HOST_DESKTOP_TYPE_ASH);
955 open_observer.Wait();
956 Browser* browser2 = content::Source<Browser>(open_observer.source()).ptr();
957 aura::Window* window2 = browser2->window()->GetNativeWindow();
958 TabStripModel* tab_strip = browser2->tab_strip_model();
959 int tab_count = tab_strip->count();
960 ash::wm::GetWindowState(window2)->Maximize();
962 ash::ShelfID shortcut_id = CreateShortcut("app1");
963 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
964 EXPECT_EQ(++tab_count, tab_strip->count());
965 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
967 window1->Show();
968 window1_state->Activate();
969 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut_id)).status);
971 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
972 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
975 // Activating the same app multiple times should launch only a single copy.
976 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ActivateApp) {
977 TabStripModel* tab_strip = browser()->tab_strip_model();
978 int tab_count = tab_strip->count();
979 const Extension* extension =
980 LoadExtension(test_data_dir_.AppendASCII("app1"));
982 ChromeLauncherController::instance()->ActivateApp(extension->id(),
983 ash::LAUNCH_FROM_UNKNOWN,
985 EXPECT_EQ(++tab_count, tab_strip->count());
986 ChromeLauncherController::instance()->ActivateApp(extension->id(),
987 ash::LAUNCH_FROM_UNKNOWN,
989 EXPECT_EQ(tab_count, tab_strip->count());
992 // Launching the same app multiple times should launch a copy for each call.
993 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchApp) {
994 TabStripModel* tab_strip = browser()->tab_strip_model();
995 int tab_count = tab_strip->count();
996 const Extension* extension =
997 LoadExtension(test_data_dir_.AppendASCII("app1"));
999 ChromeLauncherController::instance()->LaunchApp(extension->id(),
1000 ash::LAUNCH_FROM_UNKNOWN,
1002 EXPECT_EQ(++tab_count, tab_strip->count());
1003 ChromeLauncherController::instance()->LaunchApp(extension->id(),
1004 ash::LAUNCH_FROM_UNKNOWN,
1006 EXPECT_EQ(++tab_count, tab_strip->count());
1009 // Launch 2 apps and toggle which is active.
1010 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, MultipleApps) {
1011 int item_count = model_->item_count();
1012 TabStripModel* tab_strip = browser()->tab_strip_model();
1013 int tab_count = tab_strip->count();
1014 ash::ShelfID shortcut1 = CreateShortcut("app1");
1015 EXPECT_EQ(++item_count, model_->item_count());
1016 ash::ShelfID shortcut2 = CreateShortcut("app2");
1017 EXPECT_EQ(++item_count, model_->item_count());
1019 // Launch first app.
1020 ActivateShelfItem(model_->ItemIndexByID(shortcut1));
1021 EXPECT_EQ(++tab_count, tab_strip->count());
1022 WebContents* tab1 = tab_strip->GetActiveWebContents();
1023 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut1)).status);
1025 // Launch second app.
1026 ActivateShelfItem(model_->ItemIndexByID(shortcut2));
1027 EXPECT_EQ(++tab_count, tab_strip->count());
1028 WebContents* tab2 = tab_strip->GetActiveWebContents();
1029 ASSERT_NE(tab1, tab2);
1030 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut1)).status);
1031 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut2)).status);
1033 // Reactivate first app.
1034 ActivateShelfItem(model_->ItemIndexByID(shortcut1));
1035 EXPECT_EQ(tab_count, tab_strip->count());
1036 EXPECT_EQ(tab_strip->GetActiveWebContents(), tab1);
1037 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut1)).status);
1038 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut2)).status);
1040 // Open second tab for second app. This should activate it.
1041 ui_test_utils::NavigateToURLWithDisposition(
1042 browser(),
1043 GURL("http://www.example.com/path3/foo.html"),
1044 NEW_FOREGROUND_TAB,
1046 EXPECT_EQ(++tab_count, tab_strip->count());
1047 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut1)).status);
1048 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut2)).status);
1050 // Reactivate first app.
1051 ActivateShelfItem(model_->ItemIndexByID(shortcut1));
1052 EXPECT_EQ(tab_count, tab_strip->count());
1053 EXPECT_EQ(tab_strip->GetActiveWebContents(), tab1);
1054 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut1)).status);
1055 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut2)).status);
1057 // And second again. This time the second tab should become active.
1058 ActivateShelfItem(model_->ItemIndexByID(shortcut2));
1059 EXPECT_EQ(tab_count, tab_strip->count());
1060 EXPECT_EQ(tab_strip->GetActiveWebContents(), tab2);
1061 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut1)).status);
1062 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut2)).status);
1065 // Confirm that a page can be navigated from and to while maintaining the
1066 // correct running state.
1067 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, Navigation) {
1068 ash::ShelfID shortcut_id = CreateShortcut("app1");
1069 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
1070 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1071 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
1073 // Navigate away.
1074 ui_test_utils::NavigateToURL(
1075 browser(), GURL("http://www.example.com/path0/bar.html"));
1076 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
1078 // Navigate back.
1079 ui_test_utils::NavigateToURL(
1080 browser(), GURL("http://www.example.com/path1/foo.html"));
1081 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
1084 // Confirm that a tab can be moved between browsers while maintaining the
1085 // correct running state.
1086 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, TabDragAndDrop) {
1087 TabStripModel* tab_strip_model1 = browser()->tab_strip_model();
1088 EXPECT_EQ(1, tab_strip_model1->count());
1089 int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1090 EXPECT_TRUE(browser_index >= 0);
1091 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1093 // Create a shortcut for app1.
1094 ash::ShelfID shortcut_id = CreateShortcut("app1");
1095 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1096 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
1098 // Activate app1 and check its item status.
1099 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1100 EXPECT_EQ(2, tab_strip_model1->count());
1101 EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[browser_index].status);
1102 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
1104 // Create a new browser with blank tab.
1105 Browser* browser2 = CreateBrowser(profile());
1106 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1107 TabStripModel* tab_strip_model2 = browser2->tab_strip_model();
1108 EXPECT_EQ(1, tab_strip_model2->count());
1109 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1110 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut_id)).status);
1112 // Detach a tab at index 1 (app1) from |tab_strip_model1| and insert it as an
1113 // active tab at index 1 to |tab_strip_model2|.
1114 content::WebContents* detached_tab = tab_strip_model1->DetachWebContentsAt(1);
1115 tab_strip_model2->InsertWebContentsAt(1,
1116 detached_tab,
1117 TabStripModel::ADD_ACTIVE);
1118 EXPECT_EQ(1, tab_strip_model1->count());
1119 EXPECT_EQ(2, tab_strip_model2->count());
1120 EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[browser_index].status);
1121 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
1123 tab_strip_model1->CloseAllTabs();
1124 tab_strip_model2->CloseAllTabs();
1127 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, MultipleOwnedTabs) {
1128 TabStripModel* tab_strip = browser()->tab_strip_model();
1129 int tab_count = tab_strip->count();
1130 ash::ShelfID shortcut_id = CreateShortcut("app1");
1131 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1132 EXPECT_EQ(++tab_count, tab_strip->count());
1133 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1134 WebContents* first_tab = tab_strip->GetActiveWebContents();
1136 // Create new tab owned by app.
1137 ui_test_utils::NavigateToURLWithDisposition(
1138 browser(),
1139 GURL("http://www.example.com/path2/bar.html"),
1140 NEW_FOREGROUND_TAB,
1141 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1142 EXPECT_EQ(++tab_count, tab_strip->count());
1143 // Confirm app is still active.
1144 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1146 // Create new tab not owned by app.
1147 ui_test_utils::NavigateToURLWithDisposition(
1148 browser(),
1149 GURL("http://www.example.com/path3/foo.html"),
1150 NEW_FOREGROUND_TAB,
1152 EXPECT_EQ(++tab_count, tab_strip->count());
1153 // No longer active.
1154 EXPECT_EQ(ash::STATUS_RUNNING, model_->ItemByID(shortcut_id)->status);
1156 // Activating app makes first tab active again.
1157 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1158 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1159 EXPECT_EQ(tab_strip->GetActiveWebContents(), first_tab);
1162 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, RefocusFilter) {
1163 TabStripModel* tab_strip = browser()->tab_strip_model();
1164 int tab_count = tab_strip->count();
1165 ash::ShelfID shortcut_id = CreateShortcut("app1");
1166 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1167 EXPECT_EQ(++tab_count, tab_strip->count());
1168 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1169 WebContents* first_tab = tab_strip->GetActiveWebContents();
1171 controller_->SetRefocusURLPatternForTest(
1172 shortcut_id, GURL("http://www.example.com/path1/*"));
1173 // Create new tab owned by app.
1174 ui_test_utils::NavigateToURLWithDisposition(
1175 browser(),
1176 GURL("http://www.example.com/path2/bar.html"),
1177 NEW_FOREGROUND_TAB,
1178 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1179 EXPECT_EQ(++tab_count, tab_strip->count());
1180 // Confirm app is still active.
1181 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1183 // Create new tab not owned by app.
1184 ui_test_utils::NavigateToURLWithDisposition(
1185 browser(),
1186 GURL("http://www.example.com/path3/foo.html"),
1187 NEW_FOREGROUND_TAB,
1189 EXPECT_EQ(++tab_count, tab_strip->count());
1190 // No longer active.
1191 EXPECT_EQ(ash::STATUS_RUNNING, model_->ItemByID(shortcut_id)->status);
1193 // Activating app makes first tab active again, because second tab isn't
1194 // in its refocus url path.
1195 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1196 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1197 EXPECT_EQ(tab_strip->GetActiveWebContents(), first_tab);
1200 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, RefocusFilterLaunch) {
1201 TabStripModel* tab_strip = browser()->tab_strip_model();
1202 int tab_count = tab_strip->count();
1203 ash::ShelfID shortcut_id = CreateShortcut("app1");
1204 controller_->SetRefocusURLPatternForTest(
1205 shortcut_id, GURL("http://www.example.com/path1/*"));
1207 // Create new tab.
1208 ui_test_utils::NavigateToURLWithDisposition(
1209 browser(),
1210 GURL("http://www.example2.com/path2/bar.html"),
1211 NEW_FOREGROUND_TAB,
1212 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1213 EXPECT_EQ(++tab_count, tab_strip->count());
1214 WebContents* first_tab = tab_strip->GetActiveWebContents();
1215 // Confirm app is not active.
1216 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1218 // Activating app should launch new tab, because second tab isn't
1219 // in its refocus url path.
1220 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1221 EXPECT_EQ(++tab_count, tab_strip->count());
1222 WebContents* second_tab = tab_strip->GetActiveWebContents();
1223 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1224 EXPECT_NE(first_tab, second_tab);
1225 EXPECT_EQ(tab_strip->GetActiveWebContents(), second_tab);
1228 // Check the launcher activation state for applications and browser.
1229 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ActivationStateCheck) {
1230 TabStripModel* tab_strip = browser()->tab_strip_model();
1231 // Get the browser item index
1232 int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1233 EXPECT_TRUE(browser_index >= 0);
1235 // Even though we are just comming up, the browser should be active.
1236 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1238 ash::ShelfID shortcut_id = CreateShortcut("app1");
1239 controller_->SetRefocusURLPatternForTest(
1240 shortcut_id, GURL("http://www.example.com/path1/*"));
1242 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1243 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1245 // Create new tab which would be the running app.
1246 ui_test_utils::NavigateToURLWithDisposition(
1247 browser(),
1248 GURL("http://www.example.com/path1/bar.html"),
1249 NEW_FOREGROUND_TAB,
1250 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1252 // There should never be two items active at the same time.
1253 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1254 EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[browser_index].status);
1256 tab_strip->ActivateTabAt(0, false);
1257 EXPECT_EQ(ash::STATUS_RUNNING, model_->ItemByID(shortcut_id)->status);
1258 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1260 tab_strip->CloseWebContentsAt(1, TabStripModel::CLOSE_NONE);
1261 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1262 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1264 ash::wm::DeactivateWindow(browser()->window()->GetNativeWindow());
1265 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1266 EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[browser_index].status);
1269 // Check that the launcher activation state for a V1 application stays closed
1270 // even after an asynchronous browser event comes in after the tab got
1271 // destroyed.
1272 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, AsyncActivationStateCheck) {
1273 TabStripModel* tab_strip = browser()->tab_strip_model();
1275 ash::ShelfID shortcut_id = CreateShortcut("app1");
1276 controller_->SetRefocusURLPatternForTest(
1277 shortcut_id, GURL("http://www.example.com/path1/*"));
1279 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1281 // Create new tab which would be the running app.
1282 ui_test_utils::NavigateToURLWithDisposition(
1283 browser(),
1284 GURL("http://www.example.com/path1/bar.html"),
1285 NEW_FOREGROUND_TAB,
1286 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1288 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1289 // To address the issue of crbug.com/174050, the tab we are about to close
1290 // has to be active.
1291 tab_strip->ActivateTabAt(1, false);
1292 EXPECT_EQ(1, tab_strip->active_index());
1294 // Close the web contents.
1295 tab_strip->CloseWebContentsAt(1, TabStripModel::CLOSE_NONE);
1296 // The status should now be set to closed.
1297 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1300 // Checks that a windowed application does not add an item to the browser list.
1301 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1302 WindowedAppDoesNotAddToBrowser) {
1303 // Get the number of items in the browser menu.
1304 size_t items = NumberOfDetectedLauncherBrowsers(false);
1305 size_t running_browser = chrome::GetTotalBrowserCount();
1306 EXPECT_EQ(0u, items);
1307 EXPECT_EQ(0u, running_browser);
1309 LoadAndLaunchExtension(
1310 "app1", extensions::LAUNCH_CONTAINER_WINDOW, NEW_WINDOW);
1312 // No new browser should get detected, even though one more is running.
1313 EXPECT_EQ(0u, NumberOfDetectedLauncherBrowsers(false));
1314 EXPECT_EQ(++running_browser, chrome::GetTotalBrowserCount());
1316 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1318 // A new browser should get detected and one more should be running.
1319 EXPECT_EQ(NumberOfDetectedLauncherBrowsers(false), 1u);
1320 EXPECT_EQ(++running_browser, chrome::GetTotalBrowserCount());
1323 // Checks the functionality to enumerate all browsers vs. all tabs.
1324 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1325 EnumerateALlBrowsersAndTabs) {
1326 // Create at least one browser.
1327 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1328 size_t browsers = NumberOfDetectedLauncherBrowsers(false);
1329 size_t tabs = NumberOfDetectedLauncherBrowsers(true);
1331 // Create a second browser.
1332 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1334 EXPECT_EQ(++browsers, NumberOfDetectedLauncherBrowsers(false));
1335 EXPECT_EQ(++tabs, NumberOfDetectedLauncherBrowsers(true));
1337 // Create only a tab.
1338 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB,
1339 NEW_FOREGROUND_TAB);
1341 EXPECT_EQ(browsers, NumberOfDetectedLauncherBrowsers(false));
1342 EXPECT_EQ(++tabs, NumberOfDetectedLauncherBrowsers(true));
1345 // Check that the keyboard activation of a launcher item tabs properly through
1346 // the items at hand.
1347 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, AltNumberTabsTabbing) {
1348 TabStripModel* tab_strip = browser()->tab_strip_model();
1350 ash::ShelfID shortcut_id = CreateShortcut("app");
1351 controller_->SetRefocusURLPatternForTest(
1352 shortcut_id, GURL("http://www.example.com/path/*"));
1353 std::string url = "http://www.example.com/path/bla";
1355 int shortcut_index = model_->ItemIndexByID(shortcut_id);
1357 // Create an application handled browser tab.
1358 ui_test_utils::NavigateToURLWithDisposition(
1359 browser(),
1360 GURL(url),
1361 NEW_FOREGROUND_TAB,
1362 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1364 content::WebContents* content1 = tab_strip->GetActiveWebContents();
1366 // Create some other browser tab.
1367 ui_test_utils::NavigateToURLWithDisposition(
1368 browser(),
1369 GURL("http://www.test.com"),
1370 NEW_FOREGROUND_TAB,
1371 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1372 content::WebContents* content1a = tab_strip->GetActiveWebContents();
1374 // Make sure that the active tab is now our handled tab.
1375 EXPECT_NE(content1a, content1);
1377 // The active tab should still be the unnamed tab. Then we switch and reach
1378 // the first app and stay there.
1379 EXPECT_EQ(content1a, tab_strip->GetActiveWebContents());
1380 ActivateShelfItem(shortcut_index);
1381 EXPECT_EQ(content1, tab_strip->GetActiveWebContents());
1382 ActivateShelfItem(shortcut_index);
1383 EXPECT_EQ(content1, tab_strip->GetActiveWebContents());
1385 ui_test_utils::NavigateToURLWithDisposition(
1386 browser(),
1387 GURL(url),
1388 NEW_FOREGROUND_TAB,
1389 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1390 content::WebContents* content2 = tab_strip->GetActiveWebContents();
1392 EXPECT_EQ(content2, browser()->tab_strip_model()->GetActiveWebContents());
1393 ActivateShelfItem(shortcut_index);
1394 EXPECT_EQ(content1, browser()->tab_strip_model()->GetActiveWebContents());
1395 ActivateShelfItem(shortcut_index);
1396 EXPECT_EQ(content2, browser()->tab_strip_model()->GetActiveWebContents());
1399 // Check that the keyboard activation of a launcher item tabs properly through
1400 // the items at hand.
1401 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest,
1402 AltNumberAppsTabbing) {
1403 // First run app.
1404 const Extension* extension1 = LoadAndLaunchPlatformApp("launch", "Launched");
1405 ui::BaseWindow* window1 = CreateAppWindow(extension1)->GetBaseWindow();
1406 const ash::ShelfItem& item1 = GetLastLauncherItem();
1407 ash::ShelfID app_id = item1.id;
1408 int app_index = shelf_model()->ItemIndexByID(app_id);
1410 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
1411 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
1413 const Extension* extension2 = LoadAndLaunchPlatformApp("launch_2",
1414 "Launched");
1415 ui::BaseWindow* window2 = CreateAppWindow(extension2)->GetBaseWindow();
1417 // By now the browser should be active. Issue Alt keystrokes several times to
1418 // see that we stay on that application.
1419 EXPECT_TRUE(window2->IsActive());
1420 ActivateShelfItem(app_index);
1421 EXPECT_TRUE(window1->IsActive());
1422 ActivateShelfItem(app_index);
1423 EXPECT_TRUE(window1->IsActive());
1425 ui::BaseWindow* window1a = CreateAppWindow(extension1)->GetBaseWindow();
1427 EXPECT_TRUE(window1a->IsActive());
1428 EXPECT_FALSE(window1->IsActive());
1429 ActivateShelfItem(app_index);
1430 EXPECT_TRUE(window1->IsActive());
1431 ActivateShelfItem(app_index);
1432 EXPECT_TRUE(window1a->IsActive());
1435 // Test that we can launch a platform app panel and get a running item.
1436 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, LaunchPanelWindow) {
1437 int item_count = shelf_model()->item_count();
1438 const Extension* extension = LoadAndLaunchPlatformApp("launch", "Launched");
1439 AppWindow::CreateParams params;
1440 params.window_type = AppWindow::WINDOW_TYPE_PANEL;
1441 params.focused = false;
1442 AppWindow* window = CreateAppWindowFromParams(extension, params);
1443 ++item_count;
1444 ASSERT_EQ(item_count, shelf_model()->item_count());
1445 const ash::ShelfItem& item = GetLastLauncherPanelItem();
1446 EXPECT_EQ(ash::TYPE_APP_PANEL, item.type);
1447 // Opening a panel does not activate it.
1448 EXPECT_EQ(ash::STATUS_RUNNING, item.status);
1449 CloseAppWindow(window);
1450 --item_count;
1451 EXPECT_EQ(item_count, shelf_model()->item_count());
1454 // Test that we get correct shelf presence with hidden app windows.
1455 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, HiddenAppWindows) {
1456 int item_count = shelf_model()->item_count();
1457 const Extension* extension = LoadAndLaunchPlatformApp("launch", "Launched");
1458 AppWindow::CreateParams params;
1460 // Create a hidden window.
1461 params.hidden = true;
1462 AppWindow* window_1 = CreateAppWindowFromParams(extension, params);
1463 EXPECT_EQ(item_count, shelf_model()->item_count());
1465 // Create a visible window.
1466 params.hidden = false;
1467 AppWindow* window_2 = CreateAppWindowFromParams(extension, params);
1468 ++item_count;
1469 EXPECT_EQ(item_count, shelf_model()->item_count());
1471 // Minimize the visible window.
1472 window_2->Minimize();
1473 EXPECT_EQ(item_count, shelf_model()->item_count());
1475 // Hide the visible window.
1476 window_2->Hide();
1477 --item_count;
1478 EXPECT_EQ(item_count, shelf_model()->item_count());
1480 // Show the originally hidden window.
1481 window_1->Show(AppWindow::SHOW_ACTIVE);
1482 ++item_count;
1483 EXPECT_EQ(item_count, shelf_model()->item_count());
1485 // Close the originally hidden window.
1486 CloseAppWindow(window_1);
1487 --item_count;
1488 EXPECT_EQ(item_count, shelf_model()->item_count());
1491 // Test attention states of windows.
1492 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, WindowAttentionStatus) {
1493 const Extension* extension = LoadAndLaunchPlatformApp("launch", "Launched");
1494 AppWindow::CreateParams params;
1495 params.window_type = AppWindow::WINDOW_TYPE_PANEL;
1496 params.focused = false;
1497 AppWindow* panel = CreateAppWindowFromParams(extension, params);
1498 EXPECT_TRUE(panel->GetNativeWindow()->IsVisible());
1499 // Panels should not be active by default.
1500 EXPECT_FALSE(panel->GetBaseWindow()->IsActive());
1501 // Confirm that a controller item was created and is the correct state.
1502 const ash::ShelfItem& item = GetLastLauncherPanelItem();
1503 LauncherItemController* item_controller = GetItemController(item.id);
1504 EXPECT_EQ(ash::TYPE_APP_PANEL, item.type);
1505 EXPECT_EQ(ash::STATUS_RUNNING, item.status);
1506 EXPECT_EQ(LauncherItemController::TYPE_APP_PANEL, item_controller->type());
1508 // App windows should go to attention state.
1509 panel->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey, true);
1510 EXPECT_EQ(ash::STATUS_ATTENTION, item.status);
1512 // Click the item and confirm that the panel is activated.
1513 TestEvent click_event(ui::ET_MOUSE_PRESSED);
1514 item_controller->ItemSelected(click_event);
1515 EXPECT_TRUE(panel->GetBaseWindow()->IsActive());
1516 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
1518 // Active windows don't show attention.
1519 panel->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey, true);
1520 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
1523 // Checks that the browser Alt "tabbing" is properly done.
1524 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1525 AltNumberBrowserTabbing) {
1526 // Get the number of items in the browser menu.
1527 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
1528 // The first activation should create a browser at index 1 (App List @ 0).
1529 shelf_->ActivateShelfItem(1);
1530 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1531 // A second activation should not create a new instance.
1532 shelf_->ActivateShelfItem(1);
1533 Browser* browser1 = chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow());
1534 EXPECT_TRUE(browser1);
1535 aura::Window* window1 = browser1->window()->GetNativeWindow();
1536 Browser* browser2 = CreateBrowser(profile());
1537 aura::Window* window2 = browser2->window()->GetNativeWindow();
1539 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1540 EXPECT_NE(window1, window2);
1541 EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1543 // Activate multiple times the switcher to see that the windows get activated.
1544 shelf_->ActivateShelfItem(1);
1545 EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1546 shelf_->ActivateShelfItem(1);
1547 EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1549 // Create a third browser - make sure that we do not toggle simply between
1550 // two windows.
1551 Browser* browser3 = CreateBrowser(profile());
1552 aura::Window* window3 = browser3->window()->GetNativeWindow();
1554 EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
1555 EXPECT_NE(window1, window3);
1556 EXPECT_NE(window2, window3);
1557 EXPECT_EQ(window3, ash::wm::GetActiveWindow());
1559 shelf_->ActivateShelfItem(1);
1560 EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1561 shelf_->ActivateShelfItem(1);
1562 EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1563 shelf_->ActivateShelfItem(1);
1564 EXPECT_EQ(window3, ash::wm::GetActiveWindow());
1565 shelf_->ActivateShelfItem(1);
1566 EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1568 // Create anther app and make sure that none of our browsers is active.
1569 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1570 EXPECT_NE(window1, ash::wm::GetActiveWindow());
1571 EXPECT_NE(window2, ash::wm::GetActiveWindow());
1573 // After activation our browser should be active again.
1574 shelf_->ActivateShelfItem(1);
1575 EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1578 // Checks that after a session restore, we do not start applications on an
1579 // activation.
1580 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ActivateAfterSessionRestore) {
1581 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1583 // Create a known application.
1584 ash::ShelfID shortcut_id = CreateShortcut("app1");
1586 // Create a new browser - without activating it - and load an "app" into it.
1587 Browser::CreateParams params =
1588 Browser::CreateParams(profile(), chrome::GetActiveDesktop());
1589 params.initial_show_state = ui::SHOW_STATE_INACTIVE;
1590 Browser* browser2 = new Browser(params);
1591 controller_->SetRefocusURLPatternForTest(
1592 shortcut_id, GURL("http://www.example.com/path/*"));
1593 std::string url = "http://www.example.com/path/bla";
1594 ui_test_utils::NavigateToURLWithDisposition(
1595 browser2,
1596 GURL(url),
1597 NEW_FOREGROUND_TAB,
1598 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1600 // Remember the number of tabs for each browser.
1601 TabStripModel* tab_strip = browser()->tab_strip_model();
1602 int tab_count1 = tab_strip->count();
1603 TabStripModel* tab_strip2 = browser2->tab_strip_model();
1604 int tab_count2 = tab_strip2->count();
1606 // Check that we have two browsers and the inactive browser remained inactive.
1607 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1608 EXPECT_EQ(chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()),
1609 browser());
1610 // Check that the LRU browser list does only contain the original browser.
1611 BrowserList* ash_browser_list =
1612 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
1613 BrowserList::const_reverse_iterator it =
1614 ash_browser_list->begin_last_active();
1615 EXPECT_EQ(*it, browser());
1616 ++it;
1617 EXPECT_EQ(it, ash_browser_list->end_last_active());
1619 // Now request to either activate an existing app or create a new one.
1620 LauncherItemController* item_controller =
1621 controller_->GetLauncherItemController(shortcut_id);
1622 item_controller->ItemSelected(ui::KeyEvent(ui::ET_KEY_RELEASED,
1623 ui::VKEY_RETURN,
1624 ui::EF_NONE));
1626 // Check that we have set focus on the existing application and nothing new
1627 // was created.
1628 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1629 EXPECT_EQ(tab_count1, tab_strip->count());
1630 EXPECT_EQ(tab_count2, tab_strip2->count());
1631 EXPECT_EQ(chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()),
1632 browser2);
1635 // Do various drag and drop interaction tests between the application list and
1636 // the launcher.
1637 // TODO(skuhne): Test is flaky with a real compositor: crbug.com/331924
1638 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, DISABLED_DragAndDrop) {
1639 // Get a number of interfaces we need.
1640 ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
1641 gfx::Point());
1642 ash::test::ShelfViewTestAPI test(
1643 ash::test::ShelfTestAPI(shelf_).shelf_view());
1644 AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1646 // There should be two items in our launcher by this time.
1647 EXPECT_EQ(2, model_->item_count());
1648 EXPECT_FALSE(service->IsAppListVisible());
1650 // Open the app list menu and check that the drag and drop host was set.
1651 gfx::Rect app_list_bounds =
1652 test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1653 generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1654 app_list_bounds.CenterPoint().y());
1655 base::MessageLoop::current()->RunUntilIdle();
1656 generator.ClickLeftButton();
1658 EXPECT_TRUE(service->IsAppListVisible());
1659 app_list::AppsGridView* grid_view =
1660 ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
1661 GetRootGridView();
1662 ASSERT_TRUE(grid_view);
1663 ASSERT_TRUE(grid_view->has_drag_and_drop_host_for_test());
1665 // There should be 2 items in our application list.
1666 const views::ViewModelT<app_list::AppListItemView>* vm_grid =
1667 grid_view->view_model_for_test();
1668 EXPECT_EQ(2, vm_grid->view_size());
1670 // Test #1: Drag an app list which does not exist yet item into the
1671 // launcher. Keeping it dragged, see that a new item gets created. Continuing
1672 // to drag it out should remove it again.
1674 // Get over item #1 of the application list and press the mouse button.
1675 views::View* item1 = vm_grid->view_at(1);
1676 gfx::Rect bounds_grid_1 = item1->GetBoundsInScreen();
1677 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1678 bounds_grid_1.CenterPoint().y());
1679 base::MessageLoop::current()->RunUntilIdle();
1680 generator.PressLeftButton();
1682 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1684 // Drag the item into the shelf and check that a new item gets created.
1685 const views::ViewModel* vm_shelf = test.shelf_view()->view_model_for_test();
1686 views::View* shelf1 = vm_shelf->view_at(1);
1687 gfx::Rect bounds_shelf_1 = shelf1->GetBoundsInScreen();
1688 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1689 bounds_shelf_1.CenterPoint().y());
1690 base::MessageLoop::current()->RunUntilIdle();
1692 // Check that a new item got created.
1693 EXPECT_EQ(3, model_->item_count());
1694 EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1696 // Move it where the item originally was and check that it disappears again.
1697 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1698 bounds_grid_1.CenterPoint().y());
1699 base::MessageLoop::current()->RunUntilIdle();
1700 EXPECT_EQ(2, model_->item_count());
1701 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1703 // Dropping it should keep the launcher as it originally was.
1704 generator.ReleaseLeftButton();
1705 base::MessageLoop::current()->RunUntilIdle();
1706 EXPECT_EQ(2, model_->item_count());
1707 // There are a few animations which need finishing before we can continue.
1708 test.RunMessageLoopUntilAnimationsDone();
1709 // Move the mouse outside of the launcher.
1710 generator.MoveMouseTo(0, 0);
1712 // Test #2: Check that the unknown item dropped into the launcher will
1713 // create a new item.
1714 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1715 bounds_grid_1.CenterPoint().y());
1716 generator.PressLeftButton();
1717 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1718 bounds_shelf_1.CenterPoint().y());
1719 base::MessageLoop::current()->RunUntilIdle();
1720 EXPECT_EQ(3, model_->item_count());
1721 EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1722 generator.ReleaseLeftButton();
1723 base::MessageLoop::current()->RunUntilIdle();
1724 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1725 EXPECT_EQ(3, model_->item_count()); // It should be still there.
1726 test.RunMessageLoopUntilAnimationsDone();
1728 // Test #3: Check that the now known item dropped into the launcher will
1729 // not create a new item.
1730 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1731 bounds_grid_1.CenterPoint().y());
1732 generator.PressLeftButton();
1733 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1734 bounds_shelf_1.CenterPoint().y());
1735 base::MessageLoop::current()->RunUntilIdle();
1736 EXPECT_EQ(3, model_->item_count()); // No new item got added.
1737 EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1738 generator.ReleaseLeftButton();
1739 base::MessageLoop::current()->RunUntilIdle();
1740 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1741 EXPECT_EQ(3, model_->item_count()); // And it remains that way.
1743 // Test #4: Check that by pressing ESC the operation gets cancelled.
1744 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1745 bounds_grid_1.CenterPoint().y());
1746 generator.PressLeftButton();
1747 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1748 bounds_shelf_1.CenterPoint().y());
1749 base::MessageLoop::current()->RunUntilIdle();
1750 // Issue an ESC and see that the operation gets cancelled.
1751 generator.PressKey(ui::VKEY_ESCAPE, 0);
1752 generator.ReleaseKey(ui::VKEY_ESCAPE, 0);
1753 EXPECT_FALSE(grid_view->dragging());
1754 EXPECT_FALSE(grid_view->has_dragged_view());
1755 generator.ReleaseLeftButton();
1758 #if !defined(OS_WIN)
1759 // Used to test drag & drop an item between app list and shelf with multi
1760 // display environment.
1761 class ShelfAppBrowserTestWithMultiMonitor
1762 : public ShelfAppBrowserTestNoDefaultBrowser {
1763 protected:
1764 ShelfAppBrowserTestWithMultiMonitor() {}
1765 ~ShelfAppBrowserTestWithMultiMonitor() override {}
1767 void SetUpCommandLine(base::CommandLine* command_line) override {
1768 ShelfAppBrowserTestNoDefaultBrowser::SetUpCommandLine(command_line);
1769 command_line->AppendSwitchASCII("ash-host-window-bounds",
1770 "800x800,801+0-800x800");
1773 private:
1775 DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserTestWithMultiMonitor);
1778 // Do basic drag and drop interaction tests between the application list and
1779 // the launcher in the secondary monitor.
1780 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestWithMultiMonitor,
1781 BasicDragAndDrop) {
1782 // Get a number of interfaces we need.
1783 DCHECK_EQ(ash::Shell::GetAllRootWindows().size(), 2U);
1784 aura::Window* secondary_root_window = ash::Shell::GetAllRootWindows()[1];
1785 ash::Shelf* secondary_shelf = ash::Shelf::ForWindow(secondary_root_window);
1787 ui::test::EventGenerator generator(secondary_root_window, gfx::Point());
1788 ash::test::ShelfViewTestAPI test(
1789 ash::test::ShelfTestAPI(secondary_shelf).shelf_view());
1790 AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1792 // There should be two items in our shelf by this time.
1793 EXPECT_EQ(2, model_->item_count());
1794 EXPECT_FALSE(service->IsAppListVisible());
1796 // Open the app list menu and check that the drag and drop host was set.
1797 gfx::Rect app_list_bounds =
1798 test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1799 gfx::Display display =
1800 ash::Shell::GetScreen()->GetDisplayNearestWindow(secondary_root_window);
1801 const gfx::Point& origin = display.bounds().origin();
1802 app_list_bounds.Offset(-origin.x(), -origin.y());
1804 generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1805 app_list_bounds.CenterPoint().y());
1806 generator.ClickLeftButton();
1807 base::MessageLoop::current()->RunUntilIdle();
1808 EXPECT_TRUE(service->IsAppListVisible());
1810 // Click the "all apps" button on the start page.
1811 ClickAllAppsButtonFromStartPage(&generator, origin);
1812 EXPECT_TRUE(service->IsAppListVisible());
1814 app_list::AppsGridView* grid_view =
1815 ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
1816 GetRootGridView();
1817 ASSERT_TRUE(grid_view);
1818 ASSERT_TRUE(grid_view->has_drag_and_drop_host_for_test());
1820 // There should be 2 items in our application list.
1821 const views::ViewModelT<app_list::AppListItemView>* vm_grid =
1822 grid_view->view_model_for_test();
1823 EXPECT_EQ(2, vm_grid->view_size());
1825 // Drag an app list item which does not exist yet in the shelf.
1826 // Keeping it dragged, see that a new item gets created.
1827 // Continuing to drag it out should remove it again.
1829 // Get over item #1 of the application list and press the mouse button.
1830 views::View* item1 = vm_grid->view_at(1);
1831 gfx::Rect bounds_grid_1 = item1->GetBoundsInScreen();
1832 bounds_grid_1.Offset(-origin.x(), -origin.y());
1833 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1834 bounds_grid_1.CenterPoint().y());
1835 base::MessageLoop::current()->RunUntilIdle();
1836 generator.PressLeftButton();
1838 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1840 // Drag the item into the shelf and check that a new item gets created.
1841 const views::ViewModel* vm_shelf = test.shelf_view()->view_model_for_test();
1842 views::View* shelf1 = vm_shelf->view_at(1);
1843 gfx::Rect bounds_shelf_1 = shelf1->GetBoundsInScreen();
1844 bounds_shelf_1.Offset(-origin.x(), -origin.y());
1845 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1846 bounds_shelf_1.CenterPoint().y());
1847 base::MessageLoop::current()->RunUntilIdle();
1849 // Check that a new item got created.
1850 EXPECT_EQ(3, model_->item_count());
1851 EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1853 // Move it to an empty slot on grid_view.
1854 gfx::Rect empty_slot_rect = bounds_grid_1;
1855 empty_slot_rect.Offset(0, grid_view->GetTotalTileSize().height());
1856 generator.MoveMouseTo(empty_slot_rect.CenterPoint().x(),
1857 empty_slot_rect.CenterPoint().y());
1858 base::MessageLoop::current()->RunUntilIdle();
1859 EXPECT_EQ(2, model_->item_count());
1860 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1862 // Dropping it should keep the shelf as it originally was.
1863 generator.ReleaseLeftButton();
1864 base::MessageLoop::current()->RunUntilIdle();
1865 EXPECT_EQ(2, model_->item_count());
1867 #endif
1869 // Do tests for removal of items from the shelf by dragging.
1870 // Disabled due to flake: http://crbug.com/448482
1871 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, DISABLED_DragOffShelf) {
1872 ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
1873 gfx::Point());
1874 ash::test::ShelfViewTestAPI test(
1875 ash::test::ShelfTestAPI(shelf_).shelf_view());
1876 test.SetAnimationDuration(1); // Speed up animations for test.
1877 // Create a known application and check that we have 3 items in the shelf.
1878 CreateShortcut("app1");
1879 test.RunMessageLoopUntilAnimationsDone();
1880 EXPECT_EQ(3, model_->item_count());
1882 // Test #1: Ripping out the browser item should not change anything.
1883 int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1884 EXPECT_LE(0, browser_index);
1885 RipOffItemIndex(browser_index, &generator, &test, RIP_OFF_ITEM);
1886 // => It should not have been removed and the location should be unchanged.
1887 EXPECT_EQ(3, model_->item_count());
1888 EXPECT_EQ(browser_index,
1889 GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT));
1890 // Make sure that the hide state has been unset after the snap back animation
1891 // finished.
1892 ash::ShelfButton* button = test.GetButton(browser_index);
1893 EXPECT_FALSE(button->state() & ash::ShelfButton::STATE_HIDDEN);
1895 // Test #2: Ripping out the application and canceling the operation should
1896 // not change anything.
1897 int app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1898 EXPECT_LE(0, app_index);
1899 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM_AND_CANCEL);
1900 // => It should not have been removed and the location should be unchanged.
1901 ASSERT_EQ(3, model_->item_count());
1902 EXPECT_EQ(app_index, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1904 // Test #3: Ripping out the application and moving it back in should not
1905 // change anything.
1906 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM_AND_RETURN);
1907 // => It should not have been removed and the location should be unchanged.
1908 ASSERT_EQ(3, model_->item_count());
1909 // Through the operation the index might have changed.
1910 app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1912 // Test #4: Ripping out the application should remove the item.
1913 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1914 // => It should not have been removed and the location should be unchanged.
1915 EXPECT_EQ(2, model_->item_count());
1916 EXPECT_EQ(-1, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1918 // Test #5: Uninstalling an application while it is being ripped off should
1919 // not crash.
1920 ash::ShelfID app_id = CreateShortcut("app2");
1921 test.RunMessageLoopUntilAnimationsDone();
1922 int app2_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1923 EXPECT_EQ(3, model_->item_count()); // And it remains that way.
1924 RipOffItemIndex(app2_index,
1925 &generator,
1926 &test,
1927 RIP_OFF_ITEM_AND_DONT_RELEASE_MOUSE);
1928 RemoveShortcut(app_id);
1929 test.RunMessageLoopUntilAnimationsDone();
1930 EXPECT_EQ(2, model_->item_count()); // The item should now be gone.
1931 generator.ReleaseLeftButton();
1932 base::MessageLoop::current()->RunUntilIdle();
1933 EXPECT_EQ(2, model_->item_count()); // And it remains that way.
1934 EXPECT_EQ(-1, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1936 // Test #6: Ripping out the application when the overflow button exists.
1937 // After ripping out, overflow button should be removed.
1938 int items_added = 0;
1939 EXPECT_FALSE(test.IsOverflowButtonVisible());
1941 // Create fake app shortcuts until overflow button is created.
1942 while (!test.IsOverflowButtonVisible()) {
1943 std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
1944 PinFakeApp(fake_app_id);
1945 test.RunMessageLoopUntilAnimationsDone();
1947 ++items_added;
1948 ASSERT_LT(items_added, 10000);
1950 // Make one more item after creating a overflow button.
1951 std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
1952 PinFakeApp(fake_app_id);
1953 test.RunMessageLoopUntilAnimationsDone();
1955 int total_count = model_->item_count();
1956 app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1957 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1958 // When an item is ripped off from the shelf that has overflow button
1959 // (see crbug.com/3050787), it was hidden accidentally and was then
1960 // suppressing any further events. If handled correctly the operation will
1961 // however correctly done and the item will get removed (as well as the
1962 // overflow button).
1963 EXPECT_EQ(total_count - 1, model_->item_count());
1964 EXPECT_TRUE(test.IsOverflowButtonVisible());
1966 // Rip off again and the overflow button should has disappeared.
1967 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1968 EXPECT_EQ(total_count - 2, model_->item_count());
1969 EXPECT_FALSE(test.IsOverflowButtonVisible());
1972 // Check that clicking on an app shelf item launches a new browser.
1973 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ClickItem) {
1974 // Get a number of interfaces we need.
1975 ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
1976 gfx::Point());
1977 ash::test::ShelfViewTestAPI test(
1978 ash::test::ShelfTestAPI(shelf_).shelf_view());
1979 AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1980 // There should be two items in our shelf by this time.
1981 EXPECT_EQ(2, model_->item_count());
1982 EXPECT_FALSE(service->IsAppListVisible());
1984 // Open the app list menu and check that the drag and drop host was set.
1985 gfx::Rect app_list_bounds =
1986 test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1987 generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1988 app_list_bounds.CenterPoint().y());
1989 generator.ClickLeftButton();
1990 base::MessageLoop::current()->RunUntilIdle();
1991 EXPECT_TRUE(service->IsAppListVisible());
1993 // Click the "all apps" button on the start page.
1994 ClickAllAppsButtonFromStartPage(&generator, gfx::Point());
1995 EXPECT_TRUE(service->IsAppListVisible());
1997 // Click an app icon in the app grid view.
1998 app_list::AppsGridView* grid_view =
1999 ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
2000 GetRootGridView();
2001 ASSERT_TRUE(grid_view);
2002 const views::ViewModelT<app_list::AppListItemView>* vm_grid =
2003 grid_view->view_model_for_test();
2004 EXPECT_EQ(2, vm_grid->view_size());
2005 gfx::Rect bounds_grid_1 = vm_grid->view_at(1)->GetBoundsInScreen();
2006 // Test now that a click does create a new application tab.
2007 TabStripModel* tab_strip = browser()->tab_strip_model();
2008 int tab_count = tab_strip->count();
2009 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
2010 bounds_grid_1.CenterPoint().y());
2011 generator.ClickLeftButton();
2012 base::MessageLoop::current()->RunUntilIdle();
2013 EXPECT_EQ(tab_count + 1, tab_strip->count());
2016 // Check LauncherItemController of Browser Shortcut functionality.
2017 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
2018 BrowserShortcutLauncherItemController) {
2019 LauncherItemController* item_controller =
2020 controller_->GetBrowserShortcutLauncherItemController();
2022 // Get the number of browsers.
2023 size_t running_browser = chrome::GetTotalBrowserCount();
2024 EXPECT_EQ(0u, running_browser);
2025 EXPECT_FALSE(item_controller->IsOpen());
2027 // Activate. This creates new browser
2028 item_controller->Activate(ash::LAUNCH_FROM_UNKNOWN);
2029 // New Window is created.
2030 running_browser = chrome::GetTotalBrowserCount();
2031 EXPECT_EQ(1u, running_browser);
2032 EXPECT_TRUE(item_controller->IsOpen());
2034 // Minimize Window.
2035 ash::wm::WindowState* window_state = ash::wm::GetActiveWindowState();
2036 window_state->Minimize();
2037 EXPECT_TRUE(window_state->IsMinimized());
2039 // Activate again. This doesn't create new browser.
2040 // It activates window.
2041 item_controller->Activate(ash::LAUNCH_FROM_UNKNOWN);
2042 running_browser = chrome::GetTotalBrowserCount();
2043 EXPECT_EQ(1u, running_browser);
2044 EXPECT_TRUE(item_controller->IsOpen());
2045 EXPECT_FALSE(window_state->IsMinimized());
2048 // Check that GetShelfIDForWindow() returns |ShelfID| of the active tab.
2049 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, MatchingShelfIDandActiveTab) {
2050 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
2051 EXPECT_EQ(1, browser()->tab_strip_model()->count());
2052 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
2053 EXPECT_EQ(2, model_->item_count());
2055 aura::Window* window = browser()->window()->GetNativeWindow();
2057 int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
2058 ash::ShelfID browser_id = model_->items()[browser_index].id;
2059 EXPECT_EQ(browser_id, ash::GetShelfIDForWindow(window));
2061 ash::ShelfID app_id = CreateShortcut("app1");
2062 EXPECT_EQ(3, model_->item_count());
2064 // Creates a new tab for "app1" and checks that GetShelfIDForWindow()
2065 // returns |ShelfID| of "app1".
2066 ActivateShelfItem(model_->ItemIndexByID(app_id));
2067 EXPECT_EQ(2, browser()->tab_strip_model()->count());
2068 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
2069 EXPECT_EQ(app_id, ash::GetShelfIDForWindow(window));
2071 // Makes tab at index 0(NTP) as an active tab and checks that
2072 // GetShelfIDForWindow() returns |ShelfID| of browser shortcut.
2073 browser()->tab_strip_model()->ActivateTabAt(0, false);
2074 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
2075 EXPECT_EQ(browser_id, ash::GetShelfIDForWindow(window));
2078 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, OverflowBubble) {
2079 // Make sure to have a browser window
2080 chrome::NewTab(browser());
2082 // No overflow yet.
2083 EXPECT_FALSE(shelf_->IsShowingOverflowBubble());
2085 ash::test::ShelfViewTestAPI test(
2086 ash::test::ShelfTestAPI(shelf_).shelf_view());
2088 int items_added = 0;
2089 while (!test.IsOverflowButtonVisible()) {
2090 std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
2091 PinFakeApp(fake_app_id);
2093 ++items_added;
2094 ASSERT_LT(items_added, 10000);
2097 // Now show overflow bubble.
2098 test.ShowOverflowBubble();
2099 EXPECT_TRUE(shelf_->IsShowingOverflowBubble());
2101 // Unpin first pinned app and there should be no crash.
2102 controller_->UnpinAppWithID(std::string("fake_app_0"));
2104 test.RunMessageLoopUntilAnimationsDone();
2105 EXPECT_FALSE(shelf_->IsShowingOverflowBubble());
2108 // Check that a windowed V1 application can navigate away from its domain, but
2109 // still gets detected properly.
2110 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, V1AppNavigation) {
2111 // We assume that the web store is always there (which it apparently is).
2112 controller_->PinAppWithID(extensions::kWebStoreAppId);
2113 ash::ShelfID id = controller_->GetShelfIDForAppID(
2114 extensions::kWebStoreAppId);
2115 ASSERT_NE(0, id);
2116 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(id)->status);
2118 // Create a windowed application.
2119 AppLaunchParams params(
2120 profile(), controller_->GetExtensionForAppID(extensions::kWebStoreAppId),
2121 CURRENT_TAB, chrome::HOST_DESKTOP_TYPE_ASH, extensions::SOURCE_TEST);
2122 params.container = extensions::LAUNCH_CONTAINER_WINDOW;
2123 OpenApplication(params);
2124 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(id)->status);
2126 // Find the browser which holds our app.
2127 Browser* app_browser = NULL;
2128 const BrowserList* ash_browser_list =
2129 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
2130 for (BrowserList::const_reverse_iterator it =
2131 ash_browser_list->begin_last_active();
2132 it != ash_browser_list->end_last_active() && !app_browser; ++it) {
2133 if ((*it)->is_app()) {
2134 app_browser = *it;
2135 break;
2138 ASSERT_TRUE(app_browser);
2140 // After navigating away in the app, we should still be active.
2141 ui_test_utils::NavigateToURL(app_browser,
2142 GURL("http://www.foo.com/bar.html"));
2143 // Make sure the navigation was entirely performed.
2144 base::MessageLoop::current()->RunUntilIdle();
2145 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(id)->status);
2146 app_browser->tab_strip_model()->CloseWebContentsAt(0,
2147 TabStripModel::CLOSE_NONE);
2148 // Make sure that the app is really gone.
2149 base::MessageLoop::current()->RunUntilIdle();
2150 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(id)->status);
2153 // Checks that a opening a settings window creates a new launcher item.
2154 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, SettingsWindow) {
2155 chrome::SettingsWindowManager* settings_manager =
2156 chrome::SettingsWindowManager::GetInstance();
2157 ash::ShelfModel* shelf_model = ash::Shell::GetInstance()->shelf_model();
2159 // Get the number of items in the shelf and browser menu.
2160 int item_count = shelf_model->item_count();
2161 size_t browser_count = NumberOfDetectedLauncherBrowsers(false);
2163 // Open a settings window. Number of browser items should remain unchanged,
2164 // number of shelf items should increase.
2165 settings_manager->ShowChromePageForProfile(
2166 browser()->profile(),
2167 chrome::GetSettingsUrl(std::string()));
2168 Browser* settings_browser =
2169 settings_manager->FindBrowserForProfile(browser()->profile());
2170 ASSERT_TRUE(settings_browser);
2171 EXPECT_EQ(browser_count, NumberOfDetectedLauncherBrowsers(false));
2172 EXPECT_EQ(item_count + 1, shelf_model->item_count());
2174 // TODO(stevenjb): Test multiprofile on Chrome OS when test support is addded.
2175 // crbug.com/230464.