browsing_data_database_helper_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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_database_helper.h"
6
7#include "base/file_util.h"
8#include "chrome/test/testing_profile.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12class TestCompletionCallback {
13 public:
14  TestCompletionCallback()
15      : have_result_(false) {
16  }
17
18  bool have_result() const { return have_result_; }
19
20  const std::vector<BrowsingDataDatabaseHelper::DatabaseInfo>& result() {
21    return result_;
22  }
23
24  void callback(const std::vector<
25          BrowsingDataDatabaseHelper::DatabaseInfo>& info) {
26    have_result_ = true;
27    result_ = info;
28  }
29
30 private:
31  bool have_result_;
32  std::vector<BrowsingDataDatabaseHelper::DatabaseInfo> result_;
33
34  DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
35};
36}  // namespace
37
38TEST(CannedBrowsingDataDatabaseTest, AddDatabase) {
39  TestingProfile profile;
40
41  const GURL origin1("http://host1:1/");
42  const GURL origin2("http://host2:1/");
43  const char origin_str1[] = "http_host1_1";
44  const char origin_str2[] = "http_host2_1";
45  const char db1[] = "db1";
46  const char db2[] = "db2";
47  const char db3[] = "db3";
48
49  scoped_refptr<CannedBrowsingDataDatabaseHelper> helper =
50      new CannedBrowsingDataDatabaseHelper(&profile);
51  helper->AddDatabase(origin1, db1, "");
52  helper->AddDatabase(origin1, db2, "");
53  helper->AddDatabase(origin2, db3, "");
54
55  TestCompletionCallback callback;
56  helper->StartFetching(
57      NewCallback(&callback, &TestCompletionCallback::callback));
58  ASSERT_TRUE(callback.have_result());
59
60  std::vector<BrowsingDataDatabaseHelper::DatabaseInfo> result =
61      callback.result();
62
63  ASSERT_EQ(3u, result.size());
64  EXPECT_STREQ(origin_str1, result[0].origin_identifier.c_str());
65  EXPECT_STREQ(db1, result[0].database_name.c_str());
66  EXPECT_STREQ(origin_str1, result[1].origin_identifier.c_str());
67  EXPECT_STREQ(db2, result[1].database_name.c_str());
68  EXPECT_STREQ(origin_str2, result[2].origin_identifier.c_str());
69  EXPECT_STREQ(db3, result[2].database_name.c_str());
70}
71
72TEST(CannedBrowsingDataDatabaseTest, Unique) {
73  TestingProfile profile;
74
75  const GURL origin("http://host1:1/");
76  const char origin_str[] = "http_host1_1";
77  const char db[] = "db1";
78
79  scoped_refptr<CannedBrowsingDataDatabaseHelper> helper =
80      new CannedBrowsingDataDatabaseHelper(&profile);
81  helper->AddDatabase(origin, db, "");
82  helper->AddDatabase(origin, db, "");
83
84  TestCompletionCallback callback;
85  helper->StartFetching(
86      NewCallback(&callback, &TestCompletionCallback::callback));
87  ASSERT_TRUE(callback.have_result());
88
89  std::vector<BrowsingDataDatabaseHelper::DatabaseInfo> result =
90      callback.result();
91
92  ASSERT_EQ(1u, result.size());
93  EXPECT_STREQ(origin_str, result[0].origin_identifier.c_str());
94  EXPECT_STREQ(db, result[0].database_name.c_str());
95}
96
97TEST(CannedBrowsingDataDatabaseTest, Empty) {
98  TestingProfile profile;
99
100  const GURL origin("http://host1:1/");
101  const char db[] = "db1";
102
103  scoped_refptr<CannedBrowsingDataDatabaseHelper> helper =
104      new CannedBrowsingDataDatabaseHelper(&profile);
105
106  ASSERT_TRUE(helper->empty());
107  helper->AddDatabase(origin, db, "");
108  ASSERT_FALSE(helper->empty());
109  helper->Reset();
110  ASSERT_TRUE(helper->empty());
111}
112