Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 10 | #include "base/location.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
Victor Costan | 827623c | 2022-01-21 22:08:56 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 14 | #include "base/task/single_thread_task_runner.h" |
Victor Costan | 827623c | 2022-01-21 22:08:56 | [diff] [blame] | 15 | #include "base/test/bind.h" |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 16 | #include "base/test/thread_test_helper.h" |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 17 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 18 | #include "content/public/browser/browser_context.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 19 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 20 | #include "content/public/browser/browser_thread.h" |
[email protected] | f13b268 | 2012-08-22 00:37:55 | [diff] [blame] | 21 | #include "content/public/browser/storage_partition.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 22 | #include "content/public/common/content_switches.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 23 | #include "content/public/test/browser_test.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 24 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 25 | #include "content/public/test/content_browser_test.h" |
| 26 | #include "content/public/test/content_browser_test_utils.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 27 | #include "content/shell/browser/shell.h" |
pilgrim | e92c5fcd | 2014-09-10 23:31:23 | [diff] [blame] | 28 | #include "storage/browser/quota/quota_manager.h" |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 29 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 30 | using storage::QuotaManager; |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 31 | |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 32 | namespace content { |
| 33 | |
jsbell | 14785d90 | 2016-10-26 21:37:26 | [diff] [blame] | 34 | // This browser test is aimed towards exercising the File System API bindings |
| 35 | // and the actual implementation that lives in the browser side. |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 36 | class FileSystemBrowserTest : public ContentBrowserTest, |
| 37 | public testing::WithParamInterface<bool> { |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 38 | public: |
DongJun Kim | 47743f4 | 2019-10-28 03:05:51 | [diff] [blame] | 39 | FileSystemBrowserTest() { is_incognito_ = GetParam(); } |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 40 | |
Lukasz Anforowicz | 292a1d46 | 2020-04-30 19:33:23 | [diff] [blame] | 41 | void SetUpOnMainThread() override { |
| 42 | ASSERT_TRUE(embedded_test_server()->Start()); |
Jagadesh P | 1961827 | 2023-10-30 19:11:52 | [diff] [blame] | 43 | browser_ = is_incognito() ? CreateOffTheRecordBrowser() : shell(); |
Lukasz Anforowicz | 292a1d46 | 2020-04-30 19:33:23 | [diff] [blame] | 44 | } |
| 45 | |
Jagadesh P | 1961827 | 2023-10-30 19:11:52 | [diff] [blame] | 46 | void TearDownOnMainThread() override { browser_ = nullptr; } |
| 47 | |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 48 | void SimpleTest(const GURL& test_url) { |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 49 | // The test page will perform tests on FileAPI, then navigate to either |
| 50 | // a #pass or #fail ref. |
[email protected] | efde84b0 | 2013-11-23 04:18:20 | [diff] [blame] | 51 | VLOG(0) << "Navigating to URL and blocking."; |
Ramin Halavati | 9bf18b22 | 2019-04-16 18:08:36 | [diff] [blame] | 52 | NavigateToURLBlockUntilNavigationsComplete(browser(), test_url, 2); |
[email protected] | efde84b0 | 2013-11-23 04:18:20 | [diff] [blame] | 53 | VLOG(0) << "Navigation done."; |
Ramin Halavati | 9bf18b22 | 2019-04-16 18:08:36 | [diff] [blame] | 54 | std::string result = browser()->web_contents()->GetLastCommittedURL().ref(); |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 55 | if (result != "pass") { |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 56 | std::string js_result = EvalJs(browser(), "getLog()").ExtractString(); |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 57 | FAIL() << "Failed: " << js_result; |
| 58 | } |
| 59 | } |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 60 | |
Jagadesh P | 1961827 | 2023-10-30 19:11:52 | [diff] [blame] | 61 | Shell* browser() { return browser_; } |
Ramin Halavati | 9bf18b22 | 2019-04-16 18:08:36 | [diff] [blame] | 62 | |
| 63 | bool is_incognito() { return is_incognito_; } |
| 64 | |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 65 | protected: |
Ramin Halavati | 9bf18b22 | 2019-04-16 18:08:36 | [diff] [blame] | 66 | bool is_incognito_; |
Jagadesh P | 1961827 | 2023-10-30 19:11:52 | [diff] [blame] | 67 | raw_ptr<Shell> browser_ = nullptr; |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 68 | }; |
| 69 | |
Ilia Samsonov | 6d14257 | 2019-11-21 02:34:06 | [diff] [blame] | 70 | INSTANTIATE_TEST_SUITE_P(All, FileSystemBrowserTest, ::testing::Bool()); |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 71 | |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 72 | class FileSystemBrowserTestWithLowQuota : public FileSystemBrowserTest { |
| 73 | public: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 74 | void SetUpOnMainThread() override { |
Lukasz Anforowicz | 292a1d46 | 2020-04-30 19:33:23 | [diff] [blame] | 75 | FileSystemBrowserTest::SetUpOnMainThread(); |
Lukasz Anforowicz | b9a969a | 2021-04-29 15:26:25 | [diff] [blame] | 76 | SetLowQuota(browser() |
| 77 | ->web_contents() |
| 78 | ->GetBrowserContext() |
| 79 | ->GetDefaultStoragePartition() |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 80 | ->GetQuotaManager()); |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 81 | } |
| 82 | |
michaeln | 10e5fc35 | 2017-02-07 02:07:58 | [diff] [blame] | 83 | static void SetLowQuota(scoped_refptr<QuotaManager> qm) { |
Victor Costan | 827623c | 2022-01-21 22:08:56 | [diff] [blame] | 84 | DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 85 | |
| 86 | base::RunLoop run_loop; |
| 87 | GetIOThreadTaskRunner({})->PostTaskAndReply( |
| 88 | FROM_HERE, base::BindLambdaForTesting([&]() { |
| 89 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 90 | // These sizes must correspond with expectations in html and js. |
| 91 | const int kMeg = 1000 * 1024; |
| 92 | storage::QuotaSettings settings; |
| 93 | settings.pool_size = 25 * kMeg; |
Ayu Ishii | 802a1ee | 2022-11-30 17:40:19 | [diff] [blame] | 94 | settings.per_storage_key_quota = 5 * kMeg; |
Victor Costan | 827623c | 2022-01-21 22:08:56 | [diff] [blame] | 95 | settings.must_remain_available = 10 * kMeg; |
| 96 | settings.refresh_interval = base::TimeDelta::Max(); |
| 97 | qm->SetQuotaSettings(settings); |
| 98 | }), |
| 99 | run_loop.QuitClosure()); |
| 100 | run_loop.Run(); |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | |
Ilia Samsonov | 6d14257 | 2019-11-21 02:34:06 | [diff] [blame] | 104 | INSTANTIATE_TEST_SUITE_P(All, |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 105 | FileSystemBrowserTestWithLowQuota, |
Ramin Halavati | 9bf18b22 | 2019-04-16 18:08:36 | [diff] [blame] | 106 | ::testing::Bool()); |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 107 | |
| 108 | IN_PROC_BROWSER_TEST_P(FileSystemBrowserTest, RequestTest) { |
Lukasz Anforowicz | 292a1d46 | 2020-04-30 19:33:23 | [diff] [blame] | 109 | SimpleTest(embedded_test_server()->GetURL("/fileapi/request_test.html")); |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 110 | } |
| 111 | |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 112 | IN_PROC_BROWSER_TEST_P(FileSystemBrowserTest, CreateTest) { |
Lukasz Anforowicz | 292a1d46 | 2020-04-30 19:33:23 | [diff] [blame] | 113 | SimpleTest(embedded_test_server()->GetURL("/fileapi/create_test.html")); |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 114 | } |
| 115 | |
Ramin Halavati | e24c8b2 | 2019-04-11 15:03:12 | [diff] [blame] | 116 | IN_PROC_BROWSER_TEST_P(FileSystemBrowserTestWithLowQuota, QuotaTest) { |
Lukasz Anforowicz | 292a1d46 | 2020-04-30 19:33:23 | [diff] [blame] | 117 | SimpleTest(embedded_test_server()->GetURL("/fileapi/quota_test.html")); |
[email protected] | b4449206 | 2011-09-06 12:47:47 | [diff] [blame] | 118 | } |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 119 | |
| 120 | } // namespace content |