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 "base/bind.h" |
5 #include "base/command_line.h" | 6 #include "base/command_line.h" |
6 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
7 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
9 #include "chrome/browser/extensions/app_notify_channel_setup.h" | 10 #include "chrome/browser/extensions/app_notify_channel_setup.h" |
10 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
12 #include "chrome/test/base/testing_pref_service.h" | 13 #include "chrome/test/base/testing_pref_service.h" |
13 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
14 #include "content/browser/browser_thread.h" | 15 #include "content/browser/browser_thread.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // When our AppNotifyChannelSetupComplete method is called, we copy the | 58 // When our AppNotifyChannelSetupComplete method is called, we copy the |
58 // arguments into these member variables. | 59 // arguments into these member variables. |
59 std::string channel_id_; | 60 std::string channel_id_; |
60 std::string error_; | 61 std::string error_; |
61 int route_id_; | 62 int route_id_; |
62 int callback_id_; | 63 int callback_id_; |
63 | 64 |
64 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 65 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
65 }; | 66 }; |
66 | 67 |
| 68 class TestUI : public AppNotifyChannelUI { |
| 69 public: |
| 70 TestUI() : delegate_(NULL) {} |
| 71 ~TestUI() {} |
| 72 |
| 73 // AppNotifyChannelUI. |
| 74 virtual void PromptSyncSetup(Delegate* delegate) OVERRIDE { |
| 75 CHECK(!delegate_); |
| 76 delegate_ = delegate; |
| 77 |
| 78 // If we have a result, post a task to call back the delegate with |
| 79 // it. Otherwise ReportResult can be called manually at some later point. |
| 80 if (setup_result_.get()) { |
| 81 MessageLoop::current()->PostTask( |
| 82 FROM_HERE, |
| 83 base::Bind(&TestUI::ReportResult, |
| 84 base::Unretained(this), |
| 85 *setup_result_)); |
| 86 } |
| 87 } |
| 88 |
| 89 // This will make us automatically call back the delegate with |result| after |
| 90 // PromptSyncSetup is called. |
| 91 void SetSyncSetupResult(bool result) { |
| 92 setup_result_.reset(new bool); |
| 93 *setup_result_ = result; |
| 94 } |
| 95 |
| 96 void ReportResult(bool result) { |
| 97 CHECK(delegate_); |
| 98 delegate_->OnSyncSetupResult(result); |
| 99 } |
| 100 |
| 101 private: |
| 102 AppNotifyChannelUI::Delegate* delegate_; |
| 103 scoped_ptr<bool> setup_result_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(TestUI); |
| 106 }; |
| 107 |
67 } // namespace | 108 } // namespace |
68 | 109 |
69 class AppNotifyChannelSetupTest : public testing::Test { | 110 class AppNotifyChannelSetupTest : public testing::Test { |
70 public: | 111 public: |
71 AppNotifyChannelSetupTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} | 112 AppNotifyChannelSetupTest() : ui_thread_(BrowserThread::UI, &message_loop_), |
| 113 ui_(new TestUI()) { |
| 114 } |
72 | 115 |
73 virtual ~AppNotifyChannelSetupTest() {} | 116 virtual ~AppNotifyChannelSetupTest() {} |
74 | 117 |
75 virtual void SetChannelServerUrl(GURL channel_server_url) { | 118 virtual void SetChannelServerUrl(GURL channel_server_url) { |
76 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 119 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
77 command_line->AppendSwitchASCII(switches::kAppNotifyChannelServerURL, | 120 command_line->AppendSwitchASCII(switches::kAppNotifyChannelServerURL, |
78 channel_server_url.spec()); | 121 channel_server_url.spec()); |
79 } | 122 } |
80 | 123 |
81 virtual void SetLoggedInUser(const std::string username) { | 124 virtual void SetLoggedInUser(const std::string username) { |
(...skipping 14 matching lines...) Expand all Loading... |
96 | 139 |
97 SetChannelServerUrl(server_url); | 140 SetChannelServerUrl(server_url); |
98 SetLoggedInUser("user@gmail.com"); | 141 SetLoggedInUser("user@gmail.com"); |
99 | 142 |
100 scoped_refptr<AppNotifyChannelSetup > setup = | 143 scoped_refptr<AppNotifyChannelSetup > setup = |
101 new AppNotifyChannelSetup(&profile_, | 144 new AppNotifyChannelSetup(&profile_, |
102 "1234", | 145 "1234", |
103 page_url, | 146 page_url, |
104 kRouteId, | 147 kRouteId, |
105 kCallbackId, | 148 kCallbackId, |
| 149 ui_.release(), |
106 delegate_.AsWeakPtr()); | 150 delegate_.AsWeakPtr()); |
107 setup->Start(); | 151 setup->Start(); |
108 message_loop_.Run(); | 152 message_loop_.Run(); |
109 delegate_.ExpectWasCalled(expected_code, expected_error); | 153 delegate_.ExpectWasCalled(expected_code, expected_error); |
110 } | 154 } |
111 | 155 |
112 protected: | 156 protected: |
113 MessageLoop message_loop_; | 157 MessageLoop message_loop_; |
114 BrowserThread ui_thread_; | 158 BrowserThread ui_thread_; |
115 TestingProfile profile_; | 159 TestingProfile profile_; |
116 TestDelegate delegate_; | 160 TestDelegate delegate_; |
| 161 scoped_ptr<TestUI> ui_; |
117 }; | 162 }; |
118 | 163 |
119 TEST_F(AppNotifyChannelSetupTest, NotAvailable) { | 164 TEST_F(AppNotifyChannelSetupTest, DidNotLogInToSync) { |
120 GURL url("http://www.google.com"); | 165 GURL url("http://www.google.com"); |
121 | 166 |
| 167 ui_->SetSyncSetupResult(false); |
122 scoped_refptr<AppNotifyChannelSetup > setup = | 168 scoped_refptr<AppNotifyChannelSetup > setup = |
123 new AppNotifyChannelSetup(&profile_, | 169 new AppNotifyChannelSetup(&profile_, |
124 "1234", | 170 "1234", |
125 url, | 171 url, |
126 kRouteId, | 172 kRouteId, |
127 kCallbackId, | 173 kCallbackId, |
| 174 ui_.release(), |
128 delegate_.AsWeakPtr()); | 175 delegate_.AsWeakPtr()); |
129 setup->Start(); | 176 setup->Start(); |
130 message_loop_.Run(); | 177 message_loop_.Run(); |
131 delegate_.ExpectWasCalled( | 178 delegate_.ExpectWasCalled( |
132 std::string(), std::string("not_available")); | 179 std::string(), std::string("not_available")); |
133 } | 180 } |
134 | 181 |
135 TEST_F(AppNotifyChannelSetupTest, ServerSuccess) { | 182 TEST_F(AppNotifyChannelSetupTest, ServerSuccess) { |
136 RunServerTest(true, "dummy_do_not_use", ""); | 183 RunServerTest(true, "dummy_do_not_use", ""); |
137 } | 184 } |
138 | 185 |
139 TEST_F(AppNotifyChannelSetupTest, ServerFailure) { | 186 TEST_F(AppNotifyChannelSetupTest, ServerFailure) { |
140 RunServerTest(false, "", "channel_service_error"); | 187 RunServerTest(false, "", "channel_service_error"); |
141 } | 188 } |
OLD | NEW |