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 int g_callback_runs = 0; | |
Devlin
2016/07/15 17:59:39
global variables are pretty fragile in unittests t
dgn
2016/07/18 14:44:04
Thanks! I didn't think about using Bind like that.
| |
17 | |
18 void SuccessCallback(ExtensionFunction::ResponseType type, | |
19 const base::ListValue& results, | |
20 const std::string& error, | |
21 functions::HistogramValue histogram_value) { | |
22 EXPECT_EQ(ExtensionFunction::ResponseType::SUCCEEDED, type); | |
23 ++g_callback_runs; | |
24 } | |
25 | |
26 void FailCallback(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 ++g_callback_runs; | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 class ChromeExtensionFunctionUnitTest : public ExtensionServiceTestBase { | |
37 | |
38 public: | |
39 void TearDown() override { | |
40 g_callback_runs = 0; | |
41 ExtensionServiceTestBase::TearDown(); | |
42 } | |
43 | |
44 }; | |
45 | |
46 class ValidationFunction : public UIThreadExtensionFunction { | |
47 public: | |
48 explicit ValidationFunction(bool should_succeed) | |
49 : should_succeed_(should_succeed) { | |
50 set_response_callback(should_succeed ? base::Bind(&SuccessCallback) | |
51 : base::Bind(&FailCallback)); | |
52 } | |
53 | |
54 ResponseAction Run() override { | |
55 EXPECT_TRUE(should_succeed_); | |
56 return RespondNow(NoArguments()); | |
57 } | |
58 | |
59 private: | |
60 ~ValidationFunction() override {} | |
61 bool should_succeed_; | |
62 }; | |
63 | |
64 | |
65 | |
66 TEST_F(ChromeExtensionFunctionUnitTest, SimpleFunctionTest) { | |
67 scoped_refptr<ExtensionFunction> function(new ValidationFunction(true)); | |
68 function->RunWithValidation()->Execute(); | |
69 EXPECT_EQ(1, g_callback_runs); | |
70 } | |
71 | |
72 TEST_F(ChromeExtensionFunctionUnitTest, BrowserShutdownValidationFunctionTest) { | |
73 TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); | |
74 scoped_refptr<ExtensionFunction> function(new ValidationFunction(false)); | |
75 function->RunWithValidation()->Execute(); | |
76 TestingBrowserProcess::GetGlobal()->SetShuttingDown(false); | |
77 EXPECT_EQ(1, g_callback_runs); | |
78 } | |
79 | |
80 } // namespace extensions | |
OLD | NEW |