Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 namespace { | |
| 30 | |
| 31 // Code related to history borrowed from: | |
| 32 // chrome/browser/history/history_browsertest.cc | |
| 33 | |
| 34 // Note: WaitableEvent is not used for synchronization between the main thread | |
| 35 // and history backend thread because the history subsystem posts tasks back | |
| 36 // to the main thread. Had we tried to Signal an event in such a task | |
| 37 // and Wait for it on the main thread, the task would not run at all because | |
| 38 // the main thread would be blocked on the Wait call, resulting in a deadlock. | |
| 39 | |
| 40 // A task to be scheduled on the history backend thread. | |
| 41 // Notifies the main thread after all history backend thread tasks have run. | |
| 42 class WaitForHistoryTask : public history::HistoryDBTask { | |
| 43 public: | |
| 44 WaitForHistoryTask() {} | |
| 45 | |
| 46 bool RunOnDBThread(history::HistoryBackend* backend, | |
| 47 history::HistoryDatabase* db) override { | |
| 48 return true; | |
| 49 } | |
| 50 | |
| 51 void DoneRunOnMainThread() override { base::MessageLoop::current()->Quit(); } | |
| 52 | |
| 53 private: | |
| 54 ~WaitForHistoryTask() override {} | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask); | |
| 57 }; | |
| 58 | |
| 59 void WaitForHistoryBackendToRun(Profile* profile) { | |
| 60 base::CancelableTaskTracker task_tracker; | |
| 61 scoped_ptr<history::HistoryDBTask> task(new WaitForHistoryTask()); | |
| 62 HistoryService* history = HistoryServiceFactory::GetForProfile( | |
| 63 profile, ServiceAccessType::EXPLICIT_ACCESS); | |
| 64 history->HistoryService::ScheduleDBTask(task.Pass(), &task_tracker); | |
| 65 content::RunMessageLoop(); | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 class ProfileWindowBrowserTest : public InProcessBrowserTest { | |
| 71 public: | |
| 72 ProfileWindowBrowserTest() {} | |
| 73 ~ProfileWindowBrowserTest() override {} | |
| 74 | |
| 75 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 76 InProcessBrowserTest::SetUpCommandLine(command_line); | |
| 77 switches::EnableNewAvatarMenuForTesting( | |
| 78 base::CommandLine::ForCurrentProcess()); | |
| 79 } | |
| 80 | |
| 81 Browser* OpenGuestBrowser(); | |
| 82 void CloseBrowser(Browser* browser); | |
| 83 | |
| 84 private: | |
| 85 DISALLOW_COPY_AND_ASSIGN(ProfileWindowBrowserTest); | |
| 86 }; | |
| 87 | |
| 88 Browser* ProfileWindowBrowserTest::OpenGuestBrowser() { | |
| 89 size_t num_browsers = | |
| 90 BrowserList::GetInstance(chrome::GetActiveDesktop())->size(); | |
| 91 | |
| 92 // Create a guest browser nicely. Using CreateProfile() and CreateBrowser() | |
| 93 // does incomplete initialization that would lead to | |
| 94 // SystemUrlRequestContextGetter being leaked. | |
| 95 content::WindowedNotificationObserver browser_creation_observer( | |
| 96 chrome::NOTIFICATION_BROWSER_WINDOW_READY, | |
| 97 content::NotificationService::AllSources()); | |
| 98 profiles::SwitchToGuestProfile(chrome::GetActiveDesktop(), | |
| 99 ProfileManager::CreateCallback()); | |
| 100 | |
| 101 // RunUntilIdle() (racily) isn't sufficient to ensure browser creation, so | |
| 102 // listen for the notification. | |
|
noms (inactive)
2015/02/23 17:57:56
I would remove this MessaheLoop wait, maybe, if yo
Mike Lerman
2015/02/23 18:09:42
Done.
| |
| 103 base::MessageLoop::current()->RunUntilIdle(); | |
| 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); | |
|
noms (inactive)
2015/02/23 17:57:56
Should this be an ASSERT_TRUE instead?
Mike Lerman
2015/02/23 18:09:42
You can't ASSERT in a method that returns somethin
| |
| 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 OpenGuestBrowser(); | |
|
noms (inactive)
2015/02/23 17:57:56
maybe add an expect_true that this isn't null? thi
Mike Lerman
2015/02/23 18:09:42
Done.
| |
| 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), | |
|
noms (inactive)
2015/02/23 17:57:56
nit: too much indent
Mike Lerman
2015/02/23 18:09:42
Bye bye Indent.
| |
| 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 at the URL. | |
|
noms (inactive)
2015/02/23 17:57:56
nit: s/at the/for the maybe? (here and below)
Mike Lerman
2015/02/23 18:09:42
Done.
| |
| 166 std::string cookie = content::GetCookies(guest_profile, url); | |
| 167 ASSERT_EQ("", cookie); | |
| 168 | |
| 169 // After navigation there is a cookie at 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 SigninManagerBase* 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 } | |
| OLD | NEW |