1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h"
7 #include "apps/app_window_registry.h"
8 #include "apps/ui/native_app_window.h"
9 #include "base/command_line.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/apps/app_browsertest_util.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_browser_application_mac.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/lifetime/application_lifetime.h"
17 #include "chrome/browser/notifications/message_center_notification_manager.h"
18 #include "chrome/browser/notifications/notification_ui_manager.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser_iterator.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/test/test_utils.h"
25 #include "extensions/common/extension.h"
26 #include "ui/message_center/message_center.h"
28 typedef apps::AppWindowRegistry::AppWindowList AppWindowList;
32 class QuitWithAppsControllerInteractiveTest
33 : public extensions::PlatformAppBrowserTest {
35 QuitWithAppsControllerInteractiveTest() : app_(NULL) {}
37 virtual ~QuitWithAppsControllerInteractiveTest() {}
39 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
40 PlatformAppBrowserTest::SetUpCommandLine(command_line);
41 command_line->AppendSwitch(switches::kAppsKeepChromeAliveInTests);
44 const extensions::Extension* app_;
47 DISALLOW_COPY_AND_ASSIGN(QuitWithAppsControllerInteractiveTest);
52 // Test that quitting while apps are open shows a notification instead.
53 IN_PROC_BROWSER_TEST_F(QuitWithAppsControllerInteractiveTest, QuitBehavior) {
54 scoped_refptr<QuitWithAppsController> controller =
55 new QuitWithAppsController();
56 const Notification* notification;
57 message_center::MessageCenter* message_center =
58 message_center::MessageCenter::Get();
60 // With no app windows open, ShouldQuit returns true.
61 EXPECT_TRUE(controller->ShouldQuit());
62 notification = g_browser_process->notification_ui_manager()->FindById(
63 QuitWithAppsController::kQuitWithAppsNotificationID);
64 EXPECT_EQ(NULL, notification);
66 // Open an app window.
67 ExtensionTestMessageListener listener("Launched", false);
68 app_ = InstallAndLaunchPlatformApp("minimal_id");
69 ASSERT_TRUE(listener.WaitUntilSatisfied());
71 // One browser and one app window at this point.
72 EXPECT_FALSE(chrome::BrowserIterator().done());
73 EXPECT_TRUE(apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0));
75 // On the first quit, show notification.
76 EXPECT_FALSE(controller->ShouldQuit());
77 EXPECT_TRUE(apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0));
78 notification = g_browser_process->notification_ui_manager()->FindById(
79 QuitWithAppsController::kQuitWithAppsNotificationID);
80 ASSERT_TRUE(notification);
82 // If notification was dismissed by click, show again on next quit.
83 notification->delegate()->Click();
84 message_center->RemoveAllNotifications(false);
85 EXPECT_FALSE(controller->ShouldQuit());
86 EXPECT_TRUE(apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0));
87 notification = g_browser_process->notification_ui_manager()->FindById(
88 QuitWithAppsController::kQuitWithAppsNotificationID);
89 ASSERT_TRUE(notification);
91 EXPECT_FALSE(chrome::BrowserIterator().done());
92 EXPECT_TRUE(apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0));
94 // If notification is closed by user, don't show it next time.
95 notification->delegate()->Close(true);
96 message_center->RemoveAllNotifications(false);
97 EXPECT_FALSE(controller->ShouldQuit());
98 EXPECT_TRUE(apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0));
99 notification = g_browser_process->notification_ui_manager()->FindById(
100 QuitWithAppsController::kQuitWithAppsNotificationID);
101 EXPECT_EQ(NULL, notification);
103 EXPECT_FALSE(chrome::BrowserIterator().done());
104 EXPECT_TRUE(apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0));
106 // Quitting should not quit but close all browsers
107 content::WindowedNotificationObserver observer(
108 chrome::NOTIFICATION_BROWSER_CLOSED,
109 content::NotificationService::AllSources());
110 chrome_browser_application_mac::Terminate();
113 EXPECT_TRUE(chrome::BrowserIterator().done());
114 EXPECT_TRUE(apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0));
116 // Trying to quit while there are no browsers always shows notification.
117 EXPECT_FALSE(controller->ShouldQuit());
118 notification = g_browser_process->notification_ui_manager()->FindById(
119 QuitWithAppsController::kQuitWithAppsNotificationID);
120 ASSERT_TRUE(notification);
122 // Clicking "Quit All Apps." button closes all app windows. With no browsers
123 // open, this should also quit Chrome.
124 content::WindowedNotificationObserver quit_observer(
125 chrome::NOTIFICATION_APP_TERMINATING,
126 content::NotificationService::AllSources());
127 notification->delegate()->ButtonClick(0);
128 message_center->RemoveAllNotifications(false);
129 EXPECT_FALSE(apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0));
130 quit_observer.Wait();