Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "base/memory/scoped_ptr.h" | |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/synchronization/waitable_event.h" | |
| 9 #include "chrome/browser/extensions/activity_log/activity_log.h" | |
| 10 #include "chrome/browser/extensions/activity_log/stream_noargs_ui_policy.h" | |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 12 #include "chrome/browser/extensions/test_extension_system.h" | |
| 13 #include "chrome/common/chrome_constants.h" | |
| 14 #include "chrome/common/chrome_switches.h" | |
| 15 #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 "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "sql/statement.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 #if defined(OS_CHROMEOS) | |
| 23 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 24 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 25 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
| 26 #endif | |
| 27 | |
| 28 namespace extensions { | |
| 29 | |
| 30 class StreamWithoutArgsUIPolicyTest : public testing::Test { | |
| 31 public: | |
| 32 StreamWithoutArgsUIPolicyTest() | |
| 33 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | |
| 34 saved_cmdline_(CommandLine::NO_PROGRAM) { | |
| 35 #if defined OS_CHROMEOS | |
| 36 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); | |
| 37 #endif | |
| 38 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 39 saved_cmdline_ = *CommandLine::ForCurrentProcess(); | |
| 40 profile_.reset(new TestingProfile()); | |
| 41 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 42 switches::kEnableExtensionActivityLogging); | |
| 43 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 44 switches::kEnableExtensionActivityLogTesting); | |
| 45 extension_service_ = static_cast<TestExtensionSystem*>( | |
| 46 ExtensionSystem::Get(profile_.get()))->CreateExtensionService | |
| 47 (&command_line, base::FilePath(), false); | |
| 48 } | |
| 49 | |
| 50 virtual ~StreamWithoutArgsUIPolicyTest() { | |
| 51 #if defined OS_CHROMEOS | |
| 52 test_user_manager_.reset(); | |
| 53 #endif | |
| 54 base::RunLoop().RunUntilIdle(); | |
| 55 profile_.reset(NULL); | |
| 56 base::RunLoop().RunUntilIdle(); | |
| 57 // Restore the original command line and undo the affects of SetUp(). | |
| 58 *CommandLine::ForCurrentProcess() = saved_cmdline_; | |
| 59 } | |
| 60 | |
| 61 static void RetrieveActions_LogAndFetchActions( | |
| 62 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { | |
| 63 ASSERT_EQ(2, static_cast<int>(i->size())); | |
| 64 } | |
| 65 | |
| 66 static void Arguments_Missing( | |
| 67 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { | |
| 68 scoped_refptr<Action> last = i->front(); | |
| 69 std::string noargs = "ID: odlameecjipmbmbejkplpemijjgpljce, CATEGORY: " | |
| 70 "CALL, API: tabs.testMethod, ARGS: "; | |
| 71 ASSERT_EQ(noargs, last->PrintForDebug()); | |
| 72 } | |
| 73 | |
| 74 protected: | |
| 75 ExtensionService* extension_service_; | |
| 76 scoped_ptr<TestingProfile> profile_; | |
| 77 content::TestBrowserThreadBundle thread_bundle_; | |
| 78 // Used to preserve a copy of the original command line. | |
| 79 // The test framework will do this itself as well. However, by then, | |
| 80 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in | |
| 81 // TearDown(). | |
| 82 CommandLine saved_cmdline_; | |
| 83 | |
| 84 #if defined OS_CHROMEOS | |
| 85 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | |
| 86 chromeos::ScopedTestCrosSettings test_cros_settings_; | |
| 87 chromeos::ScopedTestUserManager test_user_manager_; | |
| 88 #endif | |
| 89 }; | |
| 90 | |
| 91 TEST_F(StreamWithoutArgsUIPolicyTest, Construct) { | |
| 92 scoped_ptr<ActivityLogPolicy> policy( | |
| 93 new StreamWithoutArgsUIPolicy(profile_.get())); | |
| 94 scoped_refptr<const Extension> extension = | |
| 95 ExtensionBuilder() | |
| 96 .SetManifest(DictionaryBuilder() | |
| 97 .Set("name", "Test extension") | |
| 98 .Set("version", "1.0.0") | |
| 99 .Set("manifest_version", 2)) | |
| 100 .Build(); | |
| 101 extension_service_->AddExtension(extension); | |
| 102 scoped_ptr<ListValue> args(new ListValue()); | |
| 103 policy->ProcessAction(ActivityLogPolicy::ACTION_API, extension->id(), | |
| 104 std::string("tabs.testMethod"), GURL(), args.get(), NULL); | |
| 105 } | |
| 106 | |
| 107 TEST_F(StreamWithoutArgsUIPolicyTest, LogAndFetchActions) { | |
| 108 scoped_ptr<ActivityLogPolicy> policy( | |
| 109 new StreamWithoutArgsUIPolicy(profile_.get())); | |
| 110 scoped_refptr<const Extension> extension = | |
| 111 ExtensionBuilder() | |
| 112 .SetManifest(DictionaryBuilder() | |
| 113 .Set("name", "Test extension") | |
| 114 .Set("version", "1.0.0") | |
| 115 .Set("manifest_version", 2)) | |
| 116 .Build(); | |
| 117 extension_service_->AddExtension(extension); | |
| 118 scoped_ptr<ListValue> args(new ListValue()); | |
| 119 scoped_ptr<GURL> gurl(new GURL("http://www.google.com")); | |
|
Matt Perry
2013/06/13 23:16:14
make this a stack var, no need to allocate on the
dbabic
2013/06/14 00:30:38
Done.
| |
| 120 | |
| 121 // Write some API calls | |
| 122 policy->ProcessAction(ActivityLogPolicy::ACTION_API, extension->id(), | |
| 123 std::string("tabs.testMethod"), GURL(), args.get(), NULL); | |
| 124 policy->ProcessAction(ActivityLogPolicy::ACTION_DOM, | |
| 125 extension->id(), std::string("document.write"), | |
| 126 *gurl.get(), args.get(), NULL); | |
| 127 policy->ReadData(extension->id(), 0, | |
| 128 base::Bind( | |
| 129 StreamWithoutArgsUIPolicyTest::RetrieveActions_LogAndFetchActions)); | |
| 130 } | |
| 131 | |
| 132 TEST_F(StreamWithoutArgsUIPolicyTest, LogWithoutArguments) { | |
| 133 scoped_ptr<ActivityLogPolicy> policy( | |
| 134 new StreamWithoutArgsUIPolicy(profile_.get())); | |
| 135 scoped_refptr<const Extension> extension = | |
| 136 ExtensionBuilder() | |
| 137 .SetManifest(DictionaryBuilder() | |
| 138 .Set("name", "Test extension") | |
| 139 .Set("version", "1.0.0") | |
| 140 .Set("manifest_version", 2)) | |
| 141 .Build(); | |
| 142 extension_service_->AddExtension(extension); | |
| 143 scoped_ptr<ListValue> args(new ListValue()); | |
| 144 args->Set(0, new base::StringValue("hello")); | |
| 145 args->Set(1, new base::StringValue("world")); | |
| 146 policy->ProcessAction(ActivityLogPolicy::ACTION_API, extension->id(), | |
| 147 std::string("tabs.testMethod"), GURL(), args.get(), NULL); | |
| 148 policy->ReadData(extension->id(), 0, | |
| 149 base::Bind(StreamWithoutArgsUIPolicyTest::Arguments_Missing)); | |
| 150 } | |
| 151 | |
| 152 } // namespace extensions | |
| OLD | NEW |