Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2011 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/core_options_handler.h"
      6 #include "chrome/common/url_constants.h"
      7 #include "chrome/test/ui_test_utils.h"
      8 #include "content/browser/webui/web_ui_browsertest.h"
      9 #include "googleurl/src/gurl.h"
     10 #include "testing/gmock/include/gmock/gmock.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 using ::testing::StrictMock;
     14 using ::testing::_;
     15 
     16 MATCHER_P(Eq_ListValue, inList, "") {
     17   return arg->Equals(inList);
     18 }
     19 
     20 class MockCoreOptionsHandler : public CoreOptionsHandler {
     21  public:
     22   MOCK_METHOD1(HandleInitialize,
     23       void(const ListValue* args));
     24   MOCK_METHOD1(HandleFetchPrefs,
     25       void(const ListValue* args));
     26   MOCK_METHOD1(HandleObservePrefs,
     27       void(const ListValue* args));
     28   MOCK_METHOD1(HandleSetBooleanPref,
     29       void(const ListValue* args));
     30   MOCK_METHOD1(HandleSetIntegerPref,
     31       void(const ListValue* args));
     32   MOCK_METHOD1(HandleSetDoublePref,
     33       void(const ListValue* args));
     34   MOCK_METHOD1(HandleSetStringPref,
     35       void(const ListValue* args));
     36   MOCK_METHOD1(HandleSetObjectPref,
     37       void(const ListValue* args));
     38   MOCK_METHOD1(HandleClearPref,
     39       void(const ListValue* args));
     40   MOCK_METHOD1(HandleUserMetricsAction,
     41       void(const ListValue* args));
     42 
     43   virtual void RegisterMessages() {
     44     web_ui_->RegisterMessageCallback("coreOptionsInitialize",
     45         NewCallback(this, &MockCoreOptionsHandler ::HandleInitialize));
     46     web_ui_->RegisterMessageCallback("fetchPrefs",
     47         NewCallback(this, &MockCoreOptionsHandler ::HandleFetchPrefs));
     48     web_ui_->RegisterMessageCallback("observePrefs",
     49         NewCallback(this, &MockCoreOptionsHandler ::HandleObservePrefs));
     50     web_ui_->RegisterMessageCallback("setBooleanPref",
     51         NewCallback(this, &MockCoreOptionsHandler ::HandleSetBooleanPref));
     52     web_ui_->RegisterMessageCallback("setIntegerPref",
     53         NewCallback(this, &MockCoreOptionsHandler ::HandleSetIntegerPref));
     54     web_ui_->RegisterMessageCallback("setDoublePref",
     55         NewCallback(this, &MockCoreOptionsHandler ::HandleSetDoublePref));
     56     web_ui_->RegisterMessageCallback("setStringPref",
     57         NewCallback(this, &MockCoreOptionsHandler ::HandleSetStringPref));
     58     web_ui_->RegisterMessageCallback("setObjectPref",
     59         NewCallback(this, &MockCoreOptionsHandler ::HandleSetObjectPref));
     60     web_ui_->RegisterMessageCallback("clearPref",
     61         NewCallback(this, &MockCoreOptionsHandler ::HandleClearPref));
     62     web_ui_->RegisterMessageCallback("coreOptionsUserMetricsAction",
     63         NewCallback(this, &MockCoreOptionsHandler ::HandleUserMetricsAction));
     64   }
     65 };
     66 
     67 class SettingsWebUITest : public WebUIBrowserTest {
     68  protected:
     69   virtual void SetUpInProcessBrowserTestFixture() {
     70     WebUIBrowserTest::SetUpInProcessBrowserTestFixture();
     71     AddLibrary(FILE_PATH_LITERAL("settings.js"));
     72   }
     73 
     74   virtual void SetUpOnMainThread() {
     75     mock_core_options_handler_.reset(new StrictMock<MockCoreOptionsHandler>());
     76   }
     77 
     78   virtual void CleanUpOnMainThread() {
     79     mock_core_options_handler_.reset();
     80   }
     81 
     82   virtual WebUIMessageHandler* GetMockMessageHandler() {
     83     return mock_core_options_handler_.get();
     84   }
     85 
     86   scoped_ptr<StrictMock<MockCoreOptionsHandler> > mock_core_options_handler_;
     87 };
     88 
     89 // Test the end to end js to WebUI handler code path for
     90 // the message setBooleanPref.
     91 // TODO(dtseng): add more EXPECT_CALL's when updating js test.
     92 
     93 // Crashes on Mac only. See http://crbug.com/79181
     94 #if defined(OS_MACOSX)
     95 #define MAYBE_TestSetBooleanPrefTriggers DISABLED_TestSetBooleanPrefTriggers
     96 #else
     97 #define MAYBE_TestSetBooleanPrefTriggers TestSetBooleanPrefTriggers
     98 #endif
     99 IN_PROC_BROWSER_TEST_F(SettingsWebUITest, MAYBE_TestSetBooleanPrefTriggers) {
    100   // This serves as an example of a very constrained test.
    101   ListValue true_list_value;
    102   true_list_value.Append(Value::CreateStringValue("browser.show_home_button"));
    103   true_list_value.Append(Value::CreateBooleanValue(true));
    104   true_list_value.Append(
    105       Value::CreateStringValue("Options_Homepage_HomeButton"));
    106   ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUISettingsURL));
    107   EXPECT_CALL(*mock_core_options_handler_,
    108       HandleSetBooleanPref(Eq_ListValue(&true_list_value)));
    109   ASSERT_TRUE(RunJavascriptTest("testSetBooleanPrefTriggers"));
    110 }
    111