| 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 = |  | 
| 70         "ID=odlameecjipmbmbejkplpemijjgpljce CATEGORY=api_call " |  | 
| 71         "API=tabs.testMethod"; |  | 
| 72     ASSERT_EQ(noargs, last->PrintForDebug()); |  | 
| 73   } |  | 
| 74 |  | 
| 75  protected: |  | 
| 76   ExtensionService* extension_service_; |  | 
| 77   scoped_ptr<TestingProfile> profile_; |  | 
| 78   content::TestBrowserThreadBundle thread_bundle_; |  | 
| 79   // Used to preserve a copy of the original command line. |  | 
| 80   // The test framework will do this itself as well. However, by then, |  | 
| 81   // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in |  | 
| 82   // TearDown(). |  | 
| 83   CommandLine saved_cmdline_; |  | 
| 84 |  | 
| 85 #if defined OS_CHROMEOS |  | 
| 86   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |  | 
| 87   chromeos::ScopedTestCrosSettings test_cros_settings_; |  | 
| 88   scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; |  | 
| 89 #endif |  | 
| 90 }; |  | 
| 91 |  | 
| 92 TEST_F(StreamWithoutArgsUIPolicyTest, Construct) { |  | 
| 93   ActivityLogPolicy* policy = 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.get()); |  | 
| 102   scoped_ptr<base::ListValue> args(new base::ListValue()); |  | 
| 103   scoped_refptr<Action> action = new Action(extension->id(), |  | 
| 104                                             base::Time::Now(), |  | 
| 105                                             Action::ACTION_API_CALL, |  | 
| 106                                             "tabs.testMethod"); |  | 
| 107   action->set_args(args.Pass()); |  | 
| 108   policy->ProcessAction(action); |  | 
| 109   policy->Close(); |  | 
| 110 } |  | 
| 111 |  | 
| 112 TEST_F(StreamWithoutArgsUIPolicyTest, LogAndFetchActions) { |  | 
| 113   ActivityLogPolicy* policy = new StreamWithoutArgsUIPolicy(profile_.get()); |  | 
| 114   scoped_refptr<const Extension> extension = |  | 
| 115       ExtensionBuilder() |  | 
| 116           .SetManifest(DictionaryBuilder() |  | 
| 117                        .Set("name", "Test extension") |  | 
| 118                        .Set("version", "1.0.0") |  | 
| 119                        .Set("manifest_version", 2)) |  | 
| 120           .Build(); |  | 
| 121   extension_service_->AddExtension(extension.get()); |  | 
| 122   GURL gurl("http://www.google.com"); |  | 
| 123 |  | 
| 124   // Write some API calls |  | 
| 125   scoped_refptr<Action> action_api = new Action(extension->id(), |  | 
| 126                                                 base::Time::Now(), |  | 
| 127                                                 Action::ACTION_API_CALL, |  | 
| 128                                                 "tabs.testMethod"); |  | 
| 129   action_api->set_args(make_scoped_ptr(new base::ListValue())); |  | 
| 130   policy->ProcessAction(action_api); |  | 
| 131 |  | 
| 132   scoped_refptr<Action> action_dom = new Action(extension->id(), |  | 
| 133                                                 base::Time::Now(), |  | 
| 134                                                 Action::ACTION_DOM_ACCESS, |  | 
| 135                                                 "document.write"); |  | 
| 136   action_dom->set_args(make_scoped_ptr(new base::ListValue())); |  | 
| 137   action_dom->set_page_url(gurl); |  | 
| 138   policy->ProcessAction(action_dom); |  | 
| 139 |  | 
| 140   policy->ReadData(extension->id(), 0, |  | 
| 141       base::Bind( |  | 
| 142           StreamWithoutArgsUIPolicyTest::RetrieveActions_LogAndFetchActions)); |  | 
| 143 |  | 
| 144   policy->Close(); |  | 
| 145 } |  | 
| 146 |  | 
| 147 TEST_F(StreamWithoutArgsUIPolicyTest, LogWithoutArguments) { |  | 
| 148   ActivityLogPolicy* policy = new StreamWithoutArgsUIPolicy(profile_.get()); |  | 
| 149   scoped_refptr<const Extension> extension = |  | 
| 150       ExtensionBuilder() |  | 
| 151           .SetManifest(DictionaryBuilder() |  | 
| 152                        .Set("name", "Test extension") |  | 
| 153                        .Set("version", "1.0.0") |  | 
| 154                        .Set("manifest_version", 2)) |  | 
| 155           .Build(); |  | 
| 156   extension_service_->AddExtension(extension.get()); |  | 
| 157 |  | 
| 158   scoped_ptr<base::ListValue> args(new base::ListValue()); |  | 
| 159   args->Set(0, new base::StringValue("hello")); |  | 
| 160   args->Set(1, new base::StringValue("world")); |  | 
| 161   scoped_refptr<Action> action = new Action(extension->id(), |  | 
| 162                                             base::Time::Now(), |  | 
| 163                                             Action::ACTION_API_CALL, |  | 
| 164                                             "tabs.testMethod"); |  | 
| 165   action->set_args(args.Pass()); |  | 
| 166 |  | 
| 167   policy->ProcessAction(action); |  | 
| 168   policy->ReadData(extension->id(), 0, |  | 
| 169       base::Bind(StreamWithoutArgsUIPolicyTest::Arguments_Missing)); |  | 
| 170   policy->Close(); |  | 
| 171 } |  | 
| 172 |  | 
| 173 }  // namespace extensions |  | 
| OLD | NEW | 
|---|