browser_window_property_manager_browsertest_win.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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 <string>
6
7#include <shlobj.h>  // Must be before propkey.
8#include <propkey.h>
9#include <shellapi.h>
10
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/profiles/profile.h"
19#include "chrome/browser/profiles/profile_info_cache.h"
20#include "chrome/browser/profiles/profile_manager.h"
21#include "chrome/browser/profiles/profile_shortcut_manager_win.h"
22#include "chrome/browser/profiles/profiles_state.h"
23#include "chrome/browser/ui/browser.h"
24#include "chrome/browser/ui/browser_window.h"
25#include "chrome/common/chrome_switches.h"
26#include "chrome/installer/util/browser_distribution.h"
27#include "chrome/test/base/in_process_browser_test.h"
28#include "chrome/test/base/test_switches.h"
29#include "content/public/test/test_utils.cc"
30#include "ui/views/win/hwnd_util.h"
31
32namespace {
33
34// An observer that resumes test code after a new profile is initialized by
35// quitting the message loop it's blocked on.
36void UnblockOnProfileCreation(Profile* profile,
37                              Profile::CreateStatus status) {
38  if (status == Profile::CREATE_STATUS_INITIALIZED)
39    base::MessageLoop::current()->Quit();
40}
41
42// Checks that the relaunch name, relaunch command and app icon for the given
43// |browser| are correct.
44void ValidateBrowserWindowProperties(
45    const Browser* browser,
46    const base::string16& expected_profile_name) {
47  HWND hwnd = views::HWNDForNativeWindow(browser->window()->GetNativeWindow());
48
49  base::win::ScopedComPtr<IPropertyStore> pps;
50  HRESULT result = SHGetPropertyStoreForWindow(hwnd, IID_IPropertyStore,
51                                               pps.ReceiveVoid());
52  EXPECT_TRUE(SUCCEEDED(result));
53
54  base::win::ScopedPropVariant prop_var;
55  // The relaunch name should be of the form "Chromium" if there is only 1
56  // profile and "First User - Chromium" if there are more. The expected value
57  // is given by |expected_profile_name|.
58  EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchDisplayNameResource,
59                                prop_var.Receive()));
60  EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
61  EXPECT_EQ(
62      base::FilePath(profiles::internal::GetShortcutFilenameForProfile(
63          expected_profile_name,
64          BrowserDistribution::GetDistribution())).RemoveExtension().value(),
65      prop_var.get().pwszVal);
66  prop_var.Reset();
67
68  // The relaunch command should specify the profile.
69  EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchCommand,
70                                prop_var.Receive()));
71  EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
72  CommandLine cmd_line(CommandLine::FromString(prop_var.get().pwszVal));
73  EXPECT_EQ(browser->profile()->GetPath().BaseName().value(),
74            cmd_line.GetSwitchValueNative(switches::kProfileDirectory));
75  prop_var.Reset();
76
77  // The app icon should be set to the profile icon.
78  EXPECT_EQ(S_OK, pps->GetValue(PKEY_AppUserModel_RelaunchIconResource,
79                                prop_var.Receive()));
80  EXPECT_EQ(VT_LPWSTR, prop_var.get().vt);
81  EXPECT_EQ(profiles::internal::GetProfileIconPath(
82                browser->profile()->GetPath()).value(),
83            prop_var.get().pwszVal);
84  prop_var.Reset();
85}
86
87}  // namespace
88
89// Tests that require the profile shortcut manager to be instantiated despite
90// having --user-data-dir specified.
91class BrowserTestWithProfileShortcutManager : public InProcessBrowserTest {
92 public:
93  BrowserTestWithProfileShortcutManager() {}
94
95  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
96    command_line->AppendSwitch(switches::kEnableProfileShortcutManager);
97  }
98
99 private:
100  DISALLOW_COPY_AND_ASSIGN(BrowserTestWithProfileShortcutManager);
101};
102
103// Check that the window properties on Windows are properly set.
104IN_PROC_BROWSER_TEST_F(BrowserTestWithProfileShortcutManager,
105                       WindowProperties) {
106#if defined(USE_ASH)
107  // Disable this test in Metro+Ash where Windows window properties aren't used.
108  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
109    return;
110#endif
111
112  // This test checks HWND properties that are only available on Win7+.
113  if (base::win::GetVersion() < base::win::VERSION_WIN7)
114    return;
115
116  // Single profile case. The profile name should not be shown.
117  ValidateBrowserWindowProperties(browser(), base::string16());
118
119  // If multiprofile mode is not enabled, we can't test the behavior when there
120  // are multiple profiles.
121  if (!profiles::IsMultipleProfilesEnabled())
122    return;
123
124  // Two profile case. Both profile names should be shown.
125  ProfileManager* profile_manager = g_browser_process->profile_manager();
126  ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
127
128  base::FilePath path_profile2 =
129      profile_manager->GenerateNextProfileDirectoryPath();
130  profile_manager->CreateProfileAsync(path_profile2,
131                                      base::Bind(&UnblockOnProfileCreation),
132                                      base::string16(), base::string16(),
133                                      std::string());
134
135  // Spin to allow profile creation to take place, loop is terminated
136  // by UnblockOnProfileCreation when the profile is created.
137  content::RunMessageLoop();
138
139  // The default profile's name should be part of the relaunch name.
140  ValidateBrowserWindowProperties(
141      browser(), UTF8ToUTF16(browser()->profile()->GetProfileName()));
142
143  // The second profile's name should be part of the relaunch name.
144  Browser* profile2_browser =
145      CreateBrowser(profile_manager->GetProfileByPath(path_profile2));
146  size_t profile2_index = cache.GetIndexOfProfileWithPath(path_profile2);
147  ValidateBrowserWindowProperties(
148      profile2_browser, cache.GetNameOfProfileAtIndex(profile2_index));
149}
150