Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / chrome / browser / app_controller_mac_browsertest.mm
blobd094111fe92cb558c55bbb014004b505b12f7143
1 // Copyright (c) 2012 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 #import <Cocoa/Cocoa.h>
7 #include "apps/app_window_registry.h"
8 #include "base/command_line.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #import "chrome/browser/app_controller_mac.h"
13 #include "chrome/browser/apps/app_browsertest_util.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #import "chrome/browser/ui/cocoa/user_manager_mac.h"
21 #include "chrome/browser/ui/host_desktop.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chrome/test/base/ui_test_utils.h"
28 #include "content/public/browser/web_contents.h"
29 #include "extensions/common/extension.h"
31 namespace {
33 class AppControllerPlatformAppBrowserTest
34     : public extensions::PlatformAppBrowserTest {
35  protected:
36   AppControllerPlatformAppBrowserTest()
37       : active_browser_list_(BrowserList::GetInstance(
38                                 chrome::GetActiveDesktop())) {
39   }
41   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
42     PlatformAppBrowserTest::SetUpCommandLine(command_line);
43     command_line->AppendSwitchASCII(switches::kAppId,
44                                     "1234");
45   }
47   const BrowserList* active_browser_list_;
50 // Test that if only a platform app window is open and no browser windows are
51 // open then a reopen event does nothing.
52 IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest,
53                        PlatformAppReopenWithWindows) {
54   base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
55   NSUInteger old_window_count = [[NSApp windows] count];
56   EXPECT_EQ(1u, active_browser_list_->size());
57   [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:YES];
58   // We do not EXPECT_TRUE the result here because the method
59   // deminiaturizes windows manually rather than return YES and have
60   // AppKit do it.
62   EXPECT_EQ(old_window_count, [[NSApp windows] count]);
63   EXPECT_EQ(1u, active_browser_list_->size());
66 IN_PROC_BROWSER_TEST_F(AppControllerPlatformAppBrowserTest,
67                        ActivationFocusesBrowserWindow) {
68   base::scoped_nsobject<AppController> app_controller(
69       [[AppController alloc] init]);
71   ExtensionTestMessageListener listener("Launched", false);
72   const extensions::Extension* app =
73       InstallAndLaunchPlatformApp("minimal");
74   ASSERT_TRUE(listener.WaitUntilSatisfied());
76   NSWindow* app_window = apps::AppWindowRegistry::Get(profile())
77                              ->GetAppWindowsForApp(app->id())
78                              .front()
79                              ->GetNativeWindow();
80   NSWindow* browser_window = browser()->window()->GetNativeWindow();
82   EXPECT_LE([[NSApp orderedWindows] indexOfObject:app_window],
83             [[NSApp orderedWindows] indexOfObject:browser_window]);
84   [app_controller applicationShouldHandleReopen:NSApp
85                               hasVisibleWindows:YES];
86   EXPECT_LE([[NSApp orderedWindows] indexOfObject:browser_window],
87             [[NSApp orderedWindows] indexOfObject:app_window]);
90 class AppControllerWebAppBrowserTest : public InProcessBrowserTest {
91  protected:
92   AppControllerWebAppBrowserTest()
93       : active_browser_list_(BrowserList::GetInstance(
94                                 chrome::GetActiveDesktop())) {
95   }
97   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
98     command_line->AppendSwitchASCII(switches::kApp, GetAppURL());
99   }
101   std::string GetAppURL() const {
102     return "http://example.com/";
103   }
105   const BrowserList* active_browser_list_;
108 // Test that in web app mode a reopen event opens the app URL.
109 IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest,
110                        WebAppReopenWithNoWindows) {
111   base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
112   EXPECT_EQ(1u, active_browser_list_->size());
113   BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
115   EXPECT_FALSE(result);
116   EXPECT_EQ(2u, active_browser_list_->size());
118   Browser* browser = active_browser_list_->get(0);
119   GURL current_url =
120       browser->tab_strip_model()->GetActiveWebContents()->GetURL();
121   EXPECT_EQ(GetAppURL(), current_url.spec());
124 // Called when the ProfileManager has created a profile.
125 void CreateProfileCallback(const base::Closure& quit_closure,
126                            Profile* profile,
127                            Profile::CreateStatus status) {
128   EXPECT_TRUE(profile);
129   EXPECT_NE(Profile::CREATE_STATUS_LOCAL_FAIL, status);
130   EXPECT_NE(Profile::CREATE_STATUS_REMOTE_FAIL, status);
131   // This will be called multiple times. Wait until the profile is initialized
132   // fully to quit the loop.
133   if (status == Profile::CREATE_STATUS_INITIALIZED)
134     quit_closure.Run();
137 void CreateAndWaitForGuestProfile() {
138   ProfileManager::CreateCallback create_callback =
139       base::Bind(&CreateProfileCallback,
140                  base::MessageLoop::current()->QuitClosure());
141   g_browser_process->profile_manager()->CreateProfileAsync(
142       ProfileManager::GetGuestProfilePath(),
143       create_callback,
144       base::string16(),
145       base::string16(),
146       std::string());
147   base::RunLoop().Run();
150 class AppControllerNewProfileManagementBrowserTest
151     : public InProcessBrowserTest {
152  protected:
153   AppControllerNewProfileManagementBrowserTest()
154       : active_browser_list_(BrowserList::GetInstance(
155                                 chrome::GetActiveDesktop())) {
156   }
158   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
159     command_line->AppendSwitch(switches::kNewProfileManagement);
160   }
162   const BrowserList* active_browser_list_;
165 // Test that for a regular last profile, a reopen event opens a browser.
166 IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
167                        RegularProfileReopenWithNoWindows) {
168   base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
169   EXPECT_EQ(1u, active_browser_list_->size());
170   BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
172   EXPECT_FALSE(result);
173   EXPECT_EQ(2u, active_browser_list_->size());
174   EXPECT_FALSE(UserManagerMac::IsShowing());
177 // Test that for a locked last profile, a reopen event opens the User Manager.
178 IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
179                        LockedProfileReopenWithNoWindows) {
180   // The User Manager uses the guest profile as its underlying profile. To
181   // minimize flakiness due to the scheduling/descheduling of tasks on the
182   // different threads, pre-initialize the guest profile before it is needed.
183   CreateAndWaitForGuestProfile();
184   base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
186   // Lock the active profile.
187   Profile* profile = [ac lastProfile];
188   ProfileInfoCache& cache =
189       g_browser_process->profile_manager()->GetProfileInfoCache();
190   size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
191   cache.SetProfileSigninRequiredAtIndex(profile_index, true);
192   EXPECT_TRUE(cache.ProfileIsSigninRequiredAtIndex(profile_index));
194   EXPECT_EQ(1u, active_browser_list_->size());
195   BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
196   EXPECT_FALSE(result);
198   base::RunLoop().RunUntilIdle();
199   EXPECT_EQ(1u, active_browser_list_->size());
200   EXPECT_TRUE(UserManagerMac::IsShowing());
201   UserManagerMac::Hide();
204 // Test that for a guest last profile, a reopen event opens the User Manager.
205 IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
206                        GuestProfileReopenWithNoWindows) {
207   // Create the guest profile, and set it as the last used profile so the
208   // app controller can use it on init.
209   CreateAndWaitForGuestProfile();
210   PrefService* local_state = g_browser_process->local_state();
211   local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir);
213   base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
215   Profile* profile = [ac lastProfile];
216   EXPECT_EQ(ProfileManager::GetGuestProfilePath(), profile->GetPath());
217   EXPECT_TRUE(profile->IsGuestSession());
219   EXPECT_EQ(1u, active_browser_list_->size());
220   BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
221   EXPECT_FALSE(result);
223   base::RunLoop().RunUntilIdle();
225   EXPECT_EQ(1u, active_browser_list_->size());
226   EXPECT_TRUE(UserManagerMac::IsShowing());
227   UserManagerMac::Hide();
230 }  // namespace