summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_settings_frontend_unittest.cc
blob: a7eb634d9ebf5d4b4d37901744cf3a524736a1dc (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
// 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 "testing/gtest/include/gtest/gtest.h"

#include "base/bind.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/scoped_temp_dir.h"
#include "chrome/browser/extensions/extension_settings_frontend.h"
#include "chrome/browser/extensions/extension_settings_storage.h"
#include "chrome/browser/extensions/extension_settings_test_util.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/test/test_browser_thread.h"

using content::BrowserThread;

using namespace extension_settings_test_util;

class ExtensionSettingsFrontendTest : public testing::Test {
 public:
   ExtensionSettingsFrontendTest()
      : ui_thread_(BrowserThread::UI, MessageLoop::current()),
        file_thread_(BrowserThread::FILE, MessageLoop::current()) {}

  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    profile_.reset(new MockProfile(temp_dir_.path()));
    frontend_.reset(new ExtensionSettingsFrontend(profile_.get()));
  }

  virtual void TearDown() OVERRIDE {
    frontend_.reset();
    profile_.reset();
  }

 protected:
  ScopedTempDir temp_dir_;
  scoped_ptr<MockProfile> profile_;
  scoped_ptr<ExtensionSettingsFrontend> frontend_;

 private:
  MessageLoop message_loop_;
  content::TestBrowserThread ui_thread_;
  content::TestBrowserThread file_thread_;
};

// Get a semblance of coverage for both extension and app settings by
// alternating in each test.
// TODO(kalman): explicitly test the two interact correctly.

TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) {
  const std::string id = "ext";
  profile_->GetMockExtensionService()->AddExtension(
      id, Extension::TYPE_EXTENSION);

  ExtensionSettingsStorage* storage = GetStorage(id, frontend_.get());

  // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to
  // be too rigorous.
  StringValue bar("bar");
  ExtensionSettingsStorage::Result result = storage->Set("foo", bar);
  ASSERT_FALSE(result.HasError());

  result = storage->Get();
  ASSERT_FALSE(result.HasError());
  EXPECT_FALSE(result.GetSettings()->empty());

  frontend_.reset(new ExtensionSettingsFrontend(profile_.get()));
  storage = GetStorage(id, frontend_.get());

  result = storage->Get();
  ASSERT_FALSE(result.HasError());
  EXPECT_FALSE(result.GetSettings()->empty());
}

TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) {
  const std::string id = "ext";
  profile_->GetMockExtensionService()->AddExtension(
      id, Extension::TYPE_PACKAGED_APP);

  ExtensionSettingsStorage* storage = GetStorage(id, frontend_.get());

  StringValue bar("bar");
  ExtensionSettingsStorage::Result result = storage->Set("foo", bar);
  ASSERT_FALSE(result.HasError());

  // This would be triggered by extension uninstall via an ExtensionDataDeleter.
  frontend_->DeleteStorageSoon(id);
  MessageLoop::current()->RunAllPending();

  // The storage area may no longer be valid post-uninstall, so re-request.
  storage = GetStorage(id, frontend_.get());
  result = storage->Get();
  ASSERT_FALSE(result.HasError());
  EXPECT_TRUE(result.GetSettings()->empty());
}

TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) {
  const std::string id = "ext";
  profile_->GetMockExtensionService()->AddExtension(
      id, Extension::TYPE_EXTENSION);

  ExtensionSettingsStorage* storage = GetStorage(id, frontend_.get());

  StringValue bar("bar");
  ExtensionSettingsStorage::Result result = storage->Set("foo", bar);
  ASSERT_FALSE(result.HasError());
  EXPECT_TRUE(file_util::PathExists(temp_dir_.path()));

  // Should need to both clear the database and delete the frontend for the
  // leveldb database to be deleted from disk.
  result = storage->Clear();
  ASSERT_FALSE(result.HasError());
  EXPECT_TRUE(file_util::PathExists(temp_dir_.path()));

  frontend_.reset();
  MessageLoop::current()->RunAllPending();
  // TODO(kalman): Figure out why this fails, despite appearing to work.
  // Leaving this commented out rather than disabling the whole test so that the
  // deletion code paths are at least exercised.
  //EXPECT_FALSE(file_util::PathExists(temp_dir_.path()));
}