Chromium Code Reviews| Index: chrome/browser/download/download_extension_test.cc |
| diff --git a/chrome/browser/download/download_extension_test.cc b/chrome/browser/download/download_extension_test.cc |
| index 1d9f4a47293627fc8b401e55c290adb4bbd5d1dc..2bca426ef0a986e2f0fef915faf69e1867aff88e 100644 |
| --- a/chrome/browser/download/download_extension_test.cc |
| +++ b/chrome/browser/download/download_extension_test.cc |
| @@ -59,21 +59,24 @@ class DownloadExtensionTest : public InProcessBrowserTest { |
| content::DownloadDangerType danger_type; |
| }; |
| + virtual Browser* current_browser() { return browser(); } |
| + |
| // InProcessBrowserTest |
| virtual void SetUpOnMainThread() OVERRIDE { |
| + CHECK(current_browser()); |
| BrowserThread::PostTask( |
| BrowserThread::IO, FROM_HERE, |
| base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| InProcessBrowserTest::SetUpOnMainThread(); |
| - ASSERT_TRUE(CreateAndSetDownloadsDirectory(browser())); |
| - browser()->profile()->GetPrefs()->SetBoolean(prefs::kPromptForDownload, |
| - false); |
| + CreateAndSetDownloadsDirectory(); |
| + current_browser()->profile()->GetPrefs()->SetBoolean( |
| + prefs::kPromptForDownload, false); |
| GetDownloadManager()->RemoveAllDownloads(); |
| } |
| - DownloadManager* GetDownloadManager() { |
| + virtual DownloadManager* GetDownloadManager() { |
| DownloadService* download_service = |
| - DownloadServiceFactory::GetForProfile(browser()->profile()); |
| + DownloadServiceFactory::GetForProfile(current_browser()->profile()); |
| return download_service->GetDownloadManager(); |
| } |
| @@ -124,7 +127,7 @@ class DownloadExtensionTest : public InProcessBrowserTest { |
| CreateInProgressDownloadObserver(1)); |
| GURL slow_download_url(URLRequestSlowDownloadJob::kUnknownSizeUrl); |
| ui_test_utils::NavigateToURLWithDisposition( |
| - browser(), slow_download_url, CURRENT_TAB, |
| + current_browser(), slow_download_url, CURRENT_TAB, |
| ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| observer->WaitForFinished(); |
| EXPECT_EQ( |
| @@ -147,7 +150,7 @@ class DownloadExtensionTest : public InProcessBrowserTest { |
| return NULL; |
| ui_test_utils::NavigateToURLWithDisposition( |
| - browser(), slow_download_url, CURRENT_TAB, |
| + current_browser(), slow_download_url, CURRENT_TAB, |
| ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| observer->WaitForFinished(); |
| @@ -176,7 +179,7 @@ class DownloadExtensionTest : public InProcessBrowserTest { |
| CreateDownloadObserver(1)); |
| GURL finish_url(URLRequestSlowDownloadJob::kFinishDownloadUrl); |
| ui_test_utils::NavigateToURLWithDisposition( |
| - browser(), finish_url, NEW_FOREGROUND_TAB, |
| + current_browser(), finish_url, NEW_FOREGROUND_TAB, |
| ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| observer->WaitForFinished(); |
| EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); |
| @@ -201,19 +204,19 @@ class DownloadExtensionTest : public InProcessBrowserTest { |
| // ownership of |function|. |
| scoped_refptr<ExtensionFunction> function_owner(function); |
| return extension_function_test_utils::RunFunction( |
| - function, args, browser(), extension_function_test_utils::NONE); |
| + function, args, current_browser(), extension_function_test_utils::NONE); |
| } |
| base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, |
| const std::string& args) { |
| return extension_function_test_utils::RunFunctionAndReturnResult( |
| - function, args, browser(), extension_function_test_utils::NONE); |
| + function, args, current_browser(), extension_function_test_utils::NONE); |
| } |
| std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| const std::string& args) { |
| return extension_function_test_utils::RunFunctionAndReturnError( |
| - function, args, browser(), extension_function_test_utils::NONE); |
| + function, args, current_browser(), extension_function_test_utils::NONE); |
| } |
| bool RunFunctionAndReturnString(UIThreadExtensionFunction* function, |
| @@ -256,18 +259,37 @@ class DownloadExtensionTest : public InProcessBrowserTest { |
| } |
| private: |
| - bool CreateAndSetDownloadsDirectory(Browser* browser) { |
| - if (!downloads_directory_.CreateUniqueTempDir()) |
| - return false; |
| - browser->profile()->GetPrefs()->SetFilePath( |
| + void CreateAndSetDownloadsDirectory() { |
| + CHECK(downloads_directory_.CreateUniqueTempDir()); |
|
Randy Smith (Not in Mondays)
2012/05/03 19:07:21
So I don't like doing CHECKs in tests (comment app
benjhayden
2012/05/03 21:08:12
Done.
|
| + current_browser()->profile()->GetPrefs()->SetFilePath( |
| prefs::kDownloadDefaultDirectory, |
| downloads_directory_.path()); |
| - return true; |
| } |
| ScopedTempDir downloads_directory_; |
| }; |
| +class DownloadExtensionTestIncognito : public DownloadExtensionTest { |
| + public: |
| + virtual Browser* current_browser() OVERRIDE { return current_browser_; } |
| + |
| + virtual void SetUpOnMainThread() OVERRIDE { |
| + GoOnTheRecord(); |
| + DownloadExtensionTest::SetUpOnMainThread(); |
| + incognito_browser_ = CreateIncognitoBrowser(); |
| + GoOffTheRecord(); |
| + CHECK(current_browser()->profile()->IsOffTheRecord()); |
| + GetDownloadManager()->RemoveAllDownloads(); |
| + } |
| + |
| + void GoOnTheRecord() { current_browser_ = browser(); } |
| + void GoOffTheRecord() { current_browser_ = incognito_browser_; } |
| + |
| + private: |
| + Browser* incognito_browser_; |
| + Browser* current_browser_; |
| +}; |
| + |
| class MockIconExtractorImpl : public DownloadFileIconExtractor { |
| public: |
| MockIconExtractorImpl(const FilePath& path, IconLoader::IconSize icon_size, |
| @@ -786,3 +808,139 @@ IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, DownloadsApi_SearchPlural) { |
| ASSERT_TRUE(item_value->GetString("filename", &item_name)); |
| ASSERT_EQ(items[2]->GetFullPath().value(), item_name); |
| } |
| + |
| +IN_PROC_BROWSER_TEST_F(DownloadExtensionTestIncognito, |
| + DownloadsApi_SearchIncognito) { |
| + scoped_ptr<base::Value> result_value; |
| + base::ListValue* result_list = NULL; |
| + base::DictionaryValue* result_dict = NULL; |
| + FilePath::StringType filename; |
| + bool is_incognito = false; |
| + std::string error; |
| + |
| + // Set up one on-record item and one off-record item. |
| + |
| + GoOnTheRecord(); |
| + DownloadItem* on_item = CreateSlowTestDownload(); |
| + ASSERT_TRUE(on_item); |
| + ASSERT_FALSE(on_item->IsOtr()); |
| + |
| + GoOffTheRecord(); |
| + DownloadItem* off_item = CreateSlowTestDownload(); |
| + ASSERT_TRUE(off_item); |
| + ASSERT_TRUE(off_item->IsOtr()); |
| + ASSERT_TRUE(on_item->GetFullPath() != off_item->GetFullPath()); |
| + |
| + // Extensions running in the incognito window should have access to the |
| + // incognito items. The 'incognito:only' filter should filter out the |
| + // on-record item. |
| + result_value.reset(RunFunctionAndReturnResult( |
| + new DownloadsSearchFunction(), "[{\"incognito\": \"only\"}]")); |
| + ASSERT_TRUE(result_value.get()); |
| + ASSERT_TRUE(result_value->GetAsList(&result_list)); |
| + ASSERT_EQ(1UL, result_list->GetSize()); |
| + ASSERT_TRUE(result_list->GetDictionary(0, &result_dict)); |
| + ASSERT_TRUE(result_dict->GetString("filename", &filename)); |
| + EXPECT_TRUE(off_item->GetFullPath() == FilePath(filename)); |
| + ASSERT_TRUE(result_dict->GetBoolean("incognito", &is_incognito)); |
| + EXPECT_TRUE(is_incognito); |
| + |
| + // Extensions running in the incognito window should *also* have access to the |
| + // on-record items. The 'incognito:none' filter should filter out the |
| + // off-record items. |
| + result_value.reset(RunFunctionAndReturnResult( |
| + new DownloadsSearchFunction(), "[{\"incognito\": \"none\"}]")); |
| + ASSERT_TRUE(result_value.get()); |
| + ASSERT_TRUE(result_value->GetAsList(&result_list)); |
| + ASSERT_EQ(1UL, result_list->GetSize()); |
| + ASSERT_TRUE(result_list->GetDictionary(0, &result_dict)); |
| + ASSERT_TRUE(result_dict->GetString("filename", &filename)); |
| + EXPECT_TRUE(on_item->GetFullPath() == FilePath(filename)); |
| + ASSERT_TRUE(result_dict->GetBoolean("incognito", &is_incognito)); |
| + EXPECT_FALSE(is_incognito); |
| + |
| + // Extensions running in the on-record window should *not* have access to the |
| + // incognito items. Filtering out on-record items should leave zero items. |
| + GoOnTheRecord(); |
| + result_value.reset(RunFunctionAndReturnResult( |
| + new DownloadsSearchFunction(), "[{\"incognito\": \"only\"}]")); |
| + ASSERT_TRUE(result_value.get()); |
| + ASSERT_TRUE(result_value->GetAsList(&result_list)); |
| + EXPECT_EQ(0UL, result_list->GetSize()); |
| + |
| + // Extensions running in the on-record window should still have access to the |
| + // on-record items. The 'incognito:none' filter should have no effect in |
| + // on-record windows. |
| + result_value.reset(RunFunctionAndReturnResult( |
| + new DownloadsSearchFunction(), "[{\"incognito\": \"none\"}]")); |
| + ASSERT_TRUE(result_value.get()); |
| + ASSERT_TRUE(result_value->GetAsList(&result_list)); |
| + ASSERT_EQ(1UL, result_list->GetSize()); |
| + ASSERT_TRUE(result_list->GetDictionary(0, &result_dict)); |
| + ASSERT_TRUE(result_dict->GetString("filename", &filename)); |
| + EXPECT_TRUE(on_item->GetFullPath() == FilePath(filename)); |
| + ASSERT_TRUE(result_dict->GetBoolean("incognito", &is_incognito)); |
| + EXPECT_FALSE(is_incognito); |
| + |
| + GoOffTheRecord(); |
| + // Do the pause/resume/cancel test for both the on- and off-items while in the |
| + // off-record browser. |
| + EXPECT_TRUE(RunFunction(new DownloadsPauseFunction(), |
| + DownloadItemIdAsArgList(on_item))); |
| + EXPECT_TRUE(on_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsPauseFunction(), |
| + DownloadItemIdAsArgList(on_item))); |
| + EXPECT_TRUE(on_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsResumeFunction(), |
| + DownloadItemIdAsArgList(on_item))); |
| + EXPECT_FALSE(on_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsResumeFunction(), |
| + DownloadItemIdAsArgList(on_item))); |
| + EXPECT_FALSE(on_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsPauseFunction(), |
| + DownloadItemIdAsArgList(on_item))); |
| + EXPECT_TRUE(on_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsCancelFunction(), |
| + DownloadItemIdAsArgList(on_item))); |
| + EXPECT_TRUE(on_item->IsCancelled()); |
| + EXPECT_TRUE(RunFunction(new DownloadsCancelFunction(), |
| + DownloadItemIdAsArgList(on_item))); |
| + EXPECT_TRUE(on_item->IsCancelled()); |
| + error = RunFunctionAndReturnError( |
| + new DownloadsPauseFunction(), DownloadItemIdAsArgList(on_item)); |
| + EXPECT_STREQ(download_extension_errors::kInvalidOperationError, |
| + error.c_str()); |
| + error = RunFunctionAndReturnError( |
| + new DownloadsResumeFunction(), DownloadItemIdAsArgList(on_item)); |
| + EXPECT_STREQ(download_extension_errors::kInvalidOperationError, |
| + error.c_str()); |
| + EXPECT_TRUE(RunFunction(new DownloadsPauseFunction(), |
| + DownloadItemIdAsArgList(off_item))); |
| + EXPECT_TRUE(off_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsPauseFunction(), |
| + DownloadItemIdAsArgList(off_item))); |
| + EXPECT_TRUE(off_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsResumeFunction(), |
| + DownloadItemIdAsArgList(off_item))); |
| + EXPECT_FALSE(off_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsResumeFunction(), |
| + DownloadItemIdAsArgList(off_item))); |
| + EXPECT_FALSE(off_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsPauseFunction(), |
| + DownloadItemIdAsArgList(off_item))); |
| + EXPECT_TRUE(off_item->IsPaused()); |
| + EXPECT_TRUE(RunFunction(new DownloadsCancelFunction(), |
| + DownloadItemIdAsArgList(off_item))); |
| + EXPECT_TRUE(off_item->IsCancelled()); |
| + EXPECT_TRUE(RunFunction(new DownloadsCancelFunction(), |
| + DownloadItemIdAsArgList(off_item))); |
| + EXPECT_TRUE(off_item->IsCancelled()); |
| + error = RunFunctionAndReturnError( |
| + new DownloadsPauseFunction(), DownloadItemIdAsArgList(off_item)); |
| + EXPECT_STREQ(download_extension_errors::kInvalidOperationError, |
| + error.c_str()); |
| + error = RunFunctionAndReturnError( |
| + new DownloadsResumeFunction(), DownloadItemIdAsArgList(off_item)); |
| + EXPECT_STREQ(download_extension_errors::kInvalidOperationError, |
| + error.c_str()); |
| +} |