Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: chrome/browser/extensions/external_registry_loader_win_unittest.cc

Issue 2809293004: Fix windows registry loader to carry over prefs from file thread correctly (Closed)
Patch Set: add test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/external_registry_loader_win.h" 5 #include "chrome/browser/extensions/external_registry_loader_win.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/values.h"
11 #include "content/public/test/test_browser_thread_bundle.h" 12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "extensions/common/value_builder.h"
12 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
13 15
14 namespace extensions { 16 namespace extensions {
15 17
16 namespace { 18 namespace {
17 19
20 const char kDummyRegistryKey[] = "dummyId";
21
18 class TestExternalRegistryLoader : public ExternalRegistryLoader { 22 class TestExternalRegistryLoader : public ExternalRegistryLoader {
19 public: 23 public:
20 TestExternalRegistryLoader() {} 24 TestExternalRegistryLoader() {}
21 25
22 using ExternalRegistryLoader::StartLoading; 26 using ExternalRegistryLoader::StartLoading;
23 27
24 void WaitForTwoLoadsToFinished() { 28 void WaitForTwoLoadsToFinished() {
25 // Run() returns immediately if Quit() has already been called. 29 // Run() returns immediately if Quit() has already been called.
26 run_loop_.Run(); 30 run_loop_.Run();
27 } 31 }
28 32
33 std::vector<int> GetPrefsTestIds() { return prefs_test_ids_; }
34
29 private: 35 private:
30 ~TestExternalRegistryLoader() override {} 36 ~TestExternalRegistryLoader() override {}
31 37
38 std::unique_ptr<base::DictionaryValue> LoadPrefsOnFileThread() override {
39 return DictionaryBuilder().Set(kDummyRegistryKey, id_++).Build();
40 }
32 void LoadFinished() override { 41 void LoadFinished() override {
33 ExternalRegistryLoader::LoadFinished(); 42 ExternalRegistryLoader::LoadFinished();
34 ++load_finished_count_; 43 ++load_finished_count_;
35 ASSERT_LE(load_finished_count_, 2); 44 ASSERT_LE(load_finished_count_, 2);
45
46 EXPECT_TRUE(prefs_);
Devlin 2017/04/12 19:32:06 ASSERT_TRUE
lazyboy 2017/04/12 20:56:22 Done.
47 int prefs_test_id = -1;
48 EXPECT_TRUE(prefs_->GetInteger(kDummyRegistryKey, &prefs_test_id));
49 prefs_test_ids_.push_back(prefs_test_id);
50
36 if (load_finished_count_ == 2) 51 if (load_finished_count_ == 2)
37 run_loop_.Quit(); 52 run_loop_.Quit();
38 } 53 }
39 54
40 base::RunLoop run_loop_; 55 base::RunLoop run_loop_;
41 int load_finished_count_ = 0; 56 int load_finished_count_ = 0;
57 int id_ = 0;
58 std::vector<int> prefs_test_ids_;
42 59
43 DISALLOW_COPY_AND_ASSIGN(TestExternalRegistryLoader); 60 DISALLOW_COPY_AND_ASSIGN(TestExternalRegistryLoader);
44 }; 61 };
45 62
46 } // namespace 63 } // namespace
47 64
48 class ExternalRegistryLoaderUnittest : public testing::Test { 65 class ExternalRegistryLoaderUnittest : public testing::Test {
49 public: 66 public:
50 ExternalRegistryLoaderUnittest() 67 ExternalRegistryLoaderUnittest()
51 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} 68 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
(...skipping 12 matching lines...) Expand all
64 make_scoped_refptr(new TestExternalRegistryLoader()); 81 make_scoped_refptr(new TestExternalRegistryLoader());
65 82
66 test_loader->StartLoading(); 83 test_loader->StartLoading();
67 test_loader->StartLoading(); 84 test_loader->StartLoading();
68 85
69 test_loader->WaitForTwoLoadsToFinished(); 86 test_loader->WaitForTwoLoadsToFinished();
70 // Let registry watcher code complete. 87 // Let registry watcher code complete.
71 base::RunLoop().RunUntilIdle(); 88 base::RunLoop().RunUntilIdle();
72 } 89 }
73 90
91 // Tests that calling StartLoading() twice does not overwrite previous prefs
92 // before LoadFinished consumes it.
93 // Regression test for https://crbug.com/709304: if two StartLoading() schedules
94 // two LoadPrefsOnFileThread, then the second LoadPrefsOnFileThread could
95 // overwrite the first one's prefs.
96 TEST_F(ExternalRegistryLoaderUnittest, TwoStartLoadingDoesNotOverwritePrefs) {
97 scoped_refptr<TestExternalRegistryLoader> test_loader =
98 make_scoped_refptr(new TestExternalRegistryLoader());
99
100 test_loader->StartLoading();
101 test_loader->StartLoading();
102
103 test_loader->WaitForTwoLoadsToFinished();
104 // Let registry watcher code complete.
105 base::RunLoop().RunUntilIdle();
106
107 std::vector<int> prefs_test_ids = test_loader->GetPrefsTestIds();
108 ASSERT_EQ(2u, prefs_test_ids.size());
109 EXPECT_EQ(0, prefs_test_ids[0]);
110 EXPECT_EQ(1, prefs_test_ids[1]);
111 }
112
74 } // namespace extensions 113 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698