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.
7 #include <shlobj.h> // Must be before propkey.
11 #include "base/command_line.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/scoped_comptr.h"
15 #include "base/win/scoped_propvariant.h"
16 #include "base/win/windows_version.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/extensions/extension_browsertest.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_info_cache.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/profiles/profile_shortcut_manager_win.h"
23 #include "chrome/browser/profiles/profiles_state.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_finder.h"
26 #include "chrome/browser/ui/browser_iterator.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/extensions/application_launch.h"
29 #include "chrome/browser/web_applications/web_app.h"
30 #include "chrome/browser/web_applications/web_app_win.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/extensions/extension_constants.h"
33 #include "chrome/installer/util/browser_distribution.h"
34 #include "chrome/test/base/in_process_browser_test.h"
35 #include "chrome/test/base/test_switches.h"
36 #include "content/public/test/test_utils.h"
37 #include "extensions/common/extension.h"
38 #include "ui/views/win/hwnd_util.h"
40 typedef ExtensionBrowserTest BrowserWindowPropertyManagerTest;
44 // An observer that resumes test code after a new profile is initialized by
45 // quitting the message loop it's blocked on.
46 void UnblockOnProfileCreation(Profile* profile,
47 Profile::CreateStatus status) {
48 if (status == Profile::CREATE_STATUS_INITIALIZED)
49 base::MessageLoop::current()->Quit();
52 // Checks that the relaunch name, relaunch command and app icon for the given
53 // |browser| are correct.
54 void ValidateBrowserWindowProperties(
55 const Browser* browser,
56 const base::string16& expected_profile_name) {
57 HWND hwnd = views::HWNDForNativeWindow(browser->window()->GetNativeWindow());
59 base::win::ScopedComPtr<IPropertyStore> pps;
60 HRESULT result = SHGetPropertyStoreForWindow(hwnd, IID_IPropertyStore,
62 EXPECT_TRUE(SUCCEEDED(result));
64 base::win::ScopedPropVariant prop_var;
65 // The relaunch name should be of the form "Chromium" if there is only 1
66 // profile and "First User - Chromium" if there are more. The expected value
67 // is given by |expected_profile_name|.
68 EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchDisplayNameResource,
70 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
72 base::FilePath(profiles::internal::GetShortcutFilenameForProfile(
73 expected_profile_name,
74 BrowserDistribution::GetDistribution())).RemoveExtension().value(),
75 prop_var.get().pwszVal);
78 // The relaunch command should specify the profile.
79 EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchCommand,
81 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
82 CommandLine cmd_line(CommandLine::FromString(prop_var.get().pwszVal));
83 EXPECT_EQ(browser->profile()->GetPath().BaseName().value(),
84 cmd_line.GetSwitchValueNative(switches::kProfileDirectory));
87 // The app icon should be set to the profile icon.
88 EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchIconResource,
90 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
91 EXPECT_EQ(profiles::internal::GetProfileIconPath(
92 browser->profile()->GetPath()).value(),
93 prop_var.get().pwszVal);
95 base::MessageLoop::current()->Quit();
98 void ValidateHostedAppWindowProperties(const Browser* browser,
99 const extensions::Extension* extension) {
100 HWND hwnd = views::HWNDForNativeWindow(browser->window()->GetNativeWindow());
102 base::win::ScopedComPtr<IPropertyStore> pps;
104 SHGetPropertyStoreForWindow(hwnd, IID_IPropertyStore, pps.ReceiveVoid());
105 EXPECT_TRUE(SUCCEEDED(result));
107 base::win::ScopedPropVariant prop_var;
108 // The relaunch name should be the extension name.
110 pps->GetValue(PKEY_AppUserModel_RelaunchDisplayNameResource,
111 prop_var.Receive()));
112 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
113 EXPECT_EQ(base::UTF8ToWide(extension->name()), prop_var.get().pwszVal);
116 // The relaunch command should specify the profile and the app id.
119 pps->GetValue(PKEY_AppUserModel_RelaunchCommand, prop_var.Receive()));
120 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
121 CommandLine cmd_line(CommandLine::FromString(prop_var.get().pwszVal));
122 EXPECT_EQ(browser->profile()->GetPath().BaseName().value(),
123 cmd_line.GetSwitchValueNative(switches::kProfileDirectory));
124 EXPECT_EQ(base::UTF8ToWide(extension->id()),
125 cmd_line.GetSwitchValueNative(switches::kAppId));
128 // The app icon should be set to the extension app icon.
129 base::FilePath web_app_dir = web_app::GetWebAppDataDirectory(
130 browser->profile()->GetPath(), extension->id(), GURL());
132 pps->GetValue(PKEY_AppUserModel_RelaunchIconResource,
133 prop_var.Receive()));
134 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
135 EXPECT_EQ(web_app::internals::GetIconFilePath(
136 web_app_dir, base::UTF8ToUTF16(extension->name())).value(),
137 prop_var.get().pwszVal);
139 base::MessageLoop::current()->Quit();
142 void PostValidationTaskToUIThread(const base::Closure& validation_task) {
143 content::BrowserThread::PostTask(
144 content::BrowserThread::UI, FROM_HERE, validation_task);
147 // Posts a validation task to the FILE thread which bounces back to the UI
148 // thread and then does validation. This is necessary because the icon profile
149 // pref only gets set at the end of icon creation (which happens on the FILE
150 // thread) and is set on the UI thread.
151 void WaitAndValidateBrowserWindowProperties(
152 const base::Closure& validation_task) {
153 content::BrowserThread::PostTask(
154 content::BrowserThread::FILE,
156 base::Bind(&PostValidationTaskToUIThread, validation_task));
157 content::RunMessageLoop();
162 // Tests that require the profile shortcut manager to be instantiated despite
163 // having --user-data-dir specified.
164 class BrowserTestWithProfileShortcutManager : public InProcessBrowserTest {
166 BrowserTestWithProfileShortcutManager() {}
168 virtual void SetUpCommandLine(CommandLine* command_line) override {
169 command_line->AppendSwitch(switches::kEnableProfileShortcutManager);
173 DISALLOW_COPY_AND_ASSIGN(BrowserTestWithProfileShortcutManager);
176 // Check that the window properties on Windows are properly set.
177 // http://crbug.com/396344
178 IN_PROC_BROWSER_TEST_F(BrowserTestWithProfileShortcutManager,
179 DISABLED_WindowProperties) {
180 // Disable this test in Metro+Ash where Windows window properties aren't used.
181 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
184 // This test checks HWND properties that are only available on Win7+.
185 if (base::win::GetVersion() < base::win::VERSION_WIN7)
188 // Single profile case. The profile name should not be shown.
189 WaitAndValidateBrowserWindowProperties(base::Bind(
190 &ValidateBrowserWindowProperties, browser(), base::string16()));
192 // If multiprofile mode is not enabled, we can't test the behavior when there
193 // are multiple profiles.
194 if (!profiles::IsMultipleProfilesEnabled())
197 // Two profile case. Both profile names should be shown.
198 ProfileManager* profile_manager = g_browser_process->profile_manager();
199 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
201 base::FilePath path_profile2 =
202 profile_manager->GenerateNextProfileDirectoryPath();
203 profile_manager->CreateProfileAsync(path_profile2,
204 base::Bind(&UnblockOnProfileCreation),
205 base::string16(), base::string16(),
208 // Spin to allow profile creation to take place, loop is terminated
209 // by UnblockOnProfileCreation when the profile is created.
210 content::RunMessageLoop();
212 // The default profile's name should be part of the relaunch name.
213 WaitAndValidateBrowserWindowProperties(
214 base::Bind(&ValidateBrowserWindowProperties,
216 base::UTF8ToUTF16(browser()->profile()->GetProfileName())));
218 // The second profile's name should be part of the relaunch name.
219 Browser* profile2_browser =
220 CreateBrowser(profile_manager->GetProfileByPath(path_profile2));
221 size_t profile2_index = cache.GetIndexOfProfileWithPath(path_profile2);
222 WaitAndValidateBrowserWindowProperties(
223 base::Bind(&ValidateBrowserWindowProperties,
225 cache.GetNameOfProfileAtIndex(profile2_index)));
228 // http://crbug.com/396344
229 IN_PROC_BROWSER_TEST_F(BrowserWindowPropertyManagerTest, DISABLED_HostedApp) {
231 // Disable this test in Metro+Ash where Windows window properties aren't used.
232 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
236 // This test checks HWND properties that are only available on Win7+.
237 if (base::win::GetVersion() < base::win::VERSION_WIN7)
241 const extensions::Extension* extension =
242 LoadExtension(test_data_dir_.AppendASCII("app/"));
243 EXPECT_TRUE(extension);
245 OpenApplication(AppLaunchParams(browser()->profile(),
247 extensions::LAUNCH_CONTAINER_WINDOW,
248 NEW_FOREGROUND_TAB));
250 // Check that the new browser has an app name.
251 // The launch should have created a new browser.
253 chrome::GetBrowserCount(browser()->profile(),
254 browser()->host_desktop_type()));
256 // Find the new browser.
257 Browser* app_browser = NULL;
258 for (chrome::BrowserIterator it; !it.done() && !app_browser; it.Next()) {
259 if (*it != browser())
262 ASSERT_TRUE(app_browser);
263 ASSERT_TRUE(app_browser != browser());
265 WaitAndValidateBrowserWindowProperties(
266 base::Bind(&ValidateHostedAppWindowProperties, app_browser, extension));