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