| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <utility> | 5 #include <utility> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
| 16 #include "chrome/browser/history/history_service_factory.h" | 16 #include "chrome/browser/history/history_service_factory.h" |
| 17 #include "chrome/browser/history/history_test_utils.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_commands.h" | 20 #include "chrome/browser/ui/browser_commands.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/browser/ui/webui/md_history_ui.h" | 22 #include "chrome/browser/ui/webui/md_history_ui.h" |
| 22 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 25 #include "chrome/test/base/in_process_browser_test.h" | 26 #include "chrome/test/base/in_process_browser_test.h" |
| 26 #include "chrome/test/base/ui_test_utils.h" | 27 #include "chrome/test/base/ui_test_utils.h" |
| 27 #include "components/history/core/browser/history_db_task.h" | 28 #include "components/history/core/browser/history_db_task.h" |
| 28 #include "components/history/core/browser/history_service.h" | 29 #include "components/history/core/browser/history_service.h" |
| 29 #include "components/prefs/pref_service.h" | 30 #include "components/prefs/pref_service.h" |
| 30 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/test/browser_test_utils.h" | 32 #include "content/public/test/browser_test_utils.h" |
| 32 #include "content/public/test/test_browser_thread.h" | 33 #include "content/public/test/test_browser_thread.h" |
| 33 #include "net/test/embedded_test_server/embedded_test_server.h" | 34 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 34 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 35 | 36 |
| 36 using content::BrowserThread; | 37 using content::BrowserThread; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 const base::FilePath::CharType kDocRoot[] = | 41 const base::FilePath::CharType kDocRoot[] = |
| 41 FILE_PATH_LITERAL("chrome/test/data"); | 42 FILE_PATH_LITERAL("chrome/test/data"); |
| 42 | 43 |
| 43 // Note: WaitableEvent is not used for synchronization between the main thread | |
| 44 // and history backend thread because the history subsystem posts tasks back | |
| 45 // to the main thread. Had we tried to Signal an event in such a task | |
| 46 // and Wait for it on the main thread, the task would not run at all because | |
| 47 // the main thread would be blocked on the Wait call, resulting in a deadlock. | |
| 48 | |
| 49 // A task to be scheduled on the history backend thread. | |
| 50 // Notifies the main thread after all history backend thread tasks have run. | |
| 51 class WaitForHistoryTask : public history::HistoryDBTask { | |
| 52 public: | |
| 53 WaitForHistoryTask() {} | |
| 54 | |
| 55 bool RunOnDBThread(history::HistoryBackend* backend, | |
| 56 history::HistoryDatabase* db) override { | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 void DoneRunOnMainThread() override { | |
| 61 base::MessageLoop::current()->QuitWhenIdle(); | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 ~WaitForHistoryTask() override {} | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(WaitForHistoryTask); | |
| 68 }; | |
| 69 | |
| 70 } // namespace | 44 } // namespace |
| 71 | 45 |
| 72 class HistoryBrowserTest : public InProcessBrowserTest { | 46 class HistoryBrowserTest : public InProcessBrowserTest { |
| 73 protected: | 47 protected: |
| 74 HistoryBrowserTest() : test_server_() { | 48 HistoryBrowserTest() : test_server_() { |
| 75 test_server_.ServeFilesFromSourceDirectory(base::FilePath(kDocRoot)); | 49 test_server_.ServeFilesFromSourceDirectory(base::FilePath(kDocRoot)); |
| 76 } | 50 } |
| 77 | 51 |
| 78 void SetUp() override { | 52 void SetUp() override { |
| 79 ASSERT_TRUE(test_server_.Start()); | 53 ASSERT_TRUE(test_server_.Start()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 92 ui_test_utils::HistoryEnumerator enumerator(GetProfile()); | 66 ui_test_utils::HistoryEnumerator enumerator(GetProfile()); |
| 93 return enumerator.urls(); | 67 return enumerator.urls(); |
| 94 } | 68 } |
| 95 | 69 |
| 96 GURL GetTestUrl() { | 70 GURL GetTestUrl() { |
| 97 return ui_test_utils::GetTestUrl( | 71 return ui_test_utils::GetTestUrl( |
| 98 base::FilePath(base::FilePath::kCurrentDirectory), | 72 base::FilePath(base::FilePath::kCurrentDirectory), |
| 99 base::FilePath(FILE_PATH_LITERAL("title2.html"))); | 73 base::FilePath(FILE_PATH_LITERAL("title2.html"))); |
| 100 } | 74 } |
| 101 | 75 |
| 102 void WaitForHistoryBackendToRun() { | |
| 103 base::CancelableTaskTracker task_tracker; | |
| 104 std::unique_ptr<history::HistoryDBTask> task(new WaitForHistoryTask()); | |
| 105 history::HistoryService* history = HistoryServiceFactory::GetForProfile( | |
| 106 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS); | |
| 107 history->ScheduleDBTask(std::move(task), &task_tracker); | |
| 108 content::RunMessageLoop(); | |
| 109 } | |
| 110 | |
| 111 void ExpectEmptyHistory() { | 76 void ExpectEmptyHistory() { |
| 112 std::vector<GURL> urls(GetHistoryContents()); | 77 std::vector<GURL> urls(GetHistoryContents()); |
| 113 EXPECT_EQ(0U, urls.size()); | 78 EXPECT_EQ(0U, urls.size()); |
| 114 } | 79 } |
| 115 | 80 |
| 116 void LoadAndWaitForURL(const GURL& url) { | 81 void LoadAndWaitForURL(const GURL& url) { |
| 117 base::string16 expected_title(base::ASCIIToUTF16("OK")); | 82 base::string16 expected_title(base::ASCIIToUTF16("OK")); |
| 118 content::TitleWatcher title_watcher( | 83 content::TitleWatcher title_watcher( |
| 119 browser()->tab_strip_model()->GetActiveWebContents(), expected_title); | 84 browser()->tab_strip_model()->GetActiveWebContents(), expected_title); |
| 120 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | 85 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 137 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( | 102 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( |
| 138 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS)); | 103 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS)); |
| 139 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( | 104 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( |
| 140 GetProfile(), ServiceAccessType::IMPLICIT_ACCESS)); | 105 GetProfile(), ServiceAccessType::IMPLICIT_ACCESS)); |
| 141 | 106 |
| 142 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( | 107 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( |
| 143 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS)); | 108 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS)); |
| 144 ExpectEmptyHistory(); | 109 ExpectEmptyHistory(); |
| 145 | 110 |
| 146 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); | 111 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); |
| 147 WaitForHistoryBackendToRun(); | 112 WaitForHistoryBackendToRun(GetProfile()); |
| 148 | 113 |
| 149 { | 114 { |
| 150 std::vector<GURL> urls(GetHistoryContents()); | 115 std::vector<GURL> urls(GetHistoryContents()); |
| 151 ASSERT_EQ(1U, urls.size()); | 116 ASSERT_EQ(1U, urls.size()); |
| 152 EXPECT_EQ(GetTestUrl().spec(), urls[0].spec()); | 117 EXPECT_EQ(GetTestUrl().spec(), urls[0].spec()); |
| 153 } | 118 } |
| 154 } | 119 } |
| 155 | 120 |
| 156 // Test that disabling saving browser history really works. | 121 // Test that disabling saving browser history really works. |
| 157 IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryDisabled) { | 122 IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryDisabled) { |
| 158 GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true); | 123 GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true); |
| 159 | 124 |
| 160 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( | 125 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( |
| 161 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS)); | 126 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS)); |
| 162 EXPECT_FALSE(HistoryServiceFactory::GetForProfile( | 127 EXPECT_FALSE(HistoryServiceFactory::GetForProfile( |
| 163 GetProfile(), ServiceAccessType::IMPLICIT_ACCESS)); | 128 GetProfile(), ServiceAccessType::IMPLICIT_ACCESS)); |
| 164 | 129 |
| 165 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( | 130 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( |
| 166 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS)); | 131 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS)); |
| 167 ExpectEmptyHistory(); | 132 ExpectEmptyHistory(); |
| 168 | 133 |
| 169 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); | 134 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); |
| 170 WaitForHistoryBackendToRun(); | 135 WaitForHistoryBackendToRun(GetProfile()); |
| 171 ExpectEmptyHistory(); | 136 ExpectEmptyHistory(); |
| 172 } | 137 } |
| 173 | 138 |
| 174 // Test that changing the pref takes effect immediately | 139 // Test that changing the pref takes effect immediately |
| 175 // when the browser is running. | 140 // when the browser is running. |
| 176 IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryEnabledThenDisabled) { | 141 IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryEnabledThenDisabled) { |
| 177 EXPECT_FALSE(GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled)); | 142 EXPECT_FALSE(GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled)); |
| 178 | 143 |
| 179 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( | 144 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( |
| 180 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS)); | 145 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS)); |
| 181 | 146 |
| 182 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); | 147 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); |
| 183 WaitForHistoryBackendToRun(); | 148 WaitForHistoryBackendToRun(GetProfile()); |
| 184 | 149 |
| 185 { | 150 { |
| 186 std::vector<GURL> urls(GetHistoryContents()); | 151 std::vector<GURL> urls(GetHistoryContents()); |
| 187 ASSERT_EQ(1U, urls.size()); | 152 ASSERT_EQ(1U, urls.size()); |
| 188 EXPECT_EQ(GetTestUrl().spec(), urls[0].spec()); | 153 EXPECT_EQ(GetTestUrl().spec(), urls[0].spec()); |
| 189 } | 154 } |
| 190 | 155 |
| 191 GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true); | 156 GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true); |
| 192 | 157 |
| 193 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); | 158 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); |
| 194 WaitForHistoryBackendToRun(); | 159 WaitForHistoryBackendToRun(GetProfile()); |
| 195 | 160 |
| 196 { | 161 { |
| 197 // No additional entries should be present in the history. | 162 // No additional entries should be present in the history. |
| 198 std::vector<GURL> urls(GetHistoryContents()); | 163 std::vector<GURL> urls(GetHistoryContents()); |
| 199 ASSERT_EQ(1U, urls.size()); | 164 ASSERT_EQ(1U, urls.size()); |
| 200 EXPECT_EQ(GetTestUrl().spec(), urls[0].spec()); | 165 EXPECT_EQ(GetTestUrl().spec(), urls[0].spec()); |
| 201 } | 166 } |
| 202 } | 167 } |
| 203 | 168 |
| 204 // Test that changing the pref takes effect immediately | 169 // Test that changing the pref takes effect immediately |
| 205 // when the browser is running. | 170 // when the browser is running. |
| 206 IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryDisabledThenEnabled) { | 171 IN_PROC_BROWSER_TEST_F(HistoryBrowserTest, SavingHistoryDisabledThenEnabled) { |
| 207 GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true); | 172 GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, true); |
| 208 | 173 |
| 209 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( | 174 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile( |
| 210 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS)); | 175 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS)); |
| 211 ExpectEmptyHistory(); | 176 ExpectEmptyHistory(); |
| 212 | 177 |
| 213 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); | 178 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); |
| 214 WaitForHistoryBackendToRun(); | 179 WaitForHistoryBackendToRun(GetProfile()); |
| 215 ExpectEmptyHistory(); | 180 ExpectEmptyHistory(); |
| 216 | 181 |
| 217 GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, false); | 182 GetPrefs()->SetBoolean(prefs::kSavingBrowserHistoryDisabled, false); |
| 218 | 183 |
| 219 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); | 184 ui_test_utils::NavigateToURL(browser(), GetTestUrl()); |
| 220 WaitForHistoryBackendToRun(); | 185 WaitForHistoryBackendToRun(GetProfile()); |
| 221 | 186 |
| 222 { | 187 { |
| 223 std::vector<GURL> urls(GetHistoryContents()); | 188 std::vector<GURL> urls(GetHistoryContents()); |
| 224 ASSERT_EQ(1U, urls.size()); | 189 ASSERT_EQ(1U, urls.size()); |
| 225 EXPECT_EQ(GetTestUrl().spec(), urls[0].spec()); | 190 EXPECT_EQ(GetTestUrl().spec(), urls[0].spec()); |
| 226 } | 191 } |
| 227 } | 192 } |
| 228 | 193 |
| 229 // Disabled after fixing this test class. See http://crbug.com/511442 for | 194 // Disabled after fixing this test class. See http://crbug.com/511442 for |
| 230 // details. | 195 // details. |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 | 506 |
| 542 content::WebContents* active_web_contents = | 507 content::WebContents* active_web_contents = |
| 543 browser()->tab_strip_model()->GetActiveWebContents(); | 508 browser()->tab_strip_model()->GetActiveWebContents(); |
| 544 ASSERT_EQ(web_contents, active_web_contents); | 509 ASSERT_EQ(web_contents, active_web_contents); |
| 545 ASSERT_EQ(history_url, active_web_contents->GetURL()); | 510 ASSERT_EQ(history_url, active_web_contents->GetURL()); |
| 546 | 511 |
| 547 content::WebContents* second_tab = | 512 content::WebContents* second_tab = |
| 548 browser()->tab_strip_model()->GetWebContentsAt(1); | 513 browser()->tab_strip_model()->GetWebContentsAt(1); |
| 549 ASSERT_NE(history_url, second_tab->GetURL()); | 514 ASSERT_NE(history_url, second_tab->GetURL()); |
| 550 } | 515 } |
| OLD | NEW |