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/bind.h"
6#include "base/command_line.h"
7#include "base/files/file_path.h"
8#include "base/memory/ref_counted.h"
9#include "base/test/thread_test_helper.h"
10#include "content/browser/web_contents/web_contents_impl.h"
11#include "content/public/browser/browser_context.h"
12#include "content/public/browser/browser_thread.h"
13#include "content/public/browser/storage_partition.h"
14#include "content/public/common/content_switches.h"
15#include "content/public/test/browser_test_utils.h"
16#include "content/public/test/content_browser_test.h"
17#include "content/public/test/content_browser_test_utils.h"
18#include "content/shell/browser/shell.h"
19#include "storage/browser/quota/quota_manager.h"
20
21using storage::QuotaManager;
22
23namespace content {
24
25// This browser test is aimed towards exercising the FileAPI bindings and
26// the actual implementation that lives in the browser side.
27class FileSystemBrowserTest : public ContentBrowserTest {
28 public:
29  FileSystemBrowserTest() {}
30
31  void SimpleTest(const GURL& test_url, bool incognito = false) {
32    // The test page will perform tests on FileAPI, then navigate to either
33    // a #pass or #fail ref.
34    Shell* the_browser = incognito ? CreateOffTheRecordBrowser() : shell();
35
36    VLOG(0) << "Navigating to URL and blocking.";
37    NavigateToURLBlockUntilNavigationsComplete(the_browser, test_url, 2);
38    VLOG(0) << "Navigation done.";
39    std::string result =
40        the_browser->web_contents()->GetLastCommittedURL().ref();
41    if (result != "pass") {
42      std::string js_result;
43      ASSERT_TRUE(ExecuteScriptAndExtractString(
44          the_browser->web_contents(),
45          "window.domAutomationController.send(getLog())",
46          &js_result));
47      FAIL() << "Failed: " << js_result;
48    }
49  }
50};
51
52class FileSystemBrowserTestWithLowQuota : public FileSystemBrowserTest {
53 public:
54  virtual void SetUpOnMainThread() OVERRIDE {
55    const int kInitialQuotaKilobytes = 5000;
56    const int kTemporaryStorageQuotaMaxSize =
57        kInitialQuotaKilobytes * 1024 * QuotaManager::kPerHostTemporaryPortion;
58    SetTempQuota(
59        kTemporaryStorageQuotaMaxSize,
60        BrowserContext::GetDefaultStoragePartition(
61            shell()->web_contents()->GetBrowserContext())->GetQuotaManager());
62  }
63
64  static void SetTempQuota(int64 bytes, scoped_refptr<QuotaManager> qm) {
65    if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
66      BrowserThread::PostTask(
67          BrowserThread::IO, FROM_HERE,
68          base::Bind(&FileSystemBrowserTestWithLowQuota::SetTempQuota, bytes,
69                     qm));
70      return;
71    }
72    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
73    qm->SetTemporaryGlobalOverrideQuota(bytes, storage::QuotaCallback());
74    // Don't return until the quota has been set.
75    scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
76        BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get()));
77    ASSERT_TRUE(helper->Run());
78  }
79};
80
81IN_PROC_BROWSER_TEST_F(FileSystemBrowserTest, RequestTest) {
82  SimpleTest(GetTestUrl("fileapi", "request_test.html"));
83}
84
85IN_PROC_BROWSER_TEST_F(FileSystemBrowserTest, CreateTest) {
86  SimpleTest(GetTestUrl("fileapi", "create_test.html"));
87}
88
89IN_PROC_BROWSER_TEST_F(FileSystemBrowserTestWithLowQuota, QuotaTest) {
90  SimpleTest(GetTestUrl("fileapi", "quota_test.html"));
91}
92
93}  // namespace content
94