| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/sync_internals_ui.h" | 5 #include "chrome/browser/ui/webui/sync_internals_ui.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/sync/js/js_arg_list.h" | 13 #include "chrome/browser/sync/js/js_arg_list.h" |
| 14 #include "chrome/browser/sync/js/js_event_details.h" | 14 #include "chrome/browser/sync/js/js_event_details.h" |
| 15 #include "chrome/browser/sync/js/js_test_util.h" | 15 #include "chrome/browser/sync/js/js_test_util.h" |
| 16 #include "chrome/browser/sync/profile_sync_service_mock.h" | 16 #include "chrome/browser/sync/profile_sync_service_mock.h" |
| 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "chrome/test/base/profile_mock.h" | 18 #include "chrome/test/base/profile_mock.h" |
| 18 #include "content/browser/browser_thread.h" | 19 #include "content/browser/browser_thread.h" |
| 19 #include "content/browser/renderer_host/test_render_view_host.h" | |
| 20 #include "content/browser/tab_contents/test_tab_contents.h" | 20 #include "content/browser/tab_contents/test_tab_contents.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 using browser_sync::HasArgsAsList; | 26 using browser_sync::HasArgsAsList; |
| 27 using browser_sync::JsArgList; | 27 using browser_sync::JsArgList; |
| 28 using browser_sync::JsEventDetails; | 28 using browser_sync::JsEventDetails; |
| 29 using testing::_; | 29 using testing::_; |
| 30 using testing::Mock; | 30 using testing::Mock; |
| 31 using testing::NiceMock; | 31 using testing::NiceMock; |
| 32 using testing::Return; | 32 using testing::Return; |
| 33 using testing::StrictMock; | 33 using testing::StrictMock; |
| 34 | 34 |
| 35 // Subclass of SyncInternalsUI to mock out ExecuteJavascript. | 35 // Subclass of SyncInternalsUI to mock out ExecuteJavascript. |
| 36 class TestSyncInternalsUI : public SyncInternalsUI { | 36 class TestSyncInternalsUI : public SyncInternalsUI { |
| 37 public: | 37 public: |
| 38 explicit TestSyncInternalsUI(TabContents* contents) | 38 explicit TestSyncInternalsUI(TabContents* contents) |
| 39 : SyncInternalsUI(contents) {} | 39 : SyncInternalsUI(contents) {} |
| 40 virtual ~TestSyncInternalsUI() {} | 40 virtual ~TestSyncInternalsUI() {} |
| 41 | 41 |
| 42 MOCK_METHOD1(ExecuteJavascript, void(const string16&)); | 42 MOCK_METHOD1(ExecuteJavascript, void(const string16&)); |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 // Tests with non-NULL ProfileSyncService. | 45 // Tests with non-NULL ProfileSyncService. |
| 46 class SyncInternalsUITestWithService : public RenderViewHostTestHarness { | 46 class SyncInternalsUITestWithService : public ChromeRenderViewHostTestHarness { |
| 47 protected: | 47 protected: |
| 48 SyncInternalsUITestWithService() {} | 48 SyncInternalsUITestWithService() {} |
| 49 | 49 |
| 50 virtual ~SyncInternalsUITestWithService() {} | 50 virtual ~SyncInternalsUITestWithService() {} |
| 51 | 51 |
| 52 virtual void SetUp() { | 52 virtual void SetUp() { |
| 53 NiceMock<ProfileMock>* profile_mock = new NiceMock<ProfileMock>(); | 53 NiceMock<ProfileMock>* profile_mock = new NiceMock<ProfileMock>(); |
| 54 StrictMock<ProfileSyncServiceMock> profile_sync_service_mock; | 54 StrictMock<ProfileSyncServiceMock> profile_sync_service_mock; |
| 55 EXPECT_CALL(*profile_mock, GetProfileSyncService()) | 55 EXPECT_CALL(*profile_mock, GetProfileSyncService()) |
| 56 .WillOnce(Return(&profile_sync_service_mock)); | 56 .WillOnce(Return(&profile_sync_service_mock)); |
| 57 profile_.reset(profile_mock); | 57 browser_context_.reset(profile_mock); |
| 58 | 58 |
| 59 RenderViewHostTestHarness::SetUp(); | 59 ChromeRenderViewHostTestHarness::SetUp(); |
| 60 | 60 |
| 61 EXPECT_CALL(profile_sync_service_mock, GetJsController()) | 61 EXPECT_CALL(profile_sync_service_mock, GetJsController()) |
| 62 .WillOnce(Return(mock_js_controller_.AsWeakPtr())); | 62 .WillOnce(Return(mock_js_controller_.AsWeakPtr())); |
| 63 | 63 |
| 64 EXPECT_CALL(mock_js_controller_, AddJsEventHandler(_)); | 64 EXPECT_CALL(mock_js_controller_, AddJsEventHandler(_)); |
| 65 | 65 |
| 66 { | 66 { |
| 67 // Needed by |test_sync_internals_ui_|'s constructor. The | 67 // Needed by |test_sync_internals_ui_|'s constructor. The |
| 68 // message loop is provided by RenderViewHostTestHarness. | 68 // message loop is provided by ChromeRenderViewHostTestHarness. |
| 69 BrowserThread ui_thread_(BrowserThread::UI, | 69 BrowserThread ui_thread_(BrowserThread::UI, |
| 70 MessageLoopForUI::current()); | 70 MessageLoopForUI::current()); |
| 71 // |test_sync_internals_ui_|'s constructor triggers all the | 71 // |test_sync_internals_ui_|'s constructor triggers all the |
| 72 // expectations above. | 72 // expectations above. |
| 73 test_sync_internals_ui_.reset(new TestSyncInternalsUI(contents())); | 73 test_sync_internals_ui_.reset(new TestSyncInternalsUI(contents())); |
| 74 } | 74 } |
| 75 | 75 |
| 76 Mock::VerifyAndClearExpectations(profile_mock); | 76 Mock::VerifyAndClearExpectations(profile_mock); |
| 77 Mock::VerifyAndClearExpectations(&mock_js_controller_); | 77 Mock::VerifyAndClearExpectations(&mock_js_controller_); |
| 78 } | 78 } |
| 79 | 79 |
| 80 virtual void TearDown() { | 80 virtual void TearDown() { |
| 81 Mock::VerifyAndClearExpectations(&mock_js_controller_); | 81 Mock::VerifyAndClearExpectations(&mock_js_controller_); |
| 82 | 82 |
| 83 // Called by |test_sync_internals_ui_|'s destructor. | 83 // Called by |test_sync_internals_ui_|'s destructor. |
| 84 EXPECT_CALL(mock_js_controller_, | 84 EXPECT_CALL(mock_js_controller_, |
| 85 RemoveJsEventHandler(test_sync_internals_ui_.get())); | 85 RemoveJsEventHandler(test_sync_internals_ui_.get())); |
| 86 test_sync_internals_ui_.reset(); | 86 test_sync_internals_ui_.reset(); |
| 87 | 87 |
| 88 RenderViewHostTestHarness::TearDown(); | 88 ChromeRenderViewHostTestHarness::TearDown(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 StrictMock<browser_sync::MockJsController> mock_js_controller_; | 91 StrictMock<browser_sync::MockJsController> mock_js_controller_; |
| 92 scoped_ptr<TestSyncInternalsUI> test_sync_internals_ui_; | 92 scoped_ptr<TestSyncInternalsUI> test_sync_internals_ui_; |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 TEST_F(SyncInternalsUITestWithService, HandleJsEvent) { | 95 TEST_F(SyncInternalsUITestWithService, HandleJsEvent) { |
| 96 EXPECT_CALL(*test_sync_internals_ui_, | 96 EXPECT_CALL(*test_sync_internals_ui_, |
| 97 ExecuteJavascript( | 97 ExecuteJavascript( |
| 98 ASCIIToUTF16("chrome.sync.testMessage.fire({});"))); | 98 ASCIIToUTF16("chrome.sync.testMessage.fire({});"))); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 117 ListValue args; | 117 ListValue args; |
| 118 args.Append(Value::CreateIntegerValue(10)); | 118 args.Append(Value::CreateIntegerValue(10)); |
| 119 | 119 |
| 120 EXPECT_CALL(mock_js_controller_, | 120 EXPECT_CALL(mock_js_controller_, |
| 121 ProcessJsMessage(name, HasArgsAsList(args), _)); | 121 ProcessJsMessage(name, HasArgsAsList(args), _)); |
| 122 | 122 |
| 123 test_sync_internals_ui_->OnWebUISend(GURL(), name, args); | 123 test_sync_internals_ui_->OnWebUISend(GURL(), name, args); |
| 124 } | 124 } |
| 125 | 125 |
| 126 // Tests with NULL ProfileSyncService. | 126 // Tests with NULL ProfileSyncService. |
| 127 class SyncInternalsUITestWithoutService : public RenderViewHostTestHarness { | 127 class SyncInternalsUITestWithoutService |
| 128 : public ChromeRenderViewHostTestHarness { |
| 128 protected: | 129 protected: |
| 129 SyncInternalsUITestWithoutService() {} | 130 SyncInternalsUITestWithoutService() {} |
| 130 | 131 |
| 131 virtual ~SyncInternalsUITestWithoutService() {} | 132 virtual ~SyncInternalsUITestWithoutService() {} |
| 132 | 133 |
| 133 virtual void SetUp() { | 134 virtual void SetUp() { |
| 134 NiceMock<ProfileMock>* profile_mock = new NiceMock<ProfileMock>(); | 135 NiceMock<ProfileMock>* profile_mock = new NiceMock<ProfileMock>(); |
| 135 EXPECT_CALL(*profile_mock, GetProfileSyncService()) | 136 EXPECT_CALL(*profile_mock, GetProfileSyncService()) |
| 136 .WillOnce(Return(static_cast<ProfileSyncService*>(NULL))); | 137 .WillOnce(Return(static_cast<ProfileSyncService*>(NULL))); |
| 137 profile_.reset(profile_mock); | 138 browser_context_.reset(profile_mock); |
| 138 | 139 |
| 139 RenderViewHostTestHarness::SetUp(); | 140 ChromeRenderViewHostTestHarness::SetUp(); |
| 140 | 141 |
| 141 { | 142 { |
| 142 // Needed by |test_sync_internals_ui_|'s constructor. The | 143 // Needed by |test_sync_internals_ui_|'s constructor. The |
| 143 // message loop is provided by RenderViewHostTestHarness. | 144 // message loop is provided by ChromeRenderViewHostTestHarness. |
| 144 BrowserThread ui_thread_(BrowserThread::UI, | 145 BrowserThread ui_thread_(BrowserThread::UI, |
| 145 MessageLoopForUI::current()); | 146 MessageLoopForUI::current()); |
| 146 // |test_sync_internals_ui_|'s constructor triggers all the | 147 // |test_sync_internals_ui_|'s constructor triggers all the |
| 147 // expectations above. | 148 // expectations above. |
| 148 test_sync_internals_ui_.reset(new TestSyncInternalsUI(contents())); | 149 test_sync_internals_ui_.reset(new TestSyncInternalsUI(contents())); |
| 149 } | 150 } |
| 150 | 151 |
| 151 Mock::VerifyAndClearExpectations(profile_mock); | 152 Mock::VerifyAndClearExpectations(profile_mock); |
| 152 } | 153 } |
| 153 | 154 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 const char kAboutInfoCall[] = | 191 const char kAboutInfoCall[] = |
| 191 "chrome.sync.getAboutInfo.handleReply({\"summary\":\"SYNC DISABLED\"});"; | 192 "chrome.sync.getAboutInfo.handleReply({\"summary\":\"SYNC DISABLED\"});"; |
| 192 EXPECT_CALL(*test_sync_internals_ui_, | 193 EXPECT_CALL(*test_sync_internals_ui_, |
| 193 ExecuteJavascript(ASCIIToUTF16(kAboutInfoCall))); | 194 ExecuteJavascript(ASCIIToUTF16(kAboutInfoCall))); |
| 194 | 195 |
| 195 ListValue args; | 196 ListValue args; |
| 196 test_sync_internals_ui_->OnWebUISend(GURL(), "getAboutInfo", args); | 197 test_sync_internals_ui_->OnWebUISend(GURL(), "getAboutInfo", args); |
| 197 } | 198 } |
| 198 | 199 |
| 199 } // namespace | 200 } // namespace |
| OLD | NEW |