Home | History | Annotate | Download | only in options
      1 // Copyright 2013 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/ui/webui/options/options_browsertest.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "base/values.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/ui/browser.h"
     12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     13 #include "content/public/browser/navigation_controller.h"
     14 #include "content/public/browser/navigation_entry.h"
     15 #include "content/public/browser/web_contents.h"
     16 #include "content/public/browser/web_ui.h"
     17 #include "url/gurl.h"
     18 
     19 using content::NavigationController;
     20 using content::NavigationEntry;
     21 using content::WebUIMessageHandler;
     22 
     23 OptionsBrowserTest::OptionsBrowserTest() {
     24 }
     25 
     26 OptionsBrowserTest::~OptionsBrowserTest() {
     27 }
     28 
     29 void OptionsBrowserTest::RegisterMessages() {
     30   web_ui()->RegisterMessageCallback(
     31       "optionsTestReportHistory", base::Bind(&OptionsBrowserTest::ReportHistory,
     32                                              base::Unretained(this)));
     33 
     34   web_ui()->RegisterMessageCallback(
     35       "optionsTestSetPref", base::Bind(&OptionsBrowserTest::HandleSetPref,
     36                                        base::Unretained(this)));
     37 }
     38 
     39 // Includes the current entry.
     40 void OptionsBrowserTest::ReportHistory(const base::ListValue* list_value) {
     41   const NavigationController& controller =
     42       browser()->tab_strip_model()->GetActiveWebContents()->GetController();
     43   base::ListValue history;
     44   const int current = controller.GetCurrentEntryIndex();
     45   for (int i = 0; i <= current; ++i) {
     46     GURL url = controller.GetEntryAtIndex(i)->GetVirtualURL();
     47     history.Append(new base::StringValue(url.spec()));
     48   }
     49   web_ui()->CallJavascriptFunction(
     50       "OptionsWebUIExtendedTest.verifyHistoryCallback", history);
     51 }
     52 
     53 void OptionsBrowserTest::ClearPref(const char* path) {
     54   browser()->profile()->GetPrefs()->ClearPref(path);
     55 }
     56 
     57 void OptionsBrowserTest::HandleSetPref(const base::ListValue* args) {
     58   ASSERT_EQ(2u, args->GetSize());
     59 
     60   std::string pref_name;
     61   ASSERT_TRUE(args->GetString(0, &pref_name));
     62   const base::Value* pref_value;
     63   ASSERT_TRUE(args->Get(1, &pref_value));
     64 
     65   browser()->profile()->GetPrefs()->Set(pref_name.c_str(), *pref_value);
     66 }
     67 
     68 content::WebUIMessageHandler* OptionsBrowserTest::GetMockMessageHandler() {
     69   return this;
     70 }
     71