summaryrefslogtreecommitdiffstats
path: root/chrome/browser/component_updater/component_updater_service_unittest.cc
blob: 8b470360734ad6b0649952ac4fb8641599b91bda (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
126
// 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 "chrome/browser/component_updater/component_updater_service.h"

#include "base/at_exit.h"
#include "base/compiler_specific.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/component_updater/component_updater_interceptor.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/test/test_notification_tracker.h"
#include "chrome/test/test_url_request_context_getter.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_service.h"
#include "content/common/test_url_fetcher_factory.h"
#include "googleurl/src/gurl.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace {
// Overrides some of the component updater behaviors so it is easier
// to test and loops faster.
class TestConfigurator : public ComponentUpdateService::Configurator {
 public:
  virtual int InitialDelay() OVERRIDE { return 0; }

  virtual int NextCheckDelay() OVERRIDE {
    // Returning 0 makes the component updater to quit the message loop
    // if it does not have pending update task.
    return 0;
  }

  virtual int StepDelay() OVERRIDE { return 0; }

  virtual GURL UpdateUrl() OVERRIDE { return GURL("http://localhost/upd"); }

  virtual size_t UrlSizeLimit() OVERRIDE { return 256; }

  virtual net::URLRequestContextGetter* RequestContext() OVERRIDE {
    return new TestURLRequestContextGetter();
  }

  // Don't use the utility process to decode files.
  virtual bool InProcess() OVERRIDE { return true; }
};

class TestInstaller : public ComponentInstaller {
 public :
  explicit TestInstaller(bool wrong_one)
      : error_(0), install_(0), wrong_one_(wrong_one) {
  }

  virtual void OnUpdateError(int error) OVERRIDE {
    EXPECT_NE(0, error);
    error_ = error;
  }

  virtual bool Install(base::DictionaryValue* manifest,
                       const FilePath& unpack_path) OVERRIDE {
    EXPECT_FALSE(wrong_one_);
    ++install_;
    return file_util::Delete(unpack_path, true);
  }

  int error() const { return error_; }

  int install() const { return install_; }

 private:
  int error_;
  int install_;
  bool wrong_one_;
};

}  // namespace

// Common fixture for all the component updater tests.
class ComponentUpdaterTest : public testing::Test {
 public:
  ComponentUpdaterTest() {
    PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
    test_data_dir_ = test_data_dir_.AppendASCII("components");

    const int notifications[] = {
      chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED,
      chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING,
      chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND,
      chrome::NOTIFICATION_COMPONENT_UPDATE_READY
    };

    for (int ix = 0; ix != arraysize(notifications); ++ix) {
      notification_tracker_.ListenFor(notifications[ix],
                                      NotificationService::AllSources());
    }
    URLFetcher::enable_interception_for_tests(true);
  }

  ~ComponentUpdaterTest() {
    URLFetcher::enable_interception_for_tests(false);
  }

  // Makes the full path to a component updater test file.
  const FilePath test_file(const char* file) {
    return test_data_dir_.AppendASCII(file);
  }

  TestNotificationTracker& notification_tracker() {
    return notification_tracker_;
  }

 private:
  FilePath test_data_dir_;
  TestNotificationTracker notification_tracker_;
  // Note that the shadow |at_exit_manager_| will cause the destruction
  // of the component updater when this object is destroyed.
  base::ShadowingAtExitManager at_exit_manager_;
};

TEST_F(ComponentUpdaterTest, EmptyTest) {
  EXPECT_EQ(0ul, notification_tracker().size());
}