| 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" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "content/test/test_browser_thread.h" | 21 #include "content/test/test_browser_thread.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 using browser_sync::HasArgsAsList; | 27 using browser_sync::HasArgsAsList; |
| 28 using browser_sync::JsArgList; | 28 using browser_sync::JsArgList; |
| 29 using browser_sync::JsEventDetails; | 29 using browser_sync::JsEventDetails; |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 using content::WebContents; |
| 31 using testing::_; | 32 using testing::_; |
| 32 using testing::Mock; | 33 using testing::Mock; |
| 33 using testing::NiceMock; | 34 using testing::NiceMock; |
| 34 using testing::Return; | 35 using testing::Return; |
| 35 using testing::StrictMock; | 36 using testing::StrictMock; |
| 36 | 37 |
| 37 // Subclass of SyncInternalsUI to mock out ExecuteJavascript. | 38 // Subclass of WebUI to mock out ExecuteJavascript. |
| 38 class TestSyncInternalsUI : public SyncInternalsUI { | 39 class TestSyncWebUI: public WebUI { |
| 39 public: | 40 public: |
| 40 explicit TestSyncInternalsUI(TabContents* contents) | 41 explicit TestSyncWebUI(WebContents* web_contents) |
| 41 : SyncInternalsUI(contents) {} | 42 : WebUI(web_contents) {} |
| 42 virtual ~TestSyncInternalsUI() {} | 43 virtual ~TestSyncWebUI() {} |
| 43 | 44 |
| 44 MOCK_METHOD1(ExecuteJavascript, void(const string16&)); | 45 MOCK_METHOD1(ExecuteJavascript, void(const string16&)); |
| 45 }; | 46 }; |
| 46 | 47 |
| 47 // Tests with non-NULL ProfileSyncService. | 48 // Tests with non-NULL ProfileSyncService. |
| 48 class SyncInternalsUITestWithService : public ChromeRenderViewHostTestHarness { | 49 class SyncInternalsUITestWithService : public ChromeRenderViewHostTestHarness { |
| 49 protected: | 50 protected: |
| 50 SyncInternalsUITestWithService() {} | 51 SyncInternalsUITestWithService() : sync_internals_ui_(NULL) {} |
| 51 | 52 |
| 52 virtual ~SyncInternalsUITestWithService() {} | 53 virtual ~SyncInternalsUITestWithService() {} |
| 53 | 54 |
| 54 virtual void SetUp() { | 55 virtual void SetUp() { |
| 55 NiceMock<ProfileMock>* profile_mock = new NiceMock<ProfileMock>(); | 56 NiceMock<ProfileMock>* profile_mock = new NiceMock<ProfileMock>(); |
| 56 StrictMock<ProfileSyncServiceMock> profile_sync_service_mock; | 57 StrictMock<ProfileSyncServiceMock> profile_sync_service_mock; |
| 57 EXPECT_CALL(*profile_mock, GetProfileSyncService()) | 58 EXPECT_CALL(*profile_mock, GetProfileSyncService()) |
| 58 .WillOnce(Return(&profile_sync_service_mock)); | 59 .WillOnce(Return(&profile_sync_service_mock)); |
| 59 browser_context_.reset(profile_mock); | 60 browser_context_.reset(profile_mock); |
| 60 | 61 |
| 61 ChromeRenderViewHostTestHarness::SetUp(); | 62 ChromeRenderViewHostTestHarness::SetUp(); |
| 62 | 63 |
| 63 EXPECT_CALL(profile_sync_service_mock, GetJsController()) | 64 EXPECT_CALL(profile_sync_service_mock, GetJsController()) |
| 64 .WillOnce(Return(mock_js_controller_.AsWeakPtr())); | 65 .WillOnce(Return(mock_js_controller_.AsWeakPtr())); |
| 65 | 66 |
| 66 EXPECT_CALL(mock_js_controller_, AddJsEventHandler(_)); | 67 EXPECT_CALL(mock_js_controller_, AddJsEventHandler(_)); |
| 67 | 68 |
| 68 { | 69 { |
| 69 // Needed by |test_sync_internals_ui_|'s constructor. The | 70 // Needed by |sync_internals_ui_|'s constructor. The |
| 70 // message loop is provided by ChromeRenderViewHostTestHarness. | 71 // message loop is provided by ChromeRenderViewHostTestHarness. |
| 71 content::TestBrowserThread ui_thread_(BrowserThread::UI, | 72 content::TestBrowserThread ui_thread_(BrowserThread::UI, |
| 72 MessageLoopForUI::current()); | 73 MessageLoopForUI::current()); |
| 73 // |test_sync_internals_ui_|'s constructor triggers all the | 74 // |sync_internals_ui_|'s constructor triggers all the |
| 74 // expectations above. | 75 // expectations above. |
| 75 test_sync_internals_ui_.reset(new TestSyncInternalsUI(contents())); | 76 web_ui_.reset(new TestSyncWebUI(contents())); |
| 77 sync_internals_ui_ = new SyncInternalsUI(web_ui_.get()); |
| 78 web_ui_->SetController(sync_internals_ui_); |
| 76 } | 79 } |
| 77 | 80 |
| 78 Mock::VerifyAndClearExpectations(profile_mock); | 81 Mock::VerifyAndClearExpectations(profile_mock); |
| 79 Mock::VerifyAndClearExpectations(&mock_js_controller_); | 82 Mock::VerifyAndClearExpectations(&mock_js_controller_); |
| 80 } | 83 } |
| 81 | 84 |
| 82 virtual void TearDown() { | 85 virtual void TearDown() { |
| 83 Mock::VerifyAndClearExpectations(&mock_js_controller_); | 86 Mock::VerifyAndClearExpectations(&mock_js_controller_); |
| 84 | 87 |
| 85 // Called by |test_sync_internals_ui_|'s destructor. | 88 // Called by |sync_internals_ui_|'s destructor. |
| 86 EXPECT_CALL(mock_js_controller_, | 89 EXPECT_CALL(mock_js_controller_, |
| 87 RemoveJsEventHandler(test_sync_internals_ui_.get())); | 90 RemoveJsEventHandler(sync_internals_ui_)); |
| 88 test_sync_internals_ui_.reset(); | 91 sync_internals_ui_ = NULL; |
| 92 web_ui_.reset(); |
| 89 | 93 |
| 90 ChromeRenderViewHostTestHarness::TearDown(); | 94 ChromeRenderViewHostTestHarness::TearDown(); |
| 91 } | 95 } |
| 92 | 96 |
| 93 StrictMock<browser_sync::MockJsController> mock_js_controller_; | 97 StrictMock<browser_sync::MockJsController> mock_js_controller_; |
| 94 scoped_ptr<TestSyncInternalsUI> test_sync_internals_ui_; | 98 scoped_ptr<TestSyncWebUI> web_ui_; |
| 99 SyncInternalsUI* sync_internals_ui_; |
| 95 }; | 100 }; |
| 96 | 101 |
| 97 TEST_F(SyncInternalsUITestWithService, HandleJsEvent) { | 102 TEST_F(SyncInternalsUITestWithService, HandleJsEvent) { |
| 98 EXPECT_CALL(*test_sync_internals_ui_, | 103 EXPECT_CALL(*web_ui_, |
| 99 ExecuteJavascript( | 104 ExecuteJavascript( |
| 100 ASCIIToUTF16("chrome.sync.testMessage.fire({});"))); | 105 ASCIIToUTF16("chrome.sync.testMessage.fire({});"))); |
| 101 | 106 |
| 102 test_sync_internals_ui_->HandleJsEvent("testMessage", JsEventDetails()); | 107 sync_internals_ui_->HandleJsEvent("testMessage", JsEventDetails()); |
| 103 } | 108 } |
| 104 | 109 |
| 105 TEST_F(SyncInternalsUITestWithService, HandleJsReply) { | 110 TEST_F(SyncInternalsUITestWithService, HandleJsReply) { |
| 106 EXPECT_CALL( | 111 EXPECT_CALL( |
| 107 *test_sync_internals_ui_, | 112 *web_ui_, |
| 108 ExecuteJavascript( | 113 ExecuteJavascript( |
| 109 ASCIIToUTF16("chrome.sync.testMessage.handleReply(5,true);"))); | 114 ASCIIToUTF16("chrome.sync.testMessage.handleReply(5,true);"))); |
| 110 | 115 |
| 111 ListValue args; | 116 ListValue args; |
| 112 args.Append(Value::CreateIntegerValue(5)); | 117 args.Append(Value::CreateIntegerValue(5)); |
| 113 args.Append(Value::CreateBooleanValue(true)); | 118 args.Append(Value::CreateBooleanValue(true)); |
| 114 test_sync_internals_ui_->HandleJsReply("testMessage", JsArgList(&args)); | 119 sync_internals_ui_->HandleJsReply("testMessage", JsArgList(&args)); |
| 115 } | 120 } |
| 116 | 121 |
| 117 TEST_F(SyncInternalsUITestWithService, OnWebUISendBasic) { | 122 TEST_F(SyncInternalsUITestWithService, OnWebUISendBasic) { |
| 118 const std::string& name = "testName"; | 123 const std::string& name = "testName"; |
| 119 ListValue args; | 124 ListValue args; |
| 120 args.Append(Value::CreateIntegerValue(10)); | 125 args.Append(Value::CreateIntegerValue(10)); |
| 121 | 126 |
| 122 EXPECT_CALL(mock_js_controller_, | 127 EXPECT_CALL(mock_js_controller_, |
| 123 ProcessJsMessage(name, HasArgsAsList(args), _)); | 128 ProcessJsMessage(name, HasArgsAsList(args), _)); |
| 124 | 129 |
| 125 test_sync_internals_ui_->controller()->OverrideHandleWebUIMessage( | 130 sync_internals_ui_->OverrideHandleWebUIMessage(GURL(), name, args); |
| 126 GURL(), name, args); | |
| 127 } | 131 } |
| 128 | 132 |
| 129 // Tests with NULL ProfileSyncService. | 133 // Tests with NULL ProfileSyncService. |
| 130 class SyncInternalsUITestWithoutService | 134 class SyncInternalsUITestWithoutService |
| 131 : public ChromeRenderViewHostTestHarness { | 135 : public ChromeRenderViewHostTestHarness { |
| 132 protected: | 136 protected: |
| 133 SyncInternalsUITestWithoutService() {} | 137 SyncInternalsUITestWithoutService() : sync_internals_ui_(NULL) {} |
| 134 | 138 |
| 135 virtual ~SyncInternalsUITestWithoutService() {} | 139 virtual ~SyncInternalsUITestWithoutService() {} |
| 136 | 140 |
| 137 virtual void SetUp() { | 141 virtual void SetUp() { |
| 138 NiceMock<ProfileMock>* profile_mock = new NiceMock<ProfileMock>(); | 142 NiceMock<ProfileMock>* profile_mock = new NiceMock<ProfileMock>(); |
| 139 EXPECT_CALL(*profile_mock, GetProfileSyncService()) | 143 EXPECT_CALL(*profile_mock, GetProfileSyncService()) |
| 140 .WillOnce(Return(static_cast<ProfileSyncService*>(NULL))); | 144 .WillOnce(Return(static_cast<ProfileSyncService*>(NULL))); |
| 141 browser_context_.reset(profile_mock); | 145 browser_context_.reset(profile_mock); |
| 142 | 146 |
| 143 ChromeRenderViewHostTestHarness::SetUp(); | 147 ChromeRenderViewHostTestHarness::SetUp(); |
| 144 | 148 |
| 145 { | 149 { |
| 146 // Needed by |test_sync_internals_ui_|'s constructor. The | 150 // Needed by |sync_internals_ui_|'s constructor. The |
| 147 // message loop is provided by ChromeRenderViewHostTestHarness. | 151 // message loop is provided by ChromeRenderViewHostTestHarness. |
| 148 content::TestBrowserThread ui_thread_(BrowserThread::UI, | 152 content::TestBrowserThread ui_thread_(BrowserThread::UI, |
| 149 MessageLoopForUI::current()); | 153 MessageLoopForUI::current()); |
| 150 // |test_sync_internals_ui_|'s constructor triggers all the | 154 // |sync_internals_ui_|'s constructor triggers all the |
| 151 // expectations above. | 155 // expectations above. |
| 152 test_sync_internals_ui_.reset(new TestSyncInternalsUI(contents())); | 156 web_ui_.reset(new TestSyncWebUI(contents())); |
| 157 sync_internals_ui_ = new SyncInternalsUI(web_ui_.get()); |
| 158 web_ui_->SetController(sync_internals_ui_); |
| 153 } | 159 } |
| 154 | 160 |
| 155 Mock::VerifyAndClearExpectations(profile_mock); | 161 Mock::VerifyAndClearExpectations(profile_mock); |
| 156 } | 162 } |
| 157 | 163 |
| 158 scoped_ptr<TestSyncInternalsUI> test_sync_internals_ui_; | 164 scoped_ptr<TestSyncWebUI> web_ui_; |
| 165 SyncInternalsUI* sync_internals_ui_; |
| 159 }; | 166 }; |
| 160 | 167 |
| 161 TEST_F(SyncInternalsUITestWithoutService, HandleJsEvent) { | 168 TEST_F(SyncInternalsUITestWithoutService, HandleJsEvent) { |
| 162 EXPECT_CALL(*test_sync_internals_ui_, | 169 EXPECT_CALL(*web_ui_, |
| 163 ExecuteJavascript( | 170 ExecuteJavascript( |
| 164 ASCIIToUTF16("chrome.sync.testMessage.fire({});"))); | 171 ASCIIToUTF16("chrome.sync.testMessage.fire({});"))); |
| 165 | 172 |
| 166 test_sync_internals_ui_->HandleJsEvent("testMessage", JsEventDetails()); | 173 sync_internals_ui_->HandleJsEvent("testMessage", JsEventDetails()); |
| 167 } | 174 } |
| 168 | 175 |
| 169 TEST_F(SyncInternalsUITestWithoutService, HandleJsReply) { | 176 TEST_F(SyncInternalsUITestWithoutService, HandleJsReply) { |
| 170 EXPECT_CALL( | 177 EXPECT_CALL( |
| 171 *test_sync_internals_ui_, | 178 *web_ui_, |
| 172 ExecuteJavascript( | 179 ExecuteJavascript( |
| 173 ASCIIToUTF16("chrome.sync.testMessage.handleReply(5,true);"))); | 180 ASCIIToUTF16("chrome.sync.testMessage.handleReply(5,true);"))); |
| 174 | 181 |
| 175 ListValue args; | 182 ListValue args; |
| 176 args.Append(Value::CreateIntegerValue(5)); | 183 args.Append(Value::CreateIntegerValue(5)); |
| 177 args.Append(Value::CreateBooleanValue(true)); | 184 args.Append(Value::CreateBooleanValue(true)); |
| 178 test_sync_internals_ui_->HandleJsReply( | 185 sync_internals_ui_->HandleJsReply( |
| 179 "testMessage", JsArgList(&args)); | 186 "testMessage", JsArgList(&args)); |
| 180 } | 187 } |
| 181 | 188 |
| 182 TEST_F(SyncInternalsUITestWithoutService, OnWebUISendBasic) { | 189 TEST_F(SyncInternalsUITestWithoutService, OnWebUISendBasic) { |
| 183 const std::string& name = "testName"; | 190 const std::string& name = "testName"; |
| 184 ListValue args; | 191 ListValue args; |
| 185 args.Append(Value::CreateIntegerValue(5)); | 192 args.Append(Value::CreateIntegerValue(5)); |
| 186 | 193 |
| 187 // Should drop the message. | 194 // Should drop the message. |
| 188 test_sync_internals_ui_->controller()->OverrideHandleWebUIMessage( | 195 sync_internals_ui_->OverrideHandleWebUIMessage(GURL(), name, args); |
| 189 GURL(), name, args); | |
| 190 } | 196 } |
| 191 | 197 |
| 192 // TODO(lipalani) - add a test case to test about:sync with a non null | 198 // TODO(lipalani) - add a test case to test about:sync with a non null |
| 193 // service. | 199 // service. |
| 194 TEST_F(SyncInternalsUITestWithoutService, OnWebUISendGetAboutInfo) { | 200 TEST_F(SyncInternalsUITestWithoutService, OnWebUISendGetAboutInfo) { |
| 195 const char kAboutInfoCall[] = | 201 const char kAboutInfoCall[] = |
| 196 "chrome.sync.getAboutInfo.handleReply({\"summary\":\"SYNC DISABLED\"});"; | 202 "chrome.sync.getAboutInfo.handleReply({\"summary\":\"SYNC DISABLED\"});"; |
| 197 EXPECT_CALL(*test_sync_internals_ui_, | 203 EXPECT_CALL(*web_ui_, |
| 198 ExecuteJavascript(ASCIIToUTF16(kAboutInfoCall))); | 204 ExecuteJavascript(ASCIIToUTF16(kAboutInfoCall))); |
| 199 | 205 |
| 200 ListValue args; | 206 ListValue args; |
| 201 test_sync_internals_ui_->controller()->OverrideHandleWebUIMessage( | 207 sync_internals_ui_->OverrideHandleWebUIMessage( |
| 202 GURL(), "getAboutInfo", args); | 208 GURL(), "getAboutInfo", args); |
| 203 } | 209 } |
| 204 | 210 |
| 205 } // namespace | 211 } // namespace |
| OLD | NEW |