Add unit tests for the CLD component installer

BUG=367239

Review URL: https://codereview.chromium.org/280753003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271028 0039d316-1c4b-4281-b951-d872f2087c98
parent 0cb6b9d7
......@@ -13,7 +13,7 @@
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "chrome/browser/component_updater/default_component_installer.h"
#include "base/platform_file.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
......@@ -47,31 +47,6 @@ const uint8 kPublicKeySHA256[32] = {
const char kCldManifestName[] = "CLD2 Data";
class CldComponentInstallerTraits : public ComponentInstallerTraits {
public:
CldComponentInstallerTraits();
virtual ~CldComponentInstallerTraits() {}
private:
// The following methods override ComponentInstallerTraits.
virtual bool CanAutoUpdate() const OVERRIDE;
virtual bool OnCustomInstall(const base::DictionaryValue& manifest,
const base::FilePath& install_dir) OVERRIDE;
virtual bool VerifyInstallation(
const base::FilePath& install_dir) const OVERRIDE;
virtual void ComponentReady(
const base::Version& version,
const base::FilePath& path,
scoped_ptr<base::DictionaryValue> manifest) OVERRIDE;
virtual base::FilePath GetBaseDirectory() const OVERRIDE;
virtual void GetHash(std::vector<uint8>* hash) const OVERRIDE;
virtual std::string GetName() const OVERRIDE;
base::FilePath GetInstalledPath(const base::FilePath& base) const;
void SetLatestCldDataFile(const base::FilePath& path);
DISALLOW_COPY_AND_ASSIGN(CldComponentInstallerTraits);
};
CldComponentInstallerTraits::CldComponentInstallerTraits() {
}
......@@ -140,7 +115,6 @@ void RegisterCldComponent(ComponentUpdateService* cus) {
installer->Register(cus);
}
// This method is completely threadsafe.
void CldComponentInstallerTraits::SetLatestCldDataFile(
const base::FilePath& path) {
VLOG(1) << "Setting CLD data file location: " << path.value();
......@@ -148,13 +122,10 @@ void CldComponentInstallerTraits::SetLatestCldDataFile(
cld_file.Get() = path;
}
bool GetLatestCldDataFile(base::FilePath* path) {
base::FilePath GetLatestCldDataFile() {
base::AutoLock lock(cld_file_lock.Get());
const base::FilePath cached = cld_file.Get();
if (cached.empty())
return false;
*path = cached;
return true;
// cld_file is an empty path by default, meaning "file not available yet".
return cld_file.Get();
}
} // namespace component_updater
......@@ -5,19 +5,61 @@
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_CLD_COMPONENT_INSTALLER_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_CLD_COMPONENT_INSTALLER_H_
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "chrome/browser/component_updater/default_component_installer.h"
namespace component_updater {
class ComponentUpdateService;
// Visible for testing. No need to instantiate this class directly.
class CldComponentInstallerTraits : public ComponentInstallerTraits {
public:
CldComponentInstallerTraits();
virtual ~CldComponentInstallerTraits() {}
private:
friend class CldComponentInstallerTest; // For access within SetUp()
FRIEND_TEST_ALL_PREFIXES(CldComponentInstallerTest, ComponentReady);
FRIEND_TEST_ALL_PREFIXES(CldComponentInstallerTest, GetBaseDirectory);
FRIEND_TEST_ALL_PREFIXES(CldComponentInstallerTest, GetHash);
FRIEND_TEST_ALL_PREFIXES(CldComponentInstallerTest, GetInstalledPath);
FRIEND_TEST_ALL_PREFIXES(CldComponentInstallerTest, GetName);
FRIEND_TEST_ALL_PREFIXES(CldComponentInstallerTest, OnCustomInstall);
FRIEND_TEST_ALL_PREFIXES(CldComponentInstallerTest, SetLatestCldDataFile);
FRIEND_TEST_ALL_PREFIXES(CldComponentInstallerTest, VerifyInstallation);
// The following methods override ComponentInstallerTraits.
virtual bool CanAutoUpdate() const OVERRIDE;
virtual bool OnCustomInstall(const base::DictionaryValue& manifest,
const base::FilePath& install_dir) OVERRIDE;
virtual bool VerifyInstallation(
const base::FilePath& install_dir) const OVERRIDE;
virtual void ComponentReady(
const base::Version& version,
const base::FilePath& path,
scoped_ptr<base::DictionaryValue> manifest) OVERRIDE;
virtual base::FilePath GetBaseDirectory() const OVERRIDE;
virtual void GetHash(std::vector<uint8>* hash) const OVERRIDE;
virtual std::string GetName() const OVERRIDE;
base::FilePath GetInstalledPath(const base::FilePath& base) const;
void SetLatestCldDataFile(const base::FilePath& path);
DISALLOW_COPY_AND_ASSIGN(CldComponentInstallerTraits);
};
// Call once during startup to make the component update service aware of
// the CLD component.
void RegisterCldComponent(ComponentUpdateService* cus);
// Places the path to the latest CLD data file into the specified path object.
// Returns true if and only if the file has been observed to exist at least
// once and was valid when it was observed; if the function returns false, the
// path parameter is not modified. This function is threadsafe.
bool GetLatestCldDataFile(base::FilePath* path);
// Returns the path to the latest CLD data file into the specified path object,
// or an empty path if the CLD data file has not been observed yet.
// This function is threadsafe.
base::FilePath GetLatestCldDataFile();
} // namespace component_updater
......
// Copyright 2014 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 <vector>
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "base/version.h"
#include "chrome/browser/component_updater/cld_component_installer.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace component_updater {
class CldComponentInstallerTest : public PlatformTest {
public:
virtual void SetUp() OVERRIDE {
PlatformTest::SetUp();
// ScopedTempDir automatically does a recursive delete on the entire
// directory in its destructor, so no cleanup is required in TearDown.
// Note that all files created by this test case are created within the
// directory that is created here, so even though they are not explicitly
// created *as temp files*, they will still get cleaned up automagically.
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
// The "latest CLD data file" is a static piece of information, and thus
// for correctness we empty it before each test.
traits.SetLatestCldDataFile(base::FilePath());
}
base::ScopedTempDir temp_dir_;
component_updater::CldComponentInstallerTraits traits;
};
TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) {
ASSERT_TRUE(component_updater::GetLatestCldDataFile().empty());
const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test"));
traits.SetLatestCldDataFile(expected);
base::FilePath result = component_updater::GetLatestCldDataFile();
ASSERT_EQ(expected, result);
}
TEST_F(CldComponentInstallerTest, VerifyInstallation) {
// All files are created within a ScopedTempDir, which deletes all
// children when its destructor is called (at the end of each test).
ASSERT_FALSE(traits.VerifyInstallation(temp_dir_.path()));
const base::FilePath data_file_dir =
temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append(
FILE_PATH_LITERAL("all"));
ASSERT_TRUE(base::CreateDirectory(data_file_dir));
const base::FilePath data_file =
data_file_dir.Append(chrome::kCLDDataFilename);
const std::string test_data("fake cld2 data file content here :)");
ASSERT_EQ(static_cast<int32>(test_data.length()),
base::WriteFile(data_file, test_data.c_str(), test_data.length()));
ASSERT_TRUE(traits.VerifyInstallation(temp_dir_.path()));
}
TEST_F(CldComponentInstallerTest, OnCustomInstall) {
const base::DictionaryValue manifest;
const base::FilePath install_dir;
// Sanity: shouldn't crash.
ASSERT_TRUE(traits.OnCustomInstall(manifest, install_dir));
}
TEST_F(CldComponentInstallerTest, GetInstalledPath) {
const base::FilePath base_dir;
const base::FilePath result = traits.GetInstalledPath(base_dir);
ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true));
}
TEST_F(CldComponentInstallerTest, GetBaseDirectory) {
const base::FilePath result = traits.GetBaseDirectory();
ASSERT_FALSE(result.empty());
}
TEST_F(CldComponentInstallerTest, GetHash) {
std::vector<uint8> hash;
traits.GetHash(&hash);
ASSERT_EQ(static_cast<size_t>(32), hash.size());
}
TEST_F(CldComponentInstallerTest, GetName) {
ASSERT_FALSE(traits.GetName().empty());
}
TEST_F(CldComponentInstallerTest, ComponentReady) {
scoped_ptr<base::DictionaryValue> manifest;
const base::FilePath install_dir(FILE_PATH_LITERAL("/foo"));
const base::Version version("1.2.3.4");
traits.ComponentReady(version, install_dir, manifest.Pass());
base::FilePath result = component_updater::GetLatestCldDataFile();
ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(),
install_dir.AsUTF16Unsafe(),
true));
ASSERT_TRUE(EndsWith(result.value(), chrome::kCLDDataFilename, true));
}
} // namespace component_updater
......@@ -4,6 +4,8 @@
#include "chrome/browser/translate/translate_tab_helper.h"
#include <vector>
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_split.h"
......@@ -41,7 +43,6 @@
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#endif
#if defined(CLD2_IS_COMPONENT)
......@@ -162,7 +163,7 @@ void TranslateTabHelper::ShowTranslateUI(translate::TranslateStep step,
if (TranslateService::IsTranslateBubbleEnabled()) {
// Bubble UI.
if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) {
// TODO: Move this logic out of UI code.
// TODO(droger): Move this logic out of UI code.
GetLanguageState().SetTranslateEnabled(true);
if (!GetLanguageState().HasLanguageChanged())
return;
......@@ -387,24 +388,26 @@ void TranslateTabHelper::HandleCLDDataRequest() {
{
base::AutoLock lock(s_file_lock_.Get());
if (s_cached_file_)
return; // Already done, duplicate request
return; // Already done, duplicate request
}
base::FilePath path;
#if defined(CLD2_IS_COMPONENT)
if (!component_updater::GetLatestCldDataFile(&path))
base::FilePath path = component_updater::GetLatestCldDataFile();
if (path.empty())
return;
#else
#else // CLD2 data is at a well-known file path
base::FilePath path;
if (!PathService::Get(chrome::DIR_USER_DATA, &path)) {
LOG(WARNING) << "Unable to locate user data directory";
return; // Chrome isn't properly installed.
return; // Chrome isn't properly installed.
}
// If the file exists, we can send an IPC-safe construct back to the
// renderer process immediately.
path = path.Append(chrome::kCLDDataFilename);
#endif
// If the file exists, we can send an IPC-safe construct back to the
// renderer process immediately; otherwise, nothing to do here.
if (!base::PathExists(path))
return;
#endif
// Attempt to open the file for reading.
scoped_ptr<base::File> file(
......@@ -438,7 +441,7 @@ void TranslateTabHelper::HandleCLDDataRequest() {
}
}
}
#endif // defined(CLD2_DYNAMIC_MODE)
#endif // defined(CLD2_DYNAMIC_MODE)
void TranslateTabHelper::InitiateTranslation(const std::string& page_lang,
int attempt) {
......
......@@ -784,6 +784,7 @@
'browser/chromeos/ui/idle_app_name_notification_view_unittest.cc',
'browser/chromeos/version_loader_unittest.cc',
'browser/command_updater_unittest.cc',
'browser/component_updater/test/cld_component_installer_unittest.cc',
'browser/component_updater/test/component_installers_unittest.cc',
'browser/component_updater/test/component_patcher_unittest.cc',
'browser/component_updater/test/component_updater_ping_manager_unittest.cc',
......
......@@ -370,13 +370,11 @@ bool PathProvider(int key, base::FilePath* result) {
cur = cur.Append(kGTalkPluginFileName);
break;
#endif
#if defined(CLD2_IS_COMPONENT)
case chrome::DIR_COMPONENT_CLD2:
if (!PathService::Get(chrome::DIR_USER_DATA, &cur))
return false;
cur = cur.Append(FILE_PATH_LITERAL("CLD"));
break;
#endif // defined(CLD2_IS_COMPONENT)
#if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS)
#if defined(WIDEVINE_CDM_IS_COMPONENT)
case chrome::DIR_COMPONENT_WIDEVINE_CDM:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment