Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / ash / launcher / chrome_launcher_controller_browsertest.cc
blobe5a8ff790a19cd2d48a58d5f8efae2c0b6d1f4a1
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 "apps/shell_window.h"
8 #include "apps/shell_window_registry.h"
9 #include "apps/ui/native_app_window.h"
10 #include "ash/ash_switches.h"
11 #include "ash/display/display_controller.h"
12 #include "ash/shelf/shelf.h"
13 #include "ash/shelf/shelf_button.h"
14 #include "ash/shelf/shelf_model.h"
15 #include "ash/shelf/shelf_util.h"
16 #include "ash/shelf/shelf_view.h"
17 #include "ash/shell.h"
18 #include "ash/test/app_list_controller_test_api.h"
19 #include "ash/test/shelf_test_api.h"
20 #include "ash/test/shelf_view_test_api.h"
21 #include "ash/test/shell_test_api.h"
22 #include "ash/wm/window_state.h"
23 #include "ash/wm/window_util.h"
24 #include "base/strings/stringprintf.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "chrome/browser/apps/app_browsertest_util.h"
27 #include "chrome/browser/automation/automation_util.h"
28 #include "chrome/browser/chrome_notification_types.h"
29 #include "chrome/browser/extensions/extension_apitest.h"
30 #include "chrome/browser/extensions/extension_browsertest.h"
31 #include "chrome/browser/extensions/extension_function_test_utils.h"
32 #include "chrome/browser/extensions/extension_service.h"
33 #include "chrome/browser/extensions/extension_system.h"
34 #include "chrome/browser/extensions/extension_test_message_listener.h"
35 #include "chrome/browser/profiles/profile.h"
36 #include "chrome/browser/ui/app_list/app_list_service.h"
37 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controller.h"
38 #include "chrome/browser/ui/ash/launcher/launcher_application_menu_item_model.h"
39 #include "chrome/browser/ui/ash/launcher/launcher_item_controller.h"
40 #include "chrome/browser/ui/browser.h"
41 #include "chrome/browser/ui/browser_commands.h"
42 #include "chrome/browser/ui/browser_finder.h"
43 #include "chrome/browser/ui/browser_list.h"
44 #include "chrome/browser/ui/browser_window.h"
45 #include "chrome/browser/ui/extensions/application_launch.h"
46 #include "chrome/browser/ui/host_desktop.h"
47 #include "chrome/browser/ui/tabs/tab_strip_model.h"
48 #include "chrome/common/chrome_switches.h"
49 #include "chrome/test/base/ui_test_utils.h"
50 #include "content/public/browser/notification_service.h"
51 #include "content/public/browser/notification_source.h"
52 #include "content/public/browser/web_contents.h"
53 #include "content/public/test/browser_test_utils.h"
54 #include "extensions/common/constants.h"
55 #include "extensions/common/switches.h"
56 #include "testing/gtest/include/gtest/gtest.h"
57 #include "ui/app_list/views/apps_grid_view.h"
58 #include "ui/aura/client/aura_constants.h"
59 #include "ui/aura/test/event_generator.h"
60 #include "ui/aura/window.h"
61 #include "ui/events/event.h"
63 using apps::ShellWindow;
64 using extensions::Extension;
65 using content::WebContents;
67 namespace {
69 class TestEvent : public ui::Event {
70 public:
71 explicit TestEvent(ui::EventType type)
72 : ui::Event(type, base::TimeDelta(), 0) {
74 virtual ~TestEvent() {
77 private:
78 DISALLOW_COPY_AND_ASSIGN(TestEvent);
81 class TestShellWindowRegistryObserver
82 : public apps::ShellWindowRegistry::Observer {
83 public:
84 explicit TestShellWindowRegistryObserver(Profile* profile)
85 : profile_(profile),
86 icon_updates_(0) {
87 apps::ShellWindowRegistry::Get(profile_)->AddObserver(this);
90 virtual ~TestShellWindowRegistryObserver() {
91 apps::ShellWindowRegistry::Get(profile_)->RemoveObserver(this);
94 // Overridden from ShellWindowRegistry::Observer:
95 virtual void OnShellWindowAdded(ShellWindow* shell_window) OVERRIDE {}
97 virtual void OnShellWindowIconChanged(ShellWindow* shell_window) OVERRIDE {
98 ++icon_updates_;
101 virtual void OnShellWindowRemoved(ShellWindow* shell_window) OVERRIDE {}
103 int icon_updates() { return icon_updates_; }
105 private:
106 Profile* profile_;
107 int icon_updates_;
109 DISALLOW_COPY_AND_ASSIGN(TestShellWindowRegistryObserver);
112 } // namespace
114 class LauncherPlatformAppBrowserTest
115 : public extensions::PlatformAppBrowserTest {
116 protected:
117 LauncherPlatformAppBrowserTest() : shelf_(NULL), controller_(NULL) {
120 virtual ~LauncherPlatformAppBrowserTest() {}
122 virtual void RunTestOnMainThreadLoop() OVERRIDE {
123 shelf_ = ash::Shelf::ForPrimaryDisplay();
124 controller_ = ChromeLauncherController::instance();
125 return extensions::PlatformAppBrowserTest::RunTestOnMainThreadLoop();
128 ash::ShelfModel* shelf_model() {
129 return ash::test::ShellTestApi(ash::Shell::GetInstance()).shelf_model();
132 ash::LauncherID CreateAppShortcutLauncherItem(const std::string& name) {
133 return controller_->CreateAppShortcutLauncherItem(
134 name, controller_->model()->item_count());
137 const ash::LauncherItem& GetLastLauncherItem() {
138 // Unless there are any panels, the item at index [count - 1] will be
139 // the desired item.
140 return shelf_model()->items()[shelf_model()->item_count() - 1];
143 const ash::LauncherItem& GetLastLauncherPanelItem() {
144 // Panels show up on the right side of the shelf, so the desired item
145 // will be the last one.
146 return shelf_model()->items()[shelf_model()->item_count() - 1];
149 LauncherItemController* GetItemController(ash::LauncherID id) {
150 return controller_->id_to_item_controller_map_[id];
153 // Returns the number of menu items, ignoring separators.
154 int GetNumApplicationMenuItems(const ash::LauncherItem& item) {
155 const int event_flags = 0;
156 scoped_ptr<ash::ShelfMenuModel> menu(new LauncherApplicationMenuItemModel(
157 controller_->GetApplicationList(item, event_flags)));
158 int num_items = 0;
159 for (int i = 0; i < menu->GetItemCount(); ++i) {
160 if (menu->GetTypeAt(i) != ui::MenuModel::TYPE_SEPARATOR)
161 ++num_items;
163 return num_items;
166 // Activate the shelf item with the given |id|.
167 void ActivateShelfItem(int id) {
168 shelf_->ActivateShelfItem(id);
171 ash::Shelf* shelf_;
172 ChromeLauncherController* controller_;
174 private:
176 DISALLOW_COPY_AND_ASSIGN(LauncherPlatformAppBrowserTest);
179 enum RipOffCommand {
180 // Drag the item off the shelf and let the mouse go.
181 RIP_OFF_ITEM,
182 // Drag the item off the shelf, move the mouse back and then let go.
183 RIP_OFF_ITEM_AND_RETURN,
184 // Drag the item off the shelf and then issue a cancel command.
185 RIP_OFF_ITEM_AND_CANCEL,
186 // Drag the item off the shelf and do not release the mouse.
187 RIP_OFF_ITEM_AND_DONT_RELEASE_MOUSE,
190 class ShelfAppBrowserTest : public ExtensionBrowserTest {
191 protected:
192 ShelfAppBrowserTest() : shelf_(NULL), model_(NULL), controller_(NULL) {
195 virtual ~ShelfAppBrowserTest() {}
197 virtual void RunTestOnMainThreadLoop() OVERRIDE {
198 shelf_ = ash::Shelf::ForPrimaryDisplay();
199 model_ = ash::test::ShellTestApi(ash::Shell::GetInstance()).shelf_model();
200 controller_ = ChromeLauncherController::instance();
201 return ExtensionBrowserTest::RunTestOnMainThreadLoop();
204 size_t NumberOfDetectedLauncherBrowsers(bool show_all_tabs) {
205 LauncherItemController* item_controller =
206 controller_->GetBrowserShortcutLauncherItemController();
207 int items = item_controller->GetApplicationList(
208 show_all_tabs ? ui::EF_SHIFT_DOWN : 0).size();
209 // If we have at least one item, we have also a title which we remove here.
210 return items ? (items - 1) : 0;
213 const Extension* LoadAndLaunchExtension(
214 const char* name,
215 extensions::LaunchContainer container,
216 WindowOpenDisposition disposition) {
217 EXPECT_TRUE(LoadExtension(test_data_dir_.AppendASCII(name)));
219 ExtensionService* service = extensions::ExtensionSystem::Get(
220 profile())->extension_service();
221 const Extension* extension =
222 service->GetExtensionById(last_loaded_extension_id(), false);
223 EXPECT_TRUE(extension);
225 OpenApplication(AppLaunchParams(profile(),
226 extension,
227 container,
228 disposition));
229 return extension;
232 ash::LauncherID CreateShortcut(const char* name) {
233 ExtensionService* service = extensions::ExtensionSystem::Get(
234 profile())->extension_service();
235 LoadExtension(test_data_dir_.AppendASCII(name));
237 // First get app_id.
238 const Extension* extension =
239 service->GetExtensionById(last_loaded_extension_id(), false);
240 const std::string app_id = extension->id();
242 // Then create a shortcut.
243 int item_count = model_->item_count();
244 ash::LauncherID shortcut_id = controller_->CreateAppShortcutLauncherItem(
245 app_id,
246 item_count);
247 controller_->PersistPinnedState();
248 EXPECT_EQ(++item_count, model_->item_count());
249 const ash::LauncherItem& item = *model_->ItemByID(shortcut_id);
250 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
251 return item.id;
254 void RemoveShortcut(ash::LauncherID id) {
255 controller_->Unpin(id);
258 // Activate the shelf item with the given |id|.
259 void ActivateShelfItem(int id) {
260 shelf_->ActivateShelfItem(id);
263 ash::LauncherID PinFakeApp(const std::string& name) {
264 return controller_->CreateAppShortcutLauncherItem(
265 name, model_->item_count());
268 // Get the index of an item which has the given type.
269 int GetIndexOfShelfItemType(ash::LauncherItemType type) {
270 return model_->GetItemIndexForType(type);
273 // Try to rip off |item_index|.
274 void RipOffItemIndex(int index,
275 aura::test::EventGenerator* generator,
276 ash::test::ShelfViewTestAPI* test,
277 RipOffCommand command) {
278 ash::internal::ShelfButton* button = test->GetButton(index);
279 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
280 gfx::Point rip_off_point(start_point.x(), 0);
281 generator->MoveMouseTo(start_point.x(), start_point.y());
282 base::MessageLoop::current()->RunUntilIdle();
283 generator->PressLeftButton();
284 base::MessageLoop::current()->RunUntilIdle();
285 generator->MoveMouseTo(rip_off_point.x(), rip_off_point.y());
286 base::MessageLoop::current()->RunUntilIdle();
287 test->RunMessageLoopUntilAnimationsDone();
288 if (command == RIP_OFF_ITEM_AND_RETURN) {
289 generator->MoveMouseTo(start_point.x(), start_point.y());
290 base::MessageLoop::current()->RunUntilIdle();
291 test->RunMessageLoopUntilAnimationsDone();
292 } else if (command == RIP_OFF_ITEM_AND_CANCEL) {
293 // This triggers an internal cancel. Using VKEY_ESCAPE was too unreliable.
294 button->OnMouseCaptureLost();
296 if (command != RIP_OFF_ITEM_AND_DONT_RELEASE_MOUSE) {
297 generator->ReleaseLeftButton();
298 base::MessageLoop::current()->RunUntilIdle();
299 test->RunMessageLoopUntilAnimationsDone();
303 ash::Shelf* shelf_;
304 ash::ShelfModel* model_;
305 ChromeLauncherController* controller_;
307 private:
309 DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserTest);
312 class ShelfAppBrowserTestNoDefaultBrowser : public ShelfAppBrowserTest {
313 protected:
314 ShelfAppBrowserTestNoDefaultBrowser() {}
315 virtual ~ShelfAppBrowserTestNoDefaultBrowser() {}
317 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
318 ShelfAppBrowserTest::SetUpCommandLine(command_line);
319 command_line->AppendSwitch(switches::kNoStartupWindow);
322 private:
324 DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserTestNoDefaultBrowser);
327 // Since the default for minimizing on click might change, I added both classes
328 // to either get the minimize on click or not.
329 class ShelfAppBrowserNoMinimizeOnClick : public LauncherPlatformAppBrowserTest {
330 protected:
331 ShelfAppBrowserNoMinimizeOnClick() {}
332 virtual ~ShelfAppBrowserNoMinimizeOnClick() {}
334 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
335 LauncherPlatformAppBrowserTest::SetUpCommandLine(command_line);
336 command_line->AppendSwitch(
337 switches::kDisableMinimizeOnSecondLauncherItemClick);
340 private:
342 DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserNoMinimizeOnClick);
345 typedef LauncherPlatformAppBrowserTest ShelfAppBrowserMinimizeOnClick;
347 // Test that we can launch a platform app and get a running item.
348 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, LaunchUnpinned) {
349 int item_count = shelf_model()->item_count();
350 const Extension* extension = LoadAndLaunchPlatformApp("launch");
351 ShellWindow* window = CreateShellWindow(extension);
352 ++item_count;
353 ASSERT_EQ(item_count, shelf_model()->item_count());
354 const ash::LauncherItem& item = GetLastLauncherItem();
355 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item.type);
356 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
357 CloseShellWindow(window);
358 --item_count;
359 EXPECT_EQ(item_count, shelf_model()->item_count());
362 // Test that we can launch a platform app that already has a shortcut.
363 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, LaunchPinned) {
364 int item_count = shelf_model()->item_count();
366 // First get app_id.
367 const Extension* extension = LoadAndLaunchPlatformApp("launch");
368 const std::string app_id = extension->id();
370 // Then create a shortcut.
371 ash::LauncherID shortcut_id = CreateAppShortcutLauncherItem(app_id);
372 ++item_count;
373 ASSERT_EQ(item_count, shelf_model()->item_count());
374 ash::LauncherItem item = *shelf_model()->ItemByID(shortcut_id);
375 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
376 EXPECT_EQ(ash::STATUS_CLOSED, item.status);
378 // Open a window. Confirm the item is now running.
379 ShellWindow* window = CreateShellWindow(extension);
380 ash::wm::ActivateWindow(window->GetNativeWindow());
381 ASSERT_EQ(item_count, shelf_model()->item_count());
382 item = *shelf_model()->ItemByID(shortcut_id);
383 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
384 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
386 // Then close it, make sure there's still an item.
387 CloseShellWindow(window);
388 ASSERT_EQ(item_count, shelf_model()->item_count());
389 item = *shelf_model()->ItemByID(shortcut_id);
390 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
391 EXPECT_EQ(ash::STATUS_CLOSED, item.status);
394 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, PinRunning) {
395 // Run.
396 int item_count = shelf_model()->item_count();
397 const Extension* extension = LoadAndLaunchPlatformApp("launch");
398 ShellWindow* window = CreateShellWindow(extension);
399 ++item_count;
400 ASSERT_EQ(item_count, shelf_model()->item_count());
401 const ash::LauncherItem& item1 = GetLastLauncherItem();
402 ash::LauncherID id = item1.id;
403 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
404 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
406 // Create a shortcut. The app item should be after it.
407 ash::LauncherID foo_id = CreateAppShortcutLauncherItem("foo");
408 ++item_count;
409 ASSERT_EQ(item_count, shelf_model()->item_count());
410 EXPECT_LT(shelf_model()->ItemIndexByID(foo_id),
411 shelf_model()->ItemIndexByID(id));
413 // Pin the app. The item should remain.
414 controller_->Pin(id);
415 ASSERT_EQ(item_count, shelf_model()->item_count());
416 const ash::LauncherItem& item2 = *shelf_model()->ItemByID(id);
417 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item2.type);
418 EXPECT_EQ(ash::STATUS_ACTIVE, item2.status);
420 // New shortcuts should come after the item.
421 ash::LauncherID bar_id = CreateAppShortcutLauncherItem("bar");
422 ++item_count;
423 ASSERT_EQ(item_count, shelf_model()->item_count());
424 EXPECT_LT(shelf_model()->ItemIndexByID(id),
425 shelf_model()->ItemIndexByID(bar_id));
427 // Then close it, make sure the item remains.
428 CloseShellWindow(window);
429 ASSERT_EQ(item_count, shelf_model()->item_count());
432 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, UnpinRunning) {
433 int item_count = shelf_model()->item_count();
435 // First get app_id.
436 const Extension* extension = LoadAndLaunchPlatformApp("launch");
437 const std::string app_id = extension->id();
439 // Then create a shortcut.
440 ash::LauncherID shortcut_id = CreateAppShortcutLauncherItem(app_id);
441 ++item_count;
442 ASSERT_EQ(item_count, shelf_model()->item_count());
443 ash::LauncherItem item = *shelf_model()->ItemByID(shortcut_id);
444 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
445 EXPECT_EQ(ash::STATUS_CLOSED, item.status);
447 // Create a second shortcut. This will be needed to force the first one to
448 // move once it gets unpinned.
449 ash::LauncherID foo_id = CreateAppShortcutLauncherItem("foo");
450 ++item_count;
451 ASSERT_EQ(item_count, shelf_model()->item_count());
452 EXPECT_LT(shelf_model()->ItemIndexByID(shortcut_id),
453 shelf_model()->ItemIndexByID(foo_id));
455 // Open a window. Confirm the item is now running.
456 ShellWindow* window = CreateShellWindow(extension);
457 ash::wm::ActivateWindow(window->GetNativeWindow());
458 ASSERT_EQ(item_count, shelf_model()->item_count());
459 item = *shelf_model()->ItemByID(shortcut_id);
460 EXPECT_EQ(ash::TYPE_APP_SHORTCUT, item.type);
461 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
463 // Unpin the app. The item should remain.
464 controller_->Unpin(shortcut_id);
465 ASSERT_EQ(item_count, shelf_model()->item_count());
466 item = *shelf_model()->ItemByID(shortcut_id);
467 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item.type);
468 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
469 // The item should have moved after the other shortcuts.
470 EXPECT_GT(shelf_model()->ItemIndexByID(shortcut_id),
471 shelf_model()->ItemIndexByID(foo_id));
473 // Then close it, make sure the item's gone.
474 CloseShellWindow(window);
475 --item_count;
476 ASSERT_EQ(item_count, shelf_model()->item_count());
479 // Test that we can launch a platform app with more than one window.
480 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, MultipleWindows) {
481 int item_count = shelf_model()->item_count();
483 // First run app.
484 const Extension* extension = LoadAndLaunchPlatformApp("launch");
485 ShellWindow* window1 = CreateShellWindow(extension);
486 ++item_count;
487 ASSERT_EQ(item_count, shelf_model()->item_count());
488 const ash::LauncherItem& item1 = GetLastLauncherItem();
489 ash::LauncherID item_id = item1.id;
490 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
491 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
492 EXPECT_EQ(2, GetNumApplicationMenuItems(item1)); // Title + 1 window
494 // Add second window.
495 ShellWindow* window2 = CreateShellWindow(extension);
496 // Confirm item stays.
497 ASSERT_EQ(item_count, shelf_model()->item_count());
498 const ash::LauncherItem& item2 = *shelf_model()->ItemByID(item_id);
499 EXPECT_EQ(ash::STATUS_ACTIVE, item2.status);
500 EXPECT_EQ(3, GetNumApplicationMenuItems(item2)); // Title + 2 windows
502 // Close second window.
503 CloseShellWindow(window2);
504 // Confirm item stays.
505 ASSERT_EQ(item_count, shelf_model()->item_count());
506 const ash::LauncherItem& item3 = *shelf_model()->ItemByID(item_id);
507 EXPECT_EQ(ash::STATUS_ACTIVE, item3.status);
508 EXPECT_EQ(2, GetNumApplicationMenuItems(item3)); // Title + 1 window
510 // Close first window.
511 CloseShellWindow(window1);
512 // Confirm item is removed.
513 --item_count;
514 ASSERT_EQ(item_count, shelf_model()->item_count());
517 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, MultipleApps) {
518 int item_count = shelf_model()->item_count();
520 // First run app.
521 const Extension* extension1 = LoadAndLaunchPlatformApp("launch");
522 ShellWindow* window1 = CreateShellWindow(extension1);
523 ++item_count;
524 ASSERT_EQ(item_count, shelf_model()->item_count());
525 const ash::LauncherItem& item1 = GetLastLauncherItem();
526 ash::LauncherID item_id1 = item1.id;
527 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
528 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
530 // Then run second app.
531 const Extension* extension2 = LoadAndLaunchPlatformApp("launch_2");
532 ShellWindow* window2 = CreateShellWindow(extension2);
533 ++item_count;
534 ASSERT_EQ(item_count, shelf_model()->item_count());
535 const ash::LauncherItem& item2 = GetLastLauncherItem();
536 ash::LauncherID item_id2 = item2.id;
537 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item2.type);
538 EXPECT_EQ(ash::STATUS_ACTIVE, item2.status);
540 EXPECT_NE(item_id1, item_id2);
541 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id1)->status);
543 // Close second app.
544 CloseShellWindow(window2);
545 --item_count;
546 ASSERT_EQ(item_count, shelf_model()->item_count());
547 // First app should be active again.
548 EXPECT_EQ(ash::STATUS_ACTIVE, shelf_model()->ItemByID(item_id1)->status);
550 // Close first app.
551 CloseShellWindow(window1);
552 --item_count;
553 ASSERT_EQ(item_count, shelf_model()->item_count());
556 // Confirm that app windows can be reactivated by clicking their icons and that
557 // the correct activation order is maintained.
558 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, WindowActivation) {
559 int item_count = shelf_model()->item_count();
561 // First run app.
562 const Extension* extension1 = LoadAndLaunchPlatformApp("launch");
563 ShellWindow* window1 = CreateShellWindow(extension1);
564 ++item_count;
565 ASSERT_EQ(item_count, shelf_model()->item_count());
566 const ash::LauncherItem& item1 = GetLastLauncherItem();
567 ash::LauncherID item_id1 = item1.id;
568 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
569 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
571 // Then run second app.
572 const Extension* extension2 = LoadAndLaunchPlatformApp("launch_2");
573 ShellWindow* window2 = CreateShellWindow(extension2);
574 ++item_count;
575 ASSERT_EQ(item_count, shelf_model()->item_count());
576 const ash::LauncherItem& item2 = GetLastLauncherItem();
577 ash::LauncherID item_id2 = item2.id;
578 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item2.type);
579 EXPECT_EQ(ash::STATUS_ACTIVE, item2.status);
581 EXPECT_NE(item_id1, item_id2);
582 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id1)->status);
584 // Activate first one.
585 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id1));
586 EXPECT_EQ(ash::STATUS_ACTIVE, shelf_model()->ItemByID(item_id1)->status);
587 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id2)->status);
588 EXPECT_TRUE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
589 EXPECT_FALSE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
591 // Activate second one.
592 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id2));
593 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id1)->status);
594 EXPECT_EQ(ash::STATUS_ACTIVE, shelf_model()->ItemByID(item_id2)->status);
595 EXPECT_FALSE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
596 EXPECT_TRUE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
598 // Add window for app1. This will activate it.
599 ShellWindow* window1b = CreateShellWindow(extension1);
600 ash::wm::ActivateWindow(window1b->GetNativeWindow());
601 EXPECT_FALSE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
602 EXPECT_FALSE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
603 EXPECT_TRUE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
605 // Activate launcher item for app1, this will activate the first app window.
606 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id1));
607 EXPECT_TRUE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
608 EXPECT_FALSE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
609 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id1));
610 EXPECT_TRUE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
612 // Activate the second app again
613 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id2));
614 EXPECT_FALSE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
615 EXPECT_TRUE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
616 EXPECT_FALSE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
618 // Activate the first app again
619 ActivateShelfItem(shelf_model()->ItemIndexByID(item_id1));
620 EXPECT_TRUE(ash::wm::IsActiveWindow(window1b->GetNativeWindow()));
621 EXPECT_FALSE(ash::wm::IsActiveWindow(window2->GetNativeWindow()));
622 EXPECT_FALSE(ash::wm::IsActiveWindow(window1->GetNativeWindow()));
624 // Close second app.
625 CloseShellWindow(window2);
626 --item_count;
627 EXPECT_EQ(item_count, shelf_model()->item_count());
628 // First app should be active again.
629 EXPECT_EQ(ash::STATUS_ACTIVE, shelf_model()->ItemByID(item_id1)->status);
631 // Close first app.
632 CloseShellWindow(window1b);
633 CloseShellWindow(window1);
634 --item_count;
635 EXPECT_EQ(item_count, shelf_model()->item_count());
638 // Confirm that Click behavior for app windows is correnct.
639 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserNoMinimizeOnClick, AppClickBehavior) {
640 // Launch a platform app and create a window for it.
641 const Extension* extension1 = LoadAndLaunchPlatformApp("launch");
642 ShellWindow* window1 = CreateShellWindow(extension1);
643 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
644 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
645 // Confirm that a controller item was created and is the correct state.
646 const ash::LauncherItem& item1 = GetLastLauncherItem();
647 LauncherItemController* item1_controller = GetItemController(item1.id);
648 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
649 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
650 EXPECT_EQ(LauncherItemController::TYPE_APP, item1_controller->type());
651 // Clicking the item should have no effect.
652 TestEvent click_event(ui::ET_MOUSE_PRESSED);
653 item1_controller->ItemSelected(click_event);
654 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
655 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
656 // Minimize the window and confirm that the controller item is updated.
657 window1->GetBaseWindow()->Minimize();
658 EXPECT_FALSE(window1->GetNativeWindow()->IsVisible());
659 EXPECT_FALSE(window1->GetBaseWindow()->IsActive());
660 EXPECT_EQ(ash::STATUS_RUNNING, item1.status);
661 // Clicking the item should activate the window.
662 item1_controller->ItemSelected(click_event);
663 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
664 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
665 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
666 // Maximizing a window should preserve state after minimize + click.
667 window1->GetBaseWindow()->Maximize();
668 window1->GetBaseWindow()->Minimize();
669 item1_controller->ItemSelected(click_event);
670 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
671 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
672 EXPECT_TRUE(window1->GetBaseWindow()->IsMaximized());
675 // Confirm the minimizing click behavior for apps.
676 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserMinimizeOnClick,
677 PackagedAppClickBehaviorInMinimizeMode) {
678 // Launch one platform app and create a window for it.
679 const Extension* extension1 = LoadAndLaunchPlatformApp("launch");
680 ShellWindow* window1 = CreateShellWindow(extension1);
681 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
682 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
684 // Confirm that a controller item was created and is the correct state.
685 const ash::LauncherItem& item1 = GetLastLauncherItem();
686 LauncherItemController* item1_controller = GetItemController(item1.id);
687 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
688 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
689 EXPECT_EQ(LauncherItemController::TYPE_APP, item1_controller->type());
690 // Since it is already active, clicking it should minimize.
691 TestEvent click_event(ui::ET_MOUSE_PRESSED);
692 item1_controller->ItemSelected(click_event);
693 EXPECT_FALSE(window1->GetNativeWindow()->IsVisible());
694 EXPECT_FALSE(window1->GetBaseWindow()->IsActive());
695 EXPECT_TRUE(window1->GetBaseWindow()->IsMinimized());
696 EXPECT_EQ(ash::STATUS_RUNNING, item1.status);
697 // Clicking the item again should activate the window again.
698 item1_controller->ItemSelected(click_event);
699 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
700 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
701 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
702 // Maximizing a window should preserve state after minimize + click.
703 window1->GetBaseWindow()->Maximize();
704 window1->GetBaseWindow()->Minimize();
705 item1_controller->ItemSelected(click_event);
706 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
707 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
708 EXPECT_TRUE(window1->GetBaseWindow()->IsMaximized());
709 window1->GetBaseWindow()->Restore();
710 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
711 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
712 EXPECT_FALSE(window1->GetBaseWindow()->IsMaximized());
714 // Creating a second window of the same type should change the behavior so
715 // that a click does not change the activation state.
716 ShellWindow* window1a = CreateShellWindow(extension1);
717 EXPECT_TRUE(window1a->GetNativeWindow()->IsVisible());
718 EXPECT_TRUE(window1a->GetBaseWindow()->IsActive());
719 // The first click does nothing.
720 item1_controller->ItemSelected(click_event);
721 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
722 EXPECT_TRUE(window1a->GetNativeWindow()->IsVisible());
723 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
724 EXPECT_FALSE(window1a->GetBaseWindow()->IsActive());
725 // The second neither.
726 item1_controller->ItemSelected(click_event);
727 EXPECT_TRUE(window1->GetNativeWindow()->IsVisible());
728 EXPECT_TRUE(window1a->GetNativeWindow()->IsVisible());
729 EXPECT_TRUE(window1->GetBaseWindow()->IsActive());
730 EXPECT_FALSE(window1a->GetBaseWindow()->IsActive());
733 // Confirm that click behavior for app panels is correct.
734 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, AppPanelClickBehavior) {
735 // Enable experimental APIs to allow panel creation.
736 CommandLine::ForCurrentProcess()->AppendSwitch(
737 extensions::switches::kEnableExperimentalExtensionApis);
738 // Launch a platform app and create a panel window for it.
739 const Extension* extension1 = LoadAndLaunchPlatformApp("launch");
740 ShellWindow::CreateParams params;
741 params.window_type = ShellWindow::WINDOW_TYPE_PANEL;
742 params.focused = false;
743 ShellWindow* panel = CreateShellWindowFromParams(extension1, params);
744 EXPECT_TRUE(panel->GetNativeWindow()->IsVisible());
745 // Panels should not be active by default.
746 EXPECT_FALSE(panel->GetBaseWindow()->IsActive());
747 // Confirm that a controller item was created and is the correct state.
748 const ash::LauncherItem& item1 = GetLastLauncherPanelItem();
749 LauncherItemController* item1_controller = GetItemController(item1.id);
750 EXPECT_EQ(ash::TYPE_APP_PANEL, item1.type);
751 EXPECT_EQ(ash::STATUS_RUNNING, item1.status);
752 EXPECT_EQ(LauncherItemController::TYPE_APP_PANEL, item1_controller->type());
753 // Click the item and confirm that the panel is activated.
754 TestEvent click_event(ui::ET_MOUSE_PRESSED);
755 item1_controller->ItemSelected(click_event);
756 EXPECT_TRUE(panel->GetBaseWindow()->IsActive());
757 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
758 // Click the item again and confirm that the panel is minimized.
759 item1_controller->ItemSelected(click_event);
760 EXPECT_TRUE(panel->GetBaseWindow()->IsMinimized());
761 EXPECT_EQ(ash::STATUS_RUNNING, item1.status);
762 // Click the item again and confirm that the panel is activated.
763 item1_controller->ItemSelected(click_event);
764 EXPECT_TRUE(panel->GetNativeWindow()->IsVisible());
765 EXPECT_TRUE(panel->GetBaseWindow()->IsActive());
766 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
769 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, BrowserActivation) {
770 int item_count = shelf_model()->item_count();
772 // First run app.
773 const Extension* extension1 = LoadAndLaunchPlatformApp("launch");
774 CreateShellWindow(extension1);
775 ++item_count;
776 ASSERT_EQ(item_count, shelf_model()->item_count());
777 const ash::LauncherItem& item1 = GetLastLauncherItem();
778 ash::LauncherID item_id1 = item1.id;
779 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
780 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
782 ash::wm::ActivateWindow(browser()->window()->GetNativeWindow());
783 EXPECT_EQ(ash::STATUS_RUNNING, shelf_model()->ItemByID(item_id1)->status);
786 // Test that opening an app sets the correct icon
787 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, SetIcon) {
788 TestShellWindowRegistryObserver test_observer(browser()->profile());
790 // Enable experimental APIs to allow panel creation.
791 CommandLine::ForCurrentProcess()->AppendSwitch(
792 extensions::switches::kEnableExperimentalExtensionApis);
794 int base_shelf_item_count = shelf_model()->item_count();
795 ExtensionTestMessageListener launched_listener("Launched", false);
796 ExtensionTestMessageListener completed_listener("Completed", false);
797 LoadAndLaunchPlatformApp("app_icon");
798 ASSERT_TRUE(launched_listener.WaitUntilSatisfied());
799 ASSERT_TRUE(completed_listener.WaitUntilSatisfied());
801 // Now wait until the WebContent has decoded the icons and chrome has
802 // processed it. This needs to be in a loop since the renderer runs in a
803 // different process.
804 while (test_observer.icon_updates() < 3) {
805 base::RunLoop run_loop;
806 run_loop.RunUntilIdle();
809 // This test creates one shell window and one panel window.
810 int shelf_item_count = shelf_model()->item_count();
811 ASSERT_EQ(base_shelf_item_count + 2, shelf_item_count);
812 // The Panel will be the last item, the app second-to-last.
813 const ash::LauncherItem& app_item =
814 shelf_model()->items()[shelf_item_count - 2];
815 const ash::LauncherItem& panel_item =
816 shelf_model()->items()[shelf_item_count - 1];
817 const LauncherItemController* app_item_controller =
818 GetItemController(app_item.id);
819 const LauncherItemController* panel_item_controller =
820 GetItemController(panel_item.id);
821 // Icons for Apps are set by the ShellWindowLauncherController, so
822 // image_set_by_controller() should be set.
823 EXPECT_TRUE(app_item_controller->image_set_by_controller());
824 EXPECT_TRUE(panel_item_controller->image_set_by_controller());
825 // Ensure icon heights are correct (see test.js in app_icon/ test directory)
826 EXPECT_EQ(48, app_item.image.height());
827 EXPECT_EQ(64, panel_item.image.height());
830 // Test that we can launch an app with a shortcut.
831 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchPinned) {
832 TabStripModel* tab_strip = browser()->tab_strip_model();
833 int tab_count = tab_strip->count();
834 ash::LauncherID shortcut_id = CreateShortcut("app1");
835 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
836 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
837 EXPECT_EQ(++tab_count, tab_strip->count());
838 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
839 WebContents* tab = tab_strip->GetActiveWebContents();
840 content::WebContentsDestroyedWatcher destroyed_watcher(tab);
841 browser()->tab_strip_model()->CloseSelectedTabs();
842 destroyed_watcher.Wait();
843 EXPECT_EQ(--tab_count, tab_strip->count());
844 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
847 // Launch the app first and then create the shortcut.
848 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchUnpinned) {
849 TabStripModel* tab_strip = browser()->tab_strip_model();
850 int tab_count = tab_strip->count();
851 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB,
852 NEW_FOREGROUND_TAB);
853 EXPECT_EQ(++tab_count, tab_strip->count());
854 ash::LauncherID shortcut_id = CreateShortcut("app1");
855 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
856 WebContents* tab = tab_strip->GetActiveWebContents();
857 content::WebContentsDestroyedWatcher destroyed_watcher(tab);
858 browser()->tab_strip_model()->CloseSelectedTabs();
859 destroyed_watcher.Wait();
860 EXPECT_EQ(--tab_count, tab_strip->count());
861 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
864 // Launches an app in the background and then tries to open it. This is test for
865 // a crash we had.
866 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchInBackground) {
867 TabStripModel* tab_strip = browser()->tab_strip_model();
868 int tab_count = tab_strip->count();
869 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB,
870 NEW_BACKGROUND_TAB);
871 EXPECT_EQ(++tab_count, tab_strip->count());
872 ChromeLauncherController::instance()->LaunchApp(last_loaded_extension_id(),
873 ash::LAUNCH_FROM_UNKNOWN,
877 // Confirm that clicking a icon for an app running in one of 2 maxmized windows
878 // activates the right window.
879 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchMaximized) {
880 aura::Window* window1 = browser()->window()->GetNativeWindow();
881 ash::wm::WindowState* window1_state = ash::wm::GetWindowState(window1);
882 window1_state->Maximize();
883 content::WindowedNotificationObserver open_observer(
884 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
885 content::NotificationService::AllSources());
886 chrome::NewEmptyWindow(browser()->profile(), chrome::HOST_DESKTOP_TYPE_ASH);
887 open_observer.Wait();
888 Browser* browser2 = content::Source<Browser>(open_observer.source()).ptr();
889 aura::Window* window2 = browser2->window()->GetNativeWindow();
890 TabStripModel* tab_strip = browser2->tab_strip_model();
891 int tab_count = tab_strip->count();
892 ash::wm::GetWindowState(window2)->Maximize();
894 ash::LauncherID shortcut_id = CreateShortcut("app1");
895 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
896 EXPECT_EQ(++tab_count, tab_strip->count());
897 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
899 window1->Show();
900 window1_state->Activate();
901 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut_id)).status);
903 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
904 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
907 // Activating the same app multiple times should launch only a single copy.
908 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ActivateApp) {
909 TabStripModel* tab_strip = browser()->tab_strip_model();
910 int tab_count = tab_strip->count();
911 const Extension* extension =
912 LoadExtension(test_data_dir_.AppendASCII("app1"));
914 ChromeLauncherController::instance()->ActivateApp(extension->id(),
915 ash::LAUNCH_FROM_UNKNOWN,
917 EXPECT_EQ(++tab_count, tab_strip->count());
918 ChromeLauncherController::instance()->ActivateApp(extension->id(),
919 ash::LAUNCH_FROM_UNKNOWN,
921 EXPECT_EQ(tab_count, tab_strip->count());
924 // Launching the same app multiple times should launch a copy for each call.
925 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, LaunchApp) {
926 TabStripModel* tab_strip = browser()->tab_strip_model();
927 int tab_count = tab_strip->count();
928 const Extension* extension =
929 LoadExtension(test_data_dir_.AppendASCII("app1"));
931 ChromeLauncherController::instance()->LaunchApp(extension->id(),
932 ash::LAUNCH_FROM_UNKNOWN,
934 EXPECT_EQ(++tab_count, tab_strip->count());
935 ChromeLauncherController::instance()->LaunchApp(extension->id(),
936 ash::LAUNCH_FROM_UNKNOWN,
938 EXPECT_EQ(++tab_count, tab_strip->count());
941 // Launch 2 apps and toggle which is active.
942 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, MultipleApps) {
943 int item_count = model_->item_count();
944 TabStripModel* tab_strip = browser()->tab_strip_model();
945 int tab_count = tab_strip->count();
946 ash::LauncherID shortcut1 = CreateShortcut("app1");
947 EXPECT_EQ(++item_count, model_->item_count());
948 ash::LauncherID shortcut2 = CreateShortcut("app2");
949 EXPECT_EQ(++item_count, model_->item_count());
951 // Launch first app.
952 ActivateShelfItem(model_->ItemIndexByID(shortcut1));
953 EXPECT_EQ(++tab_count, tab_strip->count());
954 WebContents* tab1 = tab_strip->GetActiveWebContents();
955 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut1)).status);
957 // Launch second app.
958 ActivateShelfItem(model_->ItemIndexByID(shortcut2));
959 EXPECT_EQ(++tab_count, tab_strip->count());
960 WebContents* tab2 = tab_strip->GetActiveWebContents();
961 ASSERT_NE(tab1, tab2);
962 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut1)).status);
963 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut2)).status);
965 // Reactivate first app.
966 ActivateShelfItem(model_->ItemIndexByID(shortcut1));
967 EXPECT_EQ(tab_count, tab_strip->count());
968 EXPECT_EQ(tab_strip->GetActiveWebContents(), tab1);
969 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut1)).status);
970 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut2)).status);
972 // Open second tab for second app. This should activate it.
973 ui_test_utils::NavigateToURLWithDisposition(
974 browser(),
975 GURL("http://www.example.com/path3/foo.html"),
976 NEW_FOREGROUND_TAB,
978 EXPECT_EQ(++tab_count, tab_strip->count());
979 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut1)).status);
980 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut2)).status);
982 // Reactivate first app.
983 ActivateShelfItem(model_->ItemIndexByID(shortcut1));
984 EXPECT_EQ(tab_count, tab_strip->count());
985 EXPECT_EQ(tab_strip->GetActiveWebContents(), tab1);
986 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut1)).status);
987 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut2)).status);
989 // And second again. This time the second tab should become active.
990 ActivateShelfItem(model_->ItemIndexByID(shortcut2));
991 EXPECT_EQ(tab_count, tab_strip->count());
992 EXPECT_EQ(tab_strip->GetActiveWebContents(), tab2);
993 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut1)).status);
994 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut2)).status);
997 // Confirm that a page can be navigated from and to while maintaining the
998 // correct running state.
999 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, Navigation) {
1000 ash::LauncherID shortcut_id = CreateShortcut("app1");
1001 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
1002 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1003 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
1005 // Navigate away.
1006 ui_test_utils::NavigateToURL(
1007 browser(), GURL("http://www.example.com/path0/bar.html"));
1008 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
1010 // Navigate back.
1011 ui_test_utils::NavigateToURL(
1012 browser(), GURL("http://www.example.com/path1/foo.html"));
1013 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
1016 // Confirm that a tab can be moved between browsers while maintaining the
1017 // correct running state.
1018 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, TabDragAndDrop) {
1019 TabStripModel* tab_strip_model1 = browser()->tab_strip_model();
1020 EXPECT_EQ(1, tab_strip_model1->count());
1021 int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1022 EXPECT_TRUE(browser_index >= 0);
1023 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1025 // Create a shortcut for app1.
1026 ash::LauncherID shortcut_id = CreateShortcut("app1");
1027 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1028 EXPECT_EQ(ash::STATUS_CLOSED, (*model_->ItemByID(shortcut_id)).status);
1030 // Activate app1 and check its item status.
1031 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1032 EXPECT_EQ(2, tab_strip_model1->count());
1033 EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[browser_index].status);
1034 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
1036 // Create a new browser with blank tab.
1037 Browser* browser2 = CreateBrowser(profile());
1038 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1039 TabStripModel* tab_strip_model2 = browser2->tab_strip_model();
1040 EXPECT_EQ(1, tab_strip_model2->count());
1041 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1042 EXPECT_EQ(ash::STATUS_RUNNING, (*model_->ItemByID(shortcut_id)).status);
1044 // Detach a tab at index 1 (app1) from |tab_strip_model1| and insert it as an
1045 // active tab at index 1 to |tab_strip_model2|.
1046 content::WebContents* detached_tab = tab_strip_model1->DetachWebContentsAt(1);
1047 tab_strip_model2->InsertWebContentsAt(1,
1048 detached_tab,
1049 TabStripModel::ADD_ACTIVE);
1050 EXPECT_EQ(1, tab_strip_model1->count());
1051 EXPECT_EQ(2, tab_strip_model2->count());
1052 EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[browser_index].status);
1053 EXPECT_EQ(ash::STATUS_ACTIVE, (*model_->ItemByID(shortcut_id)).status);
1055 tab_strip_model1->CloseAllTabs();
1056 tab_strip_model2->CloseAllTabs();
1059 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, MultipleOwnedTabs) {
1060 TabStripModel* tab_strip = browser()->tab_strip_model();
1061 int tab_count = tab_strip->count();
1062 ash::LauncherID shortcut_id = CreateShortcut("app1");
1063 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1064 EXPECT_EQ(++tab_count, tab_strip->count());
1065 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1066 WebContents* first_tab = tab_strip->GetActiveWebContents();
1068 // Create new tab owned by app.
1069 ui_test_utils::NavigateToURLWithDisposition(
1070 browser(),
1071 GURL("http://www.example.com/path2/bar.html"),
1072 NEW_FOREGROUND_TAB,
1073 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1074 EXPECT_EQ(++tab_count, tab_strip->count());
1075 // Confirm app is still active.
1076 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1078 // Create new tab not owned by app.
1079 ui_test_utils::NavigateToURLWithDisposition(
1080 browser(),
1081 GURL("http://www.example.com/path3/foo.html"),
1082 NEW_FOREGROUND_TAB,
1084 EXPECT_EQ(++tab_count, tab_strip->count());
1085 // No longer active.
1086 EXPECT_EQ(ash::STATUS_RUNNING, model_->ItemByID(shortcut_id)->status);
1088 // Activating app makes first tab active again.
1089 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1090 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1091 EXPECT_EQ(tab_strip->GetActiveWebContents(), first_tab);
1094 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, RefocusFilter) {
1095 TabStripModel* tab_strip = browser()->tab_strip_model();
1096 int tab_count = tab_strip->count();
1097 ash::LauncherID shortcut_id = CreateShortcut("app1");
1098 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1099 EXPECT_EQ(++tab_count, tab_strip->count());
1100 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1101 WebContents* first_tab = tab_strip->GetActiveWebContents();
1103 controller_->SetRefocusURLPatternForTest(
1104 shortcut_id, GURL("http://www.example.com/path1/*"));
1105 // Create new tab owned by app.
1106 ui_test_utils::NavigateToURLWithDisposition(
1107 browser(),
1108 GURL("http://www.example.com/path2/bar.html"),
1109 NEW_FOREGROUND_TAB,
1110 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1111 EXPECT_EQ(++tab_count, tab_strip->count());
1112 // Confirm app is still active.
1113 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1115 // Create new tab not owned by app.
1116 ui_test_utils::NavigateToURLWithDisposition(
1117 browser(),
1118 GURL("http://www.example.com/path3/foo.html"),
1119 NEW_FOREGROUND_TAB,
1121 EXPECT_EQ(++tab_count, tab_strip->count());
1122 // No longer active.
1123 EXPECT_EQ(ash::STATUS_RUNNING, model_->ItemByID(shortcut_id)->status);
1125 // Activating app makes first tab active again, because second tab isn't
1126 // in its refocus url path.
1127 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1128 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1129 EXPECT_EQ(tab_strip->GetActiveWebContents(), first_tab);
1132 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, RefocusFilterLaunch) {
1133 TabStripModel* tab_strip = browser()->tab_strip_model();
1134 int tab_count = tab_strip->count();
1135 ash::LauncherID shortcut_id = CreateShortcut("app1");
1136 controller_->SetRefocusURLPatternForTest(
1137 shortcut_id, GURL("http://www.example.com/path1/*"));
1139 // Create new tab.
1140 ui_test_utils::NavigateToURLWithDisposition(
1141 browser(),
1142 GURL("http://www.example2.com/path2/bar.html"),
1143 NEW_FOREGROUND_TAB,
1144 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1145 EXPECT_EQ(++tab_count, tab_strip->count());
1146 WebContents* first_tab = tab_strip->GetActiveWebContents();
1147 // Confirm app is not active.
1148 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1150 // Activating app should launch new tab, because second tab isn't
1151 // in its refocus url path.
1152 ActivateShelfItem(model_->ItemIndexByID(shortcut_id));
1153 EXPECT_EQ(++tab_count, tab_strip->count());
1154 WebContents* second_tab = tab_strip->GetActiveWebContents();
1155 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1156 EXPECT_NE(first_tab, second_tab);
1157 EXPECT_EQ(tab_strip->GetActiveWebContents(), second_tab);
1160 // Check the launcher activation state for applications and browser.
1161 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ActivationStateCheck) {
1162 TabStripModel* tab_strip = browser()->tab_strip_model();
1163 // Get the browser item index
1164 int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1165 EXPECT_TRUE(browser_index >= 0);
1167 // Even though we are just comming up, the browser should be active.
1168 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1170 ash::LauncherID shortcut_id = CreateShortcut("app1");
1171 controller_->SetRefocusURLPatternForTest(
1172 shortcut_id, GURL("http://www.example.com/path1/*"));
1174 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1175 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1177 // Create new tab which would be the running app.
1178 ui_test_utils::NavigateToURLWithDisposition(
1179 browser(),
1180 GURL("http://www.example.com/path1/bar.html"),
1181 NEW_FOREGROUND_TAB,
1182 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1184 // There should never be two items active at the same time.
1185 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1186 EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[browser_index].status);
1188 tab_strip->ActivateTabAt(0, false);
1189 EXPECT_EQ(ash::STATUS_RUNNING, model_->ItemByID(shortcut_id)->status);
1190 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1192 tab_strip->CloseWebContentsAt(1, TabStripModel::CLOSE_NONE);
1193 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1194 EXPECT_EQ(ash::STATUS_ACTIVE, model_->items()[browser_index].status);
1196 ash::wm::DeactivateWindow(browser()->window()->GetNativeWindow());
1197 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1198 EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[browser_index].status);
1201 // Check that the launcher activation state for a V1 application stays closed
1202 // even after an asynchronous browser event comes in after the tab got
1203 // destroyed.
1204 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, AsyncActivationStateCheck) {
1205 TabStripModel* tab_strip = browser()->tab_strip_model();
1207 ash::LauncherID shortcut_id = CreateShortcut("app1");
1208 controller_->SetRefocusURLPatternForTest(
1209 shortcut_id, GURL("http://www.example.com/path1/*"));
1211 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1213 // Create new tab which would be the running app.
1214 ui_test_utils::NavigateToURLWithDisposition(
1215 browser(),
1216 GURL("http://www.example.com/path1/bar.html"),
1217 NEW_FOREGROUND_TAB,
1218 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1220 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(shortcut_id)->status);
1221 // To address the issue of crbug.com/174050, the tab we are about to close
1222 // has to be active.
1223 tab_strip->ActivateTabAt(1, false);
1224 EXPECT_EQ(1, tab_strip->active_index());
1226 // Close the web contents.
1227 tab_strip->CloseWebContentsAt(1, TabStripModel::CLOSE_NONE);
1228 // The status should now be set to closed.
1229 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(shortcut_id)->status);
1232 // Checks that a windowed application does not add an item to the browser list.
1233 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1234 WindowedAppDoesNotAddToBrowser) {
1235 // Get the number of items in the browser menu.
1236 size_t items = NumberOfDetectedLauncherBrowsers(false);
1237 size_t running_browser = chrome::GetTotalBrowserCount();
1238 EXPECT_EQ(0u, items);
1239 EXPECT_EQ(0u, running_browser);
1241 LoadAndLaunchExtension(
1242 "app1", extensions::LAUNCH_CONTAINER_WINDOW, NEW_WINDOW);
1244 // No new browser should get detected, even though one more is running.
1245 EXPECT_EQ(0u, NumberOfDetectedLauncherBrowsers(false));
1246 EXPECT_EQ(++running_browser, chrome::GetTotalBrowserCount());
1248 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1250 // A new browser should get detected and one more should be running.
1251 EXPECT_EQ(NumberOfDetectedLauncherBrowsers(false), 1u);
1252 EXPECT_EQ(++running_browser, chrome::GetTotalBrowserCount());
1255 // Checks the functionality to enumerate all browsers vs. all tabs.
1256 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1257 EnumerateALlBrowsersAndTabs) {
1258 // Create at least one browser.
1259 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1260 size_t browsers = NumberOfDetectedLauncherBrowsers(false);
1261 size_t tabs = NumberOfDetectedLauncherBrowsers(true);
1263 // Create a second browser.
1264 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1266 EXPECT_EQ(++browsers, NumberOfDetectedLauncherBrowsers(false));
1267 EXPECT_EQ(++tabs, NumberOfDetectedLauncherBrowsers(true));
1269 // Create only a tab.
1270 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB,
1271 NEW_FOREGROUND_TAB);
1273 EXPECT_EQ(browsers, NumberOfDetectedLauncherBrowsers(false));
1274 EXPECT_EQ(++tabs, NumberOfDetectedLauncherBrowsers(true));
1277 // Check that the keyboard activation of a launcher item tabs properly through
1278 // the items at hand.
1279 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, AltNumberTabsTabbing) {
1280 TabStripModel* tab_strip = browser()->tab_strip_model();
1282 ash::LauncherID shortcut_id = CreateShortcut("app");
1283 controller_->SetRefocusURLPatternForTest(
1284 shortcut_id, GURL("http://www.example.com/path/*"));
1285 std::string url = "http://www.example.com/path/bla";
1287 int shortcut_index = model_->ItemIndexByID(shortcut_id);
1289 // Create an application handled browser tab.
1290 ui_test_utils::NavigateToURLWithDisposition(
1291 browser(),
1292 GURL(url),
1293 NEW_FOREGROUND_TAB,
1294 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1296 content::WebContents* content1 = tab_strip->GetActiveWebContents();
1298 // Create some other browser tab.
1299 ui_test_utils::NavigateToURLWithDisposition(
1300 browser(),
1301 GURL("http://www.test.com"),
1302 NEW_FOREGROUND_TAB,
1303 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1304 content::WebContents* content1a = tab_strip->GetActiveWebContents();
1306 // Make sure that the active tab is now our handled tab.
1307 EXPECT_NE(content1a, content1);
1309 // The active tab should still be the unnamed tab. Then we switch and reach
1310 // the first app and stay there.
1311 EXPECT_EQ(content1a, tab_strip->GetActiveWebContents());
1312 ActivateShelfItem(shortcut_index);
1313 EXPECT_EQ(content1, tab_strip->GetActiveWebContents());
1314 ActivateShelfItem(shortcut_index);
1315 EXPECT_EQ(content1, tab_strip->GetActiveWebContents());
1317 ui_test_utils::NavigateToURLWithDisposition(
1318 browser(),
1319 GURL(url),
1320 NEW_FOREGROUND_TAB,
1321 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1322 content::WebContents* content2 = tab_strip->GetActiveWebContents();
1324 EXPECT_EQ(content2, browser()->tab_strip_model()->GetActiveWebContents());
1325 ActivateShelfItem(shortcut_index);
1326 EXPECT_EQ(content1, browser()->tab_strip_model()->GetActiveWebContents());
1327 ActivateShelfItem(shortcut_index);
1328 EXPECT_EQ(content2, browser()->tab_strip_model()->GetActiveWebContents());
1331 // Check that the keyboard activation of a launcher item tabs properly through
1332 // the items at hand.
1333 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest,
1334 AltNumberAppsTabbing) {
1335 // First run app.
1336 const Extension* extension1 = LoadAndLaunchPlatformApp("launch");
1337 ui::BaseWindow* window1 = CreateShellWindow(extension1)->GetBaseWindow();
1338 const ash::LauncherItem& item1 = GetLastLauncherItem();
1339 ash::LauncherID app_id = item1.id;
1340 int app_index = shelf_model()->ItemIndexByID(app_id);
1342 EXPECT_EQ(ash::TYPE_PLATFORM_APP, item1.type);
1343 EXPECT_EQ(ash::STATUS_ACTIVE, item1.status);
1345 const Extension* extension2 = LoadAndLaunchPlatformApp("launch_2");
1346 ui::BaseWindow* window2 = CreateShellWindow(extension2)->GetBaseWindow();
1348 // By now the browser should be active. Issue Alt keystrokes several times to
1349 // see that we stay on that application.
1350 EXPECT_TRUE(window2->IsActive());
1351 ActivateShelfItem(app_index);
1352 EXPECT_TRUE(window1->IsActive());
1353 ActivateShelfItem(app_index);
1354 EXPECT_TRUE(window1->IsActive());
1356 ui::BaseWindow* window1a = CreateShellWindow(extension1)->GetBaseWindow();
1358 EXPECT_TRUE(window1a->IsActive());
1359 EXPECT_FALSE(window1->IsActive());
1360 ActivateShelfItem(app_index);
1361 EXPECT_TRUE(window1->IsActive());
1362 ActivateShelfItem(app_index);
1363 EXPECT_TRUE(window1a->IsActive());
1366 // Test that we can launch a platform app panel and get a running item.
1367 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, LaunchPanelWindow) {
1368 int item_count = shelf_model()->item_count();
1369 const Extension* extension = LoadAndLaunchPlatformApp("launch");
1370 ShellWindow::CreateParams params;
1371 params.window_type = ShellWindow::WINDOW_TYPE_PANEL;
1372 params.focused = false;
1373 ShellWindow* window = CreateShellWindowFromParams(extension, params);
1374 ++item_count;
1375 ASSERT_EQ(item_count, shelf_model()->item_count());
1376 const ash::LauncherItem& item = GetLastLauncherPanelItem();
1377 EXPECT_EQ(ash::TYPE_APP_PANEL, item.type);
1378 // Opening a panel does not activate it.
1379 EXPECT_EQ(ash::STATUS_RUNNING, item.status);
1380 CloseShellWindow(window);
1381 --item_count;
1382 EXPECT_EQ(item_count, shelf_model()->item_count());
1385 // Test attention states of windows.
1386 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, WindowAttentionStatus) {
1387 const Extension* extension = LoadAndLaunchPlatformApp("launch");
1388 ShellWindow::CreateParams params;
1389 params.window_type = ShellWindow::WINDOW_TYPE_PANEL;
1390 params.focused = false;
1391 ShellWindow* panel = CreateShellWindowFromParams(extension, params);
1392 EXPECT_TRUE(panel->GetNativeWindow()->IsVisible());
1393 // Panels should not be active by default.
1394 EXPECT_FALSE(panel->GetBaseWindow()->IsActive());
1395 // Confirm that a controller item was created and is the correct state.
1396 const ash::LauncherItem& item = GetLastLauncherPanelItem();
1397 LauncherItemController* item_controller = GetItemController(item.id);
1398 EXPECT_EQ(ash::TYPE_APP_PANEL, item.type);
1399 EXPECT_EQ(ash::STATUS_RUNNING, item.status);
1400 EXPECT_EQ(LauncherItemController::TYPE_APP_PANEL, item_controller->type());
1402 // App windows should go to attention state.
1403 panel->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey, true);
1404 EXPECT_EQ(ash::STATUS_ATTENTION, item.status);
1406 // Click the item and confirm that the panel is activated.
1407 TestEvent click_event(ui::ET_MOUSE_PRESSED);
1408 item_controller->ItemSelected(click_event);
1409 EXPECT_TRUE(panel->GetBaseWindow()->IsActive());
1410 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
1412 // Active windows don't show attention.
1413 panel->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey, true);
1414 EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
1417 // Checks that the browser Alt "tabbing" is properly done.
1418 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1419 AltNumberBrowserTabbing) {
1420 // Get the number of items in the browser menu.
1421 EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
1422 // The first activation should create a browser at index 1 (App List @ 0).
1423 shelf_->ActivateShelfItem(1);
1424 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1425 // A second activation should not create a new instance.
1426 shelf_->ActivateShelfItem(1);
1427 Browser* browser1 = chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow());
1428 EXPECT_TRUE(browser1);
1429 aura::Window* window1 = browser1->window()->GetNativeWindow();
1430 Browser* browser2 = CreateBrowser(profile());
1431 aura::Window* window2 = browser2->window()->GetNativeWindow();
1433 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1434 EXPECT_NE(window1, window2);
1435 EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1437 // Activate multiple times the switcher to see that the windows get activated.
1438 shelf_->ActivateShelfItem(1);
1439 EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1440 shelf_->ActivateShelfItem(1);
1441 EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1443 // Create a third browser - make sure that we do not toggle simply between
1444 // two windows.
1445 Browser* browser3 = CreateBrowser(profile());
1446 aura::Window* window3 = browser3->window()->GetNativeWindow();
1448 EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
1449 EXPECT_NE(window1, window3);
1450 EXPECT_NE(window2, window3);
1451 EXPECT_EQ(window3, ash::wm::GetActiveWindow());
1453 shelf_->ActivateShelfItem(1);
1454 EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1455 shelf_->ActivateShelfItem(1);
1456 EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1457 shelf_->ActivateShelfItem(1);
1458 EXPECT_EQ(window3, ash::wm::GetActiveWindow());
1459 shelf_->ActivateShelfItem(1);
1460 EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1462 // Create anther app and make sure that none of our browsers is active.
1463 LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1464 EXPECT_NE(window1, ash::wm::GetActiveWindow());
1465 EXPECT_NE(window2, ash::wm::GetActiveWindow());
1467 // After activation our browser should be active again.
1468 shelf_->ActivateShelfItem(1);
1469 EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1472 // Checks that after a session restore, we do not start applications on an
1473 // activation.
1474 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ActivateAfterSessionRestore) {
1475 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1477 // Create a known application.
1478 ash::LauncherID shortcut_id = CreateShortcut("app1");
1480 // Create a new browser - without activating it - and load an "app" into it.
1481 Browser::CreateParams params =
1482 Browser::CreateParams(profile(), chrome::GetActiveDesktop());
1483 params.initial_show_state = ui::SHOW_STATE_INACTIVE;
1484 Browser* browser2 = new Browser(params);
1485 controller_->SetRefocusURLPatternForTest(
1486 shortcut_id, GURL("http://www.example.com/path/*"));
1487 std::string url = "http://www.example.com/path/bla";
1488 ui_test_utils::NavigateToURLWithDisposition(
1489 browser2,
1490 GURL(url),
1491 NEW_FOREGROUND_TAB,
1492 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1494 // Remember the number of tabs for each browser.
1495 TabStripModel* tab_strip = browser()->tab_strip_model();
1496 int tab_count1 = tab_strip->count();
1497 TabStripModel* tab_strip2 = browser2->tab_strip_model();
1498 int tab_count2 = tab_strip2->count();
1500 // Check that we have two browsers and the inactive browser remained inactive.
1501 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1502 EXPECT_EQ(chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()),
1503 browser());
1504 // Check that the LRU browser list does only contain the original browser.
1505 BrowserList* ash_browser_list =
1506 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
1507 BrowserList::const_reverse_iterator it =
1508 ash_browser_list->begin_last_active();
1509 EXPECT_EQ(*it, browser());
1510 ++it;
1511 EXPECT_EQ(it, ash_browser_list->end_last_active());
1513 // Now request to either activate an existing app or create a new one.
1514 LauncherItemController* item_controller =
1515 controller_->GetLauncherItemController(shortcut_id);
1516 item_controller->ItemSelected(ui::KeyEvent(ui::ET_KEY_RELEASED,
1517 ui::VKEY_RETURN,
1519 false));
1521 // Check that we have set focus on the existing application and nothing new
1522 // was created.
1523 EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1524 EXPECT_EQ(tab_count1, tab_strip->count());
1525 EXPECT_EQ(tab_count2, tab_strip2->count());
1526 EXPECT_EQ(chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()),
1527 browser2);
1530 // Do various drag and drop interaction tests between the application list and
1531 // the launcher.
1532 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, DragAndDrop) {
1533 // Get a number of interfaces we need.
1534 aura::test::EventGenerator generator(
1535 ash::Shell::GetPrimaryRootWindow(), gfx::Point());
1536 ash::test::ShelfViewTestAPI test(
1537 ash::test::ShelfTestAPI(shelf_).shelf_view());
1538 AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1540 // There should be two items in our launcher by this time.
1541 EXPECT_EQ(2, model_->item_count());
1542 EXPECT_FALSE(service->IsAppListVisible());
1544 // Open the app list menu and check that the drag and drop host was set.
1545 gfx::Rect app_list_bounds =
1546 test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1547 generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1548 app_list_bounds.CenterPoint().y());
1549 base::MessageLoop::current()->RunUntilIdle();
1550 generator.ClickLeftButton();
1552 EXPECT_TRUE(service->IsAppListVisible());
1553 app_list::AppsGridView* grid_view =
1554 ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
1555 GetRootGridView();
1556 ASSERT_TRUE(grid_view);
1557 ASSERT_TRUE(grid_view->has_drag_and_drop_host_for_test());
1559 // There should be 2 items in our application list.
1560 const views::ViewModel* vm_grid = grid_view->view_model_for_test();
1561 EXPECT_EQ(2, vm_grid->view_size());
1563 // Test #1: Drag an app list which does not exist yet item into the
1564 // launcher. Keeping it dragged, see that a new item gets created. Continuing
1565 // to drag it out should remove it again.
1567 // Get over item #1 of the application list and press the mouse button.
1568 views::View* item1 = vm_grid->view_at(1);
1569 gfx::Rect bounds_grid_1 = item1->GetBoundsInScreen();
1570 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1571 bounds_grid_1.CenterPoint().y());
1572 base::MessageLoop::current()->RunUntilIdle();
1573 generator.PressLeftButton();
1575 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1577 // Drag the item into the shelf and check that a new item gets created.
1578 const views::ViewModel* vm_shelf = test.shelf_view()->view_model_for_test();
1579 views::View* shelf1 = vm_shelf->view_at(1);
1580 gfx::Rect bounds_shelf_1 = shelf1->GetBoundsInScreen();
1581 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1582 bounds_shelf_1.CenterPoint().y());
1583 base::MessageLoop::current()->RunUntilIdle();
1585 // Check that a new item got created.
1586 EXPECT_EQ(3, model_->item_count());
1587 EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1589 // Move it where the item originally was and check that it disappears again.
1590 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1591 bounds_grid_1.CenterPoint().y());
1592 base::MessageLoop::current()->RunUntilIdle();
1593 EXPECT_EQ(2, model_->item_count());
1594 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1596 // Dropping it should keep the launcher as it originally was.
1597 generator.ReleaseLeftButton();
1598 base::MessageLoop::current()->RunUntilIdle();
1599 EXPECT_EQ(2, model_->item_count());
1600 // There are a few animations which need finishing before we can continue.
1601 test.RunMessageLoopUntilAnimationsDone();
1602 // Move the mouse outside of the launcher.
1603 generator.MoveMouseTo(0, 0);
1605 // Test #2: Check that the unknown item dropped into the launcher will
1606 // create a new item.
1607 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1608 bounds_grid_1.CenterPoint().y());
1609 generator.PressLeftButton();
1610 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1611 bounds_shelf_1.CenterPoint().y());
1612 base::MessageLoop::current()->RunUntilIdle();
1613 EXPECT_EQ(3, model_->item_count());
1614 EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1615 generator.ReleaseLeftButton();
1616 base::MessageLoop::current()->RunUntilIdle();
1617 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1618 EXPECT_EQ(3, model_->item_count()); // It should be still there.
1619 test.RunMessageLoopUntilAnimationsDone();
1621 // Test #3: Check that the now known item dropped into the launcher will
1622 // not create a new item.
1623 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1624 bounds_grid_1.CenterPoint().y());
1625 generator.PressLeftButton();
1626 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1627 bounds_shelf_1.CenterPoint().y());
1628 base::MessageLoop::current()->RunUntilIdle();
1629 EXPECT_EQ(3, model_->item_count()); // No new item got added.
1630 EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1631 generator.ReleaseLeftButton();
1632 base::MessageLoop::current()->RunUntilIdle();
1633 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1634 EXPECT_EQ(3, model_->item_count()); // And it remains that way.
1636 // Test #4: Check that by pressing ESC the operation gets cancelled.
1637 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1638 bounds_grid_1.CenterPoint().y());
1639 generator.PressLeftButton();
1640 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1641 bounds_shelf_1.CenterPoint().y());
1642 base::MessageLoop::current()->RunUntilIdle();
1643 // Issue an ESC and see that the operation gets cancelled.
1644 generator.PressKey(ui::VKEY_ESCAPE, 0);
1645 generator.ReleaseKey(ui::VKEY_ESCAPE, 0);
1646 EXPECT_FALSE(grid_view->dragging());
1647 EXPECT_FALSE(grid_view->has_dragged_view());
1648 generator.ReleaseLeftButton();
1651 #if !defined(OS_WIN)
1652 // Used to test drag & drop an item between app list and shelf with multi
1653 // display environment.
1654 class ShelfAppBrowserTestWithMultiMonitor
1655 : public ShelfAppBrowserTestNoDefaultBrowser {
1656 protected:
1657 ShelfAppBrowserTestWithMultiMonitor() {}
1658 virtual ~ShelfAppBrowserTestWithMultiMonitor() {}
1660 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1661 ShelfAppBrowserTestNoDefaultBrowser::SetUpCommandLine(command_line);
1662 command_line->AppendSwitchASCII("ash-host-window-bounds",
1663 "800x600,801+0-800x600");
1666 private:
1668 DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserTestWithMultiMonitor);
1671 // Do basic drag and drop interaction tests between the application list and
1672 // the launcher in the secondary monitor.
1673 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestWithMultiMonitor,
1674 BasicDragAndDrop) {
1675 // Get a number of interfaces we need.
1676 DCHECK_EQ(ash::Shell::GetAllRootWindows().size(), 2U);
1677 aura::Window* secondary_root_window = ash::Shell::GetAllRootWindows()[1];
1678 ash::Shelf* secondary_shelf = ash::Shelf::ForWindow(secondary_root_window);
1680 aura::test::EventGenerator generator(secondary_root_window, gfx::Point());
1681 ash::test::ShelfViewTestAPI test(
1682 ash::test::ShelfTestAPI(secondary_shelf).shelf_view());
1683 AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1685 // There should be two items in our shelf by this time.
1686 EXPECT_EQ(2, model_->item_count());
1687 EXPECT_FALSE(service->IsAppListVisible());
1689 // Open the app list menu and check that the drag and drop host was set.
1690 gfx::Rect app_list_bounds =
1691 test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1692 gfx::Display display =
1693 ash::Shell::GetScreen()->GetDisplayNearestWindow(secondary_root_window);
1694 const gfx::Point& origin = display.bounds().origin();
1695 app_list_bounds.Offset(-origin.x(), -origin.y());
1697 generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1698 app_list_bounds.CenterPoint().y());
1699 base::MessageLoop::current()->RunUntilIdle();
1700 generator.ClickLeftButton();
1702 EXPECT_TRUE(service->IsAppListVisible());
1703 app_list::AppsGridView* grid_view =
1704 ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
1705 GetRootGridView();
1706 ASSERT_TRUE(grid_view);
1707 ASSERT_TRUE(grid_view->has_drag_and_drop_host_for_test());
1709 // There should be 2 items in our application list.
1710 const views::ViewModel* vm_grid = grid_view->view_model_for_test();
1711 EXPECT_EQ(2, vm_grid->view_size());
1713 // Drag an app list item which does not exist yet in the shelf.
1714 // Keeping it dragged, see that a new item gets created.
1715 // Continuing to drag it out should remove it again.
1717 // Get over item #1 of the application list and press the mouse button.
1718 views::View* item1 = vm_grid->view_at(1);
1719 gfx::Rect bounds_grid_1 = item1->GetBoundsInScreen();
1720 bounds_grid_1.Offset(-origin.x(), -origin.y());
1721 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1722 bounds_grid_1.CenterPoint().y());
1723 base::MessageLoop::current()->RunUntilIdle();
1724 generator.PressLeftButton();
1726 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1728 // Drag the item into the shelf and check that a new item gets created.
1729 const views::ViewModel* vm_shelf = test.shelf_view()->view_model_for_test();
1730 views::View* shelf1 = vm_shelf->view_at(1);
1731 gfx::Rect bounds_shelf_1 = shelf1->GetBoundsInScreen();
1732 bounds_shelf_1.Offset(-origin.x(), -origin.y());
1733 generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1734 bounds_shelf_1.CenterPoint().y());
1735 base::MessageLoop::current()->RunUntilIdle();
1737 // Check that a new item got created.
1738 EXPECT_EQ(3, model_->item_count());
1739 EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1741 // Move it where the item originally was and check that it disappears again.
1742 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1743 bounds_grid_1.CenterPoint().y());
1744 base::MessageLoop::current()->RunUntilIdle();
1745 EXPECT_EQ(2, model_->item_count());
1746 EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1748 // Dropping it should keep the shelf as it originally was.
1749 generator.ReleaseLeftButton();
1750 base::MessageLoop::current()->RunUntilIdle();
1751 EXPECT_EQ(2, model_->item_count());
1753 #endif
1755 // Do tests for removal of items from the shelf by dragging.
1756 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, DragOffShelf) {
1757 aura::test::EventGenerator generator(
1758 ash::Shell::GetPrimaryRootWindow(), gfx::Point());
1759 ash::test::ShelfViewTestAPI test(
1760 ash::test::ShelfTestAPI(shelf_).shelf_view());
1761 test.SetAnimationDuration(1); // Speed up animations for test.
1762 // Create a known application and check that we have 3 items in the shelf.
1763 CreateShortcut("app1");
1764 test.RunMessageLoopUntilAnimationsDone();
1765 EXPECT_EQ(3, model_->item_count());
1767 // Test #1: Ripping out the browser item should not change anything.
1768 int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1769 EXPECT_LE(0, browser_index);
1770 RipOffItemIndex(browser_index, &generator, &test, RIP_OFF_ITEM);
1771 // => It should not have been removed and the location should be unchanged.
1772 EXPECT_EQ(3, model_->item_count());
1773 EXPECT_EQ(browser_index,
1774 GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT));
1775 // Make sure that the hide state has been unset after the snap back animation
1776 // finished.
1777 ash::internal::ShelfButton* button = test.GetButton(browser_index);
1778 EXPECT_FALSE(button->state() & ash::internal::ShelfButton::STATE_HIDDEN);
1780 // Test #2: Ripping out the application and canceling the operation should
1781 // not change anything.
1782 int app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1783 EXPECT_LE(0, app_index);
1784 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM_AND_CANCEL);
1785 // => It should not have been removed and the location should be unchanged.
1786 ASSERT_EQ(3, model_->item_count());
1787 EXPECT_EQ(app_index, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1789 // Test #3: Ripping out the application and moving it back in should not
1790 // change anything.
1791 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM_AND_RETURN);
1792 // => It should not have been removed and the location should be unchanged.
1793 ASSERT_EQ(3, model_->item_count());
1794 // Through the operation the index might have changed.
1795 app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1797 // Test #4: Ripping out the application should remove the item.
1798 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1799 // => It should not have been removed and the location should be unchanged.
1800 EXPECT_EQ(2, model_->item_count());
1801 EXPECT_EQ(-1, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1803 // Test #5: Uninstalling an application while it is being ripped off should
1804 // not crash.
1805 ash::LauncherID app_id = CreateShortcut("app2");
1806 test.RunMessageLoopUntilAnimationsDone();
1807 int app2_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1808 EXPECT_EQ(3, model_->item_count()); // And it remains that way.
1809 RipOffItemIndex(app2_index,
1810 &generator,
1811 &test,
1812 RIP_OFF_ITEM_AND_DONT_RELEASE_MOUSE);
1813 RemoveShortcut(app_id);
1814 test.RunMessageLoopUntilAnimationsDone();
1815 EXPECT_EQ(2, model_->item_count()); // The item should now be gone.
1816 generator.ReleaseLeftButton();
1817 base::MessageLoop::current()->RunUntilIdle();
1818 EXPECT_EQ(2, model_->item_count()); // And it remains that way.
1819 EXPECT_EQ(-1, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1821 // Test #6: Ripping out the application when the overflow button exists.
1822 // After ripping out, overflow button should be removed.
1823 int items_added = 0;
1824 EXPECT_FALSE(test.IsOverflowButtonVisible());
1826 // Create fake app shortcuts until overflow button is created.
1827 while (!test.IsOverflowButtonVisible()) {
1828 std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
1829 PinFakeApp(fake_app_id);
1830 test.RunMessageLoopUntilAnimationsDone();
1832 ++items_added;
1833 ASSERT_LT(items_added, 10000);
1835 // Make one more item after creating a overflow button.
1836 std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
1837 PinFakeApp(fake_app_id);
1838 test.RunMessageLoopUntilAnimationsDone();
1840 int total_count = model_->item_count();
1841 app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1842 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1843 // When an item is ripped off from the shelf that has overflow button
1844 // (see crbug.com/3050787), it was hidden accidentally and was then
1845 // suppressing any further events. If handled correctly the operation will
1846 // however correctly done and the item will get removed (as well as the
1847 // overflow button).
1848 EXPECT_EQ(total_count - 1, model_->item_count());
1849 EXPECT_TRUE(test.IsOverflowButtonVisible());
1851 // Rip off again and the overflow button should has disappeared.
1852 RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1853 EXPECT_EQ(total_count - 2, model_->item_count());
1854 EXPECT_FALSE(test.IsOverflowButtonVisible());
1857 // Check that clicking on an app shelf item launches a new browser.
1858 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ClickItem) {
1859 // Get a number of interfaces we need.
1860 aura::test::EventGenerator generator(
1861 ash::Shell::GetPrimaryRootWindow(), gfx::Point());
1862 ash::test::ShelfViewTestAPI test(
1863 ash::test::ShelfTestAPI(shelf_).shelf_view());
1864 AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1865 // There should be two items in our shelf by this time.
1866 EXPECT_EQ(2, model_->item_count());
1867 EXPECT_FALSE(service->IsAppListVisible());
1869 // Open the app list menu and check that the drag and drop host was set.
1870 gfx::Rect app_list_bounds =
1871 test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1872 generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1873 app_list_bounds.CenterPoint().y());
1874 generator.ClickLeftButton();
1875 base::MessageLoop::current()->RunUntilIdle();
1877 EXPECT_TRUE(service->IsAppListVisible());
1878 app_list::AppsGridView* grid_view =
1879 ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
1880 GetRootGridView();
1881 ASSERT_TRUE(grid_view);
1882 const views::ViewModel* vm_grid = grid_view->view_model_for_test();
1883 EXPECT_EQ(2, vm_grid->view_size());
1884 gfx::Rect bounds_grid_1 = vm_grid->view_at(1)->GetBoundsInScreen();
1885 // Test now that a click does create a new application tab.
1886 TabStripModel* tab_strip = browser()->tab_strip_model();
1887 int tab_count = tab_strip->count();
1888 generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1889 bounds_grid_1.CenterPoint().y());
1890 generator.ClickLeftButton();
1891 base::MessageLoop::current()->RunUntilIdle();
1892 EXPECT_EQ(tab_count + 1, tab_strip->count());
1895 // Check LauncherItemController of Browser Shortcut functionality.
1896 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1897 BrowserShortcutLauncherItemController) {
1898 LauncherItemController* item_controller =
1899 controller_->GetBrowserShortcutLauncherItemController();
1901 // Get the number of browsers.
1902 size_t running_browser = chrome::GetTotalBrowserCount();
1903 EXPECT_EQ(0u, running_browser);
1904 EXPECT_FALSE(item_controller->IsOpen());
1906 // Activate. This creates new browser
1907 item_controller->Activate(ash::LAUNCH_FROM_UNKNOWN);
1908 // New Window is created.
1909 running_browser = chrome::GetTotalBrowserCount();
1910 EXPECT_EQ(1u, running_browser);
1911 EXPECT_TRUE(item_controller->IsOpen());
1913 // Minimize Window.
1914 ash::wm::WindowState* window_state = ash::wm::GetActiveWindowState();
1915 window_state->Minimize();
1916 EXPECT_TRUE(window_state->IsMinimized());
1918 // Activate again. This doesn't create new browser.
1919 // It activates window.
1920 item_controller->Activate(ash::LAUNCH_FROM_UNKNOWN);
1921 running_browser = chrome::GetTotalBrowserCount();
1922 EXPECT_EQ(1u, running_browser);
1923 EXPECT_TRUE(item_controller->IsOpen());
1924 EXPECT_FALSE(window_state->IsMinimized());
1927 // Check that GetLauncherIDForWindow() returns |LauncherID| of the active tab.
1928 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, MatchingLauncherIDandActiveTab) {
1929 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1930 EXPECT_EQ(1, browser()->tab_strip_model()->count());
1931 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
1932 EXPECT_EQ(2, model_->item_count());
1934 aura::Window* window = browser()->window()->GetNativeWindow();
1936 int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1937 ash::LauncherID browser_id = model_->items()[browser_index].id;
1938 EXPECT_EQ(browser_id, ash::GetLauncherIDForWindow(window));
1940 ash::LauncherID app_id = CreateShortcut("app1");
1941 EXPECT_EQ(3, model_->item_count());
1943 // Creates a new tab for "app1" and checks that GetLauncherIDForWindow()
1944 // returns |LauncherID| of "app1".
1945 ActivateShelfItem(model_->ItemIndexByID(app_id));
1946 EXPECT_EQ(2, browser()->tab_strip_model()->count());
1947 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
1948 EXPECT_EQ(app_id, ash::GetLauncherIDForWindow(window));
1950 // Makes tab at index 0(NTP) as an active tab and checks that
1951 // GetLauncherIDForWindow() returns |LauncherID| of browser shortcut.
1952 browser()->tab_strip_model()->ActivateTabAt(0, false);
1953 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
1954 EXPECT_EQ(browser_id, ash::GetLauncherIDForWindow(window));
1957 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, OverflowBubble) {
1958 // Make sure to have a browser window
1959 chrome::NewTab(browser());
1961 // No overflow yet.
1962 EXPECT_FALSE(shelf_->IsShowingOverflowBubble());
1964 ash::test::ShelfViewTestAPI test(
1965 ash::test::ShelfTestAPI(shelf_).shelf_view());
1967 int items_added = 0;
1968 while (!test.IsOverflowButtonVisible()) {
1969 std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
1970 PinFakeApp(fake_app_id);
1972 ++items_added;
1973 ASSERT_LT(items_added, 10000);
1976 // Now show overflow bubble.
1977 test.ShowOverflowBubble();
1978 EXPECT_TRUE(shelf_->IsShowingOverflowBubble());
1980 // Unpin first pinned app and there should be no crash.
1981 controller_->UnpinAppWithID(std::string("fake_app_0"));
1983 test.RunMessageLoopUntilAnimationsDone();
1984 EXPECT_FALSE(shelf_->IsShowingOverflowBubble());
1987 // Check that a windowed V1 application can navigate away from its domain, but
1988 // still gets detected properly.
1989 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, V1AppNavigation) {
1990 // We assume that the web store is always there (which it apparently is).
1991 controller_->PinAppWithID(extension_misc::kWebStoreAppId);
1992 ash::LauncherID id = controller_->GetLauncherIDForAppID(
1993 extension_misc::kWebStoreAppId);
1994 ASSERT_NE(0, id);
1995 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(id)->status);
1997 // Create a windowed application.
1998 AppLaunchParams params(
1999 profile(),
2000 controller_->GetExtensionForAppID(extension_misc::kWebStoreAppId),
2002 chrome::HOST_DESKTOP_TYPE_ASH);
2003 params.container = extensions::LAUNCH_CONTAINER_WINDOW;
2004 OpenApplication(params);
2005 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(id)->status);
2007 // Find the browser which holds our app.
2008 Browser* app_browser = NULL;
2009 const BrowserList* ash_browser_list =
2010 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
2011 for (BrowserList::const_reverse_iterator it =
2012 ash_browser_list->begin_last_active();
2013 it != ash_browser_list->end_last_active() && !app_browser; ++it) {
2014 if ((*it)->is_app()) {
2015 app_browser = *it;
2016 break;
2019 ASSERT_TRUE(app_browser);
2021 // After navigating away in the app, we should still be active.
2022 ui_test_utils::NavigateToURL(app_browser,
2023 GURL("http://www.foo.com/bar.html"));
2024 // Make sure the navigation was entirely performed.
2025 base::MessageLoop::current()->RunUntilIdle();
2026 EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(id)->status);
2027 app_browser->tab_strip_model()->CloseWebContentsAt(0,
2028 TabStripModel::CLOSE_NONE);
2029 // Make sure that the app is really gone.
2030 base::MessageLoop::current()->RunUntilIdle();
2031 EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(id)->status);