Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_window_property_manager_browsertest_win.cc
blobef19840ff003ccbc30f1c8ad0d6d2a1b12694ad5
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.
5 #include <string>
7 #include <shlobj.h> // Must be before propkey.
8 #include <propkey.h>
9 #include <shellapi.h>
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/app_launch_params.h"
29 #include "chrome/browser/ui/extensions/application_launch.h"
30 #include "chrome/browser/web_applications/web_app.h"
31 #include "chrome/browser/web_applications/web_app_win.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/extensions/extension_constants.h"
34 #include "chrome/installer/util/browser_distribution.h"
35 #include "chrome/test/base/in_process_browser_test.h"
36 #include "chrome/test/base/test_switches.h"
37 #include "content/public/test/test_utils.h"
38 #include "extensions/common/constants.h"
39 #include "extensions/common/extension.h"
40 #include "ui/views/win/hwnd_util.h"
42 typedef ExtensionBrowserTest BrowserWindowPropertyManagerTest;
44 namespace {
46 // An observer that resumes test code after a new profile is initialized by
47 // quitting the message loop it's blocked on.
48 void UnblockOnProfileCreation(Profile* profile,
49 Profile::CreateStatus status) {
50 if (status == Profile::CREATE_STATUS_INITIALIZED)
51 base::MessageLoop::current()->Quit();
54 // Checks that the relaunch name, relaunch command and app icon for the given
55 // |browser| are correct.
56 void ValidateBrowserWindowProperties(
57 const Browser* browser,
58 const base::string16& expected_profile_name) {
59 HWND hwnd = views::HWNDForNativeWindow(browser->window()->GetNativeWindow());
61 base::win::ScopedComPtr<IPropertyStore> pps;
62 HRESULT result = SHGetPropertyStoreForWindow(hwnd, IID_IPropertyStore,
63 pps.ReceiveVoid());
64 EXPECT_TRUE(SUCCEEDED(result));
66 base::win::ScopedPropVariant prop_var;
67 // The relaunch name should be of the form "Chromium" if there is only 1
68 // profile and "First User - Chromium" if there are more. The expected value
69 // is given by |expected_profile_name|.
70 EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchDisplayNameResource,
71 prop_var.Receive()));
72 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
73 EXPECT_EQ(
74 base::FilePath(profiles::internal::GetShortcutFilenameForProfile(
75 expected_profile_name,
76 BrowserDistribution::GetDistribution())).RemoveExtension().value(),
77 prop_var.get().pwszVal);
78 prop_var.Reset();
80 // The relaunch command should specify the profile.
81 EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchCommand,
82 prop_var.Receive()));
83 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
84 base::CommandLine cmd_line(
85 base::CommandLine::FromString(prop_var.get().pwszVal));
86 EXPECT_EQ(browser->profile()->GetPath().BaseName().value(),
87 cmd_line.GetSwitchValueNative(switches::kProfileDirectory));
88 prop_var.Reset();
90 // The app icon should be set to the profile icon.
91 EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchIconResource,
92 prop_var.Receive()));
93 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
94 EXPECT_EQ(profiles::internal::GetProfileIconPath(
95 browser->profile()->GetPath()).value(),
96 prop_var.get().pwszVal);
97 prop_var.Reset();
98 base::MessageLoop::current()->Quit();
101 void ValidateHostedAppWindowProperties(const Browser* browser,
102 const extensions::Extension* extension) {
103 HWND hwnd = views::HWNDForNativeWindow(browser->window()->GetNativeWindow());
105 base::win::ScopedComPtr<IPropertyStore> pps;
106 HRESULT result =
107 SHGetPropertyStoreForWindow(hwnd, IID_IPropertyStore, pps.ReceiveVoid());
108 EXPECT_TRUE(SUCCEEDED(result));
110 base::win::ScopedPropVariant prop_var;
111 // The relaunch name should be the extension name.
112 EXPECT_EQ(S_OK,
113 pps->GetValue(PKEY_AppUserModel_RelaunchDisplayNameResource,
114 prop_var.Receive()));
115 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
116 EXPECT_EQ(base::UTF8ToWide(extension->name()), prop_var.get().pwszVal);
117 prop_var.Reset();
119 // The relaunch command should specify the profile and the app id.
120 EXPECT_EQ(
121 S_OK,
122 pps->GetValue(PKEY_AppUserModel_RelaunchCommand, prop_var.Receive()));
123 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
124 base::CommandLine cmd_line(
125 base::CommandLine::FromString(prop_var.get().pwszVal));
126 EXPECT_EQ(browser->profile()->GetPath().BaseName().value(),
127 cmd_line.GetSwitchValueNative(switches::kProfileDirectory));
128 EXPECT_EQ(base::UTF8ToWide(extension->id()),
129 cmd_line.GetSwitchValueNative(switches::kAppId));
130 prop_var.Reset();
132 // The app icon should be set to the extension app icon.
133 base::FilePath web_app_dir = web_app::GetWebAppDataDirectory(
134 browser->profile()->GetPath(), extension->id(), GURL());
135 EXPECT_EQ(S_OK,
136 pps->GetValue(PKEY_AppUserModel_RelaunchIconResource,
137 prop_var.Receive()));
138 EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
139 EXPECT_EQ(web_app::internals::GetIconFilePath(
140 web_app_dir, base::UTF8ToUTF16(extension->name())).value(),
141 prop_var.get().pwszVal);
142 prop_var.Reset();
143 base::MessageLoop::current()->Quit();
146 void PostValidationTaskToUIThread(const base::Closure& validation_task) {
147 content::BrowserThread::PostTask(
148 content::BrowserThread::UI, FROM_HERE, validation_task);
151 // Posts a validation task to the FILE thread which bounces back to the UI
152 // thread and then does validation. This is necessary because the icon profile
153 // pref only gets set at the end of icon creation (which happens on the FILE
154 // thread) and is set on the UI thread.
155 void WaitAndValidateBrowserWindowProperties(
156 const base::Closure& validation_task) {
157 content::BrowserThread::PostTask(
158 content::BrowserThread::FILE,
159 FROM_HERE,
160 base::Bind(&PostValidationTaskToUIThread, validation_task));
161 content::RunMessageLoop();
164 } // namespace
166 // Tests that require the profile shortcut manager to be instantiated despite
167 // having --user-data-dir specified.
168 class BrowserTestWithProfileShortcutManager : public InProcessBrowserTest {
169 public:
170 BrowserTestWithProfileShortcutManager() {}
172 void SetUpCommandLine(base::CommandLine* command_line) override {
173 command_line->AppendSwitch(switches::kEnableProfileShortcutManager);
176 private:
177 DISALLOW_COPY_AND_ASSIGN(BrowserTestWithProfileShortcutManager);
180 // Check that the window properties on Windows are properly set.
181 // http://crbug.com/396344
182 IN_PROC_BROWSER_TEST_F(BrowserTestWithProfileShortcutManager,
183 DISABLED_WindowProperties) {
184 // Disable this test in Metro+Ash where Windows window properties aren't used.
185 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
186 switches::kAshBrowserTests))
187 return;
189 // This test checks HWND properties that are only available on Win7+.
190 if (base::win::GetVersion() < base::win::VERSION_WIN7)
191 return;
193 // Single profile case. The profile name should not be shown.
194 WaitAndValidateBrowserWindowProperties(base::Bind(
195 &ValidateBrowserWindowProperties, browser(), base::string16()));
197 // If multiprofile mode is not enabled, we can't test the behavior when there
198 // are multiple profiles.
199 if (!profiles::IsMultipleProfilesEnabled())
200 return;
202 // Two profile case. Both profile names should be shown.
203 ProfileManager* profile_manager = g_browser_process->profile_manager();
204 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
206 base::FilePath path_profile2 =
207 profile_manager->GenerateNextProfileDirectoryPath();
208 profile_manager->CreateProfileAsync(path_profile2,
209 base::Bind(&UnblockOnProfileCreation),
210 base::string16(), base::string16(),
211 std::string());
213 // Spin to allow profile creation to take place, loop is terminated
214 // by UnblockOnProfileCreation when the profile is created.
215 content::RunMessageLoop();
217 // The default profile's name should be part of the relaunch name.
218 WaitAndValidateBrowserWindowProperties(base::Bind(
219 &ValidateBrowserWindowProperties,
220 browser(),
221 base::UTF8ToUTF16(browser()->profile()->GetProfileUserName())));
223 // The second profile's name should be part of the relaunch name.
224 Browser* profile2_browser =
225 CreateBrowser(profile_manager->GetProfileByPath(path_profile2));
226 size_t profile2_index = cache.GetIndexOfProfileWithPath(path_profile2);
227 WaitAndValidateBrowserWindowProperties(
228 base::Bind(&ValidateBrowserWindowProperties,
229 profile2_browser,
230 cache.GetNameOfProfileAtIndex(profile2_index)));
233 // http://crbug.com/396344
234 IN_PROC_BROWSER_TEST_F(BrowserWindowPropertyManagerTest, DISABLED_HostedApp) {
235 #if defined(USE_ASH)
236 // Disable this test in Metro+Ash where Windows window properties aren't used.
237 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
238 switches::kAshBrowserTests))
239 return;
240 #endif
242 // This test checks HWND properties that are only available on Win7+.
243 if (base::win::GetVersion() < base::win::VERSION_WIN7)
244 return;
246 // Load an app.
247 const extensions::Extension* extension =
248 LoadExtension(test_data_dir_.AppendASCII("app/"));
249 EXPECT_TRUE(extension);
251 OpenApplication(AppLaunchParams(
252 browser()->profile(), extension, extensions::LAUNCH_CONTAINER_WINDOW,
253 NEW_FOREGROUND_TAB, extensions::SOURCE_TEST));
255 // Check that the new browser has an app name.
256 // The launch should have created a new browser.
257 ASSERT_EQ(2u,
258 chrome::GetBrowserCount(browser()->profile(),
259 browser()->host_desktop_type()));
261 // Find the new browser.
262 Browser* app_browser = nullptr;
263 for (chrome::BrowserIterator it; !it.done() && !app_browser; it.Next()) {
264 if (*it != browser())
265 app_browser = *it;
267 ASSERT_TRUE(app_browser);
268 ASSERT_TRUE(app_browser != browser());
270 WaitAndValidateBrowserWindowProperties(
271 base::Bind(&ValidateHostedAppWindowProperties, app_browser, extension));