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/shell/shell.h"
17#include "content/test/content_browser_test.h"
18#include "content/test/content_browser_test_utils.h"
19#include "webkit/browser/quota/quota_manager.h"
20
21using quota::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    LOG(INFO) << "Navigating to URL and blocking.";
37    NavigateToURLBlockUntilNavigationsComplete(the_browser, test_url, 2);
38    LOG(INFO) << "Navigation done.";
39    std::string result = the_browser->web_contents()->GetURL().ref();
40    if (result != "pass") {
41      std::string js_result;
42      ASSERT_TRUE(ExecuteScriptAndExtractString(
43          the_browser->web_contents(),
44          "window.domAutomationController.send(getLog())",
45          &js_result));
46      FAIL() << "Failed: " << js_result;
47    }
48  }
49};
50
51class FileSystemBrowserTestWithLowQuota : public FileSystemBrowserTest {
52 public:
53  virtual void SetUpOnMainThread() OVERRIDE {
54    const int kInitialQuotaKilobytes = 5000;
55    const int kTemporaryStorageQuotaMaxSize =
56        kInitialQuotaKilobytes * 1024 * QuotaManager::kPerHostTemporaryPortion;
57    SetTempQuota(
58        kTemporaryStorageQuotaMaxSize,
59        BrowserContext::GetDefaultStoragePartition(
60            shell()->web_contents()->GetBrowserContext())->GetQuotaManager());
61  }
62
63  static void SetTempQuota(int64 bytes, scoped_refptr<QuotaManager> qm) {
64    if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
65      BrowserThread::PostTask(
66          BrowserThread::IO, FROM_HERE,
67          base::Bind(&FileSystemBrowserTestWithLowQuota::SetTempQuota, bytes,
68                     qm));
69      return;
70    }
71    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
72    qm->SetTemporaryGlobalOverrideQuota(bytes, quota::QuotaCallback());
73    // Don't return until the quota has been set.
74    scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
75        BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get()));
76    ASSERT_TRUE(helper->Run());
77  }
78};
79
80IN_PROC_BROWSER_TEST_F(FileSystemBrowserTest, RequestTest) {
81  SimpleTest(GetTestUrl("fileapi", "request_test.html"));
82}
83
84IN_PROC_BROWSER_TEST_F(FileSystemBrowserTest, CreateTest) {
85  SimpleTest(GetTestUrl("fileapi", "create_test.html"));
86}
87
88IN_PROC_BROWSER_TEST_F(FileSystemBrowserTestWithLowQuota, QuotaTest) {
89  SimpleTest(GetTestUrl("fileapi", "quota_test.html"));
90}
91
92}  // namespace content
93