sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1 | // Copyright 2015 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 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 5 | #include "components/component_updater/component_updater_service.h" |
| 6 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 7 | #include <limits> |
Sorin Jianu | 990ee14 | 2017-06-02 22:34:08 | [diff] [blame^] | 8 | #include <memory> |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/bind.h" |
| 13 | #include "base/bind_helpers.h" |
| 14 | #include "base/files/file_path.h" |
| 15 | #include "base/files/file_util.h" |
| 16 | #include "base/macros.h" |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 18 | #include "base/memory/ref_counted.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 19 | #include "base/message_loop/message_loop.h" |
| 20 | #include "base/run_loop.h" |
fdoray | dc4d994 | 2017-05-12 20:13:50 | [diff] [blame] | 21 | #include "base/task_scheduler/post_task.h" |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 22 | #include "base/test/histogram_tester.h" |
fdoray | dc4d994 | 2017-05-12 20:13:50 | [diff] [blame] | 23 | #include "base/test/scoped_task_environment.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 24 | #include "base/threading/thread_task_runner_handle.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 25 | #include "base/values.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 26 | #include "components/component_updater/component_updater_service_internal.h" |
| 27 | #include "components/update_client/test_configurator.h" |
| 28 | #include "components/update_client/test_installer.h" |
| 29 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 30 | #include "components/update_client/update_client_errors.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 31 | #include "testing/gmock/include/gmock/gmock.h" |
| 32 | #include "testing/gtest/include/gtest/gtest.h" |
| 33 | |
| 34 | using Configurator = update_client::Configurator; |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 35 | using Result = update_client::CrxInstaller::Result; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 36 | using TestConfigurator = update_client::TestConfigurator; |
| 37 | using UpdateClient = update_client::UpdateClient; |
| 38 | |
| 39 | using ::testing::_; |
| 40 | using ::testing::AnyNumber; |
| 41 | using ::testing::Invoke; |
| 42 | using ::testing::Mock; |
| 43 | using ::testing::Return; |
| 44 | |
| 45 | namespace component_updater { |
| 46 | |
| 47 | class MockInstaller : public CrxInstaller { |
| 48 | public: |
| 49 | MockInstaller(); |
| 50 | |
Sorin Jianu | 990ee14 | 2017-06-02 22:34:08 | [diff] [blame^] | 51 | // gMock does not support mocking functions with parameters which have |
| 52 | // move semantics. This function is a shim to work around it. |
| 53 | Result Install(std::unique_ptr<base::DictionaryValue> manifest, |
| 54 | const base::FilePath& unpack_path) { |
| 55 | return Install_(manifest, unpack_path); |
| 56 | } |
| 57 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 58 | MOCK_METHOD1(OnUpdateError, void(int error)); |
Sorin Jianu | 990ee14 | 2017-06-02 22:34:08 | [diff] [blame^] | 59 | MOCK_METHOD2(Install_, |
| 60 | Result(const std::unique_ptr<base::DictionaryValue>& manifest, |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 61 | const base::FilePath& unpack_path)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 62 | MOCK_METHOD2(GetInstalledFile, |
| 63 | bool(const std::string& file, base::FilePath* installed_file)); |
| 64 | MOCK_METHOD0(Uninstall, bool()); |
| 65 | |
| 66 | private: |
| 67 | ~MockInstaller() override; |
| 68 | }; |
| 69 | |
| 70 | class MockUpdateClient : public UpdateClient { |
| 71 | public: |
| 72 | MockUpdateClient(); |
| 73 | MOCK_METHOD1(AddObserver, void(Observer* observer)); |
| 74 | MOCK_METHOD1(RemoveObserver, void(Observer* observer)); |
| 75 | MOCK_METHOD3(Install, |
| 76 | void(const std::string& id, |
| 77 | const CrxDataCallback& crx_data_callback, |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 78 | const Callback& callback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 79 | MOCK_METHOD3(Update, |
| 80 | void(const std::vector<std::string>& ids, |
| 81 | const CrxDataCallback& crx_data_callback, |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 82 | const Callback& callback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 83 | MOCK_CONST_METHOD2(GetCrxUpdateState, |
| 84 | bool(const std::string& id, CrxUpdateItem* update_item)); |
| 85 | MOCK_CONST_METHOD1(IsUpdating, bool(const std::string& id)); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 86 | MOCK_METHOD0(Stop, void()); |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 87 | MOCK_METHOD4(SendUninstallPing, |
| 88 | void(const std::string& id, |
| 89 | const base::Version& version, |
| 90 | int reason, |
| 91 | const Callback& callback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 92 | |
| 93 | private: |
| 94 | ~MockUpdateClient() override; |
| 95 | }; |
| 96 | |
| 97 | class MockServiceObserver : public ServiceObserver { |
| 98 | public: |
| 99 | MockServiceObserver(); |
| 100 | ~MockServiceObserver() override; |
| 101 | |
| 102 | MOCK_METHOD2(OnEvent, void(Events event, const std::string&)); |
| 103 | }; |
| 104 | |
| 105 | class ComponentUpdaterTest : public testing::Test { |
| 106 | public: |
| 107 | ComponentUpdaterTest(); |
| 108 | ~ComponentUpdaterTest() override; |
| 109 | |
| 110 | void SetUp() override; |
| 111 | |
| 112 | void TearDown() override; |
| 113 | |
| 114 | // Makes the full path to a component updater test file. |
| 115 | const base::FilePath test_file(const char* file); |
| 116 | |
| 117 | MockUpdateClient& update_client() { return *update_client_; } |
| 118 | ComponentUpdateService& component_updater() { return *component_updater_; } |
| 119 | scoped_refptr<TestConfigurator> configurator() const { return config_; } |
| 120 | base::Closure quit_closure() const { return quit_closure_; } |
| 121 | static void ReadyCallback() {} |
| 122 | |
| 123 | protected: |
| 124 | void RunThreads(); |
| 125 | |
| 126 | private: |
fdoray | dc4d994 | 2017-05-12 20:13:50 | [diff] [blame] | 127 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 128 | base::RunLoop runloop_; |
| 129 | base::Closure quit_closure_; |
| 130 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 131 | scoped_refptr<TestConfigurator> config_; |
| 132 | scoped_refptr<MockUpdateClient> update_client_; |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 133 | std::unique_ptr<ComponentUpdateService> component_updater_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 134 | |
| 135 | DISALLOW_COPY_AND_ASSIGN(ComponentUpdaterTest); |
| 136 | }; |
| 137 | |
| 138 | class OnDemandTester { |
| 139 | public: |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 140 | void OnDemand(ComponentUpdateService* cus, const std::string& id); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 141 | update_client::Error error() const { return error_; } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 142 | |
| 143 | private: |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 144 | void OnDemandComplete(update_client::Error error); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 145 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 146 | update_client::Error error_ = update_client::Error::NONE; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | MockInstaller::MockInstaller() { |
| 150 | } |
| 151 | |
| 152 | MockInstaller::~MockInstaller() { |
| 153 | } |
| 154 | |
| 155 | MockUpdateClient::MockUpdateClient() { |
| 156 | } |
| 157 | |
| 158 | MockUpdateClient::~MockUpdateClient() { |
| 159 | } |
| 160 | |
| 161 | MockServiceObserver::MockServiceObserver() { |
| 162 | } |
| 163 | |
| 164 | MockServiceObserver::~MockServiceObserver() { |
| 165 | } |
| 166 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 167 | void OnDemandTester::OnDemand(ComponentUpdateService* cus, |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 168 | const std::string& id) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 169 | cus->GetOnDemandUpdater().OnDemandUpdate( |
| 170 | id, |
| 171 | base::Bind(&OnDemandTester::OnDemandComplete, base::Unretained(this))); |
| 172 | } |
| 173 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 174 | void OnDemandTester::OnDemandComplete(update_client::Error error) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 175 | error_ = error; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 176 | } |
| 177 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 178 | std::unique_ptr<ComponentUpdateService> TestComponentUpdateServiceFactory( |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 179 | const scoped_refptr<Configurator>& config) { |
| 180 | DCHECK(config); |
ricea | 85ec5795 | 2016-08-31 09:34:10 | [diff] [blame] | 181 | return base::MakeUnique<CrxUpdateService>(config, new MockUpdateClient()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | ComponentUpdaterTest::ComponentUpdaterTest() |
fdoray | dc4d994 | 2017-05-12 20:13:50 | [diff] [blame] | 185 | : scoped_task_environment_( |
| 186 | base::test::ScopedTaskEnvironment::MainThreadType::UI) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 187 | quit_closure_ = runloop_.QuitClosure(); |
| 188 | |
| 189 | config_ = new TestConfigurator( |
fdoray | dc4d994 | 2017-05-12 20:13:50 | [diff] [blame] | 190 | base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()}), |
| 191 | base::ThreadTaskRunnerHandle::Get()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 192 | |
| 193 | update_client_ = new MockUpdateClient(); |
| 194 | EXPECT_CALL(update_client(), AddObserver(_)).Times(1); |
| 195 | component_updater_.reset(new CrxUpdateService(config_, update_client_)); |
| 196 | } |
| 197 | |
| 198 | ComponentUpdaterTest::~ComponentUpdaterTest() { |
| 199 | EXPECT_CALL(update_client(), RemoveObserver(_)).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 200 | component_updater_.reset(); |
| 201 | } |
| 202 | |
| 203 | void ComponentUpdaterTest::SetUp() { |
| 204 | } |
| 205 | |
| 206 | void ComponentUpdaterTest::TearDown() { |
| 207 | } |
| 208 | |
| 209 | void ComponentUpdaterTest::RunThreads() { |
| 210 | runloop_.Run(); |
| 211 | } |
| 212 | |
| 213 | TEST_F(ComponentUpdaterTest, AddObserver) { |
| 214 | MockServiceObserver observer; |
| 215 | EXPECT_CALL(update_client(), AddObserver(&observer)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 216 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 217 | component_updater().AddObserver(&observer); |
| 218 | } |
| 219 | |
| 220 | TEST_F(ComponentUpdaterTest, RemoveObserver) { |
| 221 | MockServiceObserver observer; |
| 222 | EXPECT_CALL(update_client(), RemoveObserver(&observer)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 223 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 224 | component_updater().RemoveObserver(&observer); |
| 225 | } |
| 226 | |
| 227 | // Tests that UpdateClient::Update is called by the timer loop when |
| 228 | // components are registered, and the component update starts. |
| 229 | // Also tests that Uninstall is called when a component is unregistered. |
| 230 | TEST_F(ComponentUpdaterTest, RegisterComponent) { |
| 231 | class LoopHandler { |
| 232 | public: |
| 233 | LoopHandler(int max_cnt, const base::Closure& quit_closure) |
| 234 | : max_cnt_(max_cnt), quit_closure_(quit_closure) {} |
| 235 | |
| 236 | void OnUpdate(const std::vector<std::string>& ids, |
| 237 | const UpdateClient::CrxDataCallback& crx_data_callback, |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 238 | const Callback& callback) { |
| 239 | callback.Run(update_client::Error::NONE); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 240 | static int cnt = 0; |
| 241 | ++cnt; |
| 242 | if (cnt >= max_cnt_) |
| 243 | quit_closure_.Run(); |
| 244 | } |
| 245 | |
| 246 | private: |
| 247 | const int max_cnt_; |
| 248 | base::Closure quit_closure_; |
| 249 | }; |
| 250 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 251 | base::HistogramTester ht; |
| 252 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 253 | scoped_refptr<MockInstaller> installer(new MockInstaller()); |
| 254 | EXPECT_CALL(*installer, Uninstall()).WillOnce(Return(true)); |
| 255 | |
| 256 | using update_client::jebg_hash; |
| 257 | using update_client::abag_hash; |
| 258 | |
| 259 | const std::string id1 = "abagagagagagagagagagagagagagagag"; |
| 260 | const std::string id2 = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 261 | std::vector<std::string> ids; |
| 262 | ids.push_back(id1); |
| 263 | ids.push_back(id2); |
| 264 | |
| 265 | CrxComponent crx_component1; |
| 266 | crx_component1.pk_hash.assign(abag_hash, abag_hash + arraysize(abag_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 267 | crx_component1.version = base::Version("1.0"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 268 | crx_component1.installer = installer; |
| 269 | |
| 270 | CrxComponent crx_component2; |
| 271 | crx_component2.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 272 | crx_component2.version = base::Version("0.9"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 273 | crx_component2.installer = installer; |
| 274 | |
| 275 | // Quit after two update checks have fired. |
| 276 | LoopHandler loop_handler(2, quit_closure()); |
| 277 | EXPECT_CALL(update_client(), Update(ids, _, _)) |
| 278 | .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 279 | |
| 280 | EXPECT_CALL(update_client(), IsUpdating(id1)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 281 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 282 | |
| 283 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component1)); |
| 284 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component2)); |
| 285 | |
| 286 | RunThreads(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 287 | EXPECT_TRUE(component_updater().UnregisterComponent(id1)); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 288 | |
| 289 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 1, 2); |
| 290 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2); |
| 291 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Tests that on-demand updates invoke UpdateClient::Install. |
| 295 | TEST_F(ComponentUpdaterTest, OnDemandUpdate) { |
| 296 | class LoopHandler { |
| 297 | public: |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 298 | explicit LoopHandler(int max_cnt) : max_cnt_(max_cnt) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 299 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 300 | void OnInstall(const std::string& ids, |
| 301 | const UpdateClient::CrxDataCallback& crx_data_callback, |
| 302 | const Callback& callback) { |
| 303 | callback.Run(update_client::Error::NONE); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 304 | static int cnt = 0; |
| 305 | ++cnt; |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 306 | if (cnt >= max_cnt_) { |
| 307 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 308 | FROM_HERE, base::Bind(&LoopHandler::Quit, base::Unretained(this))); |
| 309 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | private: |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 313 | void Quit() { base::MessageLoop::current()->QuitWhenIdle(); } |
| 314 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 315 | const int max_cnt_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 316 | }; |
| 317 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 318 | base::HistogramTester ht; |
| 319 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 320 | auto config = configurator(); |
| 321 | config->SetInitialDelay(3600); |
| 322 | |
| 323 | auto& cus = component_updater(); |
| 324 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 325 | // Tests calling OnDemand for an unregistered component. This call results in |
| 326 | // an error, which is recorded by the OnDemandTester instance. Since the |
| 327 | // component was not registered, the call is ignored for UMA metrics. |
| 328 | OnDemandTester ondemand_tester_component_not_registered; |
| 329 | ondemand_tester_component_not_registered.OnDemand( |
| 330 | &cus, "ihfokbkgjpifnbbojhneepfflplebdkc"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 331 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 332 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 333 | |
| 334 | using update_client::jebg_hash; |
| 335 | CrxComponent crx_component; |
| 336 | crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 337 | crx_component.version = base::Version("0.9"); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 338 | crx_component.installer = new MockInstaller(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 339 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 340 | LoopHandler loop_handler(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 341 | EXPECT_CALL(update_client(), |
| 342 | Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) |
| 343 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 344 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 345 | |
| 346 | EXPECT_TRUE(cus.RegisterComponent(crx_component)); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 347 | OnDemandTester ondemand_tester; |
| 348 | ondemand_tester.OnDemand(&cus, id); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 349 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 350 | base::RunLoop().Run(); |
| 351 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 352 | EXPECT_EQ(update_client::Error::INVALID_ARGUMENT, |
| 353 | ondemand_tester_component_not_registered.error()); |
| 354 | EXPECT_EQ(update_client::Error::NONE, ondemand_tester.error()); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 355 | |
| 356 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); |
| 357 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 358 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Tests that throttling an update invokes UpdateClient::Install. |
| 362 | TEST_F(ComponentUpdaterTest, MaybeThrottle) { |
| 363 | class LoopHandler { |
| 364 | public: |
| 365 | LoopHandler(int max_cnt, const base::Closure& quit_closure) |
| 366 | : max_cnt_(max_cnt), quit_closure_(quit_closure) {} |
| 367 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 368 | void OnInstall(const std::string& ids, |
| 369 | const UpdateClient::CrxDataCallback& crx_data_callback, |
| 370 | const Callback& callback) { |
| 371 | callback.Run(update_client::Error::NONE); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 372 | static int cnt = 0; |
| 373 | ++cnt; |
| 374 | if (cnt >= max_cnt_) |
| 375 | quit_closure_.Run(); |
| 376 | } |
| 377 | |
| 378 | private: |
| 379 | const int max_cnt_; |
| 380 | base::Closure quit_closure_; |
| 381 | }; |
| 382 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 383 | base::HistogramTester ht; |
| 384 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 385 | auto config = configurator(); |
| 386 | config->SetInitialDelay(3600); |
| 387 | |
| 388 | scoped_refptr<MockInstaller> installer(new MockInstaller()); |
| 389 | |
| 390 | using update_client::jebg_hash; |
| 391 | CrxComponent crx_component; |
| 392 | crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 393 | crx_component.version = base::Version("0.9"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 394 | crx_component.installer = installer; |
| 395 | |
| 396 | LoopHandler loop_handler(1, quit_closure()); |
| 397 | EXPECT_CALL(update_client(), |
| 398 | Install("jebgalgnebhfojomionfpkfelancnnkf", _, _)) |
| 399 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 400 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 401 | |
| 402 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component)); |
| 403 | component_updater().MaybeThrottle( |
| 404 | "jebgalgnebhfojomionfpkfelancnnkf", |
| 405 | base::Bind(&ComponentUpdaterTest::ReadyCallback)); |
| 406 | |
| 407 | RunThreads(); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 408 | |
| 409 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); |
| 410 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 411 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | } // namespace component_updater |