1 // Copyright 2013 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 "components/component_updater/test/test_installer.h"
6
7 #include <string>
8
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/values.h"
12
13 namespace component_updater {
14
TestInstaller()15 TestInstaller::TestInstaller() : error_(0), install_count_(0) {
16 }
17
OnUpdateError(int error)18 void TestInstaller::OnUpdateError(int error) {
19 error_ = error;
20 }
21
Install(const base::DictionaryValue & manifest,const base::FilePath & unpack_path)22 bool TestInstaller::Install(const base::DictionaryValue& manifest,
23 const base::FilePath& unpack_path) {
24 ++install_count_;
25 return base::DeleteFile(unpack_path, true);
26 }
27
GetInstalledFile(const std::string & file,base::FilePath * installed_file)28 bool TestInstaller::GetInstalledFile(const std::string& file,
29 base::FilePath* installed_file) {
30 return false;
31 }
32
error() const33 int TestInstaller::error() const {
34 return error_;
35 }
36
install_count() const37 int TestInstaller::install_count() const {
38 return install_count_;
39 }
40
ReadOnlyTestInstaller(const base::FilePath & install_dir)41 ReadOnlyTestInstaller::ReadOnlyTestInstaller(const base::FilePath& install_dir)
42 : install_directory_(install_dir) {
43 }
44
~ReadOnlyTestInstaller()45 ReadOnlyTestInstaller::~ReadOnlyTestInstaller() {
46 }
47
GetInstalledFile(const std::string & file,base::FilePath * installed_file)48 bool ReadOnlyTestInstaller::GetInstalledFile(const std::string& file,
49 base::FilePath* installed_file) {
50 *installed_file = install_directory_.AppendASCII(file);
51 return true;
52 }
53
VersionedTestInstaller()54 VersionedTestInstaller::VersionedTestInstaller() {
55 base::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &install_directory_);
56 }
57
~VersionedTestInstaller()58 VersionedTestInstaller::~VersionedTestInstaller() {
59 base::DeleteFile(install_directory_, true);
60 }
61
Install(const base::DictionaryValue & manifest,const base::FilePath & unpack_path)62 bool VersionedTestInstaller::Install(const base::DictionaryValue& manifest,
63 const base::FilePath& unpack_path) {
64 std::string version_string;
65 manifest.GetStringASCII("version", &version_string);
66 Version version(version_string.c_str());
67
68 base::FilePath path;
69 path = install_directory_.AppendASCII(version.GetString());
70 base::CreateDirectory(path.DirName());
71 if (!base::Move(unpack_path, path))
72 return false;
73 current_version_ = version;
74 ++install_count_;
75 return true;
76 }
77
GetInstalledFile(const std::string & file,base::FilePath * installed_file)78 bool VersionedTestInstaller::GetInstalledFile(const std::string& file,
79 base::FilePath* installed_file) {
80 base::FilePath path;
81 path = install_directory_.AppendASCII(current_version_.GetString());
82 *installed_file = path.Append(base::FilePath::FromUTF8Unsafe(file));
83 return true;
84 }
85
86 } // namespace component_updater
87