summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/app_notification_storage_unittest.cc
blob: ef4ab85f35a368e60afcf3bff42dc4c1ff338435 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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 "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/stl_util.h"
#include "chrome/browser/extensions/app_notification.h"
#include "chrome/browser/extensions/app_notification_storage.h"
#include "chrome/browser/extensions/app_notification_test_util.h"
#include "chrome/common/extensions/extension_test_util.h"
#include "content/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace util = app_notification_test_util;

class AppNotificationStorageTest : public testing::Test {
 public:
  AppNotificationStorageTest() :
      file_thread_(BrowserThread::FILE, &message_loop_) {}
  virtual ~AppNotificationStorageTest() {}

  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(dir_.CreateUniqueTempDir());
    storage_path_ = dir_.path().AppendASCII("App Notifications");
    storage_.reset(AppNotificationStorage::Create(storage_path_));
    ASSERT_TRUE(storage_.get() != NULL);
  }

 protected:
  // Returns whether the database file(s) exist on disk yet.
  bool DatabaseExistsOnDisk() {
    if (!storage_.get())
      return false;

    return file_util::PathExists(storage_path_);
  }

  MessageLoop message_loop_;
  content::TestBrowserThread file_thread_;
  ScopedTempDir dir_;
  FilePath storage_path_;
  scoped_ptr<AppNotificationStorage> storage_;
};

// Tests simple operations.
TEST_F(AppNotificationStorageTest, Basics) {
  std::set<std::string> tmp_ids;
  AppNotificationList tmp_list;
  std::string id1 = extension_test_util::MakeId("1");

  // Try operations on non-existent keys.
  EXPECT_TRUE(storage_->GetExtensionIds(&tmp_ids));
  EXPECT_TRUE(tmp_ids.empty());
  EXPECT_TRUE(storage_->Get(id1, &tmp_list));
  EXPECT_TRUE(tmp_list.empty());
  EXPECT_TRUE(storage_->Delete(id1));
  EXPECT_FALSE(DatabaseExistsOnDisk());

  // Add some items.
  AppNotificationList list;
  util::AddNotifications(&list, id1, 2, "whatever");
  EXPECT_TRUE(storage_->Set(id1, list));
  EXPECT_TRUE(DatabaseExistsOnDisk());

  // Try getting them back.
  tmp_list.clear();
  EXPECT_TRUE(storage_->Get(id1, &tmp_list));
  util::ExpectListsEqual(list, tmp_list);
  EXPECT_TRUE(storage_->GetExtensionIds(&tmp_ids));
  EXPECT_EQ(tmp_ids.size(), 1U);
  EXPECT_TRUE(ContainsKey(tmp_ids, id1));
}

// Tests with multiple extensions.
TEST_F(AppNotificationStorageTest, MultipleExtensions) {
  std::string id1 = extension_test_util::MakeId("1");
  std::string id2 = extension_test_util::MakeId("2");

  // Add items for id1.
  AppNotificationList list1;
  util::AddNotifications(&list1, id1, 2, "one");
  EXPECT_TRUE(storage_->Set(id1, list1));

  // Add items for id2.
  AppNotificationList list2;
  util::AddNotifications(&list2, id2, 3, "two");
  EXPECT_TRUE(storage_->Set(id2, list2));

  // Verify the items are present
  std::set<std::string> tmp_ids;
  EXPECT_TRUE(storage_->GetExtensionIds(&tmp_ids));
  EXPECT_EQ(tmp_ids.size(), 2U);
  EXPECT_TRUE(ContainsKey(tmp_ids, id1));
  EXPECT_TRUE(ContainsKey(tmp_ids, id2));

  AppNotificationList tmp_list;
  EXPECT_TRUE(storage_->Get(id1, &tmp_list));
  util::ExpectListsEqual(tmp_list, list1);
  tmp_list.clear();
  EXPECT_TRUE(storage_->Get(id2, &tmp_list));
  util::ExpectListsEqual(tmp_list, list2);

  // Delete the id1 items and check the results.
  EXPECT_TRUE(storage_->Delete(id1));
  tmp_ids.clear();
  EXPECT_TRUE(storage_->GetExtensionIds(&tmp_ids));
  EXPECT_EQ(tmp_ids.size(), 1U);
  EXPECT_FALSE(ContainsKey(tmp_ids, id1));
  EXPECT_TRUE(ContainsKey(tmp_ids, id2));
  tmp_list.clear();
  EXPECT_TRUE(storage_->Get(id1, &tmp_list));
  EXPECT_TRUE(tmp_list.empty());
  tmp_list.clear();
  EXPECT_TRUE(storage_->Get(id2, &tmp_list));
  util::ExpectListsEqual(tmp_list, list2);
}

// Tests using Set to replace existing entries.
TEST_F(AppNotificationStorageTest, ReplaceExisting) {
  std::string id = extension_test_util::MakeId("1");
  AppNotificationList list1;
  AppNotificationList list2;
  util::AddNotifications(&list1, id, 5, "one");
  util::AddNotifications(&list2, id, 7, "two");

  // Put list1 in, then replace with list2 and verify we get list2 back.
  EXPECT_TRUE(storage_->Set(id, list1));
  EXPECT_TRUE(storage_->Set(id, list2));
  AppNotificationList tmp_list;
  EXPECT_TRUE(storage_->Get(id, &tmp_list));
  util::ExpectListsEqual(list2, tmp_list);
}