OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/declarative_content/content_action.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/test/values_test_util.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/extensions/extension_action.h" | |
12 #include "chrome/browser/extensions/extension_action_manager.h" | |
13 #include "chrome/browser/extensions/extension_service.h" | |
14 #include "chrome/browser/extensions/extension_tab_util.h" | |
15 #include "chrome/browser/extensions/test_extension_system.h" | |
16 #include "chrome/browser/sessions/session_tab_helper.h" | |
17 #include "chrome/common/extensions/extension_builder.h" | |
18 #include "chrome/common/extensions/value_builder.h" | |
19 #include "chrome/test/base/testing_profile.h" | |
20 #include "content/public/test/test_browser_thread.h" | |
21 #include "content/public/test/web_contents_tester.h" | |
22 #include "testing/gmock/include/gmock/gmock.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 namespace extensions { | |
26 namespace { | |
27 | |
28 using base::test::ParseJson; | |
29 using testing::HasSubstr; | |
30 | |
31 class TestExtensionEnvironment { | |
32 public: | |
33 TestExtensionEnvironment() | |
34 : ui_thread_(BrowserThread::UI, &loop_), | |
35 extension_service_(NULL) {} | |
36 | |
37 TestingProfile* profile() { return &profile_; } | |
38 | |
39 ExtensionService* GetExtensionService() { | |
40 if (extension_service_ == NULL) { | |
41 TestExtensionSystem* extension_system = | |
42 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_)); | |
43 extension_service_ = extension_system->CreateExtensionService( | |
44 CommandLine::ForCurrentProcess(), FilePath(), false); | |
45 } | |
46 return extension_service_; | |
47 } | |
48 | |
49 scoped_refptr<Extension> MakeExtension(DictionaryBuilder& manifest_extra) { | |
50 scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder() | |
battre
2012/12/15 11:54:10
OMG, thanks for showing me this!!! This should be
Jeffrey Yasskin
2013/01/17 09:22:56
It's pretty awesome. Many tests are better using b
| |
51 .Set("name", "Extension") | |
52 .Set("version", "1.0") | |
53 .Set("manifest_version", 2) | |
54 .Build(); | |
55 manifest->MergeDictionary(manifest_extra.Build().get()); | |
56 | |
57 scoped_refptr<Extension> result = | |
58 ExtensionBuilder().SetManifest(manifest.Pass()).Build(); | |
59 GetExtensionService()->AddExtension(result); | |
60 return result; | |
61 } | |
62 | |
63 scoped_ptr<content::WebContents> MakeTab() { | |
64 scoped_ptr<content::WebContents> contents( | |
65 content::WebContentsTester::CreateTestWebContents(&profile_, NULL)); | |
66 // Create a tab id. | |
67 SessionTabHelper::CreateForWebContents(contents.get()); | |
68 return contents.Pass(); | |
69 } | |
70 | |
71 private: | |
72 MessageLoop loop_; | |
73 content::TestBrowserThread ui_thread_; | |
74 TestingProfile profile_; | |
75 ExtensionService* extension_service_; | |
76 }; | |
77 | |
78 TEST(ContentActionTest, ShowPageAction) { | |
79 TestExtensionEnvironment env; | |
80 std::string error; | |
81 bool bad_message = false; | |
82 scoped_ptr<ContentAction> result; | |
83 | |
84 // Test wrong data type passed. | |
85 error.clear(); | |
86 result = ContentAction::Create(*ParseJson("[]"), &error, &bad_message); | |
87 EXPECT_TRUE(bad_message); | |
88 EXPECT_EQ("", error); | |
89 EXPECT_FALSE(result); | |
90 | |
91 // Test missing instanceType element. | |
92 error.clear(); | |
93 result = ContentAction::Create(*ParseJson("{}"), &error, &bad_message); | |
94 EXPECT_TRUE(bad_message); | |
95 EXPECT_EQ("", error); | |
96 EXPECT_FALSE(result); | |
97 | |
98 // Test wrong instanceType element. | |
99 error.clear(); | |
100 result = ContentAction::Create(*ParseJson( | |
101 "{\n" | |
102 " \"instanceType\": \"declarativeContent.UnknownType\",\n" | |
103 "}"), | |
104 &error, &bad_message); | |
105 EXPECT_THAT(error, HasSubstr("invalid instanceType")); | |
106 EXPECT_FALSE(result); | |
107 | |
108 // Test success | |
109 error.clear(); | |
110 result = ContentAction::Create(*ParseJson( | |
111 "{\n" | |
112 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" | |
113 "}"), | |
114 &error, &bad_message); | |
115 EXPECT_EQ("", error); | |
116 EXPECT_FALSE(bad_message); | |
117 ASSERT_TRUE(result); | |
118 EXPECT_EQ(ContentAction::ACTION_SHOW_PAGE_ACTION, result->GetType()); | |
119 | |
120 scoped_refptr<Extension> extension = env.MakeExtension( | |
121 DictionaryBuilder().Set("page_action", DictionaryBuilder() | |
122 .Set("default_title", "Extension"))); | |
battre
2012/12/15 11:54:10
nit: is this proper formatting?
Jeffrey Yasskin
2013/01/17 09:22:56
I think so, because it expresses the nesting with
| |
123 ExtensionAction* page_action = | |
124 ExtensionActionManager::Get(env.profile())->GetPageAction(*extension); | |
125 scoped_ptr<content::WebContents> contents = env.MakeTab(); | |
126 const int tab_id = ExtensionTabUtil::GetTabId(contents.get()); | |
127 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); | |
128 ContentAction::ApplyInfo apply_info = { | |
129 env.profile(), contents.get() | |
130 }; | |
131 result->Apply(extension->id(), base::Time(), apply_info); | |
132 EXPECT_TRUE(page_action->GetIsVisible(tab_id)); | |
battre
2012/12/15 11:54:10
Add test for Revert?
Jeffrey Yasskin
2013/01/17 09:22:56
Done.
| |
133 } | |
134 | |
135 } // namespace | |
136 } // namespace extensions | |
OLD | NEW |