browsing_data_local_storage_helper_unittest.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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 "chrome/browser/browsing_data_local_storage_helper.h"
6
7#include "chrome/test/testing_profile.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace {
11class TestCompletionCallback
12    : public CallbackRunner<Tuple1<const std::vector<
13          BrowsingDataLocalStorageHelper::LocalStorageInfo>& > > {
14 public:
15  TestCompletionCallback()
16      : have_result_(false) {
17  }
18
19  bool have_result() const { return have_result_; }
20
21  const std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo>& result()
22  {
23    return result_;
24  }
25
26  virtual void RunWithParams(
27      const Tuple1<const std::vector<
28          BrowsingDataLocalStorageHelper::LocalStorageInfo>& >& params) {
29    have_result_ = true;
30    result_ = params.a;
31  }
32
33 private:
34  bool have_result_;
35  std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result_;
36
37  DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
38};
39}  // namespace
40
41TEST(CannedBrowsingDataLocalStorageTest, AddLocalStorage) {
42  TestingProfile profile;
43
44  const GURL origin1("http://host1:1/");
45  const GURL origin2("http://host2:1/");
46  const FilePath::CharType file1[] =
47      FILE_PATH_LITERAL("http_host1_1.localstorage");
48  const FilePath::CharType file2[] =
49      FILE_PATH_LITERAL("http_host2_1.localstorage");
50
51  scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper =
52      new CannedBrowsingDataLocalStorageHelper(&profile);
53  helper->AddLocalStorage(origin1);
54  helper->AddLocalStorage(origin2);
55
56  TestCompletionCallback callback;
57  helper->StartFetching(&callback);
58  ASSERT_TRUE(callback.have_result());
59
60  std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result =
61      callback.result();
62
63  ASSERT_EQ(2u, result.size());
64  EXPECT_EQ(FilePath(file1).value(), result[0].file_path.BaseName().value());
65  EXPECT_EQ(FilePath(file2).value(), result[1].file_path.BaseName().value());
66}
67
68TEST(CannedBrowsingDataLocalStorageTest, Unique) {
69  TestingProfile profile;
70
71  const GURL origin("http://host1:1/");
72  const FilePath::CharType file[] =
73      FILE_PATH_LITERAL("http_host1_1.localstorage");
74
75  scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper =
76      new CannedBrowsingDataLocalStorageHelper(&profile);
77  helper->AddLocalStorage(origin);
78  helper->AddLocalStorage(origin);
79
80  TestCompletionCallback callback;
81  helper->StartFetching(&callback);
82  ASSERT_TRUE(callback.have_result());
83
84  std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result =
85      callback.result();
86
87  ASSERT_EQ(1u, result.size());
88  EXPECT_EQ(FilePath(file).value(), result[0].file_path.BaseName().value());
89}
90