test_installer.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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
13namespace component_updater {
14
15TestInstaller::TestInstaller() : error_(0), install_count_(0) {
16}
17
18void TestInstaller::OnUpdateError(int error) {
19  error_ = error;
20}
21
22bool TestInstaller::Install(const base::DictionaryValue& manifest,
23                            const base::FilePath& unpack_path) {
24  ++install_count_;
25  return base::DeleteFile(unpack_path, true);
26}
27
28bool TestInstaller::GetInstalledFile(const std::string& file,
29                                     base::FilePath* installed_file) {
30  return false;
31}
32
33int TestInstaller::error() const {
34  return error_;
35}
36
37int TestInstaller::install_count() const {
38  return install_count_;
39}
40
41ReadOnlyTestInstaller::ReadOnlyTestInstaller(const base::FilePath& install_dir)
42    : install_directory_(install_dir) {
43}
44
45ReadOnlyTestInstaller::~ReadOnlyTestInstaller() {
46}
47
48bool ReadOnlyTestInstaller::GetInstalledFile(const std::string& file,
49                                             base::FilePath* installed_file) {
50  *installed_file = install_directory_.AppendASCII(file);
51  return true;
52}
53
54VersionedTestInstaller::VersionedTestInstaller() {
55  base::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &install_directory_);
56}
57
58VersionedTestInstaller::~VersionedTestInstaller() {
59  base::DeleteFile(install_directory_, true);
60}
61
62bool 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
78bool 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