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