38dbda858f702760edb73b5a7f31fb608a5820e6
[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 // Test attention states of windows.
1384 IN_PROC_BROWSER_TEST_F(LauncherPlatformAppBrowserTest, WindowAttentionStatus) {
1385   const Extension* extension = LoadAndLaunchPlatformApp("launch");
1386   AppWindow::CreateParams params;
1387   params.window_type = AppWindow::WINDOW_TYPE_PANEL;
1388   params.focused = false;
1389   AppWindow* panel = CreateAppWindowFromParams(extension, params);
1390   EXPECT_TRUE(panel->GetNativeWindow()->IsVisible());
1391   // Panels should not be active by default.
1392   EXPECT_FALSE(panel->GetBaseWindow()->IsActive());
1393   // Confirm that a controller item was created and is the correct state.
1394   const ash::ShelfItem& item = GetLastLauncherPanelItem();
1395   LauncherItemController* item_controller = GetItemController(item.id);
1396   EXPECT_EQ(ash::TYPE_APP_PANEL, item.type);
1397   EXPECT_EQ(ash::STATUS_RUNNING, item.status);
1398   EXPECT_EQ(LauncherItemController::TYPE_APP_PANEL, item_controller->type());
1399
1400   // App windows should go to attention state.
1401   panel->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey, true);
1402   EXPECT_EQ(ash::STATUS_ATTENTION, item.status);
1403
1404   // Click the item and confirm that the panel is activated.
1405   TestEvent click_event(ui::ET_MOUSE_PRESSED);
1406   item_controller->ItemSelected(click_event);
1407   EXPECT_TRUE(panel->GetBaseWindow()->IsActive());
1408   EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
1409
1410   // Active windows don't show attention.
1411   panel->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey, true);
1412   EXPECT_EQ(ash::STATUS_ACTIVE, item.status);
1413 }
1414
1415 // Checks that the browser Alt "tabbing" is properly done.
1416 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1417                        AltNumberBrowserTabbing) {
1418   // Get the number of items in the browser menu.
1419   EXPECT_EQ(0u, chrome::GetTotalBrowserCount());
1420   // The first activation should create a browser at index 1 (App List @ 0).
1421   shelf_->ActivateShelfItem(1);
1422   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1423   // A second activation should not create a new instance.
1424   shelf_->ActivateShelfItem(1);
1425   Browser* browser1 = chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow());
1426   EXPECT_TRUE(browser1);
1427   aura::Window* window1 = browser1->window()->GetNativeWindow();
1428   Browser* browser2 = CreateBrowser(profile());
1429   aura::Window* window2 = browser2->window()->GetNativeWindow();
1430
1431   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1432   EXPECT_NE(window1, window2);
1433   EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1434
1435   // Activate multiple times the switcher to see that the windows get activated.
1436   shelf_->ActivateShelfItem(1);
1437   EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1438   shelf_->ActivateShelfItem(1);
1439   EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1440
1441   // Create a third browser - make sure that we do not toggle simply between
1442   // two windows.
1443   Browser* browser3 = CreateBrowser(profile());
1444   aura::Window* window3 = browser3->window()->GetNativeWindow();
1445
1446   EXPECT_EQ(3u, chrome::GetTotalBrowserCount());
1447   EXPECT_NE(window1, window3);
1448   EXPECT_NE(window2, window3);
1449   EXPECT_EQ(window3, ash::wm::GetActiveWindow());
1450
1451   shelf_->ActivateShelfItem(1);
1452   EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1453   shelf_->ActivateShelfItem(1);
1454   EXPECT_EQ(window2, ash::wm::GetActiveWindow());
1455   shelf_->ActivateShelfItem(1);
1456   EXPECT_EQ(window3, ash::wm::GetActiveWindow());
1457   shelf_->ActivateShelfItem(1);
1458   EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1459
1460   // Create anther app and make sure that none of our browsers is active.
1461   LoadAndLaunchExtension("app1", extensions::LAUNCH_CONTAINER_TAB, NEW_WINDOW);
1462   EXPECT_NE(window1, ash::wm::GetActiveWindow());
1463   EXPECT_NE(window2, ash::wm::GetActiveWindow());
1464
1465   // After activation our browser should be active again.
1466   shelf_->ActivateShelfItem(1);
1467   EXPECT_EQ(window1, ash::wm::GetActiveWindow());
1468 }
1469
1470 // Checks that after a session restore, we do not start applications on an
1471 // activation.
1472 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ActivateAfterSessionRestore) {
1473   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1474
1475   // Create a known application.
1476   ash::ShelfID shortcut_id = CreateShortcut("app1");
1477
1478   // Create a new browser - without activating it - and load an "app" into it.
1479   Browser::CreateParams params =
1480       Browser::CreateParams(profile(), chrome::GetActiveDesktop());
1481   params.initial_show_state = ui::SHOW_STATE_INACTIVE;
1482   Browser* browser2 = new Browser(params);
1483   controller_->SetRefocusURLPatternForTest(
1484       shortcut_id, GURL("http://www.example.com/path/*"));
1485   std::string url = "http://www.example.com/path/bla";
1486   ui_test_utils::NavigateToURLWithDisposition(
1487       browser2,
1488       GURL(url),
1489       NEW_FOREGROUND_TAB,
1490       ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1491
1492   // Remember the number of tabs for each browser.
1493   TabStripModel* tab_strip = browser()->tab_strip_model();
1494   int tab_count1 = tab_strip->count();
1495   TabStripModel* tab_strip2 = browser2->tab_strip_model();
1496   int tab_count2 = tab_strip2->count();
1497
1498   // Check that we have two browsers and the inactive browser remained inactive.
1499   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1500   EXPECT_EQ(chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()),
1501             browser());
1502   // Check that the LRU browser list does only contain the original browser.
1503   BrowserList* ash_browser_list =
1504       BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
1505   BrowserList::const_reverse_iterator it =
1506       ash_browser_list->begin_last_active();
1507   EXPECT_EQ(*it, browser());
1508   ++it;
1509   EXPECT_EQ(it, ash_browser_list->end_last_active());
1510
1511   // Now request to either activate an existing app or create a new one.
1512   LauncherItemController* item_controller =
1513       controller_->GetLauncherItemController(shortcut_id);
1514   item_controller->ItemSelected(ui::KeyEvent(ui::ET_KEY_RELEASED,
1515                                         ui::VKEY_RETURN,
1516                                         0,
1517                                         false));
1518
1519   // Check that we have set focus on the existing application and nothing new
1520   // was created.
1521   EXPECT_EQ(2u, chrome::GetTotalBrowserCount());
1522   EXPECT_EQ(tab_count1, tab_strip->count());
1523   EXPECT_EQ(tab_count2, tab_strip2->count());
1524   EXPECT_EQ(chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()),
1525             browser2);
1526 }
1527
1528 // Do various drag and drop interaction tests between the application list and
1529 // the launcher.
1530 // TODO(skuhne): Test is flaky with a real compositor: crbug.com/331924
1531 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, DISABLED_DragAndDrop) {
1532   // Get a number of interfaces we need.
1533   aura::test::EventGenerator generator(
1534       ash::Shell::GetPrimaryRootWindow(), gfx::Point());
1535   ash::test::ShelfViewTestAPI test(
1536       ash::test::ShelfTestAPI(shelf_).shelf_view());
1537   AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1538
1539   // There should be two items in our launcher by this time.
1540   EXPECT_EQ(2, model_->item_count());
1541   EXPECT_FALSE(service->IsAppListVisible());
1542
1543   // Open the app list menu and check that the drag and drop host was set.
1544   gfx::Rect app_list_bounds =
1545       test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1546   generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1547                         app_list_bounds.CenterPoint().y());
1548   base::MessageLoop::current()->RunUntilIdle();
1549   generator.ClickLeftButton();
1550
1551   EXPECT_TRUE(service->IsAppListVisible());
1552   app_list::AppsGridView* grid_view =
1553       ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
1554           GetRootGridView();
1555   ASSERT_TRUE(grid_view);
1556   ASSERT_TRUE(grid_view->has_drag_and_drop_host_for_test());
1557
1558   // There should be 2 items in our application list.
1559   const views::ViewModel* vm_grid = grid_view->view_model_for_test();
1560   EXPECT_EQ(2, vm_grid->view_size());
1561
1562   // Test #1: Drag an app list which does not exist yet item into the
1563   // launcher. Keeping it dragged, see that a new item gets created. Continuing
1564   // to drag it out should remove it again.
1565
1566   // Get over item #1 of the application list and press the mouse button.
1567   views::View* item1 = vm_grid->view_at(1);
1568   gfx::Rect bounds_grid_1 = item1->GetBoundsInScreen();
1569   generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1570                         bounds_grid_1.CenterPoint().y());
1571   base::MessageLoop::current()->RunUntilIdle();
1572   generator.PressLeftButton();
1573
1574   EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1575
1576   // Drag the item into the shelf and check that a new item gets created.
1577   const views::ViewModel* vm_shelf = test.shelf_view()->view_model_for_test();
1578   views::View* shelf1 = vm_shelf->view_at(1);
1579   gfx::Rect bounds_shelf_1 = shelf1->GetBoundsInScreen();
1580   generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1581                         bounds_shelf_1.CenterPoint().y());
1582   base::MessageLoop::current()->RunUntilIdle();
1583
1584   // Check that a new item got created.
1585   EXPECT_EQ(3, model_->item_count());
1586   EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1587
1588   // Move it where the item originally was and check that it disappears again.
1589   generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1590                         bounds_grid_1.CenterPoint().y());
1591   base::MessageLoop::current()->RunUntilIdle();
1592   EXPECT_EQ(2, model_->item_count());
1593   EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1594
1595   // Dropping it should keep the launcher as it originally was.
1596   generator.ReleaseLeftButton();
1597   base::MessageLoop::current()->RunUntilIdle();
1598   EXPECT_EQ(2, model_->item_count());
1599   // There are a few animations which need finishing before we can continue.
1600   test.RunMessageLoopUntilAnimationsDone();
1601   // Move the mouse outside of the launcher.
1602   generator.MoveMouseTo(0, 0);
1603
1604   // Test #2: Check that the unknown item dropped into the launcher will
1605   // create a new item.
1606   generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1607                         bounds_grid_1.CenterPoint().y());
1608   generator.PressLeftButton();
1609   generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1610                         bounds_shelf_1.CenterPoint().y());
1611   base::MessageLoop::current()->RunUntilIdle();
1612   EXPECT_EQ(3, model_->item_count());
1613   EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1614   generator.ReleaseLeftButton();
1615   base::MessageLoop::current()->RunUntilIdle();
1616   EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1617   EXPECT_EQ(3, model_->item_count());  // It should be still there.
1618   test.RunMessageLoopUntilAnimationsDone();
1619
1620   // Test #3: Check that the now known item dropped into the launcher will
1621   // not create a new item.
1622   generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1623                         bounds_grid_1.CenterPoint().y());
1624   generator.PressLeftButton();
1625   generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1626                         bounds_shelf_1.CenterPoint().y());
1627   base::MessageLoop::current()->RunUntilIdle();
1628   EXPECT_EQ(3, model_->item_count());  // No new item got added.
1629   EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1630   generator.ReleaseLeftButton();
1631   base::MessageLoop::current()->RunUntilIdle();
1632   EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1633   EXPECT_EQ(3, model_->item_count());  // And it remains that way.
1634
1635   // Test #4: Check that by pressing ESC the operation gets cancelled.
1636   generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1637                         bounds_grid_1.CenterPoint().y());
1638   generator.PressLeftButton();
1639   generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1640                         bounds_shelf_1.CenterPoint().y());
1641   base::MessageLoop::current()->RunUntilIdle();
1642   // Issue an ESC and see that the operation gets cancelled.
1643   generator.PressKey(ui::VKEY_ESCAPE, 0);
1644   generator.ReleaseKey(ui::VKEY_ESCAPE, 0);
1645   EXPECT_FALSE(grid_view->dragging());
1646   EXPECT_FALSE(grid_view->has_dragged_view());
1647   generator.ReleaseLeftButton();
1648 }
1649
1650 #if !defined(OS_WIN)
1651 // Used to test drag & drop an item between app list and shelf with multi
1652 // display environment.
1653 class ShelfAppBrowserTestWithMultiMonitor
1654     : public ShelfAppBrowserTestNoDefaultBrowser {
1655  protected:
1656   ShelfAppBrowserTestWithMultiMonitor() {}
1657   virtual ~ShelfAppBrowserTestWithMultiMonitor() {}
1658
1659   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1660     ShelfAppBrowserTestNoDefaultBrowser::SetUpCommandLine(command_line);
1661     command_line->AppendSwitchASCII("ash-host-window-bounds",
1662                                     "800x600,801+0-800x600");
1663   }
1664
1665  private:
1666
1667   DISALLOW_COPY_AND_ASSIGN(ShelfAppBrowserTestWithMultiMonitor);
1668 };
1669
1670 // Do basic drag and drop interaction tests between the application list and
1671 // the launcher in the secondary monitor.
1672 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestWithMultiMonitor,
1673     BasicDragAndDrop) {
1674   // Get a number of interfaces we need.
1675   DCHECK_EQ(ash::Shell::GetAllRootWindows().size(), 2U);
1676   aura::Window* secondary_root_window = ash::Shell::GetAllRootWindows()[1];
1677   ash::Shelf* secondary_shelf = ash::Shelf::ForWindow(secondary_root_window);
1678
1679   aura::test::EventGenerator generator(secondary_root_window, gfx::Point());
1680   ash::test::ShelfViewTestAPI test(
1681       ash::test::ShelfTestAPI(secondary_shelf).shelf_view());
1682   AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1683
1684   // There should be two items in our shelf by this time.
1685   EXPECT_EQ(2, model_->item_count());
1686   EXPECT_FALSE(service->IsAppListVisible());
1687
1688   // Open the app list menu and check that the drag and drop host was set.
1689   gfx::Rect app_list_bounds =
1690       test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1691   gfx::Display display =
1692       ash::Shell::GetScreen()->GetDisplayNearestWindow(secondary_root_window);
1693   const gfx::Point& origin = display.bounds().origin();
1694   app_list_bounds.Offset(-origin.x(), -origin.y());
1695
1696   generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1697                         app_list_bounds.CenterPoint().y());
1698   base::MessageLoop::current()->RunUntilIdle();
1699   generator.ClickLeftButton();
1700
1701   EXPECT_TRUE(service->IsAppListVisible());
1702   app_list::AppsGridView* grid_view =
1703       ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
1704           GetRootGridView();
1705   ASSERT_TRUE(grid_view);
1706   ASSERT_TRUE(grid_view->has_drag_and_drop_host_for_test());
1707
1708   // There should be 2 items in our application list.
1709   const views::ViewModel* vm_grid = grid_view->view_model_for_test();
1710   EXPECT_EQ(2, vm_grid->view_size());
1711
1712   // Drag an app list item which does not exist yet in the shelf.
1713   // Keeping it dragged, see that a new item gets created.
1714   // Continuing to drag it out should remove it again.
1715
1716   // Get over item #1 of the application list and press the mouse button.
1717   views::View* item1 = vm_grid->view_at(1);
1718   gfx::Rect bounds_grid_1 = item1->GetBoundsInScreen();
1719   bounds_grid_1.Offset(-origin.x(), -origin.y());
1720   generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1721                         bounds_grid_1.CenterPoint().y());
1722   base::MessageLoop::current()->RunUntilIdle();
1723   generator.PressLeftButton();
1724
1725   EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1726
1727   // Drag the item into the shelf and check that a new item gets created.
1728   const views::ViewModel* vm_shelf = test.shelf_view()->view_model_for_test();
1729   views::View* shelf1 = vm_shelf->view_at(1);
1730   gfx::Rect bounds_shelf_1 = shelf1->GetBoundsInScreen();
1731   bounds_shelf_1.Offset(-origin.x(), -origin.y());
1732   generator.MoveMouseTo(bounds_shelf_1.CenterPoint().x(),
1733                         bounds_shelf_1.CenterPoint().y());
1734   base::MessageLoop::current()->RunUntilIdle();
1735
1736   // Check that a new item got created.
1737   EXPECT_EQ(3, model_->item_count());
1738   EXPECT_TRUE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1739
1740   // Move it to an empty slot on grid_view.
1741   gfx::Rect empty_slot_rect = bounds_grid_1;
1742   empty_slot_rect.Offset(0, bounds_grid_1.height());
1743   generator.MoveMouseTo(empty_slot_rect.CenterPoint().x(),
1744                         empty_slot_rect.CenterPoint().y());
1745   base::MessageLoop::current()->RunUntilIdle();
1746   EXPECT_EQ(2, model_->item_count());
1747   EXPECT_FALSE(grid_view->forward_events_to_drag_and_drop_host_for_test());
1748
1749   // Dropping it should keep the shelf as it originally was.
1750   generator.ReleaseLeftButton();
1751   base::MessageLoop::current()->RunUntilIdle();
1752   EXPECT_EQ(2, model_->item_count());
1753 }
1754 #endif
1755
1756 // Do tests for removal of items from the shelf by dragging.
1757 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, DragOffShelf) {
1758   aura::test::EventGenerator generator(
1759       ash::Shell::GetPrimaryRootWindow(), gfx::Point());
1760   ash::test::ShelfViewTestAPI test(
1761       ash::test::ShelfTestAPI(shelf_).shelf_view());
1762   test.SetAnimationDuration(1);  // Speed up animations for test.
1763   // Create a known application and check that we have 3 items in the shelf.
1764   CreateShortcut("app1");
1765   test.RunMessageLoopUntilAnimationsDone();
1766   EXPECT_EQ(3, model_->item_count());
1767
1768   // Test #1: Ripping out the browser item should not change anything.
1769   int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1770   EXPECT_LE(0, browser_index);
1771   RipOffItemIndex(browser_index, &generator, &test, RIP_OFF_ITEM);
1772   // => It should not have been removed and the location should be unchanged.
1773   EXPECT_EQ(3, model_->item_count());
1774   EXPECT_EQ(browser_index,
1775             GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT));
1776   // Make sure that the hide state has been unset after the snap back animation
1777   // finished.
1778   ash::internal::ShelfButton* button = test.GetButton(browser_index);
1779   EXPECT_FALSE(button->state() & ash::internal::ShelfButton::STATE_HIDDEN);
1780
1781   // Test #2: Ripping out the application and canceling the operation should
1782   // not change anything.
1783   int app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1784   EXPECT_LE(0, app_index);
1785   RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM_AND_CANCEL);
1786   // => It should not have been removed and the location should be unchanged.
1787   ASSERT_EQ(3, model_->item_count());
1788   EXPECT_EQ(app_index, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1789
1790   // Test #3: Ripping out the application and moving it back in should not
1791   // change anything.
1792   RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM_AND_RETURN);
1793   // => It should not have been removed and the location should be unchanged.
1794   ASSERT_EQ(3, model_->item_count());
1795   // Through the operation the index might have changed.
1796   app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1797
1798   // Test #4: Ripping out the application should remove the item.
1799   RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1800   // => It should not have been removed and the location should be unchanged.
1801   EXPECT_EQ(2, model_->item_count());
1802   EXPECT_EQ(-1, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1803
1804   // Test #5: Uninstalling an application while it is being ripped off should
1805   // not crash.
1806   ash::ShelfID app_id = CreateShortcut("app2");
1807   test.RunMessageLoopUntilAnimationsDone();
1808   int app2_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1809   EXPECT_EQ(3, model_->item_count());  // And it remains that way.
1810   RipOffItemIndex(app2_index,
1811                   &generator,
1812                   &test,
1813                   RIP_OFF_ITEM_AND_DONT_RELEASE_MOUSE);
1814   RemoveShortcut(app_id);
1815   test.RunMessageLoopUntilAnimationsDone();
1816   EXPECT_EQ(2, model_->item_count());  // The item should now be gone.
1817   generator.ReleaseLeftButton();
1818   base::MessageLoop::current()->RunUntilIdle();
1819   EXPECT_EQ(2, model_->item_count());  // And it remains that way.
1820   EXPECT_EQ(-1, GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT));
1821
1822   // Test #6: Ripping out the application when the overflow button exists.
1823   // After ripping out, overflow button should be removed.
1824   int items_added = 0;
1825   EXPECT_FALSE(test.IsOverflowButtonVisible());
1826
1827   // Create fake app shortcuts until overflow button is created.
1828   while (!test.IsOverflowButtonVisible()) {
1829     std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
1830     PinFakeApp(fake_app_id);
1831     test.RunMessageLoopUntilAnimationsDone();
1832
1833     ++items_added;
1834     ASSERT_LT(items_added, 10000);
1835   }
1836   // Make one more item after creating a overflow button.
1837   std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
1838   PinFakeApp(fake_app_id);
1839   test.RunMessageLoopUntilAnimationsDone();
1840
1841   int total_count = model_->item_count();
1842   app_index = GetIndexOfShelfItemType(ash::TYPE_APP_SHORTCUT);
1843   RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1844   // When an item is ripped off from the shelf that has overflow button
1845   // (see crbug.com/3050787), it was hidden accidentally and was then
1846   // suppressing any further events. If handled correctly the operation will
1847   // however correctly done and the item will get removed (as well as the
1848   // overflow button).
1849   EXPECT_EQ(total_count - 1, model_->item_count());
1850   EXPECT_TRUE(test.IsOverflowButtonVisible());
1851
1852   // Rip off again and the overflow button should has disappeared.
1853   RipOffItemIndex(app_index, &generator, &test, RIP_OFF_ITEM);
1854   EXPECT_EQ(total_count - 2, model_->item_count());
1855   EXPECT_FALSE(test.IsOverflowButtonVisible());
1856 }
1857
1858 // Check that clicking on an app shelf item launches a new browser.
1859 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, ClickItem) {
1860   // Get a number of interfaces we need.
1861   aura::test::EventGenerator generator(
1862       ash::Shell::GetPrimaryRootWindow(), gfx::Point());
1863   ash::test::ShelfViewTestAPI test(
1864       ash::test::ShelfTestAPI(shelf_).shelf_view());
1865   AppListService* service = AppListService::Get(chrome::GetActiveDesktop());
1866   // There should be two items in our shelf by this time.
1867   EXPECT_EQ(2, model_->item_count());
1868   EXPECT_FALSE(service->IsAppListVisible());
1869
1870   // Open the app list menu and check that the drag and drop host was set.
1871   gfx::Rect app_list_bounds =
1872       test.shelf_view()->GetAppListButtonView()->GetBoundsInScreen();
1873   generator.MoveMouseTo(app_list_bounds.CenterPoint().x(),
1874                         app_list_bounds.CenterPoint().y());
1875   generator.ClickLeftButton();
1876   base::MessageLoop::current()->RunUntilIdle();
1877
1878   EXPECT_TRUE(service->IsAppListVisible());
1879   app_list::AppsGridView* grid_view =
1880       ash::test::AppListControllerTestApi(ash::Shell::GetInstance()).
1881           GetRootGridView();
1882   ASSERT_TRUE(grid_view);
1883   const views::ViewModel* vm_grid = grid_view->view_model_for_test();
1884   EXPECT_EQ(2, vm_grid->view_size());
1885   gfx::Rect bounds_grid_1 = vm_grid->view_at(1)->GetBoundsInScreen();
1886   // Test now that a click does create a new application tab.
1887   TabStripModel* tab_strip = browser()->tab_strip_model();
1888   int tab_count = tab_strip->count();
1889   generator.MoveMouseTo(bounds_grid_1.CenterPoint().x(),
1890                         bounds_grid_1.CenterPoint().y());
1891   generator.ClickLeftButton();
1892   base::MessageLoop::current()->RunUntilIdle();
1893   EXPECT_EQ(tab_count + 1, tab_strip->count());
1894 }
1895
1896 // Check LauncherItemController of Browser Shortcut functionality.
1897 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTestNoDefaultBrowser,
1898                        BrowserShortcutLauncherItemController) {
1899   LauncherItemController* item_controller =
1900       controller_->GetBrowserShortcutLauncherItemController();
1901
1902   // Get the number of browsers.
1903   size_t running_browser = chrome::GetTotalBrowserCount();
1904   EXPECT_EQ(0u, running_browser);
1905   EXPECT_FALSE(item_controller->IsOpen());
1906
1907   // Activate. This creates new browser
1908   item_controller->Activate(ash::LAUNCH_FROM_UNKNOWN);
1909   // New Window is created.
1910   running_browser = chrome::GetTotalBrowserCount();
1911   EXPECT_EQ(1u, running_browser);
1912   EXPECT_TRUE(item_controller->IsOpen());
1913
1914   // Minimize Window.
1915   ash::wm::WindowState* window_state = ash::wm::GetActiveWindowState();
1916   window_state->Minimize();
1917   EXPECT_TRUE(window_state->IsMinimized());
1918
1919   // Activate again. This doesn't create new browser.
1920   // It activates window.
1921   item_controller->Activate(ash::LAUNCH_FROM_UNKNOWN);
1922   running_browser = chrome::GetTotalBrowserCount();
1923   EXPECT_EQ(1u, running_browser);
1924   EXPECT_TRUE(item_controller->IsOpen());
1925   EXPECT_FALSE(window_state->IsMinimized());
1926 }
1927
1928 // Check that GetShelfIDForWindow() returns |ShelfID| of the active tab.
1929 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, MatchingShelfIDandActiveTab) {
1930   EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
1931   EXPECT_EQ(1, browser()->tab_strip_model()->count());
1932   EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
1933   EXPECT_EQ(2, model_->item_count());
1934
1935   aura::Window* window = browser()->window()->GetNativeWindow();
1936
1937   int browser_index = GetIndexOfShelfItemType(ash::TYPE_BROWSER_SHORTCUT);
1938   ash::ShelfID browser_id = model_->items()[browser_index].id;
1939   EXPECT_EQ(browser_id, ash::GetShelfIDForWindow(window));
1940
1941   ash::ShelfID app_id = CreateShortcut("app1");
1942   EXPECT_EQ(3, model_->item_count());
1943
1944   // Creates a new tab for "app1" and checks that GetShelfIDForWindow()
1945   // returns |ShelfID| of "app1".
1946   ActivateShelfItem(model_->ItemIndexByID(app_id));
1947   EXPECT_EQ(2, browser()->tab_strip_model()->count());
1948   EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
1949   EXPECT_EQ(app_id, ash::GetShelfIDForWindow(window));
1950
1951   // Makes tab at index 0(NTP) as an active tab and checks that
1952   // GetShelfIDForWindow() returns |ShelfID| of browser shortcut.
1953   browser()->tab_strip_model()->ActivateTabAt(0, false);
1954   EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
1955   EXPECT_EQ(browser_id, ash::GetShelfIDForWindow(window));
1956 }
1957
1958 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, OverflowBubble) {
1959   // Make sure to have a browser window
1960   chrome::NewTab(browser());
1961
1962   // No overflow yet.
1963   EXPECT_FALSE(shelf_->IsShowingOverflowBubble());
1964
1965   ash::test::ShelfViewTestAPI test(
1966       ash::test::ShelfTestAPI(shelf_).shelf_view());
1967
1968   int items_added = 0;
1969   while (!test.IsOverflowButtonVisible()) {
1970     std::string fake_app_id = base::StringPrintf("fake_app_%d", items_added);
1971     PinFakeApp(fake_app_id);
1972
1973     ++items_added;
1974     ASSERT_LT(items_added, 10000);
1975   }
1976
1977   // Now show overflow bubble.
1978   test.ShowOverflowBubble();
1979   EXPECT_TRUE(shelf_->IsShowingOverflowBubble());
1980
1981   // Unpin first pinned app and there should be no crash.
1982   controller_->UnpinAppWithID(std::string("fake_app_0"));
1983
1984   test.RunMessageLoopUntilAnimationsDone();
1985   EXPECT_FALSE(shelf_->IsShowingOverflowBubble());
1986 }
1987
1988 // Check that a windowed V1 application can navigate away from its domain, but
1989 // still gets detected properly.
1990 IN_PROC_BROWSER_TEST_F(ShelfAppBrowserTest, V1AppNavigation) {
1991   // We assume that the web store is always there (which it apparently is).
1992   controller_->PinAppWithID(extension_misc::kWebStoreAppId);
1993   ash::ShelfID id = controller_->GetShelfIDForAppID(
1994       extension_misc::kWebStoreAppId);
1995   ASSERT_NE(0, id);
1996   EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(id)->status);
1997
1998   // Create a windowed application.
1999   AppLaunchParams params(
2000       profile(),
2001       controller_->GetExtensionForAppID(extension_misc::kWebStoreAppId),
2002       0,
2003       chrome::HOST_DESKTOP_TYPE_ASH);
2004   params.container = extensions::LAUNCH_CONTAINER_WINDOW;
2005   OpenApplication(params);
2006   EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(id)->status);
2007
2008   // Find the browser which holds our app.
2009   Browser* app_browser = NULL;
2010   const BrowserList* ash_browser_list =
2011       BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
2012   for (BrowserList::const_reverse_iterator it =
2013            ash_browser_list->begin_last_active();
2014        it != ash_browser_list->end_last_active() && !app_browser; ++it) {
2015     if ((*it)->is_app()) {
2016       app_browser = *it;
2017       break;
2018     }
2019   }
2020   ASSERT_TRUE(app_browser);
2021
2022   // After navigating away in the app, we should still be active.
2023   ui_test_utils::NavigateToURL(app_browser,
2024                                GURL("http://www.foo.com/bar.html"));
2025   // Make sure the navigation was entirely performed.
2026   base::MessageLoop::current()->RunUntilIdle();
2027   EXPECT_EQ(ash::STATUS_ACTIVE, model_->ItemByID(id)->status);
2028   app_browser->tab_strip_model()->CloseWebContentsAt(0,
2029                                                      TabStripModel::CLOSE_NONE);
2030   // Make sure that the app is really gone.
2031   base::MessageLoop::current()->RunUntilIdle();
2032   EXPECT_EQ(ash::STATUS_CLOSED, model_->ItemByID(id)->status);
2033 }