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