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