api_activity_logger_unittest.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2017 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. #include "extensions/renderer/api_activity_logger.h"
  5. #include "base/run_loop.h"
  6. #include "extensions/common/extension_builder.h"
  7. #include "extensions/common/extension_messages.h"
  8. #include "extensions/common/features/feature.h"
  9. #include "extensions/renderer/bindings/api_binding_test.h"
  10. #include "extensions/renderer/bindings/api_binding_test_util.h"
  11. #include "extensions/renderer/native_extension_bindings_system_test_base.h"
  12. #include "extensions/renderer/script_context.h"
  13. #include "extensions/renderer/script_context_set.h"
  14. #include "extensions/renderer/test_extensions_renderer_client.h"
  15. #include "ipc/ipc_message.h"
  16. #include "ipc/ipc_test_sink.h"
  17. namespace extensions {
  18. namespace {
  19. class ScopedAllowActivityLogging {
  20. public:
  21. ScopedAllowActivityLogging() { APIActivityLogger::set_log_for_testing(true); }
  22. ScopedAllowActivityLogging(const ScopedAllowActivityLogging&) = delete;
  23. ScopedAllowActivityLogging& operator=(const ScopedAllowActivityLogging&) =
  24. delete;
  25. ~ScopedAllowActivityLogging() {
  26. APIActivityLogger::set_log_for_testing(false);
  27. }
  28. };
  29. } // namespace
  30. using ActivityLoggerTest = APIBindingTest;
  31. using ActivityLogCallType = IPCMessageSender::ActivityLogCallType;
  32. // Regression test for crbug.com/740866.
  33. TEST_F(ActivityLoggerTest, DontCrashOnUnconvertedValues) {
  34. TestExtensionsRendererClient client;
  35. std::set<ExtensionId> extension_ids;
  36. ScriptContextSet script_context_set(&extension_ids);
  37. ScopedAllowActivityLogging scoped_allow_activity_logging;
  38. v8::HandleScope handle_scope(isolate());
  39. v8::Local<v8::Context> context = MainContext();
  40. scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
  41. extension_ids.insert(extension->id());
  42. const Feature::Context kContextType = Feature::BLESSED_EXTENSION_CONTEXT;
  43. script_context_set.AddForTesting(std::make_unique<ScriptContext>(
  44. context, nullptr, extension.get(), kContextType, extension.get(),
  45. kContextType));
  46. std::vector<v8::Local<v8::Value>> args = {v8::Undefined(isolate())};
  47. std::unique_ptr<TestIPCMessageSender> ipc_sender =
  48. std::make_unique<testing::StrictMock<TestIPCMessageSender>>();
  49. EXPECT_CALL(*ipc_sender,
  50. SendActivityLogIPC(extension->id(), ActivityLogCallType::APICALL,
  51. testing::_))
  52. .WillOnce([&](const ExtensionId& extension_id, const ActivityLogCallType,
  53. const ExtensionHostMsg_APIActionOrEvent_Params& params) {
  54. EXPECT_EQ("someApiMethod", params.api_call);
  55. ASSERT_EQ(1u, params.arguments.size());
  56. EXPECT_EQ(base::Value::Type::NONE, params.arguments[0].type());
  57. });
  58. APIActivityLogger::LogAPICall(ipc_sender.get(), context, "someApiMethod",
  59. args);
  60. ScriptContext* script_context = script_context_set.GetByV8Context(context);
  61. script_context_set.Remove(script_context);
  62. base::RunLoop().RunUntilIdle(); // Let script context destruction complete.
  63. ::testing::Mock::VerifyAndClearExpectations(ipc_sender.get());
  64. }
  65. } // namespace extensions