OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <memory> |
| 6 |
| 7 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 8 #include "chrome/test/base/testing_browser_process.h" |
| 9 #include "extensions/browser/extension_function.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace extensions { |
| 13 |
| 14 namespace { |
| 15 |
| 16 void SuccessCallback(bool* did_respond, |
| 17 ExtensionFunction::ResponseType type, |
| 18 const base::ListValue& results, |
| 19 const std::string& error, |
| 20 functions::HistogramValue histogram_value) { |
| 21 EXPECT_EQ(ExtensionFunction::ResponseType::SUCCEEDED, type); |
| 22 *did_respond = true; |
| 23 } |
| 24 |
| 25 void FailCallback(bool* did_respond, |
| 26 ExtensionFunction::ResponseType type, |
| 27 const base::ListValue& results, |
| 28 const std::string& error, |
| 29 functions::HistogramValue histogram_value) { |
| 30 EXPECT_EQ(ExtensionFunction::ResponseType::FAILED, type); |
| 31 *did_respond = true; |
| 32 } |
| 33 |
| 34 class ValidationFunction : public UIThreadExtensionFunction { |
| 35 public: |
| 36 explicit ValidationFunction(bool should_succeed) |
| 37 : should_succeed_(should_succeed), did_respond_(false) { |
| 38 set_response_callback(base::Bind( |
| 39 (should_succeed ? &SuccessCallback : &FailCallback), &did_respond_)); |
| 40 } |
| 41 |
| 42 ResponseAction Run() override { |
| 43 EXPECT_TRUE(should_succeed_); |
| 44 return RespondNow(NoArguments()); |
| 45 } |
| 46 |
| 47 bool did_respond() { return did_respond_; } |
| 48 |
| 49 private: |
| 50 ~ValidationFunction() override {} |
| 51 bool should_succeed_; |
| 52 bool did_respond_; |
| 53 }; |
| 54 } // namespace |
| 55 |
| 56 using ChromeExtensionFunctionUnitTest = ExtensionServiceTestBase; |
| 57 |
| 58 TEST_F(ChromeExtensionFunctionUnitTest, SimpleFunctionTest) { |
| 59 scoped_refptr<ValidationFunction> function(new ValidationFunction(true)); |
| 60 function->RunWithValidation()->Execute(); |
| 61 EXPECT_TRUE(function->did_respond()); |
| 62 } |
| 63 |
| 64 TEST_F(ChromeExtensionFunctionUnitTest, BrowserShutdownValidationFunctionTest) { |
| 65 TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); |
| 66 scoped_refptr<ValidationFunction> function(new ValidationFunction(false)); |
| 67 function->RunWithValidation()->Execute(); |
| 68 TestingBrowserProcess::GetGlobal()->SetShuttingDown(false); |
| 69 EXPECT_TRUE(function->did_respond()); |
| 70 } |
| 71 |
| 72 } // namespace extensions |
OLD | NEW |