- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / declarative / declarative_apitest.cc
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 "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_constants.h"
12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.h"
13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "chrome/browser/extensions/extension_system_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/extensions/extension.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/browser_thread.h"
21
22 using extensions::RulesRegistry;
23 using extensions::RulesRegistryService;
24 using extensions::WebRequestRulesRegistry;
25
26 namespace {
27
28 const char kArbitraryUrl[] = "http://www.example.com";
29
30 // The extension in "declarative/redirect_to_data" redirects every navigation to
31 // a page with title |kTestTitle|.
32 const char kTestTitle[] = ":TEST:";
33
34 }  // namespace
35
36 class DeclarativeApiTest : public ExtensionApiTest {
37  public:
38   std::string GetTitle() {
39     string16 title(
40         browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
41     return base::UTF16ToUTF8(title);
42   }
43 };
44
45 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, DeclarativeApi) {
46   ASSERT_TRUE(RunExtensionTest("declarative/api")) << message_;
47
48   // Check that unloading the page has removed all rules.
49   std::string extension_id = GetSingleLoadedExtension()->id();
50   UnloadExtension(extension_id);
51
52   // UnloadExtension posts a task to the owner thread of the extension
53   // to process this unloading. The next task to retrive all rules
54   // is therefore processed after the UnloadExtension task has been executed.
55
56   RulesRegistryService* rules_registry_service =
57       extensions::RulesRegistryService::Get(browser()->profile());
58   scoped_refptr<RulesRegistry> rules_registry =
59       rules_registry_service->GetRulesRegistry(
60           extensions::declarative_webrequest_constants::kOnRequest);
61
62   std::vector<linked_ptr<RulesRegistry::Rule> > known_rules;
63
64   content::BrowserThread::PostTask(
65       rules_registry->owner_thread(),
66       FROM_HERE,
67       base::Bind(base::IgnoreResult(&RulesRegistry::GetAllRules),
68                  rules_registry, extension_id, &known_rules));
69
70   content::RunAllPendingInMessageLoop(rules_registry->owner_thread());
71
72   EXPECT_TRUE(known_rules.empty());
73 }
74
75 // PersistRules test first installs an extension, which registers some rules.
76 // Then after browser restart, it checks that the rules are still in effect.
77 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, PRE_PersistRules) {
78   ASSERT_TRUE(RunExtensionTest("declarative/redirect_to_data")) << message_;
79 }
80
81 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, PersistRules) {
82   ui_test_utils::NavigateToURL(browser(), GURL(kArbitraryUrl));
83   EXPECT_EQ(kTestTitle, GetTitle());
84 }