1 // Copyright 2014 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 "components/feedback/feedback_data.h" 6 7 #include <set> 8 9 #include "base/message_loop/message_loop.h" 10 #include "base/prefs/testing_pref_service.h" 11 #include "base/run_loop.h" 12 #include "components/feedback/feedback_uploader.h" 13 #include "components/feedback/feedback_uploader_factory.h" 14 #include "components/keyed_service/core/keyed_service.h" 15 #include "components/user_prefs/user_prefs.h" 16 #include "content/public/test/test_browser_context.h" 17 #include "content/public/test/test_browser_thread.h" 18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 21 namespace { 22 23 const char kHistograms[] = ""; 24 const char kImageData[] = ""; 25 const char kFileData[] = ""; 26 27 const base::TimeDelta kRetryDelayForTest = 28 base::TimeDelta::FromMilliseconds(100); 29 30 using content::BrowserThread; 31 32 class MockUploader : public feedback::FeedbackUploader, public KeyedService { 33 public: 34 MockUploader(content::BrowserContext* context) 35 : FeedbackUploader(context ? context->GetPath() : base::FilePath(), 36 BrowserThread::GetBlockingPool()) {} 37 38 MOCK_METHOD1(DispatchReport, void(const std::string&)); 39 }; 40 41 MockUploader *g_uploader; 42 43 KeyedService* CreateFeedbackUploaderService(content::BrowserContext* context) { 44 if (!g_uploader) 45 g_uploader = new MockUploader(context); 46 EXPECT_CALL(*g_uploader, DispatchReport(testing::_)).Times(1); 47 return g_uploader; 48 } 49 50 scoped_ptr<std::string> MakeScoped(const char* str) { 51 return scoped_ptr<std::string>(new std::string(str)); 52 } 53 54 } // namespace 55 56 namespace feedback { 57 58 class FeedbackDataTest : public testing::Test { 59 protected: 60 FeedbackDataTest() 61 : context_(new content::TestBrowserContext()), 62 prefs_(new TestingPrefServiceSimple()), 63 data_(new FeedbackData()), 64 ui_thread_(content::BrowserThread::UI, &message_loop_) { 65 user_prefs::UserPrefs::Set(context_.get(), prefs_.get()); 66 data_->set_context(context_.get()); 67 data_->set_send_report_callback(base::Bind( 68 &FeedbackDataTest::set_send_report_callback, base::Unretained(this))); 69 70 FeedbackUploaderFactory::GetInstance()->SetTestingFactory( 71 context_.get(), &CreateFeedbackUploaderService); 72 } 73 74 void Send() { 75 bool attached_file_completed = 76 data_->attached_file_uuid().empty(); 77 bool screenshot_completed = 78 data_->screenshot_uuid().empty(); 79 80 if (screenshot_completed && attached_file_completed) { 81 data_->OnFeedbackPageDataComplete(); 82 } 83 } 84 85 void RunMessageLoop() { 86 run_loop_.reset(new base::RunLoop()); 87 quit_closure_ = run_loop_->QuitClosure(); 88 run_loop_->Run(); 89 } 90 91 void set_send_report_callback(scoped_refptr<FeedbackData> data) { 92 quit_closure_.Run(); 93 } 94 95 base::Closure quit_closure_; 96 scoped_ptr<base::RunLoop> run_loop_; 97 scoped_ptr<content::TestBrowserContext> context_; 98 scoped_ptr<PrefService> prefs_; 99 scoped_refptr<FeedbackData> data_; 100 base::MessageLoop message_loop_; 101 content::TestBrowserThread ui_thread_; 102 }; 103 104 TEST_F(FeedbackDataTest, ReportSending) { 105 data_->SetAndCompressHistograms(MakeScoped(kHistograms).Pass()); 106 data_->set_image(MakeScoped(kImageData).Pass()); 107 data_->AttachAndCompressFileData(MakeScoped(kFileData).Pass()); 108 Send(); 109 RunMessageLoop(); 110 EXPECT_TRUE(data_->IsDataComplete()); 111 delete g_uploader; 112 g_uploader = NULL; 113 } 114 115 } // namespace feedback 116