| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Copyright (C) 2016 The Qt Company Ltd. | 
					
						
							|  |  |  | ** Contact: https://www.qt.io/licensing/
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** This file is part of Qt Creator. | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Commercial License Usage | 
					
						
							|  |  |  | ** Licensees holding valid commercial Qt licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the commercial license agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							|  |  |  | ** a written agreement between you and The Qt Company. For licensing terms | 
					
						
							|  |  |  | ** and conditions see https://www.qt.io/terms-conditions. For further
 | 
					
						
							|  |  |  | ** information use the contact form at https://www.qt.io/contact-us.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** GNU General Public License Usage | 
					
						
							|  |  |  | ** Alternatively, this file may be used under the terms of the GNU | 
					
						
							|  |  |  | ** General Public License version 3 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
					
						
							|  |  |  | ** included in the packaging of this file. Please review the following | 
					
						
							|  |  |  | ** information to ensure the GNU General Public License requirements will | 
					
						
							|  |  |  | ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  | #include "googletest.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | #include "dummyclangipcclient.h"
 | 
					
						
							|  |  |  | #include "processevents-utilities.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <clangbackend_global.h>
 | 
					
						
							|  |  |  | #include <clangdocuments.h>
 | 
					
						
							|  |  |  | #include <clangexceptions.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-14 07:06:56 +02:00
										 |  |  | #include <clangsupportivetranslationunitinitializer.h>
 | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | #include <clangtranslationunit.h>
 | 
					
						
							|  |  |  | #include <clangtranslationunits.h>
 | 
					
						
							|  |  |  | #include <utf8string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <clang-c/Index.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <memory>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace ClangBackEnd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using testing::Eq; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SupportiveTranslationUnitInitializer : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2017-11-29 16:37:27 +01:00
										 |  |  |     void SetUp() override; | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  |     void parse(); | 
					
						
							|  |  |  |     Jobs::RunningJob createRunningJob(JobRequest::Type type) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void assertNoJobIsRunningAndEmptyQueue(); | 
					
						
							|  |  |  |     void assertSingleJobRunningAndEmptyQueue(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool waitUntilJobChainFinished(int timeOutInMs = 10000) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2017-11-29 16:37:27 +01:00
										 |  |  |     Utf8String filePath{Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp")}; | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 16:37:27 +01:00
										 |  |  |     UnsavedFiles unsavedFiles; | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     const QVector<FileContainer> fileContainer{FileContainer(filePath)}; | 
					
						
							|  |  |  |     Documents documents{unsavedFiles}; | 
					
						
							| 
									
										
										
										
											2017-11-29 16:37:27 +01:00
										 |  |  |     Document document{documents.create(fileContainer).front()}; | 
					
						
							|  |  |  |     DummyIpcClient dummyClientInterface; | 
					
						
							| 
									
										
										
										
											2016-12-06 15:59:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     Jobs jobs{documents, unsavedFiles, dummyClientInterface}; | 
					
						
							| 
									
										
										
										
											2017-11-29 16:37:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ClangBackEnd::SupportiveTranslationUnitInitializer initializer{document, jobs}; | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | using SupportiveTranslationUnitInitializerSlowTest = SupportiveTranslationUnitInitializer; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | TEST_F(SupportiveTranslationUnitInitializer, HasInitiallyNotInitializedState) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT_THAT(initializer.state(), Eq(ClangBackEnd::SupportiveTranslationUnitInitializer::State::NotInitialized)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SupportiveTranslationUnitInitializer, StartInitializingAbortsIfDocumentIsClosed) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     documents.remove({FileContainer(filePath)}); | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     initializer.startInitializing(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assertNoJobIsRunningAndEmptyQueue(); | 
					
						
							|  |  |  |     ASSERT_THAT(initializer.state(), Eq(ClangBackEnd::SupportiveTranslationUnitInitializer::State::Aborted)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SupportiveTranslationUnitInitializerSlowTest, StartInitializingAddsTranslationUnit) | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     initializer.startInitializing(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(document.translationUnits().size(), Eq(2)); | 
					
						
							|  |  |  |     ASSERT_FALSE(document.translationUnits().areAllTranslationUnitsParsed()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SupportiveTranslationUnitInitializerSlowTest, StartInitializingStartsJob) | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     initializer.startInitializing(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assertSingleJobRunningAndEmptyQueue(); | 
					
						
							|  |  |  |     const Jobs::RunningJob runningJob = jobs.runningJobs().first(); | 
					
						
							|  |  |  |     ASSERT_THAT(runningJob.jobRequest.type, JobRequest::Type::ParseSupportiveTranslationUnit); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 09:35:00 +01:00
										 |  |  | TEST_F(SupportiveTranslationUnitInitializerSlowTest, Abort) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     initializer.startInitializing(); | 
					
						
							|  |  |  |     assertSingleJobRunningAndEmptyQueue(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     initializer.abort(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(initializer.state(), | 
					
						
							|  |  |  |                 Eq(ClangBackEnd::SupportiveTranslationUnitInitializer::State::Aborted)); | 
					
						
							|  |  |  |     ASSERT_FALSE(jobs.jobFinishedCallback()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | TEST_F(SupportiveTranslationUnitInitializer, CheckIfParseJobFinishedAbortsIfDocumentIsClosed) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     documents.remove({FileContainer(filePath)}); | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  |     initializer.setState(ClangBackEnd::SupportiveTranslationUnitInitializer::State::WaitingForParseJob); | 
					
						
							|  |  |  |     const Jobs::RunningJob runningJob = createRunningJob(JobRequest::Type::ParseSupportiveTranslationUnit); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     initializer.checkIfParseJobFinished(runningJob); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assertNoJobIsRunningAndEmptyQueue(); | 
					
						
							|  |  |  |     ASSERT_THAT(initializer.state(), Eq(ClangBackEnd::SupportiveTranslationUnitInitializer::State::Aborted)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SupportiveTranslationUnitInitializerSlowTest, CheckIfParseJobFinishedStartsJob) | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     parse(); | 
					
						
							|  |  |  |     initializer.setState(ClangBackEnd::SupportiveTranslationUnitInitializer::State::WaitingForParseJob); | 
					
						
							|  |  |  |     Jobs::RunningJob runningJob = createRunningJob(JobRequest::Type::ParseSupportiveTranslationUnit); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     initializer.checkIfParseJobFinished(runningJob); | 
					
						
							|  |  |  |     jobs.process(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 10:44:44 +02:00
										 |  |  |     ASSERT_THAT(jobs.runningJobs(), IsEmpty()); | 
					
						
							|  |  |  |     ASSERT_THAT(jobs.queue(), IsEmpty()); | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SupportiveTranslationUnitInitializerSlowTest, FullRun) | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     parse(); | 
					
						
							|  |  |  |     initializer.startInitializing(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     waitUntilJobChainFinished(); | 
					
						
							|  |  |  |     ASSERT_THAT(initializer.state(), Eq(ClangBackEnd::SupportiveTranslationUnitInitializer::State::Initialized)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 16:37:27 +01:00
										 |  |  | void SupportiveTranslationUnitInitializer::SetUp() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     documents.setVisibleInEditors({filePath}); | 
					
						
							|  |  |  |     documents.setUsedByCurrentEditor(filePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     const auto isDocumentClosed = [this](const Utf8String &filePath) { | 
					
						
							|  |  |  |         return !documents.hasDocument(filePath); | 
					
						
							| 
									
										
										
										
											2017-11-29 16:37:27 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  |     initializer.setIsDocumentClosedChecker(isDocumentClosed); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  | void SupportiveTranslationUnitInitializer::parse() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     document.parse(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Jobs::RunningJob SupportiveTranslationUnitInitializer::createRunningJob(JobRequest::Type type) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-04 12:43:38 +02:00
										 |  |  |     const JobRequest jobRequest = jobs.createJobRequest(document, | 
					
						
							|  |  |  |                                                         type, | 
					
						
							|  |  |  |                                                         PreferredTranslationUnit::LastUninitialized); | 
					
						
							| 
									
										
										
										
											2016-09-14 16:16:10 +02:00
										 |  |  |     return Jobs::RunningJob{jobRequest, Utf8String(), QFuture<void>()}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SupportiveTranslationUnitInitializer::assertNoJobIsRunningAndEmptyQueue() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT_TRUE(jobs.runningJobs().isEmpty()); | 
					
						
							|  |  |  |     ASSERT_TRUE(jobs.queue().isEmpty()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SupportiveTranslationUnitInitializer::assertSingleJobRunningAndEmptyQueue() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT_THAT(jobs.runningJobs().size(), Eq(1)); | 
					
						
							|  |  |  |     ASSERT_TRUE(jobs.queue().isEmpty()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool SupportiveTranslationUnitInitializer::waitUntilJobChainFinished(int timeOutInMs) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const auto noJobsRunningAnymore = [this]() { | 
					
						
							|  |  |  |         return jobs.runningJobs().isEmpty() && jobs.queue().isEmpty(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ProcessEventUtilities::processEventsUntilTrue(noJobsRunningAnymore, timeOutInMs); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // anonymous namespace
 |