summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/storage/storage_namespace_test.cc
blob: ca24c51827662adf08334fdc827be7d7251bee31 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2017 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 "third_party/blink/renderer/modules/storage/storage_namespace.h"
#include <third_party/blink/renderer/modules/storage/storage_controller.h>

#include "base/task/post_task.h"
#include "base/test/scoped_feature_list.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/scheduler/test/fake_renderer_scheduler.h"
#include "third_party/blink/renderer/modules/storage/testing/fake_area_source.h"
#include "third_party/blink/renderer/platform/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/uuid.h"
#include "third_party/blink/renderer/platform/web_task_runner.h"

namespace blink {
namespace {
class NoopStoragePartitionService
    : public mojom::blink::StoragePartitionService {
 public:
  void OpenLocalStorage(const scoped_refptr<const SecurityOrigin>& origin,
                        mojom::blink::StorageAreaRequest request) override {}

  void OpenSessionStorage(
      const String& namespace_id,
      mojom::blink::SessionStorageNamespaceRequest request) override {}
};

}  // namespace

class StorageNamespaceTest : public testing::Test {
 public:
  const size_t kTestCacheLimit = 100;

  StorageNamespaceTest() {
    features_.InitAndEnableFeature(features::kOnionSoupDOMStorage);
  }
  ~StorageNamespaceTest() override {}

  base::test::ScopedFeatureList features_;
};

TEST_F(StorageNamespaceTest, BasicStorageAreas) {
  const auto kOrigin = SecurityOrigin::CreateFromString("http://dom_storage1/");
  const auto kOrigin2 =
      SecurityOrigin::CreateFromString("http://dom_storage2/");
  const auto kOrigin3 =
      SecurityOrigin::CreateFromString("http://dom_storage3/");
  const String kKey("key");
  const String kValue("value");
  const String kSessionStorageNamespace("abcd");
  const KURL kPageUrl("http://dom_storage/page");
  Persistent<FakeAreaSource> source_area = new FakeAreaSource(kPageUrl);

  blink::scheduler::FakeRendererScheduler renderer_scheduler;

  mojom::blink::StoragePartitionServicePtr storage_partition_service_ptr;
  PostCrossThreadTask(
      *base::CreateSequencedTaskRunnerWithTraits({}), FROM_HERE,
      CrossThreadBind(
          [](mojom::blink::StoragePartitionServiceRequest request) {
            mojo::MakeStrongBinding(
                std::make_unique<NoopStoragePartitionService>(),
                std::move(request));
          },
          WTF::Passed(MakeRequest(&storage_partition_service_ptr))));

  StorageController controller(renderer_scheduler.IPCTaskRunner(),
                               std::move(storage_partition_service_ptr),
                               kTestCacheLimit);
  StorageNamespace* localStorage = new StorageNamespace(&controller);
  StorageNamespace* sessionStorage =
      new StorageNamespace(&controller, kSessionStorageNamespace);

  EXPECT_FALSE(localStorage->IsSessionStorage());
  EXPECT_TRUE(sessionStorage->IsSessionStorage());

  auto cached_area1 = localStorage->GetCachedArea(kOrigin.get());
  cached_area1->RegisterSource(source_area);
  cached_area1->SetItem(kKey, kValue, source_area);
  auto cached_area2 = localStorage->GetCachedArea(kOrigin2.get());
  cached_area2->RegisterSource(source_area);
  cached_area2->SetItem(kKey, kValue, source_area);
  auto cached_area3 = sessionStorage->GetCachedArea(kOrigin3.get());
  cached_area3->RegisterSource(source_area);
  cached_area3->SetItem(kKey, kValue, source_area);

  EXPECT_EQ(cached_area1->GetItem(kKey), kValue);
  EXPECT_EQ(cached_area2->GetItem(kKey), kValue);
  EXPECT_EQ(cached_area3->GetItem(kKey), kValue);
}

}  // namespace blink