Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/json/json_reader.h" | |
| 6 #include "base/scoped_temp_dir.h" | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/browser/prefs/pref_service.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/ui/browser.h" | |
| 11 #include "chrome/browser/ui/webui/downloads_dom_handler.h" | |
| 12 #include "chrome/common/pref_names.h" | |
| 13 #include "chrome/test/base/in_process_browser_test.h" | |
| 14 #include "chrome/test/base/ui_test_utils.h" | |
| 15 #include "content/public/browser/download_persistent_store_info.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 bool ListMatches(base::ListValue* left_list, const std::string& right_json) { | |
|
James Hawkins
2012/08/19 06:17:29
Document method and parameters.
benjhayden
2012/08/19 22:20:27
Done.
| |
| 21 scoped_ptr<base::Value> right_value(base::JSONReader::Read(right_json)); | |
| 22 base::ListValue* right_list = NULL; | |
| 23 CHECK(right_value->GetAsList(&right_list)); | |
|
James Hawkins
2012/08/19 06:17:29
s/CHECK/DCHECK/
benjhayden
2012/08/19 22:20:27
Aren't DCHECKs compiled out entirely in non-debug
James Hawkins
2012/08/22 17:39:00
No side-effect inducing calls should be made in ei
| |
| 24 for (size_t i = 0; i < left_list->GetSize(); ++i) { | |
| 25 base::DictionaryValue* left_dict = NULL; | |
| 26 base::DictionaryValue* right_dict = NULL; | |
| 27 CHECK(left_list->GetDictionary(i, &left_dict)); | |
| 28 CHECK(right_list->GetDictionary(i, &right_dict)); | |
| 29 for (base::DictionaryValue::Iterator iter(*right_dict); | |
| 30 iter.HasNext(); iter.Advance()) { | |
| 31 base::Value* left_value = NULL; | |
| 32 if (left_dict->HasKey(iter.key()) && | |
| 33 left_dict->Get(iter.key(), &left_value) && | |
| 34 !iter.value().Equals(left_value)) { | |
| 35 LOG(WARNING) << iter.key(); | |
|
James Hawkins
2012/08/19 06:17:29
Remove console spam?
benjhayden
2012/08/19 22:20:27
It will only print when the test is about to fail,
James Hawkins
2012/08/22 17:39:00
Attempting to guess what's 'valuable' as far as lo
| |
| 36 return false; | |
| 37 } | |
| 38 } | |
| 39 } | |
| 40 return true; | |
| 41 } | |
| 42 | |
| 43 class MockDownloadsDOMHandler : public DownloadsDOMHandler { | |
|
James Hawkins
2012/08/19 06:17:29
Document.
benjhayden
2012/08/19 22:20:27
Done.
| |
| 44 public: | |
| 45 explicit MockDownloadsDOMHandler(content::DownloadManager* dlm) | |
| 46 : DownloadsDOMHandler(dlm) { | |
| 47 } | |
| 48 virtual ~MockDownloadsDOMHandler() {} | |
| 49 | |
| 50 base::ListValue* downloads_list() { return downloads_list_.get(); } | |
| 51 base::ListValue* download_updated() { return download_updated_.get(); } | |
| 52 | |
| 53 protected: | |
| 54 virtual content::WebContents* GetWebUIWebContents() { | |
| 55 return NULL; | |
| 56 } | |
| 57 | |
| 58 virtual void CallDownloadsList(const base::ListValue& downloads) { | |
| 59 downloads_list_.reset(downloads.DeepCopy()); | |
| 60 content::BrowserThread::PostTask( | |
| 61 content::BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | |
| 62 } | |
| 63 | |
| 64 virtual void CallDownloadUpdated(const base::ListValue& download) { | |
| 65 download_updated_.reset(download.DeepCopy()); | |
| 66 } | |
| 67 | |
| 68 private: | |
| 69 scoped_ptr<base::ListValue> downloads_list_; | |
| 70 scoped_ptr<base::ListValue> download_updated_; | |
| 71 DISALLOW_COPY_AND_ASSIGN(MockDownloadsDOMHandler); | |
| 72 }; | |
| 73 | |
| 74 } // anonymous namespace | |
|
James Hawkins
2012/08/19 06:17:29
s/anonymous//
benjhayden
2012/08/19 22:20:27
Done.
| |
| 75 | |
| 76 class DownloadsDOMHandlerTest : public InProcessBrowserTest { | |
| 77 public: | |
| 78 DownloadsDOMHandlerTest() {} | |
| 79 | |
| 80 virtual ~DownloadsDOMHandlerTest() {} | |
| 81 | |
| 82 virtual void SetUpOnMainThread() OVERRIDE { | |
| 83 CHECK(downloads_directory_.CreateUniqueTempDir()); | |
| 84 browser()->profile()->GetPrefs()->SetFilePath( | |
| 85 prefs::kDownloadDefaultDirectory, | |
| 86 downloads_directory_.path()); | |
| 87 CHECK(test_server()->Start()); | |
| 88 } | |
| 89 | |
| 90 content::DownloadManager* download_manager() { | |
| 91 return content::BrowserContext::GetDownloadManager(browser()->profile()); | |
| 92 } | |
| 93 | |
| 94 private: | |
| 95 ScopedTempDir downloads_directory_; | |
| 96 | |
| 97 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandlerTest); | |
| 98 }; | |
| 99 | |
| 100 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, | |
|
James Hawkins
2012/08/19 06:17:29
Document what the test is testing.
benjhayden
2012/08/19 22:20:27
Done.
| |
| 101 DownloadsDOMHandlerTest_Created) { | |
| 102 MockDownloadsDOMHandler mddh(download_manager()); | |
| 103 GURL url = test_server()->GetURL("files/downloads/image.jpg"); | |
| 104 base::Time current(base::Time::Now()); | |
| 105 content::DownloadPersistentStoreInfo population_entries[] = { | |
| 106 content::DownloadPersistentStoreInfo( | |
| 107 FilePath(FILE_PATH_LITERAL("/path/to/file")), | |
| 108 url, | |
| 109 GURL(""), | |
|
James Hawkins
2012/08/19 06:17:29
Optional nit: Consolidate parameters to save rows.
benjhayden
2012/08/19 22:20:27
I think that it's more readable this way.
| |
| 110 current - base::TimeDelta::FromMinutes(5), | |
| 111 current, | |
| 112 128, | |
| 113 128, | |
| 114 content::DownloadItem::COMPLETE, | |
| 115 1, | |
| 116 false), | |
| 117 }; | |
| 118 std::vector<content::DownloadPersistentStoreInfo> entries( | |
| 119 population_entries, population_entries + arraysize(population_entries)); | |
| 120 download_manager()->OnPersistentStoreQueryComplete(&entries); | |
| 121 content::RunMessageLoop(); | |
| 122 EXPECT_EQ(1, static_cast<int>(mddh.downloads_list()->GetSize())); | |
|
James Hawkins
2012/08/19 06:17:29
ASSERT_EQ
benjhayden
2012/08/19 22:20:27
Done.
| |
| 123 EXPECT_EQ(1, static_cast<int>(mddh.download_updated()->GetSize())); | |
| 124 EXPECT_TRUE(ListMatches( | |
| 125 mddh.downloads_list(), | |
| 126 "[{\"file_externally_removed\": false," | |
| 127 " \"file_name\": \"file\"," | |
| 128 " \"id\": 0," | |
| 129 " \"otr\": false," | |
| 130 " \"since_string\": \"Today\"," | |
| 131 " \"state\": \"COMPLETE\"," | |
| 132 " \"total\": 128}]")); | |
| 133 EXPECT_TRUE(ListMatches( | |
| 134 mddh.download_updated(), | |
| 135 "[{\"file_externally_removed\": true," | |
| 136 " \"id\": 0}]")); | |
| 137 | |
| 138 mddh.HandleClearAll(NULL); | |
| 139 content::RunMessageLoop(); | |
| 140 EXPECT_EQ(0, static_cast<int>(mddh.downloads_list()->GetSize())); | |
| 141 } | |
| 142 | |
| 143 // TODO(benjhayden): Test the extension downloads filter for both | |
| 144 // mddh.downloads_list() and mddh.download_updated(). | |
| 145 | |
| 146 // TODO(benjhayden): Test incognito, both downloads_list() and that on-record | |
| 147 // calls can't access off-record items. | |
| 148 | |
| 149 // TODO(benjhayden): Test that bad download ids incoming from the javascript are | |
| 150 // dropped on the floor. | |
| 151 | |
| 152 // TODO(benjhayden): Test that IsTemporary() downloads are not shown. | |
| 153 | |
| 154 // TODO(benjhayden): Test that RemoveObserver is called on all download items, | |
| 155 // including items that crossed IsTemporary() and back. | |
| OLD | NEW |