#******************************************************************
#
# Copyright 2016 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

##
# Notification Unit Test build script
##

Import('env')

if env.get('RELEASE'):
	env.AppendUnique(CCFLAGS = ['-Os'])
	env.AppendUnique(CPPDEFINES = ['NDEBUG'])
else:
	env.AppendUnique(CCFLAGS = ['-g'])

if env.get('LOGGING'):
	env.AppendUnique(CPPDEFINES = ['TB_LOG'])

lib_env = env.Clone()
SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')

######################################################################
#unit test setting
######################################################################
src_dir = lib_env.get('SRC_DIR')
gtest_dir = src_dir + '/extlibs/gtest/googletest-release-1.7.0'

notification_wrapper_test_env = lib_env.Clone()
target_os = env.get('TARGET_OS')

######################################################################
# Build flags
######################################################################
GTest = File(gtest_dir + '/lib/.libs/libgtest.a')
GTest_Main = File(gtest_dir + '/lib/.libs/libgtest_main.a')

notification_wrapper_test_env.AppendUnique(LIBPATH = [lib_env.get('BUILD_DIR')])
notification_wrapper_test_env.AppendUnique(LIBS = [
    'connectivity_abstraction', 'oc', 'octbstack', 'oc_logger', 'coap', 'resource_directory',
    GTest_Main, GTest])

if env.get('WITH_TCP') == True:
	notification_wrapper_test_env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
if env.get('SECURED') == '1':
	notification_wrapper_test_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509', 'mbedcrypto'])

if target_os not in ['windows', 'winrt']:
    notification_wrapper_test_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x'])

notification_wrapper_test_env.AppendUnique(LINKFLAGS = ['-Wl,--no-as-needed'])

notification_wrapper_test_env.AppendUnique(CXXFLAGS = ['-pthread'])
notification_wrapper_test_env.AppendUnique(LIBS = ['pthread'])

notification_wrapper_test_env.PrependUnique(CPPPATH = [
    '#/extlibs/hippomocks/hippomocks',
    gtest_dir + '/include',
    '../provider/inc',
    '../consumer/inc',
    '../common',
    '../../include',
    '../../src/provider',
    '../../src/consumer',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/include',
    '#/resource/oc_logger/include'
])

######################################################################
# Build Test
######################################################################

notification_consumer_wrapper_test_env = notification_wrapper_test_env.Clone()
notification_consumer_wrapper_test_env.AppendUnique(LIBS = ['notification_consumer_wrapper','notification_consumer'])

notification_provider_wrapper_test_env = notification_wrapper_test_env.Clone()
notification_provider_wrapper_test_env.AppendUnique(LIBS = ['notification_provider_wrapper','notification_provider'])

notification_consumer_wrapper_test_src = env.Glob('./NSConsumerServiceTest2.cpp')
notification_consumer_wrapper_test = notification_consumer_wrapper_test_env.Program('notification_consumer_wrapper_test', notification_consumer_wrapper_test_src)
Alias("notification_consumer_wrapper_test", notification_consumer_wrapper_test)
env.AppendTarget('notification_consumer_wrapper_test')

notification_consumer_wrapper_test_src = env.Glob('./NSConsumerServiceTest.cpp')
notification_consumer_wrapper_internaltest = notification_consumer_wrapper_test_env.Program('notification_consumer_wrapper_internaltest', notification_consumer_wrapper_test_src)
Alias("notification_consumer_wrapper_internaltest", notification_consumer_wrapper_internaltest)
env.AppendTarget('notification_consumer_wrapper_test')

notification_provider_wrapper_test_src = env.Glob('./NSProviderServiceTest2.cpp')
notification_provider_wrapper_test = notification_provider_wrapper_test_env.Program('notification_provider_wrapper_test', notification_provider_wrapper_test_src)
Alias("notification_provider_wrapper_test", notification_provider_wrapper_test)
env.AppendTarget('notification_provider_wrapper_test')

notification_provider_wrapper_test_src = env.Glob('./NSProviderServiceTest.cpp')
notification_provider_wrapper_internaltest = notification_provider_wrapper_test_env.Program('notification_provider_wrapper_internaltest', notification_provider_wrapper_test_src)
Alias("notification_provider_wrapper_internaltest", notification_provider_wrapper_internaltest)
env.AppendTarget('notification_provider_wrapper_test')

# TODO: Fix this test for more configs and remove this commented line
if env.get('TEST') == '1':
    if env.get('SECURED') != '1':
        if target_os in ['linux']:
            from tools.scons.RunTest import *
            run_test(notification_consumer_wrapper_test_env,
                     'service_notification_cpp-wrapper_unittest_notification_consumer_wrapper_test.memcheck',
                     'service/notification/cpp-wrapper/unittest/notification_consumer_wrapper_test',
                     notification_consumer_wrapper_test)
            run_test(notification_provider_wrapper_test_env,
                     'service_notification_cpp-wrapper_unittest_notification_provider_wrapper_test.memcheck',
                     'service/notification/cpp-wrapper/unittest/notification_provider_wrapper_test',
                     notification_provider_wrapper_test)
