OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "base/run_loop.h" |
7 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
8 #include "chrome/browser/extensions/activity_log/activity_log.h" | 10 #include "chrome/browser/extensions/activity_log/activity_log.h" |
9 #include "chrome/browser/extensions/activity_log/dom_actions.h" | 11 #include "chrome/browser/extensions/activity_log/dom_actions.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/test_extension_system.h" | 13 #include "chrome/browser/extensions/test_extension_system.h" |
12 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
13 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/common/extensions/dom_action_types.h" | 16 #include "chrome/common/extensions/dom_action_types.h" |
15 #include "chrome/common/extensions/extension_builder.h" | 17 #include "chrome/common/extensions/extension_builder.h" |
16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
17 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
20 #include "sql/statement.h" | 21 #include "sql/statement.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 #if defined(OS_CHROMEOS) | 24 #if defined(OS_CHROMEOS) |
24 #include "chrome/browser/chromeos/login/user_manager.h" | 25 #include "chrome/browser/chromeos/login/user_manager.h" |
25 #include "chrome/browser/chromeos/settings/cros_settings.h" | 26 #include "chrome/browser/chromeos/settings/cros_settings.h" |
26 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 27 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
27 #endif | 28 #endif |
28 | 29 |
| 30 namespace { |
| 31 |
| 32 const char kExtensionId[] = "abc"; |
| 33 |
| 34 } // namespace |
| 35 |
29 namespace extensions { | 36 namespace extensions { |
30 | 37 |
31 class ActivityLogTest : public ChromeRenderViewHostTestHarness { | 38 class ActivityLogTest : public testing::Test { |
32 public: | 39 public: |
33 ActivityLogTest() | 40 ActivityLogTest() |
34 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()), | 41 : message_loop_(base::MessageLoop::TYPE_IO), |
35 db_thread_(BrowserThread::DB, base::MessageLoop::current()), | 42 ui_thread_(BrowserThread::UI, &message_loop_), |
36 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {} | 43 db_thread_(BrowserThread::DB, &message_loop_), |
| 44 file_thread_(BrowserThread::FILE, &message_loop_), |
| 45 io_thread_(BrowserThread::IO, &message_loop_) {} |
37 | 46 |
38 virtual void SetUp() OVERRIDE { | 47 virtual void SetUp() OVERRIDE { |
39 ChromeRenderViewHostTestHarness::SetUp(); | |
40 CommandLine command_line(CommandLine::NO_PROGRAM); | 48 CommandLine command_line(CommandLine::NO_PROGRAM); |
41 profile_ = | 49 profile_.reset(new TestingProfile()); |
42 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
43 extension_service_ = static_cast<TestExtensionSystem*>( | |
44 ExtensionSystem::Get(profile_))->CreateExtensionService( | |
45 &command_line, base::FilePath(), false); | |
46 CommandLine::ForCurrentProcess()->AppendSwitch( | 50 CommandLine::ForCurrentProcess()->AppendSwitch( |
47 switches::kEnableExtensionActivityLogging); | 51 switches::kEnableExtensionActivityLogging); |
48 CommandLine::ForCurrentProcess()->AppendSwitch( | 52 CommandLine::ForCurrentProcess()->AppendSwitch( |
49 switches::kEnableExtensionActivityLogTesting); | 53 switches::kEnableExtensionActivityLogTesting); |
50 ActivityLog::RecomputeLoggingIsEnabled(); | 54 ActivityLog::RecomputeLoggingIsEnabled(); |
| 55 extension_service_ = static_cast<TestExtensionSystem*>( |
| 56 ExtensionSystem::Get(profile_.get()))->CreateExtensionService( |
| 57 &command_line, base::FilePath(), false); |
51 } | 58 } |
52 | 59 |
53 virtual ~ActivityLogTest() { | 60 virtual void TearDown() OVERRIDE { |
54 base::MessageLoop::current()->PostTask(FROM_HERE, | 61 base::RunLoop().RunUntilIdle(); |
55 base::MessageLoop::QuitClosure()); | 62 profile_.reset(NULL); |
56 base::MessageLoop::current()->Run(); | 63 base::MessageLoop::current()->PostDelayedTask( |
| 64 FROM_HERE, |
| 65 base::MessageLoop::QuitClosure(), |
| 66 base::TimeDelta::FromSeconds(4)); // Don't hang on failure. |
| 67 base::RunLoop().RunUntilIdle(); |
57 } | 68 } |
58 | 69 |
59 static void RetrieveActions_LogAndFetchActions( | 70 static void RetrieveActions_LogAndFetchActions( |
60 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { | 71 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { |
61 ASSERT_EQ(2, static_cast<int>(i->size())); | 72 ASSERT_EQ(2, static_cast<int>(i->size())); |
62 } | 73 } |
63 | 74 |
64 static void Arguments_Missing( | 75 static void Arguments_Missing( |
65 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { | 76 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { |
66 scoped_refptr<Action> last = i->front(); | 77 scoped_refptr<Action> last = i->front(); |
67 std::string noargs = "ID: odlameecjipmbmbejkplpemijjgpljce, CATEGORY: " | 78 std::string id(kExtensionId); |
| 79 std::string noargs = "ID: " + id + ", CATEGORY: " |
68 "CALL, API: tabs.testMethod, ARGS: "; | 80 "CALL, API: tabs.testMethod, ARGS: "; |
69 ASSERT_EQ(noargs, last->PrintForDebug()); | 81 ASSERT_EQ(noargs, last->PrintForDebug()); |
70 } | 82 } |
71 | 83 |
72 static void Arguments_Present( | 84 static void Arguments_Present( |
73 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { | 85 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { |
74 scoped_refptr<Action> last = i->front(); | 86 scoped_refptr<Action> last = i->front(); |
75 std::string args = "ID: odlameecjipmbmbejkplpemijjgpljce, CATEGORY: " | 87 std::string id(kExtensionId); |
| 88 std::string args = "ID: " + id + ", CATEGORY: " |
76 "CALL, API: extension.connect, ARGS: \"hello\", \"world\""; | 89 "CALL, API: extension.connect, ARGS: \"hello\", \"world\""; |
77 ASSERT_EQ(args, last->PrintForDebug()); | 90 ASSERT_EQ(args, last->PrintForDebug()); |
78 } | 91 } |
79 | 92 |
80 protected: | 93 protected: |
| 94 scoped_ptr<TestingProfile> profile_; |
81 ExtensionService* extension_service_; | 95 ExtensionService* extension_service_; |
82 Profile* profile_; | |
83 | 96 |
84 private: | 97 private: |
| 98 base::MessageLoop message_loop_; |
85 content::TestBrowserThread ui_thread_; | 99 content::TestBrowserThread ui_thread_; |
86 content::TestBrowserThread db_thread_; | 100 content::TestBrowserThread db_thread_; |
87 content::TestBrowserThread file_thread_; | 101 content::TestBrowserThread file_thread_; |
| 102 content::TestBrowserThread io_thread_; |
88 | 103 |
89 #if defined OS_CHROMEOS | 104 #if defined OS_CHROMEOS |
90 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 105 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
91 chromeos::ScopedTestCrosSettings test_cros_settings_; | 106 chromeos::ScopedTestCrosSettings test_cros_settings_; |
92 chromeos::ScopedTestUserManager test_user_manager_; | 107 chromeos::ScopedTestUserManager test_user_manager_; |
93 #endif | 108 #endif |
94 }; | 109 }; |
95 | 110 |
96 TEST_F(ActivityLogTest, Enabled) { | 111 TEST_F(ActivityLogTest, Enabled) { |
97 ASSERT_TRUE(ActivityLog::IsLogEnabled()); | 112 ASSERT_TRUE(ActivityLog::IsLogEnabled()); |
98 } | 113 } |
99 | 114 |
100 TEST_F(ActivityLogTest, Construct) { | 115 TEST_F(ActivityLogTest, Construct) { |
101 ActivityLog* activity_log = ActivityLog::GetInstance(profile_); | |
102 scoped_refptr<const Extension> extension = | 116 scoped_refptr<const Extension> extension = |
103 ExtensionBuilder() | 117 ExtensionBuilder() |
104 .SetManifest(DictionaryBuilder() | 118 .SetManifest(DictionaryBuilder() |
105 .Set("name", "Test extension") | 119 .Set("name", "Test extension") |
106 .Set("version", "1.0.0") | 120 .Set("version", "1.0.0") |
107 .Set("manifest_version", 2)) | 121 .Set("manifest_version", 2)) |
108 .Build(); | 122 .Build(); |
109 extension_service_->AddExtension(extension); | 123 extension_service_->AddExtension(extension); |
| 124 ActivityLog* activity_log = ActivityLog::GetInstance(profile_.get()); |
110 scoped_ptr<ListValue> args(new ListValue()); | 125 scoped_ptr<ListValue> args(new ListValue()); |
111 ASSERT_TRUE(ActivityLog::IsLogEnabled()); | 126 ASSERT_TRUE(ActivityLog::IsLogEnabled()); |
112 activity_log->LogAPIAction( | 127 activity_log->LogAPIAction( |
113 extension, std::string("tabs.testMethod"), args.get(), std::string()); | 128 kExtensionId, std::string("tabs.testMethod"), args.get(), std::string()); |
114 } | 129 } |
115 | 130 |
116 TEST_F(ActivityLogTest, LogAndFetchActions) { | 131 TEST_F(ActivityLogTest, LogAndFetchActions) { |
117 ActivityLog* activity_log = ActivityLog::GetInstance(profile_); | 132 ActivityLog* activity_log = ActivityLog::GetInstance(profile_.get()); |
118 scoped_refptr<const Extension> extension = | |
119 ExtensionBuilder() | |
120 .SetManifest(DictionaryBuilder() | |
121 .Set("name", "Test extension") | |
122 .Set("version", "1.0.0") | |
123 .Set("manifest_version", 2)) | |
124 .Build(); | |
125 extension_service_->AddExtension(extension); | |
126 scoped_ptr<ListValue> args(new ListValue()); | 133 scoped_ptr<ListValue> args(new ListValue()); |
127 ASSERT_TRUE(ActivityLog::IsLogEnabled()); | 134 ASSERT_TRUE(ActivityLog::IsLogEnabled()); |
128 | 135 |
129 // Write some API calls | 136 // Write some API calls |
130 activity_log->LogAPIAction( | 137 activity_log->LogAPIAction( |
131 extension, std::string("tabs.testMethod"), args.get(), std::string()); | 138 kExtensionId, std::string("tabs.testMethod"), args.get(), std::string()); |
132 activity_log->LogDOMAction(extension, | 139 activity_log->LogDOMAction(kExtensionId, |
133 GURL("http://www.google.com"), | 140 GURL("http://www.google.com"), |
134 string16(), | 141 string16(), |
135 std::string("document.write"), | 142 std::string("document.write"), |
136 args.get(), | 143 args.get(), |
137 DomActionType::METHOD, | 144 DomActionType::METHOD, |
138 std::string("extra")); | 145 std::string("extra")); |
139 activity_log->GetActions( | 146 activity_log->GetActions( |
140 extension->id(), | 147 kExtensionId, |
141 0, | 148 0, |
142 base::Bind(ActivityLogTest::RetrieveActions_LogAndFetchActions)); | 149 base::Bind(ActivityLogTest::RetrieveActions_LogAndFetchActions)); |
143 } | 150 } |
144 | 151 |
145 TEST_F(ActivityLogTest, LogWithoutArguments) { | 152 TEST_F(ActivityLogTest, LogWithoutArguments) { |
146 ActivityLog* activity_log = ActivityLog::GetInstance(profile_); | 153 ActivityLog* activity_log = ActivityLog::GetInstance(profile_.get()); |
147 activity_log->SetArgumentLoggingForTesting(false); | 154 activity_log->SetArgumentLoggingForTesting(false); |
148 scoped_refptr<const Extension> extension = | |
149 ExtensionBuilder() | |
150 .SetManifest(DictionaryBuilder() | |
151 .Set("name", "Test extension") | |
152 .Set("version", "1.0.0") | |
153 .Set("manifest_version", 2)) | |
154 .Build(); | |
155 extension_service_->AddExtension(extension); | |
156 ASSERT_TRUE(ActivityLog::IsLogEnabled()); | 155 ASSERT_TRUE(ActivityLog::IsLogEnabled()); |
157 | 156 |
158 scoped_ptr<ListValue> args(new ListValue()); | 157 scoped_ptr<ListValue> args(new ListValue()); |
159 args->Set(0, new base::StringValue("hello")); | 158 args->Set(0, new base::StringValue("hello")); |
160 args->Set(1, new base::StringValue("world")); | 159 args->Set(1, new base::StringValue("world")); |
161 activity_log->LogAPIAction( | 160 activity_log->LogAPIAction( |
162 extension, std::string("tabs.testMethod"), args.get(), std::string()); | 161 kExtensionId, std::string("tabs.testMethod"), args.get(), std::string()); |
163 activity_log->GetActions( | 162 activity_log->GetActions( |
164 extension->id(), 0, base::Bind(ActivityLogTest::Arguments_Missing)); | 163 kExtensionId, 0, base::Bind(ActivityLogTest::Arguments_Missing)); |
165 } | 164 } |
166 | 165 |
167 TEST_F(ActivityLogTest, LogWithArguments) { | 166 TEST_F(ActivityLogTest, LogWithArguments) { |
168 ActivityLog* activity_log = ActivityLog::GetInstance(profile_); | |
169 scoped_refptr<const Extension> extension = | 167 scoped_refptr<const Extension> extension = |
170 ExtensionBuilder() | 168 ExtensionBuilder() |
171 .SetManifest(DictionaryBuilder() | 169 .SetManifest(DictionaryBuilder() |
172 .Set("name", "Test extension") | 170 .Set("name", "Test extension") |
173 .Set("version", "1.0.0") | 171 .Set("version", "1.0.0") |
174 .Set("manifest_version", 2)) | 172 .Set("manifest_version", 2)) |
175 .Build(); | 173 .Build(); |
176 extension_service_->AddExtension(extension); | 174 extension_service_->AddExtension(extension); |
| 175 ActivityLog* activity_log = ActivityLog::GetInstance(profile_.get()); |
177 ASSERT_TRUE(ActivityLog::IsLogEnabled()); | 176 ASSERT_TRUE(ActivityLog::IsLogEnabled()); |
178 | 177 |
179 scoped_ptr<ListValue> args(new ListValue()); | 178 scoped_ptr<ListValue> args(new ListValue()); |
180 args->Set(0, new base::StringValue("hello")); | 179 args->Set(0, new base::StringValue("hello")); |
181 args->Set(1, new base::StringValue("world")); | 180 args->Set(1, new base::StringValue("world")); |
182 activity_log->LogAPIAction( | 181 activity_log->LogAPIAction(kExtensionId, |
183 extension, std::string("extension.connect"), args.get(), std::string()); | 182 std::string("extension.connect"), |
| 183 args.get(), |
| 184 std::string()); |
184 activity_log->GetActions( | 185 activity_log->GetActions( |
185 extension->id(), 0, base::Bind(ActivityLogTest::Arguments_Present)); | 186 kExtensionId, 0, base::Bind(ActivityLogTest::Arguments_Present)); |
186 } | 187 } |
187 | 188 |
188 } // namespace extensions | 189 } // namespace extensions |
189 | 190 |
OLD | NEW |