Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(512)

Side by Side Diff: chrome/browser/ui/ash/launcher/chrome_launcher_controller_browsertest.cc

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

Powered by Google App Engine
This is Rietveld 408576698