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