This source file includes following definitions.
- InitPluginService
- InitFileTaskRunner
- GarbageCollectExtensions
- TEST_F
- TEST_F
- TEST_F
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_garbage_collector.h"
#include "chrome/browser/extensions/extension_service_unittest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/test/test_browser_thread_bundle.h"
namespace extensions {
class ExtensionGarbageCollectorUnitTest : public ExtensionServiceTestBase {
protected:
void InitPluginService() {
#if defined(ENABLE_PLUGINS)
content::PluginService::GetInstance()->Init();
#endif
}
void InitFileTaskRunner() {
service_->SetFileTaskRunnerForTesting(
content::BrowserThread::GetBlockingPool()
->GetSequencedTaskRunnerWithShutdownBehavior(
content::BrowserThread::GetBlockingPool()
->GetNamedSequenceToken("ext_install-"),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
}
void GarbageCollectExtensions() {
service_->garbage_collector()->GarbageCollectExtensionsForTest();
content::BrowserThread::GetBlockingPool()->FlushForTesting();
}
};
TEST_F(ExtensionGarbageCollectorUnitTest, CleanupOnStartup) {
const std::string kExtensionId = "behllobkkfkfnphdnhnkndlbkcpglgmj";
InitPluginService();
InitializeGoodInstalledExtensionService();
InitFileTaskRunner();
{
DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings");
base::DictionaryValue* dict = update.Get();
ASSERT_TRUE(dict != NULL);
dict->Remove(kExtensionId, NULL);
}
service_->Init();
GarbageCollectExtensions();
base::FileEnumerator dirs(extensions_install_dir_,
false,
base::FileEnumerator::DIRECTORIES);
size_t count = 0;
while (!dirs.Next().empty())
count++;
EXPECT_EQ(2u, count);
base::FilePath extension_dir =
extensions_install_dir_.AppendASCII(kExtensionId);
ASSERT_FALSE(base::PathExists(extension_dir));
}
TEST_F(ExtensionGarbageCollectorUnitTest, GarbageCollectWithPendingUpdates) {
InitPluginService();
base::FilePath source_install_dir =
data_dir_.AppendASCII("pending_updates").AppendASCII("Extensions");
base::FilePath pref_path =
source_install_dir.DirName().Append(chrome::kPreferencesFilename);
InitializeInstalledExtensionService(pref_path, source_install_dir);
InitFileTaskRunner();
ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
"hpiknbiabeeppbpihjehijgoemciehgk/3")));
GarbageCollectExtensions();
EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
"bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
"bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0")));
EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
"hpiknbiabeeppbpihjehijgoemciehgk/2")));
EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII(
"hpiknbiabeeppbpihjehijgoemciehgk/3")));
}
TEST_F(ExtensionGarbageCollectorUnitTest, UpdateOnStartup) {
InitPluginService();
base::FilePath source_install_dir =
data_dir_.AppendASCII("pending_updates").AppendASCII("Extensions");
base::FilePath pref_path =
source_install_dir.DirName().Append(chrome::kPreferencesFilename);
InitializeInstalledExtensionService(pref_path, source_install_dir);
InitFileTaskRunner();
ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
"hpiknbiabeeppbpihjehijgoemciehgk/3")));
service_->Init();
GarbageCollectExtensions();
EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII(
"bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
"bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0")));
EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
"hpiknbiabeeppbpihjehijgoemciehgk/2")));
EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII(
"hpiknbiabeeppbpihjehijgoemciehgk/3")));
ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_.get());
EXPECT_FALSE(
prefs->GetDelayedInstallInfo("bjafgdebaacbbbecmhlhpofkepfkgcpa"));
}
}