summaryrefslogtreecommitdiffstats
path: root/content/browser/in_process_webkit/dom_storage_unittest.cc
blob: 03cffe47950c922365cf0488d6ff92a3d2403cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/file_path.h"
#include "base/file_util.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "content/browser/in_process_webkit/webkit_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/quota/mock_special_storage_policy.h"

class DOMStorageTest : public testing::Test {
 public:
  DOMStorageTest()
      : message_loop_(MessageLoop::TYPE_IO),
        webkit_thread_(BrowserThread::WEBKIT, &message_loop_) {
  }
 protected:
  MessageLoop message_loop_;

 private:
  BrowserThread webkit_thread_;
};

TEST_F(DOMStorageTest, SessionOnly) {
  GURL session_only_origin("http://www.sessiononly.com");
  scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy =
      new quota::MockSpecialStoragePolicy;
  special_storage_policy->AddSessionOnly(session_only_origin);

  // A scope for deleting TestingProfile early enough.
  {
    TestingProfile profile;

    // Create databases for permanent and session-only origins.
    FilePath domstorage_dir = profile.GetPath().Append(
        DOMStorageContext::kLocalStorageDirectory);
    FilePath::StringType session_only_database(
        FILE_PATH_LITERAL("http_www.sessiononly.com_0"));
    FilePath::StringType permanent_database(
        FILE_PATH_LITERAL("http_www.permanent.com_0"));
    session_only_database.append(DOMStorageContext::kLocalStorageExtension);
    permanent_database.append(DOMStorageContext::kLocalStorageExtension);
    FilePath session_only_database_path =
        domstorage_dir.Append(session_only_database);
    FilePath permanent_database_path =
        domstorage_dir.Append(permanent_database);

    ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir));

    ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1));
    ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1));

    // Inject MockSpecialStoragePolicy into DOMStorageContext.
    profile.GetWebKitContext()->dom_storage_context()->special_storage_policy_ =
        special_storage_policy;

    // Tell the WebKitContext explicitly do clean up the session-only
    // data. TestingProfile (unlike ProfileImpl) doesn't do it automatically
    // when destructed. The deletion is done immediately since we're on the
    // WEBKIT thread (created by the test).
    profile.GetWebKitContext()->DeleteSessionOnlyData();

    // Expected result: the database file for the permanent storage remains but
    // the database for the session only storage was deleted.
    EXPECT_FALSE(file_util::PathExists(session_only_database_path));
    EXPECT_TRUE(file_util::PathExists(permanent_database_path));
  }
  // Run the message loop to ensure that DOMStorageContext gets destroyed.
  message_loop_.RunAllPending();
}