Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: chrome/browser/profiles/profile_window_browsertest.cc

Issue 871893003: Browser test for launching guest mode from the User Manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test desktop only Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/ui/browser_command_controller_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015 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 "chrome/browser/profiles/profile_window.h"
6
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/history/history_service.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "components/history/core/browser/history_db_task.h"
22 #include "components/search_engines/template_url_service.h"
23 #include "components/signin/core/common/profile_management_switches.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/test/browser_test_utils.h"
26 #include "content/public/test/test_utils.h"
27 #include "net/test/spawned_test_server/spawned_test_server.h"
28
29 // This test verifies the Desktop implementation of Guest only.
30 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
31
32 namespace {
33
34 // Code related to history borrowed from:
35 // chrome/browser/history/history_browsertest.cc
36
37 // Note: WaitableEvent is not used for synchronization between the main thread
38 // and history backend thread because the history subsystem posts tasks back
39 // to the main thread. Had we tried to Signal an event in such a task
40 // and Wait for it on the main thread, the task would not run at all because
41 // the main thread would be blocked on the Wait call, resulting in a deadlock.
42
43 // A task to be scheduled on the history backend thread.
44 // Notifies the main thread after all history backend thread tasks have run.
45 class WaitForHistoryTask : public history::HistoryDBTask {
46 public:
47 WaitForHistoryTask() {}
48
49 bool RunOnDBThread(history::HistoryBackend* backend,
50 history::HistoryDatabase* db) override {
51 return true;
52 }
53
54 void DoneRunOnMainThread() override { base::MessageLoop::current()->Quit(); }
55
56 private:
57 ~WaitForHistoryTask() override {}
58
59 DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask);
60 };
61
62 void WaitForHistoryBackendToRun(Profile* profile) {
63 base::CancelableTaskTracker task_tracker;
64 scoped_ptr<history::HistoryDBTask> task(new WaitForHistoryTask());
65 HistoryService* history = HistoryServiceFactory::GetForProfile(
66 profile, ServiceAccessType::EXPLICIT_ACCESS);
67 history->HistoryService::ScheduleDBTask(task.Pass(), &task_tracker);
68 content::RunMessageLoop();
69 }
70
71 } // namespace
72
73 class ProfileWindowBrowserTest : public InProcessBrowserTest {
74 public:
75 ProfileWindowBrowserTest() {}
76 ~ProfileWindowBrowserTest() override {}
77
78 void SetUpCommandLine(base::CommandLine* command_line) override {
79 InProcessBrowserTest::SetUpCommandLine(command_line);
80 switches::EnableNewAvatarMenuForTesting(
81 base::CommandLine::ForCurrentProcess());
82 }
83
84 Browser* OpenGuestBrowser();
85 void CloseBrowser(Browser* browser);
86
87 private:
88 DISALLOW_COPY_AND_ASSIGN(ProfileWindowBrowserTest);
89 };
90
91 Browser* ProfileWindowBrowserTest::OpenGuestBrowser() {
92 size_t num_browsers =
93 BrowserList::GetInstance(chrome::GetActiveDesktop())->size();
94
95 // Create a guest browser nicely. Using CreateProfile() and CreateBrowser()
96 // does incomplete initialization that would lead to
97 // SystemUrlRequestContextGetter being leaked.
98 content::WindowedNotificationObserver browser_creation_observer(
99 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
100 content::NotificationService::AllSources());
101 profiles::SwitchToGuestProfile(chrome::GetActiveDesktop(),
102 ProfileManager::CreateCallback());
103
104 browser_creation_observer.Wait();
105 EXPECT_EQ(num_browsers + 1,
106 BrowserList::GetInstance(chrome::GetActiveDesktop())->size());
107
108 Profile* guest = g_browser_process->profile_manager()->GetProfileByPath(
109 ProfileManager::GetGuestProfilePath());
110 Browser* browser = chrome::FindAnyBrowser(
111 guest, true, chrome::GetActiveDesktop());
112 EXPECT_TRUE(browser);
113
114 // When |browser| closes a BrowsingDataRemover will be created and executed.
115 // It needs a loaded TemplateUrlService or else it hangs on to a
116 // CallbackList::Subscription forever.
117 ui_test_utils::WaitForTemplateURLServiceToLoad(
118 TemplateURLServiceFactory::GetForProfile(guest));
119
120 return browser;
121 }
122
123 void ProfileWindowBrowserTest::CloseBrowser(Browser* browser) {
124 content::WindowedNotificationObserver window_close_observer(
125 chrome::NOTIFICATION_BROWSER_CLOSED,
126 content::Source<Browser>(browser));
127 browser->window()->Close();
128 window_close_observer.Wait();
129 }
130
131 IN_PROC_BROWSER_TEST_F(ProfileWindowBrowserTest, OpenGuestBrowser) {
132 EXPECT_TRUE(OpenGuestBrowser());
133 }
134
135 IN_PROC_BROWSER_TEST_F(ProfileWindowBrowserTest, GuestIsIncognito) {
136 Browser* guest_browser = OpenGuestBrowser();
137 EXPECT_TRUE(guest_browser->profile()->IsOffTheRecord());
138 }
139
140 IN_PROC_BROWSER_TEST_F(ProfileWindowBrowserTest, GuestIgnoresHistory) {
141 Browser* guest_browser = OpenGuestBrowser();
142
143 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
144 guest_browser->profile(), ServiceAccessType::EXPLICIT_ACCESS));
145
146 GURL test_url = ui_test_utils::GetTestUrl(
147 base::FilePath(base::FilePath::kCurrentDirectory),
148 base::FilePath(FILE_PATH_LITERAL("title2.html")));
149
150 ui_test_utils::NavigateToURL(guest_browser, test_url);
151 WaitForHistoryBackendToRun(guest_browser->profile());
152
153 std::vector<GURL> urls =
154 ui_test_utils::HistoryEnumerator(guest_browser->profile()).urls();
155 ASSERT_EQ(0U, urls.size());
156 }
157
158 IN_PROC_BROWSER_TEST_F(ProfileWindowBrowserTest, GuestClearsCookies) {
159 Browser* guest_browser = OpenGuestBrowser();
160 Profile* guest_profile = guest_browser->profile();
161
162 ASSERT_TRUE(test_server()->Start());
163 GURL url(test_server()->GetURL("set-cookie?cookie1"));
164
165 // Before navigation there are no cookies for the URL.
166 std::string cookie = content::GetCookies(guest_profile, url);
167 ASSERT_EQ("", cookie);
168
169 // After navigation there is a cookie for the URL.
170 ui_test_utils::NavigateToURL(guest_browser, url);
171 cookie = content::GetCookies(guest_profile, url);
172 EXPECT_EQ("cookie1", cookie);
173
174 CloseBrowser(guest_browser);
175
176 // Closing the browser has removed the cookie.
177 cookie = content::GetCookies(guest_profile, url);
178 ASSERT_EQ("", cookie);
179 }
180
181 IN_PROC_BROWSER_TEST_F(ProfileWindowBrowserTest, GuestCannotSignin) {
182 Browser* guest_browser = OpenGuestBrowser();
183
184 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(
185 guest_browser->profile());
186
187 // Guest profiles can't sign in without a SigninManager.
188 ASSERT_FALSE(signin_manager);
189 }
190
191 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS)
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/browser_command_controller_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698