| 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 // Reads |right_json| into a ListValue |left_list|; returns true if all |
| 21 // key-value pairs in in all dictionaries in |right_list| are also in the |
| 22 // corresponding dictionary in |left_list|. Ignores keys in dictionaries in |
| 23 // |left_list| that are not in the corresponding dictionary in |right_list|. |
| 24 bool ListMatches(base::ListValue* left_list, const std::string& right_json) { |
| 25 scoped_ptr<base::Value> right_value(base::JSONReader::Read(right_json)); |
| 26 base::ListValue* right_list = NULL; |
| 27 CHECK(right_value->GetAsList(&right_list)); |
| 28 for (size_t i = 0; i < left_list->GetSize(); ++i) { |
| 29 base::DictionaryValue* left_dict = NULL; |
| 30 base::DictionaryValue* right_dict = NULL; |
| 31 CHECK(left_list->GetDictionary(i, &left_dict)); |
| 32 CHECK(right_list->GetDictionary(i, &right_dict)); |
| 33 for (base::DictionaryValue::Iterator iter(*right_dict); |
| 34 iter.HasNext(); iter.Advance()) { |
| 35 base::Value* left_value = NULL; |
| 36 if (left_dict->HasKey(iter.key()) && |
| 37 left_dict->Get(iter.key(), &left_value) && |
| 38 !iter.value().Equals(left_value)) { |
| 39 LOG(WARNING) << iter.key(); |
| 40 return false; |
| 41 } |
| 42 } |
| 43 } |
| 44 return true; |
| 45 } |
| 46 |
| 47 // A |DownloadsDOMHandler| that doesn't use a real WebUI object, but is real in |
| 48 // all other respects. |
| 49 class MockDownloadsDOMHandler : public DownloadsDOMHandler { |
| 50 public: |
| 51 explicit MockDownloadsDOMHandler(content::DownloadManager* dlm) |
| 52 : DownloadsDOMHandler(dlm) { |
| 53 } |
| 54 virtual ~MockDownloadsDOMHandler() {} |
| 55 |
| 56 base::ListValue* downloads_list() { return downloads_list_.get(); } |
| 57 base::ListValue* download_updated() { return download_updated_.get(); } |
| 58 |
| 59 protected: |
| 60 virtual content::WebContents* GetWebUIWebContents() { |
| 61 return NULL; |
| 62 } |
| 63 |
| 64 virtual void CallDownloadsList(const base::ListValue& downloads) { |
| 65 downloads_list_.reset(downloads.DeepCopy()); |
| 66 content::BrowserThread::PostTask( |
| 67 content::BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 68 } |
| 69 |
| 70 virtual void CallDownloadUpdated(const base::ListValue& download) { |
| 71 download_updated_.reset(download.DeepCopy()); |
| 72 } |
| 73 |
| 74 private: |
| 75 scoped_ptr<base::ListValue> downloads_list_; |
| 76 scoped_ptr<base::ListValue> download_updated_; |
| 77 DISALLOW_COPY_AND_ASSIGN(MockDownloadsDOMHandler); |
| 78 }; |
| 79 |
| 80 } // namespace |
| 81 |
| 82 class DownloadsDOMHandlerTest : public InProcessBrowserTest { |
| 83 public: |
| 84 DownloadsDOMHandlerTest() {} |
| 85 |
| 86 virtual ~DownloadsDOMHandlerTest() {} |
| 87 |
| 88 virtual void SetUpOnMainThread() OVERRIDE { |
| 89 CHECK(downloads_directory_.CreateUniqueTempDir()); |
| 90 browser()->profile()->GetPrefs()->SetFilePath( |
| 91 prefs::kDownloadDefaultDirectory, |
| 92 downloads_directory_.path()); |
| 93 CHECK(test_server()->Start()); |
| 94 } |
| 95 |
| 96 content::DownloadManager* download_manager() { |
| 97 return content::BrowserContext::GetDownloadManager(browser()->profile()); |
| 98 } |
| 99 |
| 100 private: |
| 101 ScopedTempDir downloads_directory_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandlerTest); |
| 104 }; |
| 105 |
| 106 // Tests that DownloadsDOMHandler detects new downloads and relays them to the |
| 107 // renderer. |
| 108 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, |
| 109 DownloadsDOMHandlerTest_Created) { |
| 110 MockDownloadsDOMHandler mddh(download_manager()); |
| 111 |
| 112 GURL url = test_server()->GetURL("files/downloads/image.jpg"); |
| 113 base::Time current(base::Time::Now()); |
| 114 content::DownloadPersistentStoreInfo population_entries[] = { |
| 115 content::DownloadPersistentStoreInfo( |
| 116 FilePath(FILE_PATH_LITERAL("/path/to/file")), |
| 117 url, |
| 118 GURL(""), |
| 119 current - base::TimeDelta::FromMinutes(5), |
| 120 current, |
| 121 128, |
| 122 128, |
| 123 content::DownloadItem::COMPLETE, |
| 124 1, |
| 125 false), |
| 126 }; |
| 127 std::vector<content::DownloadPersistentStoreInfo> entries( |
| 128 population_entries, population_entries + arraysize(population_entries)); |
| 129 download_manager()->OnPersistentStoreQueryComplete(&entries); |
| 130 content::RunMessageLoop(); |
| 131 |
| 132 ASSERT_EQ(1, static_cast<int>(mddh.downloads_list()->GetSize())); |
| 133 ASSERT_EQ(1, static_cast<int>(mddh.download_updated()->GetSize())); |
| 134 EXPECT_TRUE(ListMatches( |
| 135 mddh.downloads_list(), |
| 136 "[{\"file_externally_removed\": false," |
| 137 " \"file_name\": \"file\"," |
| 138 " \"id\": 0," |
| 139 " \"otr\": false," |
| 140 " \"since_string\": \"Today\"," |
| 141 " \"state\": \"COMPLETE\"," |
| 142 " \"total\": 128}]")); |
| 143 EXPECT_TRUE(ListMatches( |
| 144 mddh.download_updated(), |
| 145 "[{\"file_externally_removed\": true," |
| 146 " \"id\": 0}]")); |
| 147 |
| 148 mddh.HandleClearAll(NULL); |
| 149 content::RunMessageLoop(); |
| 150 EXPECT_EQ(0, static_cast<int>(mddh.downloads_list()->GetSize())); |
| 151 } |
| 152 |
| 153 // TODO(benjhayden): Test the extension downloads filter for both |
| 154 // mddh.downloads_list() and mddh.download_updated(). |
| 155 |
| 156 // TODO(benjhayden): Test incognito, both downloads_list() and that on-record |
| 157 // calls can't access off-record items. |
| 158 |
| 159 // TODO(benjhayden): Test that bad download ids incoming from the javascript are |
| 160 // dropped on the floor. |
| 161 |
| 162 // TODO(benjhayden): Test that IsTemporary() downloads are not shown. |
| 163 |
| 164 // TODO(benjhayden): Test that RemoveObserver is called on all download items, |
| 165 // including items that crossed IsTemporary() and back. |
| OLD | NEW |