OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/extension_user_script_loader.h" | 5 #include "extensions/browser/extension_user_script_loader.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/message_loop/message_loop.h" | |
17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
18 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/threading/thread_task_runner_handle.h" | |
20 #include "chrome/browser/chrome_notification_types.h" | 22 #include "chrome/browser/chrome_notification_types.h" |
21 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
22 #include "content/public/browser/notification_observer.h" | 24 #include "content/public/browser/notification_observer.h" |
23 #include "content/public/browser/notification_registrar.h" | 25 #include "content/public/browser/notification_registrar.h" |
24 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
25 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
26 #include "extensions/common/host_id.h" | 28 #include "extensions/common/host_id.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
28 | 30 |
29 using content::BrowserThread; | |
30 using extensions::URLPatternSet; | 31 using extensions::URLPatternSet; |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 35 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
35 int schemes = URLPattern::SCHEME_ALL; | 36 int schemes = URLPattern::SCHEME_ALL; |
36 extent->AddPattern(URLPattern(schemes, pattern)); | 37 extent->AddPattern(URLPattern(schemes, pattern)); |
37 } | 38 } |
38 } | 39 } |
39 | 40 |
40 namespace extensions { | 41 namespace extensions { |
41 | 42 |
42 // Test bringing up a script loader on a specific directory, putting a script | 43 // Test bringing up a script loader on a specific directory, putting a script |
43 // in there, etc. | 44 // in there, etc. |
44 | 45 |
45 class ExtensionUserScriptLoaderTest : public testing::Test, | 46 class ExtensionUserScriptLoaderTest : public testing::Test, |
46 public content::NotificationObserver { | 47 public content::NotificationObserver { |
47 public: | 48 public: |
48 ExtensionUserScriptLoaderTest() : shared_memory_(NULL) {} | 49 ExtensionUserScriptLoaderTest() |
50 : scoped_task_environment_( | |
51 base::test::ScopedTaskEnvironment::MainThreadType::UI), | |
52 shared_memory_(NULL) {} | |
49 | 53 |
50 void SetUp() override { | 54 void SetUp() override { |
51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
52 | 56 |
53 // Register for all user script notifications. | 57 // Register for all user script notifications. |
54 registrar_.Add(this, | 58 registrar_.Add(this, |
55 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, | 59 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, |
56 content::NotificationService::AllSources()); | 60 content::NotificationService::AllSources()); |
57 | |
58 // ExtensionUserScriptLoader posts tasks to the file thread so make the | |
sky
2017/04/27 22:17:46
Is this the same with ScopedTaskEvironment?
fdoray
2017/05/01 17:44:59
I added a TestBrowserThreadBundle instead of a Sco
sky
2017/05/01 18:07:07
I'm not sure that is the same thing. Could you get
fdoray
2017/05/01 18:15:47
I'll do this part in a separate CL.
| |
59 // current thread look like one. | |
60 file_thread_.reset(new content::TestBrowserThread( | |
61 BrowserThread::FILE, base::MessageLoop::current())); | |
62 ui_thread_.reset(new content::TestBrowserThread( | |
63 BrowserThread::UI, base::MessageLoop::current())); | |
64 } | |
65 | |
66 void TearDown() override { | |
67 file_thread_.reset(); | |
68 ui_thread_.reset(); | |
69 } | 61 } |
70 | 62 |
71 void Observe(int type, | 63 void Observe(int type, |
72 const content::NotificationSource& source, | 64 const content::NotificationSource& source, |
73 const content::NotificationDetails& details) override { | 65 const content::NotificationDetails& details) override { |
74 DCHECK(type == extensions::NOTIFICATION_USER_SCRIPTS_UPDATED); | 66 DCHECK(type == extensions::NOTIFICATION_USER_SCRIPTS_UPDATED); |
75 | 67 |
76 shared_memory_ = content::Details<base::SharedMemory>(details).ptr(); | 68 shared_memory_ = content::Details<base::SharedMemory>(details).ptr(); |
77 if (base::MessageLoop::current() == &message_loop_) | 69 if (main_thread_task_runner_->RunsTasksOnCurrentThread()) |
78 base::MessageLoop::current()->QuitWhenIdle(); | 70 base::MessageLoop::current()->QuitWhenIdle(); |
79 } | 71 } |
80 | 72 |
73 content::TestBrowserThreadBundle test_browser_thread_bundle_; | |
74 | |
75 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_ = | |
76 base::ThreadTaskRunnerHandle::Get(); | |
77 | |
81 // Directory containing user scripts. | 78 // Directory containing user scripts. |
82 base::ScopedTempDir temp_dir_; | 79 base::ScopedTempDir temp_dir_; |
83 | 80 |
84 content::NotificationRegistrar registrar_; | 81 content::NotificationRegistrar registrar_; |
85 | 82 |
86 // MessageLoop used in tests. | |
87 base::MessageLoopForUI message_loop_; | |
88 | |
89 std::unique_ptr<content::TestBrowserThread> file_thread_; | |
90 std::unique_ptr<content::TestBrowserThread> ui_thread_; | |
91 | |
92 // Updated to the script shared memory when we get notified. | 83 // Updated to the script shared memory when we get notified. |
93 base::SharedMemory* shared_memory_; | 84 base::SharedMemory* shared_memory_; |
94 }; | 85 }; |
95 | 86 |
96 // Test that we get notified even when there are no scripts. | 87 // Test that we get notified even when there are no scripts. |
97 TEST_F(ExtensionUserScriptLoaderTest, NoScripts) { | 88 TEST_F(ExtensionUserScriptLoaderTest, NoScripts) { |
98 TestingProfile profile; | 89 TestingProfile profile; |
99 ExtensionUserScriptLoader loader( | 90 ExtensionUserScriptLoader loader( |
100 &profile, | 91 &profile, |
101 HostID(), | 92 HostID(), |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 &profile, | 263 &profile, |
273 HostID(), | 264 HostID(), |
274 true /* listen_for_extension_system_loaded */); | 265 true /* listen_for_extension_system_loaded */); |
275 loader.LoadScriptsForTest(&user_scripts); | 266 loader.LoadScriptsForTest(&user_scripts); |
276 | 267 |
277 EXPECT_EQ(content, | 268 EXPECT_EQ(content, |
278 user_scripts[0]->js_scripts()[0]->GetContent().as_string()); | 269 user_scripts[0]->js_scripts()[0]->GetContent().as_string()); |
279 } | 270 } |
280 | 271 |
281 } // namespace extensions | 272 } // namespace extensions |
OLD | NEW |